blob: 3792b354b7fc452ae0662f1c5f3b12e2bb104dd2 [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 Moolenaar3b5f9292016-01-28 22:37:01 +01003486 * Returns what was read in allocated memory.
3487 * Returns NULL in case of error or timeout.
3488 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003489 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003490channel_read_block(
3491 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003492{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003493 char_u *buf;
3494 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003495 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003497 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003498 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003499
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003500 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003501 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003502
3503 while (TRUE)
3504 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003505 node = channel_peek(channel, part);
3506 if (node != NULL)
3507 {
3508 if (mode == MODE_RAW || (mode == MODE_NL
3509 && channel_first_nl(node) != NULL))
3510 /* got a complete message */
3511 break;
3512 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3513 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003514 /* If not blocking or nothing more is coming then return what we
3515 * have. */
3516 if (raw || fd == INVALID_FD)
3517 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003518 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003519
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003520 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003521 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003522 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003523 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003524 {
3525 ch_log(channel, "Timed out");
3526 return NULL;
3527 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003528 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003529 }
3530
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003531 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003532 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003533 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003534 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003535 }
3536 else
3537 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003538 char_u *p;
3539
3540 buf = node->rq_buffer;
3541 nl = channel_first_nl(node);
3542
3543 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003544 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003545 if (*p == NUL)
3546 *p = NL;
3547
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003548 if (nl == NULL)
3549 {
3550 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003551 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003552 }
3553 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003554 {
3555 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003556 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003557 *nl = NUL;
3558 }
3559 else
3560 {
3561 /* Copy the message into allocated memory and remove it from the
3562 * buffer. */
3563 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003564 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003565 }
3566 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003567 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003568 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003569 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003570}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003571
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003572/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003573 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003574 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003575 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003576 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003577 * In corner cases this can be called recursively, that is why ch_block_ids is
3578 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003579 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003580 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003581channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003582 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003583 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003584 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003585 int id,
3586 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003587{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003588 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003589 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003590 int timeout;
3591 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003592 int retval = FAIL;
Bram Moolenaard7ece102016-02-02 23:23:02 +01003593
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003594 ch_log(channel, "Blocking read JSON for id %d", id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003595
3596 // Not considered a safe state here, since we are processing a JSON message
3597 // and parsing other messages while waiting.
3598 enter_unsafe_state();
3599
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003600 if (id >= 0)
3601 channel_add_block_id(chanpart, id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003602
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003603 for (;;)
3604 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003605 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003606
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003607 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003608 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003609 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003610 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003611 retval = OK;
3612 break;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003613 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003614
Bram Moolenaard7ece102016-02-02 23:23:02 +01003615 if (!more)
3616 {
3617 /* Handle any other messages in the queue. If done some more
3618 * messages may have arrived. */
3619 if (channel_parse_messages())
3620 continue;
3621
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003622 /* Wait for up to the timeout. If there was an incomplete message
3623 * use the deadline for that. */
3624 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003625 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003626 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003627#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003628 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3629#else
3630 {
3631 struct timeval now_tv;
3632
3633 gettimeofday(&now_tv, NULL);
3634 timeout = (chanpart->ch_deadline.tv_sec
3635 - now_tv.tv_sec) * 1000
3636 + (chanpart->ch_deadline.tv_usec
3637 - now_tv.tv_usec) / 1000
3638 + 1;
3639 }
3640#endif
3641 if (timeout < 0)
3642 {
3643 /* Something went wrong, channel_parse_json() didn't
3644 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003645 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003646 timeout = timeout_arg;
3647 }
3648 else if (timeout > timeout_arg)
3649 timeout = timeout_arg;
3650 }
3651 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003652 if (fd == INVALID_FD
3653 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003654 {
3655 if (timeout == timeout_arg)
3656 {
3657 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003658 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003659 break;
3660 }
3661 }
3662 else
3663 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003664 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003665 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003666 if (id >= 0)
3667 channel_remove_block_id(chanpart, id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003668
3669 // This may trigger a SafeState autocommand.
3670 leave_unsafe_state();
3671
3672 return retval;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003673}
3674
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003675/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003676 * Get the channel from the argument.
3677 * Returns NULL if the handle is invalid.
3678 * When "check_open" is TRUE check that the channel can be used.
3679 * When "reading" is TRUE "check_open" considers typeahead useful.
3680 * "part" is used to check typeahead, when PART_COUNT use the default part.
3681 */
3682 static channel_T *
3683get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3684{
3685 channel_T *channel = NULL;
3686 int has_readahead = FALSE;
3687
3688 if (tv->v_type == VAR_JOB)
3689 {
3690 if (tv->vval.v_job != NULL)
3691 channel = tv->vval.v_job->jv_channel;
3692 }
3693 else if (tv->v_type == VAR_CHANNEL)
3694 {
3695 channel = tv->vval.v_channel;
3696 }
3697 else
3698 {
3699 semsg(_(e_invarg2), tv_get_string(tv));
3700 return NULL;
3701 }
3702 if (channel != NULL && reading)
3703 has_readahead = channel_has_readahead(channel,
3704 part != PART_COUNT ? part : channel_part_read(channel));
3705
3706 if (check_open && (channel == NULL || (!channel_is_open(channel)
3707 && !(reading && has_readahead))))
3708 {
3709 emsg(_("E906: not an open channel"));
3710 return NULL;
3711 }
3712 return channel;
3713}
3714
3715/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003716 * Common for ch_read() and ch_readraw().
3717 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003718 static void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003719common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003720{
3721 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003722 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003723 jobopt_T opt;
3724 int mode;
3725 int timeout;
3726 int id = -1;
3727 typval_T *listtv = NULL;
3728
3729 /* return an empty string by default */
3730 rettv->v_type = VAR_STRING;
3731 rettv->vval.v_string = NULL;
3732
3733 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003734 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003735 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003736 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003737
Bram Moolenaar437905c2016-04-26 19:01:05 +02003738 if (opt.jo_set & JO_PART)
3739 part = opt.jo_part;
3740 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003741 if (channel != NULL)
3742 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003743 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003744 part = channel_part_read(channel);
3745 mode = channel_get_mode(channel, part);
3746 timeout = channel_get_timeout(channel, part);
3747 if (opt.jo_set & JO_TIMEOUT)
3748 timeout = opt.jo_timeout;
3749
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003750 if (blob)
3751 {
3752 int outlen = 0;
3753 char_u *p = channel_read_block(channel, part,
3754 timeout, TRUE, &outlen);
3755 if (p != NULL)
3756 {
3757 blob_T *b = blob_alloc();
3758
3759 if (b != NULL)
3760 {
3761 b->bv_ga.ga_len = outlen;
3762 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3763 blob_free(b);
3764 else
3765 {
3766 memcpy(b->bv_ga.ga_data, p, outlen);
3767 rettv_blob_set(rettv, b);
3768 }
3769 }
3770 vim_free(p);
3771 }
3772 }
3773 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003774 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003775 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003776 else
3777 {
3778 if (opt.jo_set & JO_ID)
3779 id = opt.jo_id;
3780 channel_read_json_block(channel, part, timeout, id, &listtv);
3781 if (listtv != NULL)
3782 {
3783 *rettv = *listtv;
3784 vim_free(listtv);
3785 }
3786 else
3787 {
3788 rettv->v_type = VAR_SPECIAL;
3789 rettv->vval.v_number = VVAL_NONE;
3790 }
3791 }
3792 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003793
3794theend:
3795 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003796}
3797
Bram Moolenaar4f974752019-02-17 17:44:42 +01003798# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003799/*
3800 * Check the channels for anything that is ready to be read.
3801 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003802 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003803 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003804 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003805channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003806{
3807 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003808 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003809 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003810
3811 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3812 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003813 if (only_keep_open && !channel->ch_keep_open)
3814 continue;
3815
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003816 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003817 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003818 {
3819 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003820 if (fd != INVALID_FD)
3821 {
3822 int r = channel_wait(channel, fd, 0);
3823
3824 if (r == CW_READY)
3825 channel_read(channel, part, "channel_handle_events");
3826 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003827 ch_close_part_on_error(channel, part, TRUE,
3828 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003829 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003830 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003831 }
3832}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003833# endif
3834
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003835# if defined(FEAT_GUI) || defined(PROTO)
3836/*
3837 * Return TRUE when there is any channel with a keep_open flag.
3838 */
3839 int
3840channel_any_keep_open()
3841{
3842 channel_T *channel;
3843
3844 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3845 if (channel->ch_keep_open)
3846 return TRUE;
3847 return FALSE;
3848}
3849# endif
3850
Bram Moolenaard04a0202016-01-26 23:30:18 +01003851/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003852 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003853 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003854 */
3855 void
3856channel_set_nonblock(channel_T *channel, ch_part_T part)
3857{
3858 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003859 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003860
3861 if (fd != INVALID_FD)
3862 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003863#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003864 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003865
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003866 ioctlsocket(fd, FIONBIO, &val);
3867#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003868 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003869#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003870 ch_part->ch_nonblocking = TRUE;
3871 }
3872}
3873
3874/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003875 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003876 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003877 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003878 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003879 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003880channel_send(
3881 channel_T *channel,
3882 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003883 char_u *buf_arg,
3884 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003885 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003886{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003887 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003888 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003889 chanpart_T *ch_part = &channel->ch_part[part];
3890 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003891
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003892 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003893 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003894 {
3895 if (!channel->ch_error && fun != NULL)
3896 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003897 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003898 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003899 }
3900 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003901 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003902 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003903
Bram Moolenaar0b146882018-09-06 16:27:24 +02003904 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3905 channel_set_nonblock(channel, part);
3906
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003907 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003908 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003909 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003910 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003911 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003912 fprintf(log_fd, "'\n");
3913 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003914 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003915 }
3916
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003917 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003918 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003919 writeq_T *wq = &ch_part->ch_writeque;
3920 char_u *buf;
3921 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003922
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003923 if (wq->wq_next != NULL)
3924 {
3925 /* first write what was queued */
3926 buf = wq->wq_next->wq_ga.ga_data;
3927 len = wq->wq_next->wq_ga.ga_len;
3928 did_use_queue = TRUE;
3929 }
3930 else
3931 {
3932 if (len_arg == 0)
3933 /* nothing to write, called from channel_select_check() */
3934 return OK;
3935 buf = buf_arg;
3936 len = len_arg;
3937 }
3938
3939 if (part == PART_SOCK)
3940 res = sock_write(fd, (char *)buf, len);
3941 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003942 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003943 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003944#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003945 if (channel->ch_named_pipe && res < 0)
3946 {
3947 DisconnectNamedPipe((HANDLE)fd);
3948 ConnectNamedPipe((HANDLE)fd, NULL);
3949 }
3950#endif
3951 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003952 if (res < 0 && (errno == EWOULDBLOCK
3953#ifdef EAGAIN
3954 || errno == EAGAIN
3955#endif
3956 ))
3957 res = 0; /* nothing got written */
3958
3959 if (res >= 0 && ch_part->ch_nonblocking)
3960 {
3961 writeq_T *entry = wq->wq_next;
3962
3963 if (did_use_queue)
3964 ch_log(channel, "Sent %d bytes now", res);
3965 if (res == len)
3966 {
3967 /* Wrote all the buf[len] bytes. */
3968 if (entry != NULL)
3969 {
3970 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003971 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003972 continue;
3973 }
3974 if (did_use_queue)
3975 ch_log(channel, "Write queue empty");
3976 }
3977 else
3978 {
3979 /* Wrote only buf[res] bytes, can't write more now. */
3980 if (entry != NULL)
3981 {
3982 if (res > 0)
3983 {
3984 /* Remove the bytes that were written. */
3985 mch_memmove(entry->wq_ga.ga_data,
3986 (char *)entry->wq_ga.ga_data + res,
3987 len - res);
3988 entry->wq_ga.ga_len -= res;
3989 }
3990 buf = buf_arg;
3991 len = len_arg;
3992 }
3993 else
3994 {
3995 buf += res;
3996 len -= res;
3997 }
3998 ch_log(channel, "Adding %d bytes to the write queue", len);
3999
4000 /* Append the not written bytes of the argument to the write
4001 * buffer. Limit entries to 4000 bytes. */
4002 if (wq->wq_prev != NULL
4003 && wq->wq_prev->wq_ga.ga_len + len < 4000)
4004 {
4005 writeq_T *last = wq->wq_prev;
4006
4007 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02004008 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004009 {
4010 mch_memmove((char *)last->wq_ga.ga_data
4011 + last->wq_ga.ga_len,
4012 buf, len);
4013 last->wq_ga.ga_len += len;
4014 }
4015 }
4016 else
4017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004018 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004019
4020 if (last != NULL)
4021 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004022 last->wq_prev = wq->wq_prev;
4023 last->wq_next = NULL;
4024 if (wq->wq_prev == NULL)
4025 wq->wq_next = last;
4026 else
4027 wq->wq_prev->wq_next = last;
4028 wq->wq_prev = last;
4029 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004030 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004031 {
4032 mch_memmove(last->wq_ga.ga_data, buf, len);
4033 last->wq_ga.ga_len = len;
4034 }
4035 }
4036 }
4037 }
4038 }
4039 else if (res != len)
4040 {
4041 if (!channel->ch_error && fun != NULL)
4042 {
4043 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004044 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004045 }
4046 channel->ch_error = TRUE;
4047 return FAIL;
4048 }
4049
4050 channel->ch_error = FALSE;
4051 return OK;
4052 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004053}
4054
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055/*
4056 * Common for "ch_sendexpr()" and "ch_sendraw()".
4057 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004058 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059 * Otherwise returns NULL.
4060 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004061 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062send_common(
4063 typval_T *argvars,
4064 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004065 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004066 int id,
4067 int eval,
4068 jobopt_T *opt,
4069 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004070 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004071{
4072 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004073 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004074
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004075 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004076 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077 if (channel == NULL)
4078 return NULL;
4079 part_send = channel_part_send(channel);
4080 *part_read = channel_part_read(channel);
4081
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004082 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004083 return NULL;
4084
4085 /* Set the callback. An empty callback means no callback and not reading
4086 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4087 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004088 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004089 {
4090 if (eval)
4091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004092 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004093 return NULL;
4094 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004095 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004096 }
4097
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004098 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004099 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004100 return channel;
4101 return NULL;
4102}
4103
4104/*
4105 * common for "ch_evalexpr()" and "ch_sendexpr()"
4106 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004107 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004108ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4109{
4110 char_u *text;
4111 typval_T *listtv;
4112 channel_T *channel;
4113 int id;
4114 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004115 ch_part_T part_send;
4116 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004117 jobopt_T opt;
4118 int timeout;
4119
4120 /* return an empty string by default */
4121 rettv->v_type = VAR_STRING;
4122 rettv->vval.v_string = NULL;
4123
Bram Moolenaar437905c2016-04-26 19:01:05 +02004124 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 if (channel == NULL)
4126 return;
4127 part_send = channel_part_send(channel);
4128
4129 ch_mode = channel_get_mode(channel, part_send);
4130 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004132 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004133 return;
4134 }
4135
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004136 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004137 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004138 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004139 if (text == NULL)
4140 return;
4141
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004142 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004143 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4144 vim_free(text);
4145 if (channel != NULL && eval)
4146 {
4147 if (opt.jo_set & JO_TIMEOUT)
4148 timeout = opt.jo_timeout;
4149 else
4150 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004151 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4152 == OK)
4153 {
4154 list_T *list = listtv->vval.v_list;
4155
4156 /* Move the item from the list and then change the type to
4157 * avoid the value being freed. */
4158 *rettv = list->lv_last->li_tv;
4159 list->lv_last->li_tv.v_type = VAR_NUMBER;
4160 free_tv(listtv);
4161 }
4162 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004163 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004164}
4165
4166/*
4167 * common for "ch_evalraw()" and "ch_sendraw()"
4168 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004169 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004170ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4171{
4172 char_u buf[NUMBUFLEN];
4173 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004174 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004175 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004176 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004177 jobopt_T opt;
4178 int timeout;
4179
4180 /* return an empty string by default */
4181 rettv->v_type = VAR_STRING;
4182 rettv->vval.v_string = NULL;
4183
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004184 if (argvars[1].v_type == VAR_BLOB)
4185 {
4186 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4187 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4188 }
4189 else
4190 {
4191 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004192 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004193 }
4194 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004195 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4196 if (channel != NULL && eval)
4197 {
4198 if (opt.jo_set & JO_TIMEOUT)
4199 timeout = opt.jo_timeout;
4200 else
4201 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004202 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004203 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004204 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004205 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004206}
4207
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004208#define KEEP_OPEN_TIME 20 /* msec */
Bram Moolenaarf3360612017-10-01 16:21:31 +02004209
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004210#if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004211/*
4212 * Add open channels to the poll struct.
4213 * Return the adjusted struct index.
4214 * The type of "fds" is hidden to avoid problems with the function proto.
4215 */
4216 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004217channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004218{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004219 int nfd = nfd_in;
4220 channel_T *channel;
4221 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004222 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004223
Bram Moolenaar77073442016-02-13 23:23:53 +01004224 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004225 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004226 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004227 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004228 chanpart_T *ch_part = &channel->ch_part[part];
4229
4230 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004231 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004232 if (channel->ch_keep_open)
4233 {
4234 /* For unknown reason poll() returns immediately for a
4235 * keep-open channel. Instead of adding it to the fds add
4236 * a short timeout and check, like polling. */
4237 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4238 *towait = KEEP_OPEN_TIME;
4239 }
4240 else
4241 {
4242 ch_part->ch_poll_idx = nfd;
4243 fds[nfd].fd = ch_part->ch_fd;
4244 fds[nfd].events = POLLIN;
4245 nfd++;
4246 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004247 }
4248 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004249 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004250 }
4251 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004252
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004253 nfd = channel_fill_poll_write(nfd, fds);
4254
Bram Moolenaare0874f82016-01-24 20:36:41 +01004255 return nfd;
4256}
4257
4258/*
4259 * The type of "fds" is hidden to avoid problems with the function proto.
4260 */
4261 int
4262channel_poll_check(int ret_in, void *fds_in)
4263{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004264 int ret = ret_in;
4265 channel_T *channel;
4266 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004267 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004268 int idx;
4269 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004270
Bram Moolenaar77073442016-02-13 23:23:53 +01004271 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004272 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004273 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004274 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004275 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004276
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004277 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004278 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004279 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004280 --ret;
4281 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004282 else if (channel->ch_part[part].ch_fd != INVALID_FD
4283 && channel->ch_keep_open)
4284 {
4285 /* polling a keep-open channel */
4286 channel_read(channel, part, "channel_poll_check_keep_open");
4287 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004288 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004289
4290 in_part = &channel->ch_part[PART_IN];
4291 idx = in_part->ch_poll_idx;
4292 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4293 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004294 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004295 --ret;
4296 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004297 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004298
4299 return ret;
4300}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004301#endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004302
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004303#if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004304
Bram Moolenaare0874f82016-01-24 20:36:41 +01004305/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004306 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004307 */
4308 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004309channel_select_setup(
4310 int maxfd_in,
4311 void *rfds_in,
4312 void *wfds_in,
4313 struct timeval *tv,
4314 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004315{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004316 int maxfd = maxfd_in;
4317 channel_T *channel;
4318 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004319 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004320 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004321
Bram Moolenaar77073442016-02-13 23:23:53 +01004322 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004323 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004324 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004325 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004326 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004327
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004328 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004329 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004330 if (channel->ch_keep_open)
4331 {
4332 /* For unknown reason select() returns immediately for a
4333 * keep-open channel. Instead of adding it to the rfds add
4334 * a short timeout and check, like polling. */
4335 if (*tvp == NULL || tv->tv_sec > 0
4336 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4337 {
4338 *tvp = tv;
4339 tv->tv_sec = 0;
4340 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4341 }
4342 }
4343 else
4344 {
4345 FD_SET((int)fd, rfds);
4346 if (maxfd < (int)fd)
4347 maxfd = (int)fd;
4348 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004349 }
4350 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004351 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004352
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004353 maxfd = channel_fill_wfds(maxfd, wfds);
4354
Bram Moolenaare0874f82016-01-24 20:36:41 +01004355 return maxfd;
4356}
4357
4358/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004359 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004360 */
4361 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004362channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004363{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004364 int ret = ret_in;
4365 channel_T *channel;
4366 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004367 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004368 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004369 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004370
Bram Moolenaar77073442016-02-13 23:23:53 +01004371 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004372 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004373 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004374 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004375 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004376
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004377 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004378 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004379 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004380 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004381 --ret;
4382 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004383 else if (fd != INVALID_FD && channel->ch_keep_open)
4384 {
4385 /* polling a keep-open channel */
4386 channel_read(channel, part, "channel_select_check_keep_open");
4387 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004388 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004389
4390 in_part = &channel->ch_part[PART_IN];
4391 if (ret > 0 && in_part->ch_fd != INVALID_FD
4392 && FD_ISSET(in_part->ch_fd, wfds))
4393 {
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004394 // Clear the flag first, ch_fd may change in channel_write_input().
Bram Moolenaar3c518402017-09-08 20:47:00 +02004395 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004396 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004397 --ret;
4398 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004399 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004400
4401 return ret;
4402}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004403#endif // !MSWIN && HAVE_SELECT
Bram Moolenaare0874f82016-01-24 20:36:41 +01004404
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004405/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004406 * Execute queued up commands.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004407 * Invoked from the main loop when it's safe to execute received commands,
4408 * and during a blocking wait for ch_evalexpr().
Bram Moolenaard7ece102016-02-02 23:23:02 +01004409 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004410 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004411 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004412channel_parse_messages(void)
4413{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004414 channel_T *channel = first_channel;
4415 int ret = FALSE;
4416 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004417 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004418#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004419 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004420
4421 ELAPSED_INIT(start_tv);
4422#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004423
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004424 ++safe_to_invoke_callback;
4425
Bram Moolenaard0b65022016-03-06 21:50:33 +01004426 /* Only do this message when another message was given, otherwise we get
4427 * lots of them. */
4428 if (did_log_msg)
4429 {
4430 ch_log(NULL, "looking for messages on channels");
4431 did_log_msg = FALSE;
4432 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004433 while (channel != NULL)
4434 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004435 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004436 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004437 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004438 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004439 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004440 channel = first_channel;
4441 continue;
4442 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004443 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004444 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004445 if (channel->ch_killing)
4446 {
4447 channel_free_contents(channel);
4448 channel->ch_job->jv_channel = NULL;
4449 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004450 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004451 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004452 channel = first_channel;
4453 continue;
4454 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004455 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004456 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004457 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004458 channel_free(channel);
4459 channel = first_channel;
4460 part = PART_SOCK;
4461 continue;
4462 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004463 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004464 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004465 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004466 /* Increase the refcount, in case the handler causes the channel
4467 * to be unreferenced or closed. */
4468 ++channel->ch_refcount;
4469 r = may_invoke_callback(channel, part);
4470 if (r == OK)
4471 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004472 if (channel_unref(channel) || (r == OK
4473#ifdef ELAPSED_FUNC
4474 /* Limit the time we loop here to 100 msec, otherwise
4475 * Vim becomes unresponsive when the callback takes
4476 * more than a bit of time. */
4477 && ELAPSED_FUNC(start_tv) < 100L
4478#endif
4479 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004480 {
4481 /* channel was freed or something was done, start over */
4482 channel = first_channel;
4483 part = PART_SOCK;
4484 continue;
4485 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004486 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004487 if (part < PART_ERR)
4488 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004489 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004490 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004491 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004492 part = PART_SOCK;
4493 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004494 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004495
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004496 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004497 {
4498 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004499 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004500 }
4501
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004502 --safe_to_invoke_callback;
4503
Bram Moolenaard7ece102016-02-02 23:23:02 +01004504 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004505}
4506
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004507/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004508 * Return TRUE if any channel has readahead. That means we should not block on
4509 * waiting for input.
4510 */
4511 int
4512channel_any_readahead(void)
4513{
4514 channel_T *channel = first_channel;
4515 ch_part_T part = PART_SOCK;
4516
4517 while (channel != NULL)
4518 {
4519 if (channel_has_readahead(channel, part))
4520 return TRUE;
4521 if (part < PART_ERR)
4522 ++part;
4523 else
4524 {
4525 channel = channel->ch_next;
4526 part = PART_SOCK;
4527 }
4528 }
4529 return FALSE;
4530}
4531
4532/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004533 * Mark references to lists used in channels.
4534 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004535 int
4536set_ref_in_channel(int copyID)
4537{
Bram Moolenaar77073442016-02-13 23:23:53 +01004538 int abort = FALSE;
4539 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004540 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004541
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004542 for (channel = first_channel; !abort && channel != NULL;
4543 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004544 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004545 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004546 tv.v_type = VAR_CHANNEL;
4547 tv.vval.v_channel = channel;
4548 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004549 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004550 return abort;
4551}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004552
4553/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004554 * Return the "part" to write to for "channel".
4555 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004556 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004557channel_part_send(channel_T *channel)
4558{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004559 if (channel->CH_SOCK_FD == INVALID_FD)
4560 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004561 return PART_SOCK;
4562}
4563
4564/*
4565 * Return the default "part" to read from for "channel".
4566 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004567 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004568channel_part_read(channel_T *channel)
4569{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004570 if (channel->CH_SOCK_FD == INVALID_FD)
4571 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004572 return PART_SOCK;
4573}
4574
4575/*
4576 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004577 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004578 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004579 static ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004580channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004581{
Bram Moolenaar77073442016-02-13 23:23:53 +01004582 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004583 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004584 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004585}
4586
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004587/*
4588 * Return the timeout of "channel"/"part"
4589 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004590 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004591channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004592{
4593 return channel->ch_part[part].ch_timeout;
4594}
4595
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 static int
4597handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4598{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004599 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600
4601 opt->jo_set |= jo;
4602 if (STRCMP(val, "nl") == 0)
4603 *modep = MODE_NL;
4604 else if (STRCMP(val, "raw") == 0)
4605 *modep = MODE_RAW;
4606 else if (STRCMP(val, "js") == 0)
4607 *modep = MODE_JS;
4608 else if (STRCMP(val, "json") == 0)
4609 *modep = MODE_JSON;
4610 else
4611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004612 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004613 return FAIL;
4614 }
4615 return OK;
4616}
4617
4618 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004619handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004620{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004621 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004622
4623 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4624 if (STRCMP(val, "null") == 0)
4625 opt->jo_io[part] = JIO_NULL;
4626 else if (STRCMP(val, "pipe") == 0)
4627 opt->jo_io[part] = JIO_PIPE;
4628 else if (STRCMP(val, "file") == 0)
4629 opt->jo_io[part] = JIO_FILE;
4630 else if (STRCMP(val, "buffer") == 0)
4631 opt->jo_io[part] = JIO_BUFFER;
4632 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4633 opt->jo_io[part] = JIO_OUT;
4634 else
4635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004636 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004637 return FAIL;
4638 }
4639 return OK;
4640}
4641
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004642/*
4643 * Clear a jobopt_T before using it.
4644 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004645 void
4646clear_job_options(jobopt_T *opt)
4647{
4648 vim_memset(opt, 0, sizeof(jobopt_T));
4649}
4650
4651/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004652 * Free any members of a jobopt_T.
4653 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004654 static void
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004655free_job_options(jobopt_T *opt)
4656{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004657 if (opt->jo_callback.cb_partial != NULL)
4658 partial_unref(opt->jo_callback.cb_partial);
4659 else if (opt->jo_callback.cb_name != NULL)
4660 func_unref(opt->jo_callback.cb_name);
4661 if (opt->jo_out_cb.cb_partial != NULL)
4662 partial_unref(opt->jo_out_cb.cb_partial);
4663 else if (opt->jo_out_cb.cb_name != NULL)
4664 func_unref(opt->jo_out_cb.cb_name);
4665 if (opt->jo_err_cb.cb_partial != NULL)
4666 partial_unref(opt->jo_err_cb.cb_partial);
4667 else if (opt->jo_err_cb.cb_name != NULL)
4668 func_unref(opt->jo_err_cb.cb_name);
4669 if (opt->jo_close_cb.cb_partial != NULL)
4670 partial_unref(opt->jo_close_cb.cb_partial);
4671 else if (opt->jo_close_cb.cb_name != NULL)
4672 func_unref(opt->jo_close_cb.cb_name);
4673 if (opt->jo_exit_cb.cb_partial != NULL)
4674 partial_unref(opt->jo_exit_cb.cb_partial);
4675 else if (opt->jo_exit_cb.cb_name != NULL)
4676 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004677 if (opt->jo_env != NULL)
4678 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004679}
4680
4681/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004682 * Get the PART_ number from the first character of an option name.
4683 */
4684 static int
4685part_from_char(int c)
4686{
4687 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4688}
4689
4690/*
4691 * Get the option entries from the dict in "tv", parse them and put the result
4692 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004693 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004694 * If an option value is invalid return FAIL.
4695 */
4696 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004697get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004698{
4699 typval_T *item;
4700 char_u *val;
4701 dict_T *dict;
4702 int todo;
4703 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004704 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004705
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 if (tv->v_type == VAR_UNKNOWN)
4707 return OK;
4708 if (tv->v_type != VAR_DICT)
4709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004710 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004711 return FAIL;
4712 }
4713 dict = tv->vval.v_dict;
4714 if (dict == NULL)
4715 return OK;
4716
4717 todo = (int)dict->dv_hashtab.ht_used;
4718 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4719 if (!HASHITEM_EMPTY(hi))
4720 {
4721 item = &dict_lookup(hi)->di_tv;
4722
4723 if (STRCMP(hi->hi_key, "mode") == 0)
4724 {
4725 if (!(supported & JO_MODE))
4726 break;
4727 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4728 return FAIL;
4729 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004730 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004731 {
4732 if (!(supported & JO_IN_MODE))
4733 break;
4734 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4735 == FAIL)
4736 return FAIL;
4737 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004738 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004739 {
4740 if (!(supported & JO_OUT_MODE))
4741 break;
4742 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4743 == FAIL)
4744 return FAIL;
4745 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004746 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004747 {
4748 if (!(supported & JO_ERR_MODE))
4749 break;
4750 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4751 == FAIL)
4752 return FAIL;
4753 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004754 else if (STRCMP(hi->hi_key, "noblock") == 0)
4755 {
4756 if (!(supported & JO_MODE))
4757 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004758 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004759 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004760 else if (STRCMP(hi->hi_key, "in_io") == 0
4761 || STRCMP(hi->hi_key, "out_io") == 0
4762 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 {
4764 if (!(supported & JO_OUT_IO))
4765 break;
4766 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4767 return FAIL;
4768 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004769 else if (STRCMP(hi->hi_key, "in_name") == 0
4770 || STRCMP(hi->hi_key, "out_name") == 0
4771 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004772 {
4773 part = part_from_char(*hi->hi_key);
4774
4775 if (!(supported & JO_OUT_IO))
4776 break;
4777 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4778 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004779 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004780 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004781 else if (STRCMP(hi->hi_key, "pty") == 0)
4782 {
4783 if (!(supported & JO_MODE))
4784 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004786 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004787 else if (STRCMP(hi->hi_key, "in_buf") == 0
4788 || STRCMP(hi->hi_key, "out_buf") == 0
4789 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 {
4791 part = part_from_char(*hi->hi_key);
4792
4793 if (!(supported & JO_OUT_IO))
4794 break;
4795 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004796 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004797 if (opt->jo_io_buf[part] <= 0)
4798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004799 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004800 return FAIL;
4801 }
4802 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004804 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004805 return FAIL;
4806 }
4807 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004808 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4809 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4810 {
4811 part = part_from_char(*hi->hi_key);
4812
4813 if (!(supported & JO_OUT_IO))
4814 break;
4815 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004816 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004817 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004818 else if (STRCMP(hi->hi_key, "out_msg") == 0
4819 || STRCMP(hi->hi_key, "err_msg") == 0)
4820 {
4821 part = part_from_char(*hi->hi_key);
4822
4823 if (!(supported & JO_OUT_IO))
4824 break;
4825 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004826 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004827 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004828 else if (STRCMP(hi->hi_key, "in_top") == 0
4829 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004830 {
4831 linenr_T *lp;
4832
4833 if (!(supported & JO_OUT_IO))
4834 break;
4835 if (hi->hi_key[3] == 't')
4836 {
4837 lp = &opt->jo_in_top;
4838 opt->jo_set |= JO_IN_TOP;
4839 }
4840 else
4841 {
4842 lp = &opt->jo_in_bot;
4843 opt->jo_set |= JO_IN_BOT;
4844 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004846 if (*lp < 0)
4847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004848 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004849 return FAIL;
4850 }
4851 }
4852 else if (STRCMP(hi->hi_key, "channel") == 0)
4853 {
4854 if (!(supported & JO_OUT_IO))
4855 break;
4856 opt->jo_set |= JO_CHANNEL;
4857 if (item->v_type != VAR_CHANNEL)
4858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004860 return FAIL;
4861 }
4862 opt->jo_channel = item->vval.v_channel;
4863 }
4864 else if (STRCMP(hi->hi_key, "callback") == 0)
4865 {
4866 if (!(supported & JO_CALLBACK))
4867 break;
4868 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004869 opt->jo_callback = get_callback(item);
4870 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004871 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004872 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004873 return FAIL;
4874 }
4875 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004876 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004877 {
4878 if (!(supported & JO_OUT_CALLBACK))
4879 break;
4880 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004881 opt->jo_out_cb = get_callback(item);
4882 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004884 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004885 return FAIL;
4886 }
4887 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004888 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004889 {
4890 if (!(supported & JO_ERR_CALLBACK))
4891 break;
4892 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004893 opt->jo_err_cb = get_callback(item);
4894 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004896 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004897 return FAIL;
4898 }
4899 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004900 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004901 {
4902 if (!(supported & JO_CLOSE_CALLBACK))
4903 break;
4904 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004905 opt->jo_close_cb = get_callback(item);
4906 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004908 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004909 return FAIL;
4910 }
4911 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004912 else if (STRCMP(hi->hi_key, "drop") == 0)
4913 {
4914 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004915 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004916
4917 if (STRCMP(val, "never") == 0)
4918 never = TRUE;
4919 else if (STRCMP(val, "auto") != 0)
4920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004921 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004922 return FAIL;
4923 }
4924 opt->jo_drop_never = never;
4925 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004926 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4927 {
4928 if (!(supported & JO_EXIT_CB))
4929 break;
4930 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004931 opt->jo_exit_cb = get_callback(item);
4932 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004934 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004935 return FAIL;
4936 }
4937 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004938#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004939 else if (STRCMP(hi->hi_key, "term_name") == 0)
4940 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004941 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004942 break;
4943 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004944 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004945 if (opt->jo_term_name == NULL)
4946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004947 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004948 return FAIL;
4949 }
4950 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004951 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4952 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004953 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004954 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004955 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004956 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004958 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004959 return FAIL;
4960 }
4961 opt->jo_set2 |= JO2_TERM_FINISH;
4962 opt->jo_term_finish = *val;
4963 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004964 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4965 {
4966 char_u *p;
4967
4968 if (!(supported2 & JO2_TERM_OPENCMD))
4969 break;
4970 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004971 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004972 if (p != NULL)
4973 {
4974 /* Must have %d and no other %. */
4975 p = vim_strchr(p, '%');
4976 if (p != NULL && (p[1] != 'd'
4977 || vim_strchr(p + 2, '%') != NULL))
4978 p = NULL;
4979 }
4980 if (p == NULL)
4981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004982 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004983 return FAIL;
4984 }
4985 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004986 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4987 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004988 char_u *p;
4989
4990 if (!(supported2 & JO2_EOF_CHARS))
4991 break;
4992 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004993 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004994 if (p == NULL)
4995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004996 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004997 return FAIL;
4998 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004999 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005000 else if (STRCMP(hi->hi_key, "term_rows") == 0)
5001 {
5002 if (!(supported2 & JO2_TERM_ROWS))
5003 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005004 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005005 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005006 }
5007 else if (STRCMP(hi->hi_key, "term_cols") == 0)
5008 {
5009 if (!(supported2 & JO2_TERM_COLS))
5010 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005011 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005013 }
5014 else if (STRCMP(hi->hi_key, "vertical") == 0)
5015 {
5016 if (!(supported2 & JO2_VERTICAL))
5017 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005018 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005020 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005021 else if (STRCMP(hi->hi_key, "curwin") == 0)
5022 {
5023 if (!(supported2 & JO2_CURWIN))
5024 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005025 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005026 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005027 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005028 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5029 {
5030 int nr;
5031
5032 if (!(supported2 & JO2_CURWIN))
5033 break;
5034 opt->jo_set2 |= JO2_BUFNR;
5035 nr = tv_get_number(item);
5036 if (nr <= 0)
5037 {
5038 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5039 return FAIL;
5040 }
5041 opt->jo_bufnr_buf = buflist_findnr(nr);
5042 if (opt->jo_bufnr_buf == NULL)
5043 {
5044 semsg(_(e_nobufnr), (long)nr);
5045 return FAIL;
5046 }
5047 if (opt->jo_bufnr_buf->b_nwindows == 0
5048 || opt->jo_bufnr_buf->b_term == NULL)
5049 {
5050 semsg(_(e_invarg2), "bufnr");
5051 return FAIL;
5052 }
5053 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005054 else if (STRCMP(hi->hi_key, "hidden") == 0)
5055 {
5056 if (!(supported2 & JO2_HIDDEN))
5057 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005058 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005059 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005060 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005061 else if (STRCMP(hi->hi_key, "norestore") == 0)
5062 {
5063 if (!(supported2 & JO2_NORESTORE))
5064 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005065 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005066 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005067 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005068 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5069 {
5070 if (!(supported2 & JO2_TERM_KILL))
5071 break;
5072 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005073 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005074 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005075 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005076 {
5077 char_u *p;
5078
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005079 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005080 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005081 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005082 p = tv_get_string_chk(item);
5083 if (p == NULL)
5084 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005085 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005086 return FAIL;
5087 }
5088 // Allow empty string, "winpty", "conpty".
5089 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5090 || STRCMP(p, "conpty") == 0))
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 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005095 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005096 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005097# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5098 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5099 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005100 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005101 listitem_T *li;
5102 long_u rgb[16];
5103
5104 if (!(supported2 & JO2_ANSI_COLORS))
5105 break;
5106
5107 if (item == NULL || item->v_type != VAR_LIST
5108 || item->vval.v_list == NULL)
5109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005110 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005111 return FAIL;
5112 }
5113
5114 li = item->vval.v_list->lv_first;
5115 for (; li != NULL && n < 16; li = li->li_next, n++)
5116 {
5117 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005118 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005120 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005121 if (color_name == NULL)
5122 return FAIL;
5123
5124 guicolor = GUI_GET_COLOR(color_name);
5125 if (guicolor == INVALCOLOR)
5126 return FAIL;
5127
5128 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5129 }
5130
5131 if (n != 16 || li != NULL)
5132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005134 return FAIL;
5135 }
5136
5137 opt->jo_set2 |= JO2_ANSI_COLORS;
5138 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5139 }
5140# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005141#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005142 else if (STRCMP(hi->hi_key, "env") == 0)
5143 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005144 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005145 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005146 if (item->v_type != VAR_DICT)
5147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005148 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005149 return FAIL;
5150 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005151 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005152 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005153 if (opt->jo_env != NULL)
5154 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005155 }
5156 else if (STRCMP(hi->hi_key, "cwd") == 0)
5157 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005158 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005159 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005160 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005161 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005162#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005163 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5164#endif
5165 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005167 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005168 return FAIL;
5169 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005170 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005171 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005172 else if (STRCMP(hi->hi_key, "waittime") == 0)
5173 {
5174 if (!(supported & JO_WAITTIME))
5175 break;
5176 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005177 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005178 }
5179 else if (STRCMP(hi->hi_key, "timeout") == 0)
5180 {
5181 if (!(supported & JO_TIMEOUT))
5182 break;
5183 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005184 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005185 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005186 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005187 {
5188 if (!(supported & JO_OUT_TIMEOUT))
5189 break;
5190 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 opt->jo_out_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, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005194 {
5195 if (!(supported & JO_ERR_TIMEOUT))
5196 break;
5197 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005199 }
5200 else if (STRCMP(hi->hi_key, "part") == 0)
5201 {
5202 if (!(supported & JO_PART))
5203 break;
5204 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005205 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005206 if (STRCMP(val, "err") == 0)
5207 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005208 else if (STRCMP(val, "out") == 0)
5209 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005210 else
5211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005212 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 return FAIL;
5214 }
5215 }
5216 else if (STRCMP(hi->hi_key, "id") == 0)
5217 {
5218 if (!(supported & JO_ID))
5219 break;
5220 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005221 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005222 }
5223 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5224 {
5225 if (!(supported & JO_STOPONEXIT))
5226 break;
5227 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005229 opt->jo_soe_buf);
5230 if (opt->jo_stoponexit == NULL)
5231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005232 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005233 return FAIL;
5234 }
5235 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005236 else if (STRCMP(hi->hi_key, "block_write") == 0)
5237 {
5238 if (!(supported & JO_BLOCK_WRITE))
5239 break;
5240 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005241 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005242 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005243 else
5244 break;
5245 --todo;
5246 }
5247 if (todo > 0)
5248 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005249 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005250 return FAIL;
5251 }
5252
5253 return OK;
5254}
5255
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005256static job_T *first_job = NULL;
5257
5258 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005259job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005260{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005261 int i;
5262
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005263 ch_log(job->jv_channel, "Freeing job");
5264 if (job->jv_channel != NULL)
5265 {
5266 /* The link from the channel to the job doesn't count as a reference,
5267 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005268 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005269 * NULL the reference. We don't set ch_job_killed, unreferencing the
5270 * job doesn't mean it stops running. */
5271 job->jv_channel->ch_job = NULL;
5272 channel_unref(job->jv_channel);
5273 }
5274 mch_clear_job(job);
5275
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005276 vim_free(job->jv_tty_in);
5277 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005278 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005279#ifdef UNIX
5280 vim_free(job->jv_termsig);
5281#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005282#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005283 vim_free(job->jv_tty_type);
5284#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005285 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005286 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005287 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005288 for (i = 0; job->jv_argv[i] != NULL; i++)
5289 vim_free(job->jv_argv[i]);
5290 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005291 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005292}
5293
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005294/*
5295 * Remove "job" from the list of jobs.
5296 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005297 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005298job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005299{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005300 if (job->jv_next != NULL)
5301 job->jv_next->jv_prev = job->jv_prev;
5302 if (job->jv_prev == NULL)
5303 first_job = job->jv_next;
5304 else
5305 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005306}
5307
5308 static void
5309job_free_job(job_T *job)
5310{
5311 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005312 vim_free(job);
5313}
5314
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005315 static void
5316job_free(job_T *job)
5317{
5318 if (!in_free_unref_items)
5319 {
5320 job_free_contents(job);
5321 job_free_job(job);
5322 }
5323}
5324
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005325static job_T *jobs_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005326
5327/*
5328 * Put "job" in a list to be freed later, when it's no longer referenced.
5329 */
5330 static void
5331job_free_later(job_T *job)
5332{
5333 job_unlink(job);
5334 job->jv_next = jobs_to_free;
5335 jobs_to_free = job;
5336}
5337
5338 static void
5339free_jobs_to_free_later(void)
5340{
5341 job_T *job;
5342
5343 while (jobs_to_free != NULL)
5344 {
5345 job = jobs_to_free;
5346 jobs_to_free = job->jv_next;
5347 job_free_contents(job);
5348 vim_free(job);
5349 }
5350}
5351
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005352#if defined(EXITFREE) || defined(PROTO)
5353 void
5354job_free_all(void)
5355{
5356 while (first_job != NULL)
5357 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005358 free_jobs_to_free_later();
5359
5360# ifdef FEAT_TERMINAL
5361 free_unused_terminals();
5362# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005363}
5364#endif
5365
5366/*
5367 * Return TRUE if we need to check if the process of "job" has ended.
5368 */
5369 static int
5370job_need_end_check(job_T *job)
5371{
5372 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005373 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005374}
5375
5376/*
5377 * Return TRUE if the channel of "job" is still useful.
5378 */
5379 static int
5380job_channel_still_useful(job_T *job)
5381{
5382 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5383}
5384
5385/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005386 * Return TRUE if the channel of "job" is closeable.
5387 */
5388 static int
5389job_channel_can_close(job_T *job)
5390{
5391 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5392}
5393
5394/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005395 * Return TRUE if the job should not be freed yet. Do not free the job when
5396 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5397 * or when the associated channel will do something with the job output.
5398 */
5399 static int
5400job_still_useful(job_T *job)
5401{
5402 return job_need_end_check(job) || job_channel_still_useful(job);
5403}
5404
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005405#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005406/*
5407 * Return TRUE when there is any running job that we care about.
5408 */
5409 int
5410job_any_running()
5411{
5412 job_T *job;
5413
5414 for (job = first_job; job != NULL; job = job->jv_next)
5415 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005416 {
5417 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005418 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005419 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005420 return FALSE;
5421}
5422#endif
5423
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005424#if !defined(USE_ARGV) || defined(PROTO)
5425/*
5426 * Escape one argument for an external command.
5427 * Returns the escaped string in allocated memory. NULL when out of memory.
5428 */
5429 static char_u *
5430win32_escape_arg(char_u *arg)
5431{
5432 int slen, dlen;
5433 int escaping = 0;
5434 int i;
5435 char_u *s, *d;
5436 char_u *escaped_arg;
5437 int has_spaces = FALSE;
5438
5439 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005440 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005441 dlen = slen;
5442 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5443 {
5444 if (*s == '"' || *s == '\\')
5445 ++dlen;
5446 if (*s == ' ' || *s == '\t')
5447 has_spaces = TRUE;
5448 }
5449
5450 if (has_spaces)
5451 dlen += 2;
5452
5453 if (dlen == slen)
5454 return vim_strsave(arg);
5455
5456 /* Allocate memory for the result and fill it. */
5457 escaped_arg = alloc(dlen + 1);
5458 if (escaped_arg == NULL)
5459 return NULL;
5460 memset(escaped_arg, 0, dlen+1);
5461
5462 d = escaped_arg;
5463
5464 if (has_spaces)
5465 *d++ = '"';
5466
5467 for (s = arg; *s != NUL;)
5468 {
5469 switch (*s)
5470 {
5471 case '"':
5472 for (i = 0; i < escaping; i++)
5473 *d++ = '\\';
5474 escaping = 0;
5475 *d++ = '\\';
5476 *d++ = *s++;
5477 break;
5478 case '\\':
5479 escaping++;
5480 *d++ = *s++;
5481 break;
5482 default:
5483 escaping = 0;
5484 MB_COPY_CHAR(s, d);
5485 break;
5486 }
5487 }
5488
5489 /* add terminating quote and finish with a NUL */
5490 if (has_spaces)
5491 {
5492 for (i = 0; i < escaping; i++)
5493 *d++ = '\\';
5494 *d++ = '"';
5495 }
5496 *d = NUL;
5497
5498 return escaped_arg;
5499}
5500
5501/*
5502 * Build a command line from a list, taking care of escaping.
5503 * The result is put in gap->ga_data.
5504 * Returns FAIL when out of memory.
5505 */
5506 int
5507win32_build_cmd(list_T *l, garray_T *gap)
5508{
5509 listitem_T *li;
5510 char_u *s;
5511
5512 for (li = l->lv_first; li != NULL; li = li->li_next)
5513 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005514 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005515 if (s == NULL)
5516 return FAIL;
5517 s = win32_escape_arg(s);
5518 if (s == NULL)
5519 return FAIL;
5520 ga_concat(gap, s);
5521 vim_free(s);
5522 if (li->li_next != NULL)
5523 ga_append(gap, ' ');
5524 }
5525 return OK;
5526}
5527#endif
5528
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005529/*
5530 * NOTE: Must call job_cleanup() only once right after the status of "job"
5531 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5532 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005533 * If the job is no longer used it will be removed from the list of jobs, and
5534 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005535 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005536 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005537job_cleanup(job_T *job)
5538{
5539 if (job->jv_status != JOB_ENDED)
5540 return;
5541
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005542 /* Ready to cleanup the job. */
5543 job->jv_status = JOB_FINISHED;
5544
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005545 /* When only channel-in is kept open, close explicitly. */
5546 if (job->jv_channel != NULL)
5547 ch_close_part(job->jv_channel, PART_IN);
5548
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005549 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005550 {
5551 typval_T argv[3];
5552 typval_T rettv;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005553
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005554 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005555 ch_log(job->jv_channel, "Invoking exit callback %s",
5556 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005557 ++job->jv_refcount;
5558 argv[0].v_type = VAR_JOB;
5559 argv[0].vval.v_job = job;
5560 argv[1].v_type = VAR_NUMBER;
5561 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005562 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005563 clear_tv(&rettv);
5564 --job->jv_refcount;
5565 channel_need_redraw = TRUE;
5566 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005567
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005568 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5569 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005570
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005571 // Do not free the job in case the close callback of the associated channel
5572 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005573 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005574 // The job was already unreferenced and the associated channel was
5575 // detached, now that it ended it can be freed. However, a caller might
5576 // still use it, thus free it a bit later.
5577 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005578}
5579
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005580/*
5581 * Mark references in jobs that are still useful.
5582 */
5583 int
5584set_ref_in_job(int copyID)
5585{
5586 int abort = FALSE;
5587 job_T *job;
5588 typval_T tv;
5589
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005590 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005591 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005592 {
5593 tv.v_type = VAR_JOB;
5594 tv.vval.v_job = job;
5595 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5596 }
5597 return abort;
5598}
5599
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005600/*
5601 * Dereference "job". Note that after this "job" may have been freed.
5602 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005603 void
5604job_unref(job_T *job)
5605{
5606 if (job != NULL && --job->jv_refcount <= 0)
5607 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005608 /* Do not free the job if there is a channel where the close callback
5609 * may get the job info. */
5610 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005611 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005612 /* Do not free the job when it has not ended yet and there is a
5613 * "stoponexit" flag or an exit callback. */
5614 if (!job_need_end_check(job))
5615 {
5616 job_free(job);
5617 }
5618 else if (job->jv_channel != NULL)
5619 {
5620 /* Do remove the link to the channel, otherwise it hangs
5621 * around until Vim exits. See job_free() for refcount. */
5622 ch_log(job->jv_channel, "detaching channel from job");
5623 job->jv_channel->ch_job = NULL;
5624 channel_unref(job->jv_channel);
5625 job->jv_channel = NULL;
5626 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005627 }
5628 }
5629}
5630
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005631 int
5632free_unused_jobs_contents(int copyID, int mask)
5633{
5634 int did_free = FALSE;
5635 job_T *job;
5636
5637 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005638 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005639 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005640 {
5641 /* Free the channel and ordinary items it contains, but don't
5642 * recurse into Lists, Dictionaries etc. */
5643 job_free_contents(job);
5644 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005645 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005646 return did_free;
5647}
5648
5649 void
5650free_unused_jobs(int copyID, int mask)
5651{
5652 job_T *job;
5653 job_T *job_next;
5654
5655 for (job = first_job; job != NULL; job = job_next)
5656 {
5657 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005658 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005659 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005660 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005661 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005662 job_free_job(job);
5663 }
5664 }
5665}
5666
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005667/*
5668 * Allocate a job. Sets the refcount to one and sets options default.
5669 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005670 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005671job_alloc(void)
5672{
5673 job_T *job;
5674
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005675 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005676 if (job != NULL)
5677 {
5678 job->jv_refcount = 1;
5679 job->jv_stoponexit = vim_strsave((char_u *)"term");
5680
5681 if (first_job != NULL)
5682 {
5683 first_job->jv_prev = job;
5684 job->jv_next = first_job;
5685 }
5686 first_job = job;
5687 }
5688 return job;
5689}
5690
5691 void
5692job_set_options(job_T *job, jobopt_T *opt)
5693{
5694 if (opt->jo_set & JO_STOPONEXIT)
5695 {
5696 vim_free(job->jv_stoponexit);
5697 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5698 job->jv_stoponexit = NULL;
5699 else
5700 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5701 }
5702 if (opt->jo_set & JO_EXIT_CB)
5703 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005704 free_callback(&job->jv_exit_cb);
5705 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005706 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005707 job->jv_exit_cb.cb_name = NULL;
5708 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005709 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005710 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005711 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005712 }
5713}
5714
5715/*
5716 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5717 */
5718 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005719job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005720{
5721 job_T *job;
5722
5723 for (job = first_job; job != NULL; job = job->jv_next)
5724 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005725 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005726}
5727
5728/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005729 * Return TRUE when there is any job that has an exit callback and might exit,
5730 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005731 */
5732 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005733has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005734{
5735 job_T *job;
5736
5737 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005738 /* Only should check if the channel has been closed, if the channel is
5739 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005740 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5741 || (job->jv_status == JOB_FINISHED
5742 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005743 return TRUE;
5744 return FALSE;
5745}
5746
Bram Moolenaar97792de2016-10-15 18:36:49 +02005747#define MAX_CHECK_ENDED 8
5748
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005749/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005750 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005751 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005752 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005753 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005754job_check_ended(void)
5755{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005756 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005757 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005758
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005759 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005760 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005761 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005762
Bram Moolenaar97792de2016-10-15 18:36:49 +02005763 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005764 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005765 // NOTE: mch_detect_ended_job() must only return a job of which the
5766 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005767 job_T *job = mch_detect_ended_job(first_job);
5768
5769 if (job == NULL)
5770 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005771 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005772 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005773 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005774
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005775 // Actually free jobs that were cleaned up.
5776 free_jobs_to_free_later();
5777
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005778 if (channel_need_redraw)
5779 {
5780 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005781 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005782 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005783 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005784}
5785
5786/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005787 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005788 * "argv_arg" is only for Unix.
5789 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005790 * The returned job has a refcount of one.
5791 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005792 */
5793 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005794job_start(
5795 typval_T *argvars,
5796 char **argv_arg,
5797 jobopt_T *opt_arg,
5798 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005799{
5800 job_T *job;
5801 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005802 char **argv = NULL;
5803 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005804#if defined(UNIX)
5805# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005806 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005807#else
5808 garray_T ga;
5809#endif
5810 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005811 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005812
5813 job = job_alloc();
5814 if (job == NULL)
5815 return NULL;
5816
5817 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005818#ifndef USE_ARGV
5819 ga_init2(&ga, (int)sizeof(char*), 20);
5820#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005821
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005822 if (opt_arg != NULL)
5823 opt = *opt_arg;
5824 else
5825 {
5826 /* Default mode is NL. */
5827 clear_job_options(&opt);
5828 opt.jo_mode = MODE_NL;
5829 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005830 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5831 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5832 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005833 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005834 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005835
5836 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005837 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005838 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5839 && opt.jo_io[part] == JIO_FILE
5840 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5841 || *opt.jo_io_name[part] == NUL))
5842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005843 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005844 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005845 }
5846
5847 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5848 {
5849 buf_T *buf = NULL;
5850
5851 /* check that we can find the buffer before starting the job */
5852 if (opt.jo_set & JO_IN_BUF)
5853 {
5854 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5855 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005856 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005857 }
5858 else if (!(opt.jo_set & JO_IN_NAME))
5859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005860 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005861 }
5862 else
5863 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5864 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005865 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005866 if (buf->b_ml.ml_mfp == NULL)
5867 {
5868 char_u numbuf[NUMBUFLEN];
5869 char_u *s;
5870
5871 if (opt.jo_set & JO_IN_BUF)
5872 {
5873 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5874 s = numbuf;
5875 }
5876 else
5877 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005878 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005879 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005880 }
5881 job->jv_in_buf = buf;
5882 }
5883
5884 job_set_options(job, &opt);
5885
Bram Moolenaar13568252018-03-16 20:46:58 +01005886#ifdef USE_ARGV
5887 if (argv_arg != NULL)
5888 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005889 /* Make a copy of argv_arg for job->jv_argv. */
5890 for (i = 0; argv_arg[i] != NULL; i++)
5891 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005892 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005893 if (argv == NULL)
5894 goto theend;
5895 for (i = 0; i < argc; i++)
5896 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5897 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005898 }
5899 else
5900#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005901 if (argvars[0].v_type == VAR_STRING)
5902 {
5903 /* Command is a string. */
5904 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005905 if (cmd == NULL || *cmd == NUL)
5906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005907 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005908 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005909 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005910
5911 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005912 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005913 }
5914 else if (argvars[0].v_type != VAR_LIST
5915 || argvars[0].vval.v_list == NULL
5916 || argvars[0].vval.v_list->lv_len < 1)
5917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005918 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005919 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005920 }
5921 else
5922 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005923 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005924
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005925 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005926 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005927#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005928 if (win32_build_cmd(l, &ga) == FAIL)
5929 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005930 cmd = ga.ga_data;
5931#endif
5932 }
5933
Bram Moolenaar20608922018-04-21 22:30:08 +02005934 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005935 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005936
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005937#ifdef USE_ARGV
5938 if (ch_log_active())
5939 {
5940 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005941
5942 ga_init2(&ga, (int)sizeof(char), 200);
5943 for (i = 0; i < argc; ++i)
5944 {
5945 if (i > 0)
5946 ga_concat(&ga, (char_u *)" ");
5947 ga_concat(&ga, (char_u *)argv[i]);
5948 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005949 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005950 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005951 ga_clear(&ga);
5952 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005953 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005954#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005955 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005956 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005957#endif
5958
5959 /* If the channel is reading from a buffer, write lines now. */
5960 if (job->jv_channel != NULL)
5961 channel_write_in(job->jv_channel);
5962
5963theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005964#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005965 vim_free(ga.ga_data);
5966#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005967 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005968 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005969 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005970 return job;
5971}
5972
5973/*
5974 * Get the status of "job" and invoke the exit callback when needed.
5975 * The returned string is not allocated.
5976 */
5977 char *
5978job_status(job_T *job)
5979{
5980 char *result;
5981
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005982 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005983 /* No need to check, dead is dead. */
5984 result = "dead";
5985 else if (job->jv_status == JOB_FAILED)
5986 result = "fail";
5987 else
5988 {
5989 result = mch_job_status(job);
5990 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005991 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005992 }
5993 return result;
5994}
5995
Bram Moolenaar8950a562016-03-12 15:22:55 +01005996/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005997 * Send a signal to "job". Implements job_stop().
5998 * When "type" is not NULL use this for the type.
5999 * Otherwise use argvars[1] for the type.
6000 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006001 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006002job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006003{
6004 char_u *arg;
6005
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006006 if (type != NULL)
6007 arg = (char_u *)type;
6008 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006009 arg = (char_u *)"";
6010 else
6011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006012 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006013 if (arg == NULL)
6014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006015 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006016 return 0;
6017 }
6018 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006019 if (job->jv_status == JOB_FAILED)
6020 {
6021 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6022 return 0;
6023 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006024 if (job->jv_status == JOB_ENDED)
6025 {
6026 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6027 return 0;
6028 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006029 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006030 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006031 return 0;
6032
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006033 /* Assume that only "kill" will kill the job. */
6034 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006035 job->jv_channel->ch_job_killed = TRUE;
6036
6037 /* We don't try freeing the job, obviously the caller still has a
6038 * reference to it. */
6039 return 1;
6040}
6041
Bram Moolenaarf2732452018-06-03 14:47:35 +02006042 void
6043invoke_prompt_callback(void)
6044{
6045 typval_T rettv;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006046 typval_T argv[2];
6047 char_u *text;
6048 char_u *prompt;
6049 linenr_T lnum = curbuf->b_ml.ml_line_count;
6050
6051 // Add a new line for the prompt before invoking the callback, so that
6052 // text can always be inserted above the last line.
6053 ml_append(lnum, (char_u *)"", 0, FALSE);
6054 curwin->w_cursor.lnum = lnum + 1;
6055 curwin->w_cursor.col = 0;
6056
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006057 if (curbuf->b_prompt_callback.cb_name == NULL
6058 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006059 return;
6060 text = ml_get(lnum);
6061 prompt = prompt_text();
6062 if (STRLEN(text) >= STRLEN(prompt))
6063 text += STRLEN(prompt);
6064 argv[0].v_type = VAR_STRING;
6065 argv[0].vval.v_string = vim_strsave(text);
6066 argv[1].v_type = VAR_UNKNOWN;
6067
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006068 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006069 clear_tv(&argv[0]);
6070 clear_tv(&rettv);
6071}
6072
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006073/*
6074 * Return TRUE when the interrupt callback was invoked.
6075 */
6076 int
6077invoke_prompt_interrupt(void)
6078{
6079 typval_T rettv;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006080 typval_T argv[1];
6081
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006082 if (curbuf->b_prompt_interrupt.cb_name == NULL
6083 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006084 return FALSE;
6085 argv[0].v_type = VAR_UNKNOWN;
6086
6087 got_int = FALSE; // don't skip executing commands
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006088 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006089 clear_tv(&rettv);
6090 return TRUE;
6091}
6092
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006093/*
6094 * "prompt_setcallback({buffer}, {callback})" function
6095 */
6096 void
6097f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6098{
6099 buf_T *buf;
6100 callback_T callback;
6101
6102 if (check_secure())
6103 return;
6104 buf = tv_get_buf(&argvars[0], FALSE);
6105 if (buf == NULL)
6106 return;
6107
6108 callback = get_callback(&argvars[1]);
6109 if (callback.cb_name == NULL)
6110 return;
6111
6112 free_callback(&buf->b_prompt_callback);
6113 set_callback(&buf->b_prompt_callback, &callback);
6114}
6115
6116/*
6117 * "prompt_setinterrupt({buffer}, {callback})" function
6118 */
6119 void
6120f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6121{
6122 buf_T *buf;
6123 callback_T callback;
6124
6125 if (check_secure())
6126 return;
6127 buf = tv_get_buf(&argvars[0], FALSE);
6128 if (buf == NULL)
6129 return;
6130
6131 callback = get_callback(&argvars[1]);
6132 if (callback.cb_name == NULL)
6133 return;
6134
6135 free_callback(&buf->b_prompt_interrupt);
6136 set_callback(&buf->b_prompt_interrupt, &callback);
6137}
6138
6139/*
6140 * "prompt_setprompt({buffer}, {text})" function
6141 */
6142 void
6143f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6144{
6145 buf_T *buf;
6146 char_u *text;
6147
6148 if (check_secure())
6149 return;
6150 buf = tv_get_buf(&argvars[0], FALSE);
6151 if (buf == NULL)
6152 return;
6153
6154 text = tv_get_string(&argvars[1]);
6155 vim_free(buf->b_prompt_text);
6156 buf->b_prompt_text = vim_strsave(text);
6157}
6158
6159/*
6160 * "ch_canread()" function
6161 */
6162 void
6163f_ch_canread(typval_T *argvars, typval_T *rettv)
6164{
6165 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6166
6167 rettv->vval.v_number = 0;
6168 if (channel != NULL)
6169 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6170 || channel_has_readahead(channel, PART_OUT)
6171 || channel_has_readahead(channel, PART_ERR);
6172}
6173
6174/*
6175 * "ch_close()" function
6176 */
6177 void
6178f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6179{
6180 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6181
6182 if (channel != NULL)
6183 {
6184 channel_close(channel, FALSE);
6185 channel_clear(channel);
6186 }
6187}
6188
6189/*
6190 * "ch_close()" function
6191 */
6192 void
6193f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6194{
6195 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6196
6197 if (channel != NULL)
6198 channel_close_in(channel);
6199}
6200
6201/*
6202 * "ch_getbufnr()" function
6203 */
6204 void
6205f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6206{
6207 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6208
6209 rettv->vval.v_number = -1;
6210 if (channel != NULL)
6211 {
6212 char_u *what = tv_get_string(&argvars[1]);
6213 int part;
6214
6215 if (STRCMP(what, "err") == 0)
6216 part = PART_ERR;
6217 else if (STRCMP(what, "out") == 0)
6218 part = PART_OUT;
6219 else if (STRCMP(what, "in") == 0)
6220 part = PART_IN;
6221 else
6222 part = PART_SOCK;
6223 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6224 rettv->vval.v_number =
6225 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6226 }
6227}
6228
6229/*
6230 * "ch_getjob()" function
6231 */
6232 void
6233f_ch_getjob(typval_T *argvars, typval_T *rettv)
6234{
6235 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6236
6237 if (channel != NULL)
6238 {
6239 rettv->v_type = VAR_JOB;
6240 rettv->vval.v_job = channel->ch_job;
6241 if (channel->ch_job != NULL)
6242 ++channel->ch_job->jv_refcount;
6243 }
6244}
6245
6246/*
6247 * "ch_info()" function
6248 */
6249 void
6250f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6251{
6252 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6253
6254 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6255 channel_info(channel, rettv->vval.v_dict);
6256}
6257
6258/*
6259 * "ch_log()" function
6260 */
6261 void
6262f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6263{
6264 char_u *msg = tv_get_string(&argvars[0]);
6265 channel_T *channel = NULL;
6266
6267 if (argvars[1].v_type != VAR_UNKNOWN)
6268 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6269
6270 ch_log(channel, "%s", msg);
6271}
6272
6273/*
6274 * "ch_logfile()" function
6275 */
6276 void
6277f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6278{
6279 char_u *fname;
6280 char_u *opt = (char_u *)"";
6281 char_u buf[NUMBUFLEN];
6282
6283 /* Don't open a file in restricted mode. */
6284 if (check_restricted() || check_secure())
6285 return;
6286 fname = tv_get_string(&argvars[0]);
6287 if (argvars[1].v_type == VAR_STRING)
6288 opt = tv_get_string_buf(&argvars[1], buf);
6289 ch_logfile(fname, opt);
6290}
6291
6292/*
6293 * "ch_open()" function
6294 */
6295 void
6296f_ch_open(typval_T *argvars, typval_T *rettv)
6297{
6298 rettv->v_type = VAR_CHANNEL;
6299 if (check_restricted() || check_secure())
6300 return;
6301 rettv->vval.v_channel = channel_open_func(argvars);
6302}
6303
6304/*
6305 * "ch_read()" function
6306 */
6307 void
6308f_ch_read(typval_T *argvars, typval_T *rettv)
6309{
6310 common_channel_read(argvars, rettv, FALSE, FALSE);
6311}
6312
6313/*
6314 * "ch_readblob()" function
6315 */
6316 void
6317f_ch_readblob(typval_T *argvars, typval_T *rettv)
6318{
6319 common_channel_read(argvars, rettv, TRUE, TRUE);
6320}
6321
6322/*
6323 * "ch_readraw()" function
6324 */
6325 void
6326f_ch_readraw(typval_T *argvars, typval_T *rettv)
6327{
6328 common_channel_read(argvars, rettv, TRUE, FALSE);
6329}
6330
6331/*
6332 * "ch_evalexpr()" function
6333 */
6334 void
6335f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6336{
6337 ch_expr_common(argvars, rettv, TRUE);
6338}
6339
6340/*
6341 * "ch_sendexpr()" function
6342 */
6343 void
6344f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6345{
6346 ch_expr_common(argvars, rettv, FALSE);
6347}
6348
6349/*
6350 * "ch_evalraw()" function
6351 */
6352 void
6353f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6354{
6355 ch_raw_common(argvars, rettv, TRUE);
6356}
6357
6358/*
6359 * "ch_sendraw()" function
6360 */
6361 void
6362f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6363{
6364 ch_raw_common(argvars, rettv, FALSE);
6365}
6366
6367/*
6368 * "ch_setoptions()" function
6369 */
6370 void
6371f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6372{
6373 channel_T *channel;
6374 jobopt_T opt;
6375
6376 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6377 if (channel == NULL)
6378 return;
6379 clear_job_options(&opt);
6380 if (get_job_options(&argvars[1], &opt,
6381 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6382 channel_set_options(channel, &opt);
6383 free_job_options(&opt);
6384}
6385
6386/*
6387 * "ch_status()" function
6388 */
6389 void
6390f_ch_status(typval_T *argvars, typval_T *rettv)
6391{
6392 channel_T *channel;
6393 jobopt_T opt;
6394 int part = -1;
6395
6396 /* return an empty string by default */
6397 rettv->v_type = VAR_STRING;
6398 rettv->vval.v_string = NULL;
6399
6400 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6401
6402 if (argvars[1].v_type != VAR_UNKNOWN)
6403 {
6404 clear_job_options(&opt);
6405 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6406 && (opt.jo_set & JO_PART))
6407 part = opt.jo_part;
6408 }
6409
6410 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6411}
6412
6413/*
6414 * Get the job from the argument.
6415 * Returns NULL if the job is invalid.
6416 */
6417 static job_T *
6418get_job_arg(typval_T *tv)
6419{
6420 job_T *job;
6421
6422 if (tv->v_type != VAR_JOB)
6423 {
6424 semsg(_(e_invarg2), tv_get_string(tv));
6425 return NULL;
6426 }
6427 job = tv->vval.v_job;
6428
6429 if (job == NULL)
6430 emsg(_("E916: not a valid job"));
6431 return job;
6432}
6433
6434/*
6435 * "job_getchannel()" function
6436 */
6437 void
6438f_job_getchannel(typval_T *argvars, typval_T *rettv)
6439{
6440 job_T *job = get_job_arg(&argvars[0]);
6441
6442 if (job != NULL)
6443 {
6444 rettv->v_type = VAR_CHANNEL;
6445 rettv->vval.v_channel = job->jv_channel;
6446 if (job->jv_channel != NULL)
6447 ++job->jv_channel->ch_refcount;
6448 }
6449}
6450
6451/*
6452 * Implementation of job_info().
6453 */
6454 static void
6455job_info(job_T *job, dict_T *dict)
6456{
6457 dictitem_T *item;
6458 varnumber_T nr;
6459 list_T *l;
6460 int i;
6461
6462 dict_add_string(dict, "status", (char_u *)job_status(job));
6463
6464 item = dictitem_alloc((char_u *)"channel");
6465 if (item == NULL)
6466 return;
6467 item->di_tv.v_type = VAR_CHANNEL;
6468 item->di_tv.vval.v_channel = job->jv_channel;
6469 if (job->jv_channel != NULL)
6470 ++job->jv_channel->ch_refcount;
6471 if (dict_add(dict, item) == FAIL)
6472 dictitem_free(item);
6473
6474#ifdef UNIX
6475 nr = job->jv_pid;
6476#else
6477 nr = job->jv_proc_info.dwProcessId;
6478#endif
6479 dict_add_number(dict, "process", nr);
6480 dict_add_string(dict, "tty_in", job->jv_tty_in);
6481 dict_add_string(dict, "tty_out", job->jv_tty_out);
6482
6483 dict_add_number(dict, "exitval", job->jv_exitval);
6484 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6485 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6486#ifdef UNIX
6487 dict_add_string(dict, "termsig", job->jv_termsig);
6488#endif
6489#ifdef MSWIN
6490 dict_add_string(dict, "tty_type", job->jv_tty_type);
6491#endif
6492
6493 l = list_alloc();
6494 if (l != NULL)
6495 {
6496 dict_add_list(dict, "cmd", l);
6497 if (job->jv_argv != NULL)
6498 for (i = 0; job->jv_argv[i] != NULL; i++)
6499 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6500 }
6501}
6502
6503/*
6504 * Implementation of job_info() to return info for all jobs.
6505 */
6506 static void
6507job_info_all(list_T *l)
6508{
6509 job_T *job;
6510 typval_T tv;
6511
6512 for (job = first_job; job != NULL; job = job->jv_next)
6513 {
6514 tv.v_type = VAR_JOB;
6515 tv.vval.v_job = job;
6516
6517 if (list_append_tv(l, &tv) != OK)
6518 return;
6519 }
6520}
6521
6522/*
6523 * "job_info()" function
6524 */
6525 void
6526f_job_info(typval_T *argvars, typval_T *rettv)
6527{
6528 if (argvars[0].v_type != VAR_UNKNOWN)
6529 {
6530 job_T *job = get_job_arg(&argvars[0]);
6531
6532 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6533 job_info(job, rettv->vval.v_dict);
6534 }
6535 else if (rettv_list_alloc(rettv) == OK)
6536 job_info_all(rettv->vval.v_list);
6537}
6538
6539/*
6540 * "job_setoptions()" function
6541 */
6542 void
6543f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6544{
6545 job_T *job = get_job_arg(&argvars[0]);
6546 jobopt_T opt;
6547
6548 if (job == NULL)
6549 return;
6550 clear_job_options(&opt);
6551 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6552 job_set_options(job, &opt);
6553 free_job_options(&opt);
6554}
6555
6556/*
6557 * "job_start()" function
6558 */
6559 void
6560f_job_start(typval_T *argvars, typval_T *rettv)
6561{
6562 rettv->v_type = VAR_JOB;
6563 if (check_restricted() || check_secure())
6564 return;
6565 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6566}
6567
6568/*
6569 * "job_status()" function
6570 */
6571 void
6572f_job_status(typval_T *argvars, typval_T *rettv)
6573{
6574 job_T *job = get_job_arg(&argvars[0]);
6575
6576 if (job != NULL)
6577 {
6578 rettv->v_type = VAR_STRING;
6579 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6580 }
6581}
6582
6583/*
6584 * "job_stop()" function
6585 */
6586 void
6587f_job_stop(typval_T *argvars, typval_T *rettv)
6588{
6589 job_T *job = get_job_arg(&argvars[0]);
6590
6591 if (job != NULL)
6592 rettv->vval.v_number = job_stop(job, argvars, NULL);
6593}
6594
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006595#endif /* FEAT_JOB_CHANNEL */