blob: 0dab3be5f43a7ee39c91acb2a85a5ff635f10d4a [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
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003596 if (id >= 0)
3597 channel_add_block_id(chanpart, id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003598
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003599 for (;;)
3600 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003601 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003602
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003603 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003604 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003605 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003606 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003607 retval = OK;
3608 break;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003609 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003610
Bram Moolenaard7ece102016-02-02 23:23:02 +01003611 if (!more)
3612 {
3613 /* Handle any other messages in the queue. If done some more
3614 * messages may have arrived. */
3615 if (channel_parse_messages())
3616 continue;
3617
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003618 /* Wait for up to the timeout. If there was an incomplete message
3619 * use the deadline for that. */
3620 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003621 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003622 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003623#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003624 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3625#else
3626 {
3627 struct timeval now_tv;
3628
3629 gettimeofday(&now_tv, NULL);
3630 timeout = (chanpart->ch_deadline.tv_sec
3631 - now_tv.tv_sec) * 1000
3632 + (chanpart->ch_deadline.tv_usec
3633 - now_tv.tv_usec) / 1000
3634 + 1;
3635 }
3636#endif
3637 if (timeout < 0)
3638 {
3639 /* Something went wrong, channel_parse_json() didn't
3640 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003641 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003642 timeout = timeout_arg;
3643 }
3644 else if (timeout > timeout_arg)
3645 timeout = timeout_arg;
3646 }
3647 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003648 if (fd == INVALID_FD
3649 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003650 {
3651 if (timeout == timeout_arg)
3652 {
3653 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003654 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003655 break;
3656 }
3657 }
3658 else
3659 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003660 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003661 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003662 if (id >= 0)
3663 channel_remove_block_id(chanpart, id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003664
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003665 return retval;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003666}
3667
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003668/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003669 * Get the channel from the argument.
3670 * Returns NULL if the handle is invalid.
3671 * When "check_open" is TRUE check that the channel can be used.
3672 * When "reading" is TRUE "check_open" considers typeahead useful.
3673 * "part" is used to check typeahead, when PART_COUNT use the default part.
3674 */
3675 static channel_T *
3676get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3677{
3678 channel_T *channel = NULL;
3679 int has_readahead = FALSE;
3680
3681 if (tv->v_type == VAR_JOB)
3682 {
3683 if (tv->vval.v_job != NULL)
3684 channel = tv->vval.v_job->jv_channel;
3685 }
3686 else if (tv->v_type == VAR_CHANNEL)
3687 {
3688 channel = tv->vval.v_channel;
3689 }
3690 else
3691 {
3692 semsg(_(e_invarg2), tv_get_string(tv));
3693 return NULL;
3694 }
3695 if (channel != NULL && reading)
3696 has_readahead = channel_has_readahead(channel,
3697 part != PART_COUNT ? part : channel_part_read(channel));
3698
3699 if (check_open && (channel == NULL || (!channel_is_open(channel)
3700 && !(reading && has_readahead))))
3701 {
3702 emsg(_("E906: not an open channel"));
3703 return NULL;
3704 }
3705 return channel;
3706}
3707
3708/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003709 * Common for ch_read() and ch_readraw().
3710 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003711 static void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003712common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003713{
3714 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003715 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003716 jobopt_T opt;
3717 int mode;
3718 int timeout;
3719 int id = -1;
3720 typval_T *listtv = NULL;
3721
3722 /* return an empty string by default */
3723 rettv->v_type = VAR_STRING;
3724 rettv->vval.v_string = NULL;
3725
3726 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003727 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003728 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003729 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003730
Bram Moolenaar437905c2016-04-26 19:01:05 +02003731 if (opt.jo_set & JO_PART)
3732 part = opt.jo_part;
3733 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003734 if (channel != NULL)
3735 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003736 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003737 part = channel_part_read(channel);
3738 mode = channel_get_mode(channel, part);
3739 timeout = channel_get_timeout(channel, part);
3740 if (opt.jo_set & JO_TIMEOUT)
3741 timeout = opt.jo_timeout;
3742
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003743 if (blob)
3744 {
3745 int outlen = 0;
3746 char_u *p = channel_read_block(channel, part,
3747 timeout, TRUE, &outlen);
3748 if (p != NULL)
3749 {
3750 blob_T *b = blob_alloc();
3751
3752 if (b != NULL)
3753 {
3754 b->bv_ga.ga_len = outlen;
3755 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3756 blob_free(b);
3757 else
3758 {
3759 memcpy(b->bv_ga.ga_data, p, outlen);
3760 rettv_blob_set(rettv, b);
3761 }
3762 }
3763 vim_free(p);
3764 }
3765 }
3766 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003767 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003768 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003769 else
3770 {
3771 if (opt.jo_set & JO_ID)
3772 id = opt.jo_id;
3773 channel_read_json_block(channel, part, timeout, id, &listtv);
3774 if (listtv != NULL)
3775 {
3776 *rettv = *listtv;
3777 vim_free(listtv);
3778 }
3779 else
3780 {
3781 rettv->v_type = VAR_SPECIAL;
3782 rettv->vval.v_number = VVAL_NONE;
3783 }
3784 }
3785 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003786
3787theend:
3788 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003789}
3790
Bram Moolenaar4f974752019-02-17 17:44:42 +01003791# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003792/*
3793 * Check the channels for anything that is ready to be read.
3794 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003795 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003796 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003797 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003798channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003799{
3800 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003801 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003802 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003803
3804 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3805 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003806 if (only_keep_open && !channel->ch_keep_open)
3807 continue;
3808
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003809 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003810 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003811 {
3812 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003813 if (fd != INVALID_FD)
3814 {
3815 int r = channel_wait(channel, fd, 0);
3816
3817 if (r == CW_READY)
3818 channel_read(channel, part, "channel_handle_events");
3819 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003820 ch_close_part_on_error(channel, part, TRUE,
3821 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003822 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003823 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003824 }
3825}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003826# endif
3827
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003828# if defined(FEAT_GUI) || defined(PROTO)
3829/*
3830 * Return TRUE when there is any channel with a keep_open flag.
3831 */
3832 int
3833channel_any_keep_open()
3834{
3835 channel_T *channel;
3836
3837 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3838 if (channel->ch_keep_open)
3839 return TRUE;
3840 return FALSE;
3841}
3842# endif
3843
Bram Moolenaard04a0202016-01-26 23:30:18 +01003844/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003845 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003846 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003847 */
3848 void
3849channel_set_nonblock(channel_T *channel, ch_part_T part)
3850{
3851 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003852 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003853
3854 if (fd != INVALID_FD)
3855 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003856#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003857 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003858
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003859 ioctlsocket(fd, FIONBIO, &val);
3860#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003861 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003862#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003863 ch_part->ch_nonblocking = TRUE;
3864 }
3865}
3866
3867/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003868 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003869 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003870 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003871 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003872 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003873channel_send(
3874 channel_T *channel,
3875 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003876 char_u *buf_arg,
3877 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003878 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003879{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003880 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003881 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003882 chanpart_T *ch_part = &channel->ch_part[part];
3883 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003884
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003885 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003886 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003887 {
3888 if (!channel->ch_error && fun != NULL)
3889 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003890 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003891 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003892 }
3893 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003894 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003895 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003896
Bram Moolenaar0b146882018-09-06 16:27:24 +02003897 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3898 channel_set_nonblock(channel, part);
3899
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003900 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003901 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003902 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003903 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003904 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003905 fprintf(log_fd, "'\n");
3906 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003907 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003908 }
3909
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003910 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003911 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003912 writeq_T *wq = &ch_part->ch_writeque;
3913 char_u *buf;
3914 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003915
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003916 if (wq->wq_next != NULL)
3917 {
3918 /* first write what was queued */
3919 buf = wq->wq_next->wq_ga.ga_data;
3920 len = wq->wq_next->wq_ga.ga_len;
3921 did_use_queue = TRUE;
3922 }
3923 else
3924 {
3925 if (len_arg == 0)
3926 /* nothing to write, called from channel_select_check() */
3927 return OK;
3928 buf = buf_arg;
3929 len = len_arg;
3930 }
3931
3932 if (part == PART_SOCK)
3933 res = sock_write(fd, (char *)buf, len);
3934 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003935 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003936 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003937#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003938 if (channel->ch_named_pipe && res < 0)
3939 {
3940 DisconnectNamedPipe((HANDLE)fd);
3941 ConnectNamedPipe((HANDLE)fd, NULL);
3942 }
3943#endif
3944 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003945 if (res < 0 && (errno == EWOULDBLOCK
3946#ifdef EAGAIN
3947 || errno == EAGAIN
3948#endif
3949 ))
3950 res = 0; /* nothing got written */
3951
3952 if (res >= 0 && ch_part->ch_nonblocking)
3953 {
3954 writeq_T *entry = wq->wq_next;
3955
3956 if (did_use_queue)
3957 ch_log(channel, "Sent %d bytes now", res);
3958 if (res == len)
3959 {
3960 /* Wrote all the buf[len] bytes. */
3961 if (entry != NULL)
3962 {
3963 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003964 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003965 continue;
3966 }
3967 if (did_use_queue)
3968 ch_log(channel, "Write queue empty");
3969 }
3970 else
3971 {
3972 /* Wrote only buf[res] bytes, can't write more now. */
3973 if (entry != NULL)
3974 {
3975 if (res > 0)
3976 {
3977 /* Remove the bytes that were written. */
3978 mch_memmove(entry->wq_ga.ga_data,
3979 (char *)entry->wq_ga.ga_data + res,
3980 len - res);
3981 entry->wq_ga.ga_len -= res;
3982 }
3983 buf = buf_arg;
3984 len = len_arg;
3985 }
3986 else
3987 {
3988 buf += res;
3989 len -= res;
3990 }
3991 ch_log(channel, "Adding %d bytes to the write queue", len);
3992
3993 /* Append the not written bytes of the argument to the write
3994 * buffer. Limit entries to 4000 bytes. */
3995 if (wq->wq_prev != NULL
3996 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3997 {
3998 writeq_T *last = wq->wq_prev;
3999
4000 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02004001 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004002 {
4003 mch_memmove((char *)last->wq_ga.ga_data
4004 + last->wq_ga.ga_len,
4005 buf, len);
4006 last->wq_ga.ga_len += len;
4007 }
4008 }
4009 else
4010 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004011 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004012
4013 if (last != NULL)
4014 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004015 last->wq_prev = wq->wq_prev;
4016 last->wq_next = NULL;
4017 if (wq->wq_prev == NULL)
4018 wq->wq_next = last;
4019 else
4020 wq->wq_prev->wq_next = last;
4021 wq->wq_prev = last;
4022 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004023 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004024 {
4025 mch_memmove(last->wq_ga.ga_data, buf, len);
4026 last->wq_ga.ga_len = len;
4027 }
4028 }
4029 }
4030 }
4031 }
4032 else if (res != len)
4033 {
4034 if (!channel->ch_error && fun != NULL)
4035 {
4036 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004037 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004038 }
4039 channel->ch_error = TRUE;
4040 return FAIL;
4041 }
4042
4043 channel->ch_error = FALSE;
4044 return OK;
4045 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004046}
4047
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004048/*
4049 * Common for "ch_sendexpr()" and "ch_sendraw()".
4050 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004051 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004052 * Otherwise returns NULL.
4053 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004054 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055send_common(
4056 typval_T *argvars,
4057 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004058 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059 int id,
4060 int eval,
4061 jobopt_T *opt,
4062 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004063 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064{
4065 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004066 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004067
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004068 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004069 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070 if (channel == NULL)
4071 return NULL;
4072 part_send = channel_part_send(channel);
4073 *part_read = channel_part_read(channel);
4074
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004075 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 return NULL;
4077
4078 /* Set the callback. An empty callback means no callback and not reading
4079 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4080 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004081 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004082 {
4083 if (eval)
4084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004085 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004086 return NULL;
4087 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004088 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004089 }
4090
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004091 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004092 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004093 return channel;
4094 return NULL;
4095}
4096
4097/*
4098 * common for "ch_evalexpr()" and "ch_sendexpr()"
4099 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004100 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004101ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4102{
4103 char_u *text;
4104 typval_T *listtv;
4105 channel_T *channel;
4106 int id;
4107 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004108 ch_part_T part_send;
4109 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004110 jobopt_T opt;
4111 int timeout;
4112
4113 /* return an empty string by default */
4114 rettv->v_type = VAR_STRING;
4115 rettv->vval.v_string = NULL;
4116
Bram Moolenaar437905c2016-04-26 19:01:05 +02004117 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 if (channel == NULL)
4119 return;
4120 part_send = channel_part_send(channel);
4121
4122 ch_mode = channel_get_mode(channel, part_send);
4123 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4124 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004125 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004126 return;
4127 }
4128
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004129 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004130 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004131 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 if (text == NULL)
4133 return;
4134
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004135 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004136 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4137 vim_free(text);
4138 if (channel != NULL && eval)
4139 {
4140 if (opt.jo_set & JO_TIMEOUT)
4141 timeout = opt.jo_timeout;
4142 else
4143 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4145 == OK)
4146 {
4147 list_T *list = listtv->vval.v_list;
4148
4149 /* Move the item from the list and then change the type to
4150 * avoid the value being freed. */
4151 *rettv = list->lv_last->li_tv;
4152 list->lv_last->li_tv.v_type = VAR_NUMBER;
4153 free_tv(listtv);
4154 }
4155 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004156 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004157}
4158
4159/*
4160 * common for "ch_evalraw()" and "ch_sendraw()"
4161 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004162 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004163ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4164{
4165 char_u buf[NUMBUFLEN];
4166 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004167 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004168 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004169 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004170 jobopt_T opt;
4171 int timeout;
4172
4173 /* return an empty string by default */
4174 rettv->v_type = VAR_STRING;
4175 rettv->vval.v_string = NULL;
4176
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004177 if (argvars[1].v_type == VAR_BLOB)
4178 {
4179 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4180 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4181 }
4182 else
4183 {
4184 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004185 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004186 }
4187 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004188 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4189 if (channel != NULL && eval)
4190 {
4191 if (opt.jo_set & JO_TIMEOUT)
4192 timeout = opt.jo_timeout;
4193 else
4194 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004195 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004196 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004197 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004198 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004199}
4200
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004201#define KEEP_OPEN_TIME 20 /* msec */
Bram Moolenaarf3360612017-10-01 16:21:31 +02004202
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004203#if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004204/*
4205 * Add open channels to the poll struct.
4206 * Return the adjusted struct index.
4207 * The type of "fds" is hidden to avoid problems with the function proto.
4208 */
4209 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004210channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004211{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004212 int nfd = nfd_in;
4213 channel_T *channel;
4214 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004215 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004216
Bram Moolenaar77073442016-02-13 23:23:53 +01004217 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004218 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004219 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004220 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004221 chanpart_T *ch_part = &channel->ch_part[part];
4222
4223 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004224 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004225 if (channel->ch_keep_open)
4226 {
4227 /* For unknown reason poll() returns immediately for a
4228 * keep-open channel. Instead of adding it to the fds add
4229 * a short timeout and check, like polling. */
4230 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4231 *towait = KEEP_OPEN_TIME;
4232 }
4233 else
4234 {
4235 ch_part->ch_poll_idx = nfd;
4236 fds[nfd].fd = ch_part->ch_fd;
4237 fds[nfd].events = POLLIN;
4238 nfd++;
4239 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004240 }
4241 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004242 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004243 }
4244 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004245
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004246 nfd = channel_fill_poll_write(nfd, fds);
4247
Bram Moolenaare0874f82016-01-24 20:36:41 +01004248 return nfd;
4249}
4250
4251/*
4252 * The type of "fds" is hidden to avoid problems with the function proto.
4253 */
4254 int
4255channel_poll_check(int ret_in, void *fds_in)
4256{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004257 int ret = ret_in;
4258 channel_T *channel;
4259 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004260 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004261 int idx;
4262 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004263
Bram Moolenaar77073442016-02-13 23:23:53 +01004264 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004265 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004266 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004267 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004268 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004269
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004270 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004271 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004272 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004273 --ret;
4274 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004275 else if (channel->ch_part[part].ch_fd != INVALID_FD
4276 && channel->ch_keep_open)
4277 {
4278 /* polling a keep-open channel */
4279 channel_read(channel, part, "channel_poll_check_keep_open");
4280 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004281 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004282
4283 in_part = &channel->ch_part[PART_IN];
4284 idx = in_part->ch_poll_idx;
4285 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4286 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004287 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004288 --ret;
4289 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004290 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004291
4292 return ret;
4293}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004294#endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004295
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004296#if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004297
Bram Moolenaare0874f82016-01-24 20:36:41 +01004298/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004299 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004300 */
4301 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004302channel_select_setup(
4303 int maxfd_in,
4304 void *rfds_in,
4305 void *wfds_in,
4306 struct timeval *tv,
4307 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004308{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004309 int maxfd = maxfd_in;
4310 channel_T *channel;
4311 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004312 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004313 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004314
Bram Moolenaar77073442016-02-13 23:23:53 +01004315 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004316 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004317 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004318 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004319 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004320
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004321 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004322 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004323 if (channel->ch_keep_open)
4324 {
4325 /* For unknown reason select() returns immediately for a
4326 * keep-open channel. Instead of adding it to the rfds add
4327 * a short timeout and check, like polling. */
4328 if (*tvp == NULL || tv->tv_sec > 0
4329 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4330 {
4331 *tvp = tv;
4332 tv->tv_sec = 0;
4333 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4334 }
4335 }
4336 else
4337 {
4338 FD_SET((int)fd, rfds);
4339 if (maxfd < (int)fd)
4340 maxfd = (int)fd;
4341 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004342 }
4343 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004344 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004345
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004346 maxfd = channel_fill_wfds(maxfd, wfds);
4347
Bram Moolenaare0874f82016-01-24 20:36:41 +01004348 return maxfd;
4349}
4350
4351/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004352 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004353 */
4354 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004355channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004356{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004357 int ret = ret_in;
4358 channel_T *channel;
4359 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004360 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004361 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004362 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004363
Bram Moolenaar77073442016-02-13 23:23:53 +01004364 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004365 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004366 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004367 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004368 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004369
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004370 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004371 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004372 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004373 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004374 --ret;
4375 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004376 else if (fd != INVALID_FD && channel->ch_keep_open)
4377 {
4378 /* polling a keep-open channel */
4379 channel_read(channel, part, "channel_select_check_keep_open");
4380 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004381 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004382
4383 in_part = &channel->ch_part[PART_IN];
4384 if (ret > 0 && in_part->ch_fd != INVALID_FD
4385 && FD_ISSET(in_part->ch_fd, wfds))
4386 {
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004387 // Clear the flag first, ch_fd may change in channel_write_input().
Bram Moolenaar3c518402017-09-08 20:47:00 +02004388 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004389 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004390 --ret;
4391 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004392 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004393
4394 return ret;
4395}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004396#endif // !MSWIN && HAVE_SELECT
Bram Moolenaare0874f82016-01-24 20:36:41 +01004397
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004398/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004399 * Execute queued up commands.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004400 * Invoked from the main loop when it's safe to execute received commands,
4401 * and during a blocking wait for ch_evalexpr().
Bram Moolenaard7ece102016-02-02 23:23:02 +01004402 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004403 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004404 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004405channel_parse_messages(void)
4406{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004407 channel_T *channel = first_channel;
4408 int ret = FALSE;
4409 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004410 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004411#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004412 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004413
4414 ELAPSED_INIT(start_tv);
4415#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004416
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004417 ++safe_to_invoke_callback;
4418
Bram Moolenaard0b65022016-03-06 21:50:33 +01004419 /* Only do this message when another message was given, otherwise we get
4420 * lots of them. */
4421 if (did_log_msg)
4422 {
4423 ch_log(NULL, "looking for messages on channels");
4424 did_log_msg = FALSE;
4425 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004426 while (channel != NULL)
4427 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004428 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004429 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004430 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004431 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004432 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004433 channel = first_channel;
4434 continue;
4435 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004436 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004437 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004438 if (channel->ch_killing)
4439 {
4440 channel_free_contents(channel);
4441 channel->ch_job->jv_channel = NULL;
4442 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004443 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004444 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004445 channel = first_channel;
4446 continue;
4447 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004448 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004449 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004450 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004451 channel_free(channel);
4452 channel = first_channel;
4453 part = PART_SOCK;
4454 continue;
4455 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004456 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004457 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004458 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004459 /* Increase the refcount, in case the handler causes the channel
4460 * to be unreferenced or closed. */
4461 ++channel->ch_refcount;
4462 r = may_invoke_callback(channel, part);
4463 if (r == OK)
4464 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004465 if (channel_unref(channel) || (r == OK
4466#ifdef ELAPSED_FUNC
4467 /* Limit the time we loop here to 100 msec, otherwise
4468 * Vim becomes unresponsive when the callback takes
4469 * more than a bit of time. */
4470 && ELAPSED_FUNC(start_tv) < 100L
4471#endif
4472 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004473 {
4474 /* channel was freed or something was done, start over */
4475 channel = first_channel;
4476 part = PART_SOCK;
4477 continue;
4478 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004479 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004480 if (part < PART_ERR)
4481 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004482 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004483 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004484 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004485 part = PART_SOCK;
4486 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004487 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004488
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004489 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004490 {
4491 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004492 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004493 }
4494
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004495 --safe_to_invoke_callback;
4496
Bram Moolenaard7ece102016-02-02 23:23:02 +01004497 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004498}
4499
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004500/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004501 * Return TRUE if any channel has readahead. That means we should not block on
4502 * waiting for input.
4503 */
4504 int
4505channel_any_readahead(void)
4506{
4507 channel_T *channel = first_channel;
4508 ch_part_T part = PART_SOCK;
4509
4510 while (channel != NULL)
4511 {
4512 if (channel_has_readahead(channel, part))
4513 return TRUE;
4514 if (part < PART_ERR)
4515 ++part;
4516 else
4517 {
4518 channel = channel->ch_next;
4519 part = PART_SOCK;
4520 }
4521 }
4522 return FALSE;
4523}
4524
4525/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004526 * Mark references to lists used in channels.
4527 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004528 int
4529set_ref_in_channel(int copyID)
4530{
Bram Moolenaar77073442016-02-13 23:23:53 +01004531 int abort = FALSE;
4532 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004533 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004534
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004535 for (channel = first_channel; !abort && channel != NULL;
4536 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004537 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004538 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004539 tv.v_type = VAR_CHANNEL;
4540 tv.vval.v_channel = channel;
4541 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004542 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004543 return abort;
4544}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004545
4546/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004547 * Return the "part" to write to for "channel".
4548 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004549 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004550channel_part_send(channel_T *channel)
4551{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004552 if (channel->CH_SOCK_FD == INVALID_FD)
4553 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004554 return PART_SOCK;
4555}
4556
4557/*
4558 * Return the default "part" to read from for "channel".
4559 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004560 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004561channel_part_read(channel_T *channel)
4562{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004563 if (channel->CH_SOCK_FD == INVALID_FD)
4564 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004565 return PART_SOCK;
4566}
4567
4568/*
4569 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004570 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004571 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004572 static ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004573channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004574{
Bram Moolenaar77073442016-02-13 23:23:53 +01004575 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004576 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004577 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004578}
4579
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004580/*
4581 * Return the timeout of "channel"/"part"
4582 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004583 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004584channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004585{
4586 return channel->ch_part[part].ch_timeout;
4587}
4588
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004589 static int
4590handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004592 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004593
4594 opt->jo_set |= jo;
4595 if (STRCMP(val, "nl") == 0)
4596 *modep = MODE_NL;
4597 else if (STRCMP(val, "raw") == 0)
4598 *modep = MODE_RAW;
4599 else if (STRCMP(val, "js") == 0)
4600 *modep = MODE_JS;
4601 else if (STRCMP(val, "json") == 0)
4602 *modep = MODE_JSON;
4603 else
4604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004605 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606 return FAIL;
4607 }
4608 return OK;
4609}
4610
4611 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004612handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004613{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004614 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004615
4616 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4617 if (STRCMP(val, "null") == 0)
4618 opt->jo_io[part] = JIO_NULL;
4619 else if (STRCMP(val, "pipe") == 0)
4620 opt->jo_io[part] = JIO_PIPE;
4621 else if (STRCMP(val, "file") == 0)
4622 opt->jo_io[part] = JIO_FILE;
4623 else if (STRCMP(val, "buffer") == 0)
4624 opt->jo_io[part] = JIO_BUFFER;
4625 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4626 opt->jo_io[part] = JIO_OUT;
4627 else
4628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004629 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004630 return FAIL;
4631 }
4632 return OK;
4633}
4634
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004635/*
4636 * Clear a jobopt_T before using it.
4637 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004638 void
4639clear_job_options(jobopt_T *opt)
4640{
4641 vim_memset(opt, 0, sizeof(jobopt_T));
4642}
4643
4644/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004645 * Free any members of a jobopt_T.
4646 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004647 static void
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004648free_job_options(jobopt_T *opt)
4649{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004650 if (opt->jo_callback.cb_partial != NULL)
4651 partial_unref(opt->jo_callback.cb_partial);
4652 else if (opt->jo_callback.cb_name != NULL)
4653 func_unref(opt->jo_callback.cb_name);
4654 if (opt->jo_out_cb.cb_partial != NULL)
4655 partial_unref(opt->jo_out_cb.cb_partial);
4656 else if (opt->jo_out_cb.cb_name != NULL)
4657 func_unref(opt->jo_out_cb.cb_name);
4658 if (opt->jo_err_cb.cb_partial != NULL)
4659 partial_unref(opt->jo_err_cb.cb_partial);
4660 else if (opt->jo_err_cb.cb_name != NULL)
4661 func_unref(opt->jo_err_cb.cb_name);
4662 if (opt->jo_close_cb.cb_partial != NULL)
4663 partial_unref(opt->jo_close_cb.cb_partial);
4664 else if (opt->jo_close_cb.cb_name != NULL)
4665 func_unref(opt->jo_close_cb.cb_name);
4666 if (opt->jo_exit_cb.cb_partial != NULL)
4667 partial_unref(opt->jo_exit_cb.cb_partial);
4668 else if (opt->jo_exit_cb.cb_name != NULL)
4669 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004670 if (opt->jo_env != NULL)
4671 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004672}
4673
4674/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004675 * Get the PART_ number from the first character of an option name.
4676 */
4677 static int
4678part_from_char(int c)
4679{
4680 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4681}
4682
4683/*
4684 * Get the option entries from the dict in "tv", parse them and put the result
4685 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004686 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004687 * If an option value is invalid return FAIL.
4688 */
4689 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004690get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691{
4692 typval_T *item;
4693 char_u *val;
4694 dict_T *dict;
4695 int todo;
4696 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004697 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004698
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004699 if (tv->v_type == VAR_UNKNOWN)
4700 return OK;
4701 if (tv->v_type != VAR_DICT)
4702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004703 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004704 return FAIL;
4705 }
4706 dict = tv->vval.v_dict;
4707 if (dict == NULL)
4708 return OK;
4709
4710 todo = (int)dict->dv_hashtab.ht_used;
4711 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4712 if (!HASHITEM_EMPTY(hi))
4713 {
4714 item = &dict_lookup(hi)->di_tv;
4715
4716 if (STRCMP(hi->hi_key, "mode") == 0)
4717 {
4718 if (!(supported & JO_MODE))
4719 break;
4720 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4721 return FAIL;
4722 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004723 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004724 {
4725 if (!(supported & JO_IN_MODE))
4726 break;
4727 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4728 == FAIL)
4729 return FAIL;
4730 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004731 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004732 {
4733 if (!(supported & JO_OUT_MODE))
4734 break;
4735 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4736 == FAIL)
4737 return FAIL;
4738 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004739 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004740 {
4741 if (!(supported & JO_ERR_MODE))
4742 break;
4743 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4744 == FAIL)
4745 return FAIL;
4746 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004747 else if (STRCMP(hi->hi_key, "noblock") == 0)
4748 {
4749 if (!(supported & JO_MODE))
4750 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004751 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004752 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004753 else if (STRCMP(hi->hi_key, "in_io") == 0
4754 || STRCMP(hi->hi_key, "out_io") == 0
4755 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004756 {
4757 if (!(supported & JO_OUT_IO))
4758 break;
4759 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4760 return FAIL;
4761 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004762 else if (STRCMP(hi->hi_key, "in_name") == 0
4763 || STRCMP(hi->hi_key, "out_name") == 0
4764 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004765 {
4766 part = part_from_char(*hi->hi_key);
4767
4768 if (!(supported & JO_OUT_IO))
4769 break;
4770 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4771 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004772 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004773 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004774 else if (STRCMP(hi->hi_key, "pty") == 0)
4775 {
4776 if (!(supported & JO_MODE))
4777 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004778 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004779 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004780 else if (STRCMP(hi->hi_key, "in_buf") == 0
4781 || STRCMP(hi->hi_key, "out_buf") == 0
4782 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004783 {
4784 part = part_from_char(*hi->hi_key);
4785
4786 if (!(supported & JO_OUT_IO))
4787 break;
4788 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004789 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 if (opt->jo_io_buf[part] <= 0)
4791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004792 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004793 return FAIL;
4794 }
4795 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004797 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004798 return FAIL;
4799 }
4800 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004801 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4802 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4803 {
4804 part = part_from_char(*hi->hi_key);
4805
4806 if (!(supported & JO_OUT_IO))
4807 break;
4808 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004809 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004810 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004811 else if (STRCMP(hi->hi_key, "out_msg") == 0
4812 || STRCMP(hi->hi_key, "err_msg") == 0)
4813 {
4814 part = part_from_char(*hi->hi_key);
4815
4816 if (!(supported & JO_OUT_IO))
4817 break;
4818 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004819 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004820 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004821 else if (STRCMP(hi->hi_key, "in_top") == 0
4822 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004823 {
4824 linenr_T *lp;
4825
4826 if (!(supported & JO_OUT_IO))
4827 break;
4828 if (hi->hi_key[3] == 't')
4829 {
4830 lp = &opt->jo_in_top;
4831 opt->jo_set |= JO_IN_TOP;
4832 }
4833 else
4834 {
4835 lp = &opt->jo_in_bot;
4836 opt->jo_set |= JO_IN_BOT;
4837 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004839 if (*lp < 0)
4840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004841 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004842 return FAIL;
4843 }
4844 }
4845 else if (STRCMP(hi->hi_key, "channel") == 0)
4846 {
4847 if (!(supported & JO_OUT_IO))
4848 break;
4849 opt->jo_set |= JO_CHANNEL;
4850 if (item->v_type != VAR_CHANNEL)
4851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004853 return FAIL;
4854 }
4855 opt->jo_channel = item->vval.v_channel;
4856 }
4857 else if (STRCMP(hi->hi_key, "callback") == 0)
4858 {
4859 if (!(supported & JO_CALLBACK))
4860 break;
4861 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004862 opt->jo_callback = get_callback(item);
4863 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 return FAIL;
4867 }
4868 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004869 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004870 {
4871 if (!(supported & JO_OUT_CALLBACK))
4872 break;
4873 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004874 opt->jo_out_cb = get_callback(item);
4875 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004877 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004878 return FAIL;
4879 }
4880 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004881 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004882 {
4883 if (!(supported & JO_ERR_CALLBACK))
4884 break;
4885 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004886 opt->jo_err_cb = get_callback(item);
4887 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004889 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004890 return FAIL;
4891 }
4892 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004893 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004894 {
4895 if (!(supported & JO_CLOSE_CALLBACK))
4896 break;
4897 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004898 opt->jo_close_cb = get_callback(item);
4899 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004901 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004902 return FAIL;
4903 }
4904 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004905 else if (STRCMP(hi->hi_key, "drop") == 0)
4906 {
4907 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004908 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004909
4910 if (STRCMP(val, "never") == 0)
4911 never = TRUE;
4912 else if (STRCMP(val, "auto") != 0)
4913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004914 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004915 return FAIL;
4916 }
4917 opt->jo_drop_never = never;
4918 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004919 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4920 {
4921 if (!(supported & JO_EXIT_CB))
4922 break;
4923 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004924 opt->jo_exit_cb = get_callback(item);
4925 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004927 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004928 return FAIL;
4929 }
4930 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004931#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004932 else if (STRCMP(hi->hi_key, "term_name") == 0)
4933 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004934 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004935 break;
4936 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004937 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004938 if (opt->jo_term_name == NULL)
4939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004940 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004941 return FAIL;
4942 }
4943 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004944 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4945 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004946 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004947 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004948 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004949 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004951 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004952 return FAIL;
4953 }
4954 opt->jo_set2 |= JO2_TERM_FINISH;
4955 opt->jo_term_finish = *val;
4956 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004957 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4958 {
4959 char_u *p;
4960
4961 if (!(supported2 & JO2_TERM_OPENCMD))
4962 break;
4963 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004964 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004965 if (p != NULL)
4966 {
4967 /* Must have %d and no other %. */
4968 p = vim_strchr(p, '%');
4969 if (p != NULL && (p[1] != 'd'
4970 || vim_strchr(p + 2, '%') != NULL))
4971 p = NULL;
4972 }
4973 if (p == NULL)
4974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004975 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004976 return FAIL;
4977 }
4978 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004979 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4980 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004981 char_u *p;
4982
4983 if (!(supported2 & JO2_EOF_CHARS))
4984 break;
4985 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004986 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004987 if (p == NULL)
4988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004989 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004990 return FAIL;
4991 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004992 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004993 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4994 {
4995 if (!(supported2 & JO2_TERM_ROWS))
4996 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004997 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004998 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004999 }
5000 else if (STRCMP(hi->hi_key, "term_cols") == 0)
5001 {
5002 if (!(supported2 & JO2_TERM_COLS))
5003 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005004 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005005 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005006 }
5007 else if (STRCMP(hi->hi_key, "vertical") == 0)
5008 {
5009 if (!(supported2 & JO2_VERTICAL))
5010 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005011 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005013 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005014 else if (STRCMP(hi->hi_key, "curwin") == 0)
5015 {
5016 if (!(supported2 & JO2_CURWIN))
5017 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005018 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005020 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005021 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5022 {
5023 int nr;
5024
5025 if (!(supported2 & JO2_CURWIN))
5026 break;
5027 opt->jo_set2 |= JO2_BUFNR;
5028 nr = tv_get_number(item);
5029 if (nr <= 0)
5030 {
5031 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5032 return FAIL;
5033 }
5034 opt->jo_bufnr_buf = buflist_findnr(nr);
5035 if (opt->jo_bufnr_buf == NULL)
5036 {
5037 semsg(_(e_nobufnr), (long)nr);
5038 return FAIL;
5039 }
5040 if (opt->jo_bufnr_buf->b_nwindows == 0
5041 || opt->jo_bufnr_buf->b_term == NULL)
5042 {
5043 semsg(_(e_invarg2), "bufnr");
5044 return FAIL;
5045 }
5046 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005047 else if (STRCMP(hi->hi_key, "hidden") == 0)
5048 {
5049 if (!(supported2 & JO2_HIDDEN))
5050 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005051 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005052 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005053 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005054 else if (STRCMP(hi->hi_key, "norestore") == 0)
5055 {
5056 if (!(supported2 & JO2_NORESTORE))
5057 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005058 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005059 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005060 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005061 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5062 {
5063 if (!(supported2 & JO2_TERM_KILL))
5064 break;
5065 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005066 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005067 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005068 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005069 {
5070 char_u *p;
5071
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005072 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005073 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005074 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005075 p = tv_get_string_chk(item);
5076 if (p == NULL)
5077 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005078 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005079 return FAIL;
5080 }
5081 // Allow empty string, "winpty", "conpty".
5082 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5083 || STRCMP(p, "conpty") == 0))
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 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005088 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005089 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005090# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5091 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5092 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005093 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005094 listitem_T *li;
5095 long_u rgb[16];
5096
5097 if (!(supported2 & JO2_ANSI_COLORS))
5098 break;
5099
5100 if (item == NULL || item->v_type != VAR_LIST
5101 || item->vval.v_list == NULL)
5102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005103 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005104 return FAIL;
5105 }
5106
5107 li = item->vval.v_list->lv_first;
5108 for (; li != NULL && n < 16; li = li->li_next, n++)
5109 {
5110 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005113 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005114 if (color_name == NULL)
5115 return FAIL;
5116
5117 guicolor = GUI_GET_COLOR(color_name);
5118 if (guicolor == INVALCOLOR)
5119 return FAIL;
5120
5121 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5122 }
5123
5124 if (n != 16 || li != NULL)
5125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005126 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005127 return FAIL;
5128 }
5129
5130 opt->jo_set2 |= JO2_ANSI_COLORS;
5131 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5132 }
5133# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005134#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005135 else if (STRCMP(hi->hi_key, "env") == 0)
5136 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005137 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005138 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005139 if (item->v_type != VAR_DICT)
5140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005141 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005142 return FAIL;
5143 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005144 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005145 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005146 if (opt->jo_env != NULL)
5147 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005148 }
5149 else if (STRCMP(hi->hi_key, "cwd") == 0)
5150 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005151 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005152 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005153 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005154 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005155#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005156 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5157#endif
5158 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005160 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005161 return FAIL;
5162 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005163 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005164 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005165 else if (STRCMP(hi->hi_key, "waittime") == 0)
5166 {
5167 if (!(supported & JO_WAITTIME))
5168 break;
5169 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005170 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005171 }
5172 else if (STRCMP(hi->hi_key, "timeout") == 0)
5173 {
5174 if (!(supported & JO_TIMEOUT))
5175 break;
5176 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005177 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005178 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005179 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005180 {
5181 if (!(supported & JO_OUT_TIMEOUT))
5182 break;
5183 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005184 opt->jo_out_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, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005187 {
5188 if (!(supported & JO_ERR_TIMEOUT))
5189 break;
5190 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005192 }
5193 else if (STRCMP(hi->hi_key, "part") == 0)
5194 {
5195 if (!(supported & JO_PART))
5196 break;
5197 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005199 if (STRCMP(val, "err") == 0)
5200 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005201 else if (STRCMP(val, "out") == 0)
5202 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005203 else
5204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005205 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005206 return FAIL;
5207 }
5208 }
5209 else if (STRCMP(hi->hi_key, "id") == 0)
5210 {
5211 if (!(supported & JO_ID))
5212 break;
5213 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005214 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005215 }
5216 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5217 {
5218 if (!(supported & JO_STOPONEXIT))
5219 break;
5220 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005221 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005222 opt->jo_soe_buf);
5223 if (opt->jo_stoponexit == NULL)
5224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005225 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005226 return FAIL;
5227 }
5228 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005229 else if (STRCMP(hi->hi_key, "block_write") == 0)
5230 {
5231 if (!(supported & JO_BLOCK_WRITE))
5232 break;
5233 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005234 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005235 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005236 else
5237 break;
5238 --todo;
5239 }
5240 if (todo > 0)
5241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005242 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005243 return FAIL;
5244 }
5245
5246 return OK;
5247}
5248
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005249static job_T *first_job = NULL;
5250
5251 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005252job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005253{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005254 int i;
5255
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005256 ch_log(job->jv_channel, "Freeing job");
5257 if (job->jv_channel != NULL)
5258 {
5259 /* The link from the channel to the job doesn't count as a reference,
5260 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005261 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005262 * NULL the reference. We don't set ch_job_killed, unreferencing the
5263 * job doesn't mean it stops running. */
5264 job->jv_channel->ch_job = NULL;
5265 channel_unref(job->jv_channel);
5266 }
5267 mch_clear_job(job);
5268
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005269 vim_free(job->jv_tty_in);
5270 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005271 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005272#ifdef UNIX
5273 vim_free(job->jv_termsig);
5274#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005275#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005276 vim_free(job->jv_tty_type);
5277#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005278 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005279 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005280 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005281 for (i = 0; job->jv_argv[i] != NULL; i++)
5282 vim_free(job->jv_argv[i]);
5283 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005284 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005285}
5286
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005287/*
5288 * Remove "job" from the list of jobs.
5289 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005290 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005291job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005292{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005293 if (job->jv_next != NULL)
5294 job->jv_next->jv_prev = job->jv_prev;
5295 if (job->jv_prev == NULL)
5296 first_job = job->jv_next;
5297 else
5298 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005299}
5300
5301 static void
5302job_free_job(job_T *job)
5303{
5304 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005305 vim_free(job);
5306}
5307
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005308 static void
5309job_free(job_T *job)
5310{
5311 if (!in_free_unref_items)
5312 {
5313 job_free_contents(job);
5314 job_free_job(job);
5315 }
5316}
5317
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005318static job_T *jobs_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005319
5320/*
5321 * Put "job" in a list to be freed later, when it's no longer referenced.
5322 */
5323 static void
5324job_free_later(job_T *job)
5325{
5326 job_unlink(job);
5327 job->jv_next = jobs_to_free;
5328 jobs_to_free = job;
5329}
5330
5331 static void
5332free_jobs_to_free_later(void)
5333{
5334 job_T *job;
5335
5336 while (jobs_to_free != NULL)
5337 {
5338 job = jobs_to_free;
5339 jobs_to_free = job->jv_next;
5340 job_free_contents(job);
5341 vim_free(job);
5342 }
5343}
5344
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005345#if defined(EXITFREE) || defined(PROTO)
5346 void
5347job_free_all(void)
5348{
5349 while (first_job != NULL)
5350 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005351 free_jobs_to_free_later();
5352
5353# ifdef FEAT_TERMINAL
5354 free_unused_terminals();
5355# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005356}
5357#endif
5358
5359/*
5360 * Return TRUE if we need to check if the process of "job" has ended.
5361 */
5362 static int
5363job_need_end_check(job_T *job)
5364{
5365 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005366 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005367}
5368
5369/*
5370 * Return TRUE if the channel of "job" is still useful.
5371 */
5372 static int
5373job_channel_still_useful(job_T *job)
5374{
5375 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5376}
5377
5378/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005379 * Return TRUE if the channel of "job" is closeable.
5380 */
5381 static int
5382job_channel_can_close(job_T *job)
5383{
5384 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5385}
5386
5387/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005388 * Return TRUE if the job should not be freed yet. Do not free the job when
5389 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5390 * or when the associated channel will do something with the job output.
5391 */
5392 static int
5393job_still_useful(job_T *job)
5394{
5395 return job_need_end_check(job) || job_channel_still_useful(job);
5396}
5397
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005398#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005399/*
5400 * Return TRUE when there is any running job that we care about.
5401 */
5402 int
5403job_any_running()
5404{
5405 job_T *job;
5406
5407 for (job = first_job; job != NULL; job = job->jv_next)
5408 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005409 {
5410 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005411 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005412 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005413 return FALSE;
5414}
5415#endif
5416
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005417#if !defined(USE_ARGV) || defined(PROTO)
5418/*
5419 * Escape one argument for an external command.
5420 * Returns the escaped string in allocated memory. NULL when out of memory.
5421 */
5422 static char_u *
5423win32_escape_arg(char_u *arg)
5424{
5425 int slen, dlen;
5426 int escaping = 0;
5427 int i;
5428 char_u *s, *d;
5429 char_u *escaped_arg;
5430 int has_spaces = FALSE;
5431
5432 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005433 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005434 dlen = slen;
5435 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5436 {
5437 if (*s == '"' || *s == '\\')
5438 ++dlen;
5439 if (*s == ' ' || *s == '\t')
5440 has_spaces = TRUE;
5441 }
5442
5443 if (has_spaces)
5444 dlen += 2;
5445
5446 if (dlen == slen)
5447 return vim_strsave(arg);
5448
5449 /* Allocate memory for the result and fill it. */
5450 escaped_arg = alloc(dlen + 1);
5451 if (escaped_arg == NULL)
5452 return NULL;
5453 memset(escaped_arg, 0, dlen+1);
5454
5455 d = escaped_arg;
5456
5457 if (has_spaces)
5458 *d++ = '"';
5459
5460 for (s = arg; *s != NUL;)
5461 {
5462 switch (*s)
5463 {
5464 case '"':
5465 for (i = 0; i < escaping; i++)
5466 *d++ = '\\';
5467 escaping = 0;
5468 *d++ = '\\';
5469 *d++ = *s++;
5470 break;
5471 case '\\':
5472 escaping++;
5473 *d++ = *s++;
5474 break;
5475 default:
5476 escaping = 0;
5477 MB_COPY_CHAR(s, d);
5478 break;
5479 }
5480 }
5481
5482 /* add terminating quote and finish with a NUL */
5483 if (has_spaces)
5484 {
5485 for (i = 0; i < escaping; i++)
5486 *d++ = '\\';
5487 *d++ = '"';
5488 }
5489 *d = NUL;
5490
5491 return escaped_arg;
5492}
5493
5494/*
5495 * Build a command line from a list, taking care of escaping.
5496 * The result is put in gap->ga_data.
5497 * Returns FAIL when out of memory.
5498 */
5499 int
5500win32_build_cmd(list_T *l, garray_T *gap)
5501{
5502 listitem_T *li;
5503 char_u *s;
5504
5505 for (li = l->lv_first; li != NULL; li = li->li_next)
5506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005507 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005508 if (s == NULL)
5509 return FAIL;
5510 s = win32_escape_arg(s);
5511 if (s == NULL)
5512 return FAIL;
5513 ga_concat(gap, s);
5514 vim_free(s);
5515 if (li->li_next != NULL)
5516 ga_append(gap, ' ');
5517 }
5518 return OK;
5519}
5520#endif
5521
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005522/*
5523 * NOTE: Must call job_cleanup() only once right after the status of "job"
5524 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5525 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005526 * If the job is no longer used it will be removed from the list of jobs, and
5527 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005528 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005529 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005530job_cleanup(job_T *job)
5531{
5532 if (job->jv_status != JOB_ENDED)
5533 return;
5534
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005535 /* Ready to cleanup the job. */
5536 job->jv_status = JOB_FINISHED;
5537
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005538 /* When only channel-in is kept open, close explicitly. */
5539 if (job->jv_channel != NULL)
5540 ch_close_part(job->jv_channel, PART_IN);
5541
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005542 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005543 {
5544 typval_T argv[3];
5545 typval_T rettv;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005546
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005547 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005548 ch_log(job->jv_channel, "Invoking exit callback %s",
5549 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005550 ++job->jv_refcount;
5551 argv[0].v_type = VAR_JOB;
5552 argv[0].vval.v_job = job;
5553 argv[1].v_type = VAR_NUMBER;
5554 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005555 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005556 clear_tv(&rettv);
5557 --job->jv_refcount;
5558 channel_need_redraw = TRUE;
5559 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005560
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005561 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5562 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005563
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005564 // Do not free the job in case the close callback of the associated channel
5565 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005566 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005567 // The job was already unreferenced and the associated channel was
5568 // detached, now that it ended it can be freed. However, a caller might
5569 // still use it, thus free it a bit later.
5570 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005571}
5572
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005573/*
5574 * Mark references in jobs that are still useful.
5575 */
5576 int
5577set_ref_in_job(int copyID)
5578{
5579 int abort = FALSE;
5580 job_T *job;
5581 typval_T tv;
5582
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005583 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005584 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005585 {
5586 tv.v_type = VAR_JOB;
5587 tv.vval.v_job = job;
5588 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5589 }
5590 return abort;
5591}
5592
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005593/*
5594 * Dereference "job". Note that after this "job" may have been freed.
5595 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005596 void
5597job_unref(job_T *job)
5598{
5599 if (job != NULL && --job->jv_refcount <= 0)
5600 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005601 /* Do not free the job if there is a channel where the close callback
5602 * may get the job info. */
5603 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005604 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005605 /* Do not free the job when it has not ended yet and there is a
5606 * "stoponexit" flag or an exit callback. */
5607 if (!job_need_end_check(job))
5608 {
5609 job_free(job);
5610 }
5611 else if (job->jv_channel != NULL)
5612 {
5613 /* Do remove the link to the channel, otherwise it hangs
5614 * around until Vim exits. See job_free() for refcount. */
5615 ch_log(job->jv_channel, "detaching channel from job");
5616 job->jv_channel->ch_job = NULL;
5617 channel_unref(job->jv_channel);
5618 job->jv_channel = NULL;
5619 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005620 }
5621 }
5622}
5623
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005624 int
5625free_unused_jobs_contents(int copyID, int mask)
5626{
5627 int did_free = FALSE;
5628 job_T *job;
5629
5630 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005631 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005632 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005633 {
5634 /* Free the channel and ordinary items it contains, but don't
5635 * recurse into Lists, Dictionaries etc. */
5636 job_free_contents(job);
5637 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005638 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005639 return did_free;
5640}
5641
5642 void
5643free_unused_jobs(int copyID, int mask)
5644{
5645 job_T *job;
5646 job_T *job_next;
5647
5648 for (job = first_job; job != NULL; job = job_next)
5649 {
5650 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005651 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005652 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005653 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005654 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005655 job_free_job(job);
5656 }
5657 }
5658}
5659
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005660/*
5661 * Allocate a job. Sets the refcount to one and sets options default.
5662 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005663 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005664job_alloc(void)
5665{
5666 job_T *job;
5667
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005668 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005669 if (job != NULL)
5670 {
5671 job->jv_refcount = 1;
5672 job->jv_stoponexit = vim_strsave((char_u *)"term");
5673
5674 if (first_job != NULL)
5675 {
5676 first_job->jv_prev = job;
5677 job->jv_next = first_job;
5678 }
5679 first_job = job;
5680 }
5681 return job;
5682}
5683
5684 void
5685job_set_options(job_T *job, jobopt_T *opt)
5686{
5687 if (opt->jo_set & JO_STOPONEXIT)
5688 {
5689 vim_free(job->jv_stoponexit);
5690 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5691 job->jv_stoponexit = NULL;
5692 else
5693 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5694 }
5695 if (opt->jo_set & JO_EXIT_CB)
5696 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005697 free_callback(&job->jv_exit_cb);
5698 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005699 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005700 job->jv_exit_cb.cb_name = NULL;
5701 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005702 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005703 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005704 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005705 }
5706}
5707
5708/*
5709 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5710 */
5711 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005712job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005713{
5714 job_T *job;
5715
5716 for (job = first_job; job != NULL; job = job->jv_next)
5717 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005718 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005719}
5720
5721/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005722 * Return TRUE when there is any job that has an exit callback and might exit,
5723 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005724 */
5725 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005726has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005727{
5728 job_T *job;
5729
5730 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005731 /* Only should check if the channel has been closed, if the channel is
5732 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005733 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5734 || (job->jv_status == JOB_FINISHED
5735 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005736 return TRUE;
5737 return FALSE;
5738}
5739
Bram Moolenaar97792de2016-10-15 18:36:49 +02005740#define MAX_CHECK_ENDED 8
5741
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005742/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005743 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005744 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005745 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005746 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005747job_check_ended(void)
5748{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005749 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005750 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005751
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005752 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005753 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005754 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005755
Bram Moolenaar97792de2016-10-15 18:36:49 +02005756 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005757 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005758 // NOTE: mch_detect_ended_job() must only return a job of which the
5759 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005760 job_T *job = mch_detect_ended_job(first_job);
5761
5762 if (job == NULL)
5763 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005764 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005765 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005766 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005767
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005768 // Actually free jobs that were cleaned up.
5769 free_jobs_to_free_later();
5770
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005771 if (channel_need_redraw)
5772 {
5773 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005774 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005775 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005776 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005777}
5778
5779/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005780 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005781 * "argv_arg" is only for Unix.
5782 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005783 * The returned job has a refcount of one.
5784 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005785 */
5786 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005787job_start(
5788 typval_T *argvars,
5789 char **argv_arg,
5790 jobopt_T *opt_arg,
5791 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005792{
5793 job_T *job;
5794 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005795 char **argv = NULL;
5796 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005797#if defined(UNIX)
5798# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005799 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005800#else
5801 garray_T ga;
5802#endif
5803 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005804 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005805
5806 job = job_alloc();
5807 if (job == NULL)
5808 return NULL;
5809
5810 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005811#ifndef USE_ARGV
5812 ga_init2(&ga, (int)sizeof(char*), 20);
5813#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005814
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005815 if (opt_arg != NULL)
5816 opt = *opt_arg;
5817 else
5818 {
5819 /* Default mode is NL. */
5820 clear_job_options(&opt);
5821 opt.jo_mode = MODE_NL;
5822 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005823 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5824 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5825 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005826 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005827 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005828
5829 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005830 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005831 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5832 && opt.jo_io[part] == JIO_FILE
5833 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5834 || *opt.jo_io_name[part] == NUL))
5835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005836 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005837 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005838 }
5839
5840 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5841 {
5842 buf_T *buf = NULL;
5843
5844 /* check that we can find the buffer before starting the job */
5845 if (opt.jo_set & JO_IN_BUF)
5846 {
5847 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5848 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005849 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005850 }
5851 else if (!(opt.jo_set & JO_IN_NAME))
5852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005853 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005854 }
5855 else
5856 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5857 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005858 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005859 if (buf->b_ml.ml_mfp == NULL)
5860 {
5861 char_u numbuf[NUMBUFLEN];
5862 char_u *s;
5863
5864 if (opt.jo_set & JO_IN_BUF)
5865 {
5866 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5867 s = numbuf;
5868 }
5869 else
5870 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005871 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005872 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005873 }
5874 job->jv_in_buf = buf;
5875 }
5876
5877 job_set_options(job, &opt);
5878
Bram Moolenaar13568252018-03-16 20:46:58 +01005879#ifdef USE_ARGV
5880 if (argv_arg != NULL)
5881 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005882 /* Make a copy of argv_arg for job->jv_argv. */
5883 for (i = 0; argv_arg[i] != NULL; i++)
5884 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005885 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005886 if (argv == NULL)
5887 goto theend;
5888 for (i = 0; i < argc; i++)
5889 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5890 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005891 }
5892 else
5893#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005894 if (argvars[0].v_type == VAR_STRING)
5895 {
5896 /* Command is a string. */
5897 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005898 if (cmd == NULL || *cmd == NUL)
5899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005900 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005901 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005902 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005903
5904 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005905 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005906 }
5907 else if (argvars[0].v_type != VAR_LIST
5908 || argvars[0].vval.v_list == NULL
5909 || argvars[0].vval.v_list->lv_len < 1)
5910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005911 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005912 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005913 }
5914 else
5915 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005916 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005917
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005918 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005919 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005920#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005921 if (win32_build_cmd(l, &ga) == FAIL)
5922 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005923 cmd = ga.ga_data;
5924#endif
5925 }
5926
Bram Moolenaar20608922018-04-21 22:30:08 +02005927 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005928 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005929
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005930#ifdef USE_ARGV
5931 if (ch_log_active())
5932 {
5933 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005934
5935 ga_init2(&ga, (int)sizeof(char), 200);
5936 for (i = 0; i < argc; ++i)
5937 {
5938 if (i > 0)
5939 ga_concat(&ga, (char_u *)" ");
5940 ga_concat(&ga, (char_u *)argv[i]);
5941 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005942 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005943 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005944 ga_clear(&ga);
5945 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005946 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005947#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005948 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005949 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005950#endif
5951
5952 /* If the channel is reading from a buffer, write lines now. */
5953 if (job->jv_channel != NULL)
5954 channel_write_in(job->jv_channel);
5955
5956theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005957#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005958 vim_free(ga.ga_data);
5959#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005960 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005961 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005962 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005963 return job;
5964}
5965
5966/*
5967 * Get the status of "job" and invoke the exit callback when needed.
5968 * The returned string is not allocated.
5969 */
5970 char *
5971job_status(job_T *job)
5972{
5973 char *result;
5974
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005975 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005976 /* No need to check, dead is dead. */
5977 result = "dead";
5978 else if (job->jv_status == JOB_FAILED)
5979 result = "fail";
5980 else
5981 {
5982 result = mch_job_status(job);
5983 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005984 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005985 }
5986 return result;
5987}
5988
Bram Moolenaar8950a562016-03-12 15:22:55 +01005989/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005990 * Send a signal to "job". Implements job_stop().
5991 * When "type" is not NULL use this for the type.
5992 * Otherwise use argvars[1] for the type.
5993 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005994 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005995job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005996{
5997 char_u *arg;
5998
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005999 if (type != NULL)
6000 arg = (char_u *)type;
6001 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006002 arg = (char_u *)"";
6003 else
6004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006005 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006006 if (arg == NULL)
6007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006008 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006009 return 0;
6010 }
6011 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006012 if (job->jv_status == JOB_FAILED)
6013 {
6014 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6015 return 0;
6016 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006017 if (job->jv_status == JOB_ENDED)
6018 {
6019 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6020 return 0;
6021 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006022 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006023 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006024 return 0;
6025
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006026 /* Assume that only "kill" will kill the job. */
6027 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006028 job->jv_channel->ch_job_killed = TRUE;
6029
6030 /* We don't try freeing the job, obviously the caller still has a
6031 * reference to it. */
6032 return 1;
6033}
6034
Bram Moolenaarf2732452018-06-03 14:47:35 +02006035 void
6036invoke_prompt_callback(void)
6037{
6038 typval_T rettv;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006039 typval_T argv[2];
6040 char_u *text;
6041 char_u *prompt;
6042 linenr_T lnum = curbuf->b_ml.ml_line_count;
6043
6044 // Add a new line for the prompt before invoking the callback, so that
6045 // text can always be inserted above the last line.
6046 ml_append(lnum, (char_u *)"", 0, FALSE);
6047 curwin->w_cursor.lnum = lnum + 1;
6048 curwin->w_cursor.col = 0;
6049
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006050 if (curbuf->b_prompt_callback.cb_name == NULL
6051 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006052 return;
6053 text = ml_get(lnum);
6054 prompt = prompt_text();
6055 if (STRLEN(text) >= STRLEN(prompt))
6056 text += STRLEN(prompt);
6057 argv[0].v_type = VAR_STRING;
6058 argv[0].vval.v_string = vim_strsave(text);
6059 argv[1].v_type = VAR_UNKNOWN;
6060
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006061 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006062 clear_tv(&argv[0]);
6063 clear_tv(&rettv);
6064}
6065
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006066/*
6067 * Return TRUE when the interrupt callback was invoked.
6068 */
6069 int
6070invoke_prompt_interrupt(void)
6071{
6072 typval_T rettv;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006073 typval_T argv[1];
6074
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006075 if (curbuf->b_prompt_interrupt.cb_name == NULL
6076 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006077 return FALSE;
6078 argv[0].v_type = VAR_UNKNOWN;
6079
6080 got_int = FALSE; // don't skip executing commands
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006081 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006082 clear_tv(&rettv);
6083 return TRUE;
6084}
6085
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006086/*
6087 * "prompt_setcallback({buffer}, {callback})" function
6088 */
6089 void
6090f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6091{
6092 buf_T *buf;
6093 callback_T callback;
6094
6095 if (check_secure())
6096 return;
6097 buf = tv_get_buf(&argvars[0], FALSE);
6098 if (buf == NULL)
6099 return;
6100
6101 callback = get_callback(&argvars[1]);
6102 if (callback.cb_name == NULL)
6103 return;
6104
6105 free_callback(&buf->b_prompt_callback);
6106 set_callback(&buf->b_prompt_callback, &callback);
6107}
6108
6109/*
6110 * "prompt_setinterrupt({buffer}, {callback})" function
6111 */
6112 void
6113f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6114{
6115 buf_T *buf;
6116 callback_T callback;
6117
6118 if (check_secure())
6119 return;
6120 buf = tv_get_buf(&argvars[0], FALSE);
6121 if (buf == NULL)
6122 return;
6123
6124 callback = get_callback(&argvars[1]);
6125 if (callback.cb_name == NULL)
6126 return;
6127
6128 free_callback(&buf->b_prompt_interrupt);
6129 set_callback(&buf->b_prompt_interrupt, &callback);
6130}
6131
6132/*
6133 * "prompt_setprompt({buffer}, {text})" function
6134 */
6135 void
6136f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6137{
6138 buf_T *buf;
6139 char_u *text;
6140
6141 if (check_secure())
6142 return;
6143 buf = tv_get_buf(&argvars[0], FALSE);
6144 if (buf == NULL)
6145 return;
6146
6147 text = tv_get_string(&argvars[1]);
6148 vim_free(buf->b_prompt_text);
6149 buf->b_prompt_text = vim_strsave(text);
6150}
6151
6152/*
6153 * "ch_canread()" function
6154 */
6155 void
6156f_ch_canread(typval_T *argvars, typval_T *rettv)
6157{
6158 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6159
6160 rettv->vval.v_number = 0;
6161 if (channel != NULL)
6162 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6163 || channel_has_readahead(channel, PART_OUT)
6164 || channel_has_readahead(channel, PART_ERR);
6165}
6166
6167/*
6168 * "ch_close()" function
6169 */
6170 void
6171f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6172{
6173 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6174
6175 if (channel != NULL)
6176 {
6177 channel_close(channel, FALSE);
6178 channel_clear(channel);
6179 }
6180}
6181
6182/*
6183 * "ch_close()" function
6184 */
6185 void
6186f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6187{
6188 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6189
6190 if (channel != NULL)
6191 channel_close_in(channel);
6192}
6193
6194/*
6195 * "ch_getbufnr()" function
6196 */
6197 void
6198f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6199{
6200 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6201
6202 rettv->vval.v_number = -1;
6203 if (channel != NULL)
6204 {
6205 char_u *what = tv_get_string(&argvars[1]);
6206 int part;
6207
6208 if (STRCMP(what, "err") == 0)
6209 part = PART_ERR;
6210 else if (STRCMP(what, "out") == 0)
6211 part = PART_OUT;
6212 else if (STRCMP(what, "in") == 0)
6213 part = PART_IN;
6214 else
6215 part = PART_SOCK;
6216 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6217 rettv->vval.v_number =
6218 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6219 }
6220}
6221
6222/*
6223 * "ch_getjob()" function
6224 */
6225 void
6226f_ch_getjob(typval_T *argvars, typval_T *rettv)
6227{
6228 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6229
6230 if (channel != NULL)
6231 {
6232 rettv->v_type = VAR_JOB;
6233 rettv->vval.v_job = channel->ch_job;
6234 if (channel->ch_job != NULL)
6235 ++channel->ch_job->jv_refcount;
6236 }
6237}
6238
6239/*
6240 * "ch_info()" function
6241 */
6242 void
6243f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6244{
6245 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6246
6247 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6248 channel_info(channel, rettv->vval.v_dict);
6249}
6250
6251/*
6252 * "ch_log()" function
6253 */
6254 void
6255f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6256{
6257 char_u *msg = tv_get_string(&argvars[0]);
6258 channel_T *channel = NULL;
6259
6260 if (argvars[1].v_type != VAR_UNKNOWN)
6261 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6262
6263 ch_log(channel, "%s", msg);
6264}
6265
6266/*
6267 * "ch_logfile()" function
6268 */
6269 void
6270f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6271{
6272 char_u *fname;
6273 char_u *opt = (char_u *)"";
6274 char_u buf[NUMBUFLEN];
6275
6276 /* Don't open a file in restricted mode. */
6277 if (check_restricted() || check_secure())
6278 return;
6279 fname = tv_get_string(&argvars[0]);
6280 if (argvars[1].v_type == VAR_STRING)
6281 opt = tv_get_string_buf(&argvars[1], buf);
6282 ch_logfile(fname, opt);
6283}
6284
6285/*
6286 * "ch_open()" function
6287 */
6288 void
6289f_ch_open(typval_T *argvars, typval_T *rettv)
6290{
6291 rettv->v_type = VAR_CHANNEL;
6292 if (check_restricted() || check_secure())
6293 return;
6294 rettv->vval.v_channel = channel_open_func(argvars);
6295}
6296
6297/*
6298 * "ch_read()" function
6299 */
6300 void
6301f_ch_read(typval_T *argvars, typval_T *rettv)
6302{
6303 common_channel_read(argvars, rettv, FALSE, FALSE);
6304}
6305
6306/*
6307 * "ch_readblob()" function
6308 */
6309 void
6310f_ch_readblob(typval_T *argvars, typval_T *rettv)
6311{
6312 common_channel_read(argvars, rettv, TRUE, TRUE);
6313}
6314
6315/*
6316 * "ch_readraw()" function
6317 */
6318 void
6319f_ch_readraw(typval_T *argvars, typval_T *rettv)
6320{
6321 common_channel_read(argvars, rettv, TRUE, FALSE);
6322}
6323
6324/*
6325 * "ch_evalexpr()" function
6326 */
6327 void
6328f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6329{
6330 ch_expr_common(argvars, rettv, TRUE);
6331}
6332
6333/*
6334 * "ch_sendexpr()" function
6335 */
6336 void
6337f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6338{
6339 ch_expr_common(argvars, rettv, FALSE);
6340}
6341
6342/*
6343 * "ch_evalraw()" function
6344 */
6345 void
6346f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6347{
6348 ch_raw_common(argvars, rettv, TRUE);
6349}
6350
6351/*
6352 * "ch_sendraw()" function
6353 */
6354 void
6355f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6356{
6357 ch_raw_common(argvars, rettv, FALSE);
6358}
6359
6360/*
6361 * "ch_setoptions()" function
6362 */
6363 void
6364f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6365{
6366 channel_T *channel;
6367 jobopt_T opt;
6368
6369 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6370 if (channel == NULL)
6371 return;
6372 clear_job_options(&opt);
6373 if (get_job_options(&argvars[1], &opt,
6374 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6375 channel_set_options(channel, &opt);
6376 free_job_options(&opt);
6377}
6378
6379/*
6380 * "ch_status()" function
6381 */
6382 void
6383f_ch_status(typval_T *argvars, typval_T *rettv)
6384{
6385 channel_T *channel;
6386 jobopt_T opt;
6387 int part = -1;
6388
6389 /* return an empty string by default */
6390 rettv->v_type = VAR_STRING;
6391 rettv->vval.v_string = NULL;
6392
6393 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6394
6395 if (argvars[1].v_type != VAR_UNKNOWN)
6396 {
6397 clear_job_options(&opt);
6398 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6399 && (opt.jo_set & JO_PART))
6400 part = opt.jo_part;
6401 }
6402
6403 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6404}
6405
6406/*
6407 * Get the job from the argument.
6408 * Returns NULL if the job is invalid.
6409 */
6410 static job_T *
6411get_job_arg(typval_T *tv)
6412{
6413 job_T *job;
6414
6415 if (tv->v_type != VAR_JOB)
6416 {
6417 semsg(_(e_invarg2), tv_get_string(tv));
6418 return NULL;
6419 }
6420 job = tv->vval.v_job;
6421
6422 if (job == NULL)
6423 emsg(_("E916: not a valid job"));
6424 return job;
6425}
6426
6427/*
6428 * "job_getchannel()" function
6429 */
6430 void
6431f_job_getchannel(typval_T *argvars, typval_T *rettv)
6432{
6433 job_T *job = get_job_arg(&argvars[0]);
6434
6435 if (job != NULL)
6436 {
6437 rettv->v_type = VAR_CHANNEL;
6438 rettv->vval.v_channel = job->jv_channel;
6439 if (job->jv_channel != NULL)
6440 ++job->jv_channel->ch_refcount;
6441 }
6442}
6443
6444/*
6445 * Implementation of job_info().
6446 */
6447 static void
6448job_info(job_T *job, dict_T *dict)
6449{
6450 dictitem_T *item;
6451 varnumber_T nr;
6452 list_T *l;
6453 int i;
6454
6455 dict_add_string(dict, "status", (char_u *)job_status(job));
6456
6457 item = dictitem_alloc((char_u *)"channel");
6458 if (item == NULL)
6459 return;
6460 item->di_tv.v_type = VAR_CHANNEL;
6461 item->di_tv.vval.v_channel = job->jv_channel;
6462 if (job->jv_channel != NULL)
6463 ++job->jv_channel->ch_refcount;
6464 if (dict_add(dict, item) == FAIL)
6465 dictitem_free(item);
6466
6467#ifdef UNIX
6468 nr = job->jv_pid;
6469#else
6470 nr = job->jv_proc_info.dwProcessId;
6471#endif
6472 dict_add_number(dict, "process", nr);
6473 dict_add_string(dict, "tty_in", job->jv_tty_in);
6474 dict_add_string(dict, "tty_out", job->jv_tty_out);
6475
6476 dict_add_number(dict, "exitval", job->jv_exitval);
6477 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6478 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6479#ifdef UNIX
6480 dict_add_string(dict, "termsig", job->jv_termsig);
6481#endif
6482#ifdef MSWIN
6483 dict_add_string(dict, "tty_type", job->jv_tty_type);
6484#endif
6485
6486 l = list_alloc();
6487 if (l != NULL)
6488 {
6489 dict_add_list(dict, "cmd", l);
6490 if (job->jv_argv != NULL)
6491 for (i = 0; job->jv_argv[i] != NULL; i++)
6492 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6493 }
6494}
6495
6496/*
6497 * Implementation of job_info() to return info for all jobs.
6498 */
6499 static void
6500job_info_all(list_T *l)
6501{
6502 job_T *job;
6503 typval_T tv;
6504
6505 for (job = first_job; job != NULL; job = job->jv_next)
6506 {
6507 tv.v_type = VAR_JOB;
6508 tv.vval.v_job = job;
6509
6510 if (list_append_tv(l, &tv) != OK)
6511 return;
6512 }
6513}
6514
6515/*
6516 * "job_info()" function
6517 */
6518 void
6519f_job_info(typval_T *argvars, typval_T *rettv)
6520{
6521 if (argvars[0].v_type != VAR_UNKNOWN)
6522 {
6523 job_T *job = get_job_arg(&argvars[0]);
6524
6525 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6526 job_info(job, rettv->vval.v_dict);
6527 }
6528 else if (rettv_list_alloc(rettv) == OK)
6529 job_info_all(rettv->vval.v_list);
6530}
6531
6532/*
6533 * "job_setoptions()" function
6534 */
6535 void
6536f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6537{
6538 job_T *job = get_job_arg(&argvars[0]);
6539 jobopt_T opt;
6540
6541 if (job == NULL)
6542 return;
6543 clear_job_options(&opt);
6544 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6545 job_set_options(job, &opt);
6546 free_job_options(&opt);
6547}
6548
6549/*
6550 * "job_start()" function
6551 */
6552 void
6553f_job_start(typval_T *argvars, typval_T *rettv)
6554{
6555 rettv->v_type = VAR_JOB;
6556 if (check_restricted() || check_secure())
6557 return;
6558 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6559}
6560
6561/*
6562 * "job_status()" function
6563 */
6564 void
6565f_job_status(typval_T *argvars, typval_T *rettv)
6566{
6567 job_T *job = get_job_arg(&argvars[0]);
6568
6569 if (job != NULL)
6570 {
6571 rettv->v_type = VAR_STRING;
6572 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6573 }
6574}
6575
6576/*
6577 * "job_stop()" function
6578 */
6579 void
6580f_job_stop(typval_T *argvars, typval_T *rettv)
6581{
6582 job_T *job = get_job_arg(&argvars[0]);
6583
6584 if (job != NULL)
6585 rettv->vval.v_number = job_stop(job, argvars, NULL);
6586}
6587
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006588#endif /* FEAT_JOB_CHANNEL */