blob: 7671e19a6277a7fda636e4113bb49b66c2a9e4ec [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaar4f974752019-02-17 17:44:42 +010023#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +010024/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaar4f974752019-02-17 17:44:42 +010068#ifdef MSWIN
Bram Moolenaard8070362016-02-15 21:56:54 +010069 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
Bram Moolenaar24058382019-01-24 23:11:49 +010083 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010084 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010085 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010086 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010087
Bram Moolenaar24058382019-01-24 23:11:49 +010088 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 {
Bram Moolenaar24058382019-01-24 23:11:49 +010090 if (todo > MAX_NAMED_PIPE_SIZE)
91 size = MAX_NAMED_PIPE_SIZE;
92 else
Bram Moolenaardec01202019-01-28 20:04:24 +010093 size = (DWORD)todo;
Bram Moolenaar65240682019-02-10 22:23:26 +010094 // If the pipe overflows while the job does not read the data,
95 // WriteFile() will block forever. This abandons the write.
Bram Moolenaar24058382019-01-24 23:11:49 +010096 memset(&ov, 0, sizeof(ov));
Bram Moolenaar65240682019-02-10 22:23:26 +010097 nwrite = 0;
Bram Moolenaar24058382019-01-24 23:11:49 +010098 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
99 {
100 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100101
Bram Moolenaar24058382019-01-24 23:11:49 +0100102 if (err != ERROR_IO_PENDING)
103 return -1;
104 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
105 return -1;
106 FlushFileBuffers(h);
107 }
Bram Moolenaar65240682019-02-10 22:23:26 +0100108 else if (nwrite == 0)
109 // WriteFile() returns TRUE but did not write anything. This causes
110 // a hang, so bail out.
111 break;
Bram Moolenaar24058382019-01-24 23:11:49 +0100112 todo -= nwrite;
113 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100114 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100115 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100116}
117
118 static void
119fd_close(sock_T fd)
120{
121 HANDLE h = (HANDLE)fd;
122
123 CloseHandle(h);
124}
125#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100126
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127/* Log file opened with ch_logfile(). */
128static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100129#ifdef FEAT_RELTIME
130static proftime_T log_start;
131#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100132
133 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100134ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100136 FILE *file = NULL;
137
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138 if (log_fd != NULL)
139 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100140
141 if (*fname != NUL)
142 {
143 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
144 if (file == NULL)
145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100146 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100147 return;
148 }
149 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100150 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100151
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100152 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100153 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100155#ifdef FEAT_RELTIME
156 profile_start(&log_start);
157#endif
158 }
159}
160
161 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200162ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163{
164 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100165}
166
167 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200168ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169{
170 if (log_fd != NULL)
171 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172#ifdef FEAT_RELTIME
173 proftime_T log_now;
174
175 profile_start(&log_now);
176 profile_sub(&log_now, &log_start);
177 fprintf(log_fd, "%s ", profile_msg(&log_now));
178#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200180 {
181 if (part < PART_COUNT)
182 fprintf(log_fd, "%son %d(%s): ",
183 what, ch->ch_id, part_names[part]);
184 else
185 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
186 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100187 else
188 fprintf(log_fd, "%s: ", what);
189 }
190}
191
Bram Moolenaard0b65022016-03-06 21:50:33 +0100192static int did_log_msg = TRUE;
193
Bram Moolenaar8caa43d2019-02-20 22:45:06 +0100194#ifndef PROTO // prototype is in proto.h
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100195 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200196ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200200 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200202 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200203 va_start(ap, fmt);
204 vfprintf(log_fd, fmt, ap);
205 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100206 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100207 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100208 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100209 }
210}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200211#endif
212
213 static void
214ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200215#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
216 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200217#endif
218 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219
220 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200221ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100222{
223 if (log_fd != NULL)
224 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200225 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100226
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200227 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200228 va_start(ap, fmt);
229 vfprintf(log_fd, fmt, ap);
230 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100231 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100233 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 }
235}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100236
Bram Moolenaar4f974752019-02-17 17:44:42 +0100237#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100238# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100239# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100240
241 static char *
242strerror_win32(int eno)
243{
244 static LPVOID msgbuf = NULL;
245 char_u *ptr;
246
247 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200248 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100249 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200250 msgbuf = NULL;
251 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252 FormatMessage(
253 FORMAT_MESSAGE_ALLOCATE_BUFFER |
254 FORMAT_MESSAGE_FROM_SYSTEM |
255 FORMAT_MESSAGE_IGNORE_INSERTS,
256 NULL,
257 eno,
258 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
259 (LPTSTR) &msgbuf,
260 0,
261 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200262 if (msgbuf != NULL)
263 /* chomp \r or \n */
264 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
265 switch (*ptr)
266 {
267 case '\r':
268 STRMOVE(ptr, ptr + 1);
269 ptr--;
270 break;
271 case '\n':
272 if (*(ptr + 1) == '\0')
273 *ptr = '\0';
274 else
275 *ptr = ' ';
276 break;
277 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100278 return msgbuf;
279}
280#endif
281
Bram Moolenaar77073442016-02-13 23:23:53 +0100282/*
283 * The list of all allocated channels.
284 */
285static channel_T *first_channel = NULL;
286static int next_ch_id = 0;
287
288/*
289 * Allocate a new channel. The refcount is set to 1.
290 * The channel isn't actually used until it is opened.
291 * Returns NULL if out of memory.
292 */
293 channel_T *
294add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100295{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200296 ch_part_T part;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200297 channel_T *channel = ALLOC_CLEAR_ONE(channel_T);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100298
Bram Moolenaar77073442016-02-13 23:23:53 +0100299 if (channel == NULL)
300 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100301
Bram Moolenaar77073442016-02-13 23:23:53 +0100302 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100303 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100304
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200305 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100306 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100307 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100309 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100310#endif
311#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100315 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100316
Bram Moolenaar77073442016-02-13 23:23:53 +0100317 if (first_channel != NULL)
318 {
319 first_channel->ch_prev = channel;
320 channel->ch_next = first_channel;
321 }
322 first_channel = channel;
323
324 channel->ch_refcount = 1;
325 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100326}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100327
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200328 int
329has_any_channel(void)
330{
331 return first_channel != NULL;
332}
333
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100334/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100335 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100336 * Return TRUE if "channel" has a callback and the associated job wasn't
337 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100338 */
339 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100340channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100341{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_out_msg;
344 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100345
346 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100347 if (channel->ch_job_killed && channel->ch_job == NULL)
348 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349
350 /* If there is a close callback it may still need to be invoked. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200351 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100352 return TRUE;
353
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200354 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200355 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200356 return TRUE;
357
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 /* If there is no callback then nobody can get readahead. If the fd is
359 * closed and there is no readahead then the callback won't be called. */
360 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100361 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
362 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100363 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
364 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
365 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
366 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
367 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
368 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200369 return (channel->ch_callback.cb_name != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100370 || has_out_msg || has_err_msg))
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200371 || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200372 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
373 && has_out_msg)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200374 || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200375 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
376 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377}
378
379/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200380 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
381 */
382 static int
383channel_can_close(channel_T *channel)
384{
385 return channel->ch_to_be_closed == 0;
386}
387
388/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200389 * Close a channel and free all its resources.
390 */
391 static void
392channel_free_contents(channel_T *channel)
393{
394 channel_close(channel, TRUE);
395 channel_clear(channel);
396 ch_log(channel, "Freeing channel");
397}
398
399 static void
400channel_free_channel(channel_T *channel)
401{
402 if (channel->ch_next != NULL)
403 channel->ch_next->ch_prev = channel->ch_prev;
404 if (channel->ch_prev == NULL)
405 first_channel = channel->ch_next;
406 else
407 channel->ch_prev->ch_next = channel->ch_next;
408 vim_free(channel);
409}
410
411 static void
412channel_free(channel_T *channel)
413{
414 if (!in_free_unref_items)
415 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200416 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200417 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200418 else
419 {
420 channel_free_contents(channel);
421 channel_free_channel(channel);
422 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200423 }
424}
425
426/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100427 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100428 * possible, there is no callback to be invoked or the associated job was
429 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100430 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100431 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100432 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100433channel_may_free(channel_T *channel)
434{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100436 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100437 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100438 return TRUE;
439 }
440 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100441}
442
443/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100444 * Decrement the reference count on "channel" and maybe free it when it goes
445 * down to zero. Don't free it if there is a pending action.
446 * Returns TRUE when the channel is no longer referenced.
447 */
448 int
449channel_unref(channel_T *channel)
450{
451 if (channel != NULL && --channel->ch_refcount <= 0)
452 return channel_may_free(channel);
453 return FALSE;
454}
455
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200456 int
457free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100458{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 int did_free = FALSE;
460 channel_T *ch;
461
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200462 /* This is invoked from the garbage collector, which only runs at a safe
463 * point. */
464 ++safe_to_invoke_callback;
465
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200467 if (!channel_still_useful(ch)
468 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469 {
470 /* Free the channel and ordinary items it contains, but don't
471 * recurse into Lists, Dictionaries etc. */
472 channel_free_contents(ch);
473 did_free = TRUE;
474 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200475
476 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200477 return did_free;
478}
479
480 void
481free_unused_channels(int copyID, int mask)
482{
483 channel_T *ch;
484 channel_T *ch_next;
485
486 for (ch = first_channel; ch != NULL; ch = ch_next)
487 {
488 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200489 if (!channel_still_useful(ch)
490 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200491 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200492 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200493 channel_free_channel(ch);
494 }
495 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
497
Bram Moolenaard04a0202016-01-26 23:30:18 +0100498#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100499
500#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
501 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100502channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100503{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200505 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100507 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100508 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200509 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100510 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200511 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100512}
513#endif
514
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100516 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100518#ifdef FEAT_GUI_X11
519 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200520messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200521 int *unused1 UNUSED,
522 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100523{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100524 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100525}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100526#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100527
Bram Moolenaard04a0202016-01-26 23:30:18 +0100528#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100529# if GTK_CHECK_VERSION(3,0,0)
530 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200531messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200532 GIOCondition unused2 UNUSED,
533 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100534{
535 channel_read_fd(GPOINTER_TO_INT(clientData));
536 return TRUE; /* Return FALSE instead in case the event source is to
537 * be removed after this function returns. */
538}
539# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100540 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200541messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200542 gint unused1 UNUSED,
543 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100544{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100545 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100546}
Bram Moolenaar98921892016-02-23 17:14:37 +0100547# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100548#endif
549
550 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200551channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100552{
Bram Moolenaarde279892016-03-11 22:19:44 +0100553 if (!CH_HAS_GUI)
554 return;
555
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200556 /* gets stuck in handling events for a not connected channel */
557 if (channel->ch_keep_open)
558 return;
559
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200561 /* Tell notifier we are interested in being called when there is input on
562 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200564 {
565 ch_log(channel, "Registering part %s with fd %d",
566 part_names[part], channel->ch_part[part].ch_fd);
567
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100569 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100571 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100573 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200574 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100575# else
576# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200577 /* Tell gdk we are interested in being called when there is input on the
578 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100579 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100580 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200581 ch_log(channel, "Registering part %s with fd %d",
582 part_names[part], channel->ch_part[part].ch_fd);
583# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100584 GIOChannel *chnnl = g_io_channel_unix_new(
585 (gint)channel->ch_part[part].ch_fd);
586
587 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
588 chnnl,
589 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200590 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100591 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
592
593 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100594# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100595 channel->ch_part[part].ch_inputHandler = gdk_input_add(
596 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100597 (GdkInputCondition)
598 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200599 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100600 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100601# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200602 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100603# endif
604# endif
605}
606
Bram Moolenaarde279892016-03-11 22:19:44 +0100607 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100608channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100609{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100610 if (channel->CH_SOCK_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200612 if (channel->CH_OUT_FD != INVALID_FD
613 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100614 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200615 if (channel->CH_ERR_FD != INVALID_FD
616 && channel->CH_ERR_FD != channel->CH_SOCK_FD
617 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100619}
620
621/*
622 * Register any of our file descriptors with the GUI event handling system.
623 * Called when the GUI has started.
624 */
625 void
626channel_gui_register_all(void)
627{
Bram Moolenaar77073442016-02-13 23:23:53 +0100628 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100629
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100631 channel_gui_register(channel);
632}
633
634 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200635channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100636{
637# ifdef FEAT_GUI_X11
638 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
639 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200640 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100641 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
642 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
643 }
644# else
645# ifdef FEAT_GUI_GTK
646 if (channel->ch_part[part].ch_inputHandler != 0)
647 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200648 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100649# if GTK_CHECK_VERSION(3,0,0)
650 g_source_remove(channel->ch_part[part].ch_inputHandler);
651# else
652 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
653# endif
654 channel->ch_part[part].ch_inputHandler = 0;
655 }
656# endif
657# endif
658}
659
660 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100661channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100662{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200663 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100664
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100665 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100666 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667}
668
669#endif
670
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100671static char *e_cannot_connect = N_("E902: Cannot connect to port");
672
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100674 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100675 * "waittime" is the time in msec to wait for the connection.
676 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 * Returns the channel for success.
678 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100680 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100681channel_open(
682 char *hostname,
683 int port_in,
684 int waittime,
685 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100689 struct hostent *host;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100690#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100692 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693#else
694 int port = port_in;
695#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100696 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100697 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698
Bram Moolenaar4f974752019-02-17 17:44:42 +0100699#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700 channel_init_winsock();
701#endif
702
Bram Moolenaar77073442016-02-13 23:23:53 +0100703 channel = add_channel();
704 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100706 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100707 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 }
709
710 /* Get the server internet address and put into addr structure */
711 /* fill in the socket address structure and connect to server */
712 vim_memset((char *)&server, 0, sizeof(server));
713 server.sin_family = AF_INET;
714 server.sin_port = htons(port);
715 if ((host = gethostbyname(hostname)) == NULL)
716 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100717 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200718 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100719 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100720 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100722 {
723 char *p;
724
Bram Moolenaar2e324952018-04-14 14:37:07 +0200725 /* When using host->h_addr_list[0] directly ubsan warns for it to not
726 * be aligned. First copy the pointer to avoid that. */
727 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100728 memcpy((char *)&server.sin_addr, p, host->h_length);
729 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100730
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100731 /* On Mac and Solaris a zero timeout almost never works. At least wait
732 * one millisecond. Let's do it for all systems, because we don't know why
733 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100734 if (waittime == 0)
735 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100736
737 /*
738 * For Unix we need to call connect() again after connect() failed.
739 * On Win32 one time is sufficient.
740 */
741 while (TRUE)
742 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100743 long elapsed_msec = 0;
744 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100745
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100746 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100747 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100748 sd = socket(AF_INET, SOCK_STREAM, 0);
749 if (sd == -1)
750 {
751 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200752 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100753 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100754 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100755 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100756
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100757 if (waittime >= 0)
758 {
759 /* Make connect() non-blocking. */
760 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +0100761#ifdef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100762 ioctlsocket(sd, FIONBIO, &val) < 0
763#else
764 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
765#endif
766 )
767 {
768 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200769 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 "channel_open: Connect failed with errno %d", errno);
771 sock_close(sd);
772 channel_free(channel);
773 return NULL;
774 }
775 }
776
777 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100779 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
780
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781 if (ret == 0)
782 /* The connection could be established. */
783 break;
784
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100785 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100786 if (waittime < 0 || (errno != EWOULDBLOCK
787 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100788#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100790#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 ))
792 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200793 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100794 "channel_open: Connect failed with errno %d", errno);
795 PERROR(_(e_cannot_connect));
796 sock_close(sd);
797 channel_free(channel);
798 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100799 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100800
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100801 /* Limit the waittime to 50 msec. If it doesn't work within this
802 * time we close the socket and try creating it again. */
803 waitnow = waittime > 50 ? 50 : waittime;
804
Bram Moolenaar045a2842016-03-08 22:33:07 +0100805 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100806 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100807#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 if (errno != ECONNREFUSED)
809#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100810 {
811 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813 fd_set wfds;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100814#ifndef MSWIN
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 int so_error = 0;
816 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 struct timeval start_tv;
818 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100820 FD_ZERO(&rfds);
821 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100822 FD_ZERO(&wfds);
823 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100824
Bram Moolenaar562ca712016-03-09 21:50:05 +0100825 tv.tv_sec = waitnow / 1000;
826 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100827#ifndef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100828 gettimeofday(&start_tv, NULL);
829#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200830 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100831 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100832 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100833
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100834 if (ret < 0)
835 {
836 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200837 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100838 "channel_open: Connect failed with errno %d", errno);
839 PERROR(_(e_cannot_connect));
840 sock_close(sd);
841 channel_free(channel);
842 return NULL;
843 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100844
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#ifdef MSWIN
Bram Moolenaar562ca712016-03-09 21:50:05 +0100846 /* On Win32: select() is expected to work and wait for up to
847 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100848 if (FD_ISSET(sd, &wfds))
849 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100850 elapsed_msec = waitnow;
851 if (waittime > 1 && elapsed_msec < waittime)
852 {
853 waittime -= elapsed_msec;
854 continue;
855 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100856#else
857 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100858 * After putting the socket in non-blocking mode, connect() will
859 * return EINPROGRESS, select() will not wait (as if writing is
860 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100861 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100864 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100865 {
866 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100867 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100868 if (ret < 0 || (so_error != 0
869 && so_error != EWOULDBLOCK
870 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100871# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100872 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100873# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100874 ))
875 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200876 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100877 "channel_open: Connect failed with errno %d",
878 so_error);
879 PERROR(_(e_cannot_connect));
880 sock_close(sd);
881 channel_free(channel);
882 return NULL;
883 }
884 }
885
Bram Moolenaar045a2842016-03-08 22:33:07 +0100886 if (FD_ISSET(sd, &wfds) && so_error == 0)
887 /* Did not detect an error, connection is established. */
888 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100889
Bram Moolenaar045a2842016-03-08 22:33:07 +0100890 gettimeofday(&end_tv, NULL);
891 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
892 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100893#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100894 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100897 if (waittime > 1 && elapsed_msec < waittime)
898 {
899 /* The port isn't ready but we also didn't get an error.
900 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100901 * yet. Select() may return early, wait until the remaining
902 * "waitnow" and try again. */
903 waitnow -= elapsed_msec;
904 waittime -= elapsed_msec;
905 if (waitnow > 0)
906 {
907 mch_delay((long)waitnow, TRUE);
908 ui_breakcheck();
909 waittime -= waitnow;
910 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100911 if (!got_int)
912 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100913 if (waittime <= 0)
914 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100915 waittime = 1;
916 continue;
917 }
918 /* we were interrupted, behave as if timed out */
919 }
920#endif
921
922 /* We timed out. */
923 ch_error(channel, "Connection timed out");
924 sock_close(sd);
925 channel_free(channel);
926 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100927 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100928
Bram Moolenaar045a2842016-03-08 22:33:07 +0100929 ch_log(channel, "Connection made");
930
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100931 if (waittime >= 0)
932 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100933#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100934 val = 0;
935 ioctlsocket(sd, FIONBIO, &val);
936#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100937 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100938#endif
939 }
940
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100941 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100942 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100943 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
944 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200945 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100946
947#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100948 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100949#endif
950
Bram Moolenaar77073442016-02-13 23:23:53 +0100951 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100952}
953
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100954/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +0200955 * Copy callback from "src" to "dest", incrementing the refcounts.
956 */
957 static void
958copy_callback(callback_T *dest, callback_T *src)
959{
960 dest->cb_partial = src->cb_partial;
961 if (dest->cb_partial != NULL)
962 {
963 dest->cb_name = src->cb_name;
964 dest->cb_free_name = FALSE;
965 ++dest->cb_partial->pt_refcount;
966 }
967 else
968 {
969 dest->cb_name = vim_strsave(src->cb_name);
970 dest->cb_free_name = TRUE;
971 func_ref(src->cb_name);
972 }
973}
974
975 static void
976free_set_callback(callback_T *cbp, callback_T *callback)
977{
978 free_callback(cbp);
979
980 if (callback->cb_name != NULL && *callback->cb_name != NUL)
981 copy_callback(cbp, callback);
982 else
983 cbp->cb_name = NULL;
984}
985
986/*
987 * Prepare buffer "buf" for writing channel output to.
988 */
989 static void
990prepare_buffer(buf_T *buf)
991{
992 buf_T *save_curbuf = curbuf;
993
994 buf_copy_options(buf, BCO_ENTER);
995 curbuf = buf;
996#ifdef FEAT_QUICKFIX
997 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
998 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
999#endif
1000 if (curbuf->b_ml.ml_mfp == NULL)
1001 ml_open(curbuf);
1002 curbuf = save_curbuf;
1003}
1004
1005/*
1006 * Find a buffer matching "name" or create a new one.
1007 * Returns NULL if there is something very wrong (error already reported).
1008 */
1009 static buf_T *
1010find_buffer(char_u *name, int err, int msg)
1011{
1012 buf_T *buf = NULL;
1013 buf_T *save_curbuf = curbuf;
1014
1015 if (name != NULL && *name != NUL)
1016 {
1017 buf = buflist_findname(name);
1018 if (buf == NULL)
1019 buf = buflist_findname_exp(name);
1020 }
1021 if (buf == NULL)
1022 {
1023 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
1024 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
1025 if (buf == NULL)
1026 return NULL;
1027 prepare_buffer(buf);
1028
1029 curbuf = buf;
1030 if (msg)
1031 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1032 : "Reading from channel output..."), TRUE);
1033 changed_bytes(1, 0);
1034 curbuf = save_curbuf;
1035 }
1036
1037 return buf;
1038}
1039
1040/*
1041 * Set various properties from an "opt" argument.
1042 */
1043 static void
1044channel_set_options(channel_T *channel, jobopt_T *opt)
1045{
1046 ch_part_T part;
1047
1048 if (opt->jo_set & JO_MODE)
1049 for (part = PART_SOCK; part < PART_COUNT; ++part)
1050 channel->ch_part[part].ch_mode = opt->jo_mode;
1051 if (opt->jo_set & JO_IN_MODE)
1052 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1053 if (opt->jo_set & JO_OUT_MODE)
1054 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1055 if (opt->jo_set & JO_ERR_MODE)
1056 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
1057 channel->ch_nonblock = opt->jo_noblock;
1058
1059 if (opt->jo_set & JO_TIMEOUT)
1060 for (part = PART_SOCK; part < PART_COUNT; ++part)
1061 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1062 if (opt->jo_set & JO_OUT_TIMEOUT)
1063 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1064 if (opt->jo_set & JO_ERR_TIMEOUT)
1065 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1066 if (opt->jo_set & JO_BLOCK_WRITE)
1067 channel->ch_part[PART_IN].ch_block_write = 1;
1068
1069 if (opt->jo_set & JO_CALLBACK)
1070 free_set_callback(&channel->ch_callback, &opt->jo_callback);
1071 if (opt->jo_set & JO_OUT_CALLBACK)
1072 free_set_callback(&channel->ch_part[PART_OUT].ch_callback,
1073 &opt->jo_out_cb);
1074 if (opt->jo_set & JO_ERR_CALLBACK)
1075 free_set_callback(&channel->ch_part[PART_ERR].ch_callback,
1076 &opt->jo_err_cb);
1077 if (opt->jo_set & JO_CLOSE_CALLBACK)
1078 free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb);
1079 channel->ch_drop_never = opt->jo_drop_never;
1080
1081 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1082 {
1083 buf_T *buf;
1084
1085 /* writing output to a buffer. Default mode is NL. */
1086 if (!(opt->jo_set & JO_OUT_MODE))
1087 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1088 if (opt->jo_set & JO_OUT_BUF)
1089 {
1090 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1091 if (buf == NULL)
1092 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1093 }
1094 else
1095 {
1096 int msg = TRUE;
1097
1098 if (opt->jo_set2 & JO2_OUT_MSG)
1099 msg = opt->jo_message[PART_OUT];
1100 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
1101 }
1102 if (buf != NULL)
1103 {
1104 if (opt->jo_set & JO_OUT_MODIFIABLE)
1105 channel->ch_part[PART_OUT].ch_nomodifiable =
1106 !opt->jo_modifiable[PART_OUT];
1107
1108 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1109 {
1110 emsg(_(e_modifiable));
1111 }
1112 else
1113 {
1114 ch_log(channel, "writing out to buffer '%s'",
1115 (char *)buf->b_ffname);
1116 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1117 // if the buffer was deleted or unloaded resurrect it
1118 if (buf->b_ml.ml_mfp == NULL)
1119 prepare_buffer(buf);
1120 }
1121 }
1122 }
1123
1124 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1125 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1126 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1127 {
1128 buf_T *buf;
1129
1130 /* writing err to a buffer. Default mode is NL. */
1131 if (!(opt->jo_set & JO_ERR_MODE))
1132 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1133 if (opt->jo_io[PART_ERR] == JIO_OUT)
1134 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1135 else if (opt->jo_set & JO_ERR_BUF)
1136 {
1137 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1138 if (buf == NULL)
1139 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1140 }
1141 else
1142 {
1143 int msg = TRUE;
1144
1145 if (opt->jo_set2 & JO2_ERR_MSG)
1146 msg = opt->jo_message[PART_ERR];
1147 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1148 }
1149 if (buf != NULL)
1150 {
1151 if (opt->jo_set & JO_ERR_MODIFIABLE)
1152 channel->ch_part[PART_ERR].ch_nomodifiable =
1153 !opt->jo_modifiable[PART_ERR];
1154 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1155 {
1156 emsg(_(e_modifiable));
1157 }
1158 else
1159 {
1160 ch_log(channel, "writing err to buffer '%s'",
1161 (char *)buf->b_ffname);
1162 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1163 // if the buffer was deleted or unloaded resurrect it
1164 if (buf->b_ml.ml_mfp == NULL)
1165 prepare_buffer(buf);
1166 }
1167 }
1168 }
1169
1170 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1171 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1172 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
1173}
1174
1175/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001176 * Implements ch_open().
1177 */
1178 channel_T *
1179channel_open_func(typval_T *argvars)
1180{
1181 char_u *address;
1182 char_u *p;
1183 char *rest;
1184 int port;
1185 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001186 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001188 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001189 if (argvars[1].v_type != VAR_UNKNOWN
1190 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
1191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001193 return NULL;
1194 }
1195
1196 /* parse address */
1197 p = vim_strchr(address, ':');
1198 if (p == NULL)
1199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001201 return NULL;
1202 }
1203 *p++ = NUL;
1204 port = strtol((char *)p, &rest, 10);
1205 if (*address == NUL || port <= 0 || *rest != NUL)
1206 {
1207 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001209 return NULL;
1210 }
1211
1212 /* parse options */
1213 clear_job_options(&opt);
1214 opt.jo_mode = MODE_JSON;
1215 opt.jo_timeout = 2000;
1216 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02001217 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001218 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001219 if (opt.jo_timeout < 0)
1220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001222 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001223 }
1224
1225 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1226 if (channel != NULL)
1227 {
1228 opt.jo_set = JO_ALL;
1229 channel_set_options(channel, &opt);
1230 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001231theend:
1232 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001233 return channel;
1234}
1235
Bram Moolenaarde279892016-03-11 22:19:44 +01001236 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001237ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001238{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001239 sock_T *fd = &channel->ch_part[part].ch_fd;
1240
Bram Moolenaarde279892016-03-11 22:19:44 +01001241 if (*fd != INVALID_FD)
1242 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001243 if (part == PART_SOCK)
1244 sock_close(*fd);
1245 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001246 {
1247 /* When using a pty the same FD is set on multiple parts, only
1248 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001249 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1250 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1251 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001252 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001253#ifdef MSWIN
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001254 if (channel->ch_named_pipe)
1255 DisconnectNamedPipe((HANDLE)fd);
1256#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001257 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001258 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001259 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001260 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001261
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001262 /* channel is closed, may want to end the job if it was the last */
1263 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001264 }
1265}
1266
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001267 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001268channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001269{
Bram Moolenaarde279892016-03-11 22:19:44 +01001270 if (in != INVALID_FD)
1271 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001272 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001273 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001274# if defined(UNIX)
1275 /* Do not end the job when all output channels are closed, wait until
1276 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001277 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001278 channel->ch_to_be_closed |= (1U << PART_IN);
1279# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001280 }
1281 if (out != INVALID_FD)
1282 {
1283# if defined(FEAT_GUI)
1284 channel_gui_unregister_one(channel, PART_OUT);
1285# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001286 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001287 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001288 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001289# if defined(FEAT_GUI)
1290 channel_gui_register_one(channel, PART_OUT);
1291# endif
1292 }
1293 if (err != INVALID_FD)
1294 {
1295# if defined(FEAT_GUI)
1296 channel_gui_unregister_one(channel, PART_ERR);
1297# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001298 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001299 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001300 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001301# if defined(FEAT_GUI)
1302 channel_gui_register_one(channel, PART_ERR);
1303# endif
1304 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001305}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001306
Bram Moolenaard6051b52016-02-28 15:49:03 +01001307/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001308 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001309 * This does not keep a refcount, when the job is freed ch_job is cleared.
1310 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001311 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001312channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001313{
Bram Moolenaar77073442016-02-13 23:23:53 +01001314 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001315
1316 channel_set_options(channel, options);
1317
1318 if (job->jv_in_buf != NULL)
1319 {
1320 chanpart_T *in_part = &channel->ch_part[PART_IN];
1321
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001322 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001323 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001324 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001325 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001326 {
1327 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1328 {
1329 /* Special mode: send last-but-one line when appending a line
1330 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001331 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001332 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001334 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001335 }
1336 else
1337 in_part->ch_buf_top = options->jo_in_top;
1338 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001339 else
1340 in_part->ch_buf_top = 1;
1341 if (options->jo_set & JO_IN_BOT)
1342 in_part->ch_buf_bot = options->jo_in_bot;
1343 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001344 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001345 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001346}
1347
1348/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001349 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001350 */
1351 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001352channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001353 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001354 ch_part_T part,
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001355 callback_T *callback,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001356 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001357{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001358 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001359 cbq_T *item = ALLOC_ONE(cbq_T);
Bram Moolenaara07fec92016-02-05 21:04:08 +01001360
1361 if (item != NULL)
1362 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001363 copy_callback(&item->cq_callback, callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001364 item->cq_seq_nr = id;
1365 item->cq_prev = head->cq_prev;
1366 head->cq_prev = item;
1367 item->cq_next = NULL;
1368 if (item->cq_prev == NULL)
1369 head->cq_next = item;
1370 else
1371 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001372 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001373}
1374
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375 static void
1376write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1377{
1378 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001379 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001380 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001381 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001382
Bram Moolenaar655da312016-05-28 22:22:34 +02001383 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001384 if ((p = alloc(len + 2)) == NULL)
1385 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001386 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001387
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001388 if (channel->ch_write_text_mode)
1389 p[len] = CAR;
1390 else
1391 {
1392 for (i = 0; i < len; ++i)
1393 if (p[i] == NL)
1394 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001395
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001396 p[len] = NL;
1397 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001398 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001399 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001400 vim_free(p);
1401}
1402
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001403/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001404 * Return TRUE if "channel" can be written to.
1405 * Returns FALSE if the input is closed or the write would block.
1406 */
1407 static int
1408can_write_buf_line(channel_T *channel)
1409{
1410 chanpart_T *in_part = &channel->ch_part[PART_IN];
1411
1412 if (in_part->ch_fd == INVALID_FD)
1413 return FALSE; /* pipe was closed */
1414
1415 /* for testing: block every other attempt to write */
1416 if (in_part->ch_block_write == 1)
1417 in_part->ch_block_write = -1;
1418 else if (in_part->ch_block_write == -1)
1419 in_part->ch_block_write = 1;
1420
1421 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001422#ifndef MSWIN
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001423 {
1424# if defined(HAVE_SELECT)
1425 struct timeval tval;
1426 fd_set wfds;
1427 int ret;
1428
1429 FD_ZERO(&wfds);
1430 FD_SET((int)in_part->ch_fd, &wfds);
1431 tval.tv_sec = 0;
1432 tval.tv_usec = 0;
1433 for (;;)
1434 {
1435 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1436# ifdef EINTR
1437 SOCK_ERRNO;
1438 if (ret == -1 && errno == EINTR)
1439 continue;
1440# endif
1441 if (ret <= 0 || in_part->ch_block_write == 1)
1442 {
1443 if (ret > 0)
1444 ch_log(channel, "FAKED Input not ready for writing");
1445 else
1446 ch_log(channel, "Input not ready for writing");
1447 return FALSE;
1448 }
1449 break;
1450 }
1451# else
1452 struct pollfd fds;
1453
1454 fds.fd = in_part->ch_fd;
1455 fds.events = POLLOUT;
1456 if (poll(&fds, 1, 0) <= 0)
1457 {
1458 ch_log(channel, "Input not ready for writing");
1459 return FALSE;
1460 }
1461 if (in_part->ch_block_write == 1)
1462 {
1463 ch_log(channel, "FAKED Input not ready for writing");
1464 return FALSE;
1465 }
1466# endif
1467 }
1468#endif
1469 return TRUE;
1470}
1471
1472/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001473 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001474 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001475 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001476channel_write_in(channel_T *channel)
1477{
1478 chanpart_T *in_part = &channel->ch_part[PART_IN];
1479 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001480 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001481 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001482
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 if (buf == NULL || in_part->ch_buf_append)
1484 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001485 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001486 {
1487 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001488 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001489 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001490 return;
1491 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001492
1493 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1494 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1495 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001496 if (!can_write_buf_line(channel))
1497 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001498 write_buf_line(buf, lnum, channel);
1499 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501
1502 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001503 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001505 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506
Bram Moolenaar014069a2016-03-03 22:51:40 +01001507 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001508 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001509 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001510#if defined(FEAT_TERMINAL)
1511 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001512 if (channel->ch_job != NULL)
1513 term_send_eof(channel);
1514#endif
1515
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001516 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001517 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001519
1520 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001521 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001522 }
1523 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001524 ch_log(channel, "Still %ld more lines to write",
1525 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001526}
1527
1528/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001529 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001530 */
1531 void
1532channel_buffer_free(buf_T *buf)
1533{
1534 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001535 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001536
1537 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001538 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001539 {
1540 chanpart_T *ch_part = &channel->ch_part[part];
1541
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001542 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001543 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001544 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001545 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001546 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001547 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001548 }
1549}
1550
1551/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001552 * Write any lines waiting to be written to "channel".
1553 */
1554 static void
1555channel_write_input(channel_T *channel)
1556{
1557 chanpart_T *in_part = &channel->ch_part[PART_IN];
1558
1559 if (in_part->ch_writeque.wq_next != NULL)
1560 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1561 else if (in_part->ch_bufref.br_buf != NULL)
1562 {
1563 if (in_part->ch_buf_append)
1564 channel_write_new_lines(in_part->ch_bufref.br_buf);
1565 else
1566 channel_write_in(channel);
1567 }
1568}
1569
1570/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001571 * Write any lines waiting to be written to a channel.
1572 */
1573 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001574channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001575{
1576 channel_T *channel;
1577
1578 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001579 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001580}
1581
1582/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001583 * Write appended lines above the last one in "buf" to the channel.
1584 */
1585 void
1586channel_write_new_lines(buf_T *buf)
1587{
1588 channel_T *channel;
1589 int found_one = FALSE;
1590
1591 /* There could be more than one channel for the buffer, loop over all of
1592 * them. */
1593 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1594 {
1595 chanpart_T *in_part = &channel->ch_part[PART_IN];
1596 linenr_T lnum;
1597 int written = 0;
1598
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001599 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001600 {
1601 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001602 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001603 found_one = TRUE;
1604 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1605 ++lnum)
1606 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001607 if (!can_write_buf_line(channel))
1608 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001609 write_buf_line(buf, lnum, channel);
1610 ++written;
1611 }
1612
1613 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001614 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001615 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001616 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001617 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001618 ch_log(channel, "Still %ld more lines to write",
1619 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001620
1621 in_part->ch_buf_bot = lnum;
1622 }
1623 }
1624 if (!found_one)
1625 buf->b_write_to_channel = FALSE;
1626}
1627
1628/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001629 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001630 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001631 */
1632 static void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001633invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001634{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001635 typval_T rettv;
1636 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001637
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001638 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001640
Bram Moolenaar77073442016-02-13 23:23:53 +01001641 argv[0].v_type = VAR_CHANNEL;
1642 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001643
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001644 call_callback(callback, -1, &rettv, 2, argv, NULL,
1645 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001646 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001647 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001648}
1649
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001650/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001651 * Return the first node from "channel"/"part" without removing it.
1652 * Returns NULL if there is nothing.
1653 */
1654 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001655channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001656{
1657 readq_T *head = &channel->ch_part[part].ch_head;
1658
1659 return head->rq_next;
1660}
1661
1662/*
1663 * Return a pointer to the first NL in "node".
1664 * Skips over NUL characters.
1665 * Returns NULL if there is no NL.
1666 */
1667 char_u *
1668channel_first_nl(readq_T *node)
1669{
1670 char_u *buffer = node->rq_buffer;
1671 long_u i;
1672
1673 for (i = 0; i < node->rq_buflen; ++i)
1674 if (buffer[i] == NL)
1675 return buffer + i;
1676 return NULL;
1677}
1678
1679/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001680 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681 * The caller must free it.
1682 * Returns NULL if there is nothing.
1683 */
1684 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001687 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001688 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689 char_u *p;
1690
Bram Moolenaar77073442016-02-13 23:23:53 +01001691 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001693 if (outlen != NULL)
1694 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001696 p = node->rq_buffer;
1697 head->rq_next = node->rq_next;
1698 if (node->rq_next == NULL)
1699 head->rq_prev = NULL;
1700 else
1701 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 vim_free(node);
1703 return p;
1704}
1705
1706/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001708 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001709 */
1710 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001713 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001714 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001715 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001716 char_u *res;
1717 char_u *p;
1718
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001719 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001720 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001721 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001722 res = alloc(len + 1);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001723 if (res == NULL)
1724 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001726 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001727 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001728 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001729 p += node->rq_buflen;
1730 }
1731 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001732
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001733 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001734 do
1735 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001737 vim_free(p);
1738 } while (p != NULL);
1739
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (outlen != NULL)
1741 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001742 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 *outlen += len;
1744 return res;
1745 }
1746
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001747 // Turn all NUL into NL, so that the result can be used as a string.
1748 p = res;
1749 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001750 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001751 if (*p == NUL)
1752 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001753#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001754 else if (*p == 0x1b)
1755 {
1756 // crush the escape sequence OSC 0/1/2: ESC ]0;
1757 if (p + 3 < res + len
1758 && p[1] == ']'
1759 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1760 && p[3] == ';')
1761 {
1762 // '\a' becomes a NL
1763 while (p < res + (len - 1) && *p != '\a')
1764 ++p;
1765 // BEL is zero width characters, suppress display mistake
1766 // ConPTY (after 10.0.18317) requires advance checking
1767 if (p[-1] == NUL)
1768 p[-1] = 0x07;
1769 }
1770 }
1771#endif
1772 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001773 }
1774
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001775 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001776}
1777
1778/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001779 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001780 * Caller must check these bytes are available.
1781 */
1782 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001783channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001784{
1785 readq_T *head = &channel->ch_part[part].ch_head;
1786 readq_T *node = head->rq_next;
1787 char_u *buf = node->rq_buffer;
1788
1789 mch_memmove(buf, buf + len, node->rq_buflen - len);
1790 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001791 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001792}
1793
1794/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001797 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798 */
1799 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001800channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001803 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001804 readq_T *last_node;
1805 readq_T *n;
1806 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001808 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001809
Bram Moolenaar77073442016-02-13 23:23:53 +01001810 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001811 return FAIL;
1812
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001813 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001814 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001815 if (want_nl)
1816 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001817 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001818 {
1819 last_node = last_node->rq_next;
1820 len += last_node->rq_buflen;
1821 }
1822
Bram Moolenaar772153f2019-03-04 12:09:49 +01001823 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001825 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001826 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001827 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001828 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001829 node->rq_buffer = newbuf;
1830 for (n = node; n != last_node; )
1831 {
1832 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001833 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001834 p += n->rq_buflen;
1835 vim_free(n->rq_buffer);
1836 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001837 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001838 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001839
1840 /* dispose of the collapsed nodes and their buffers */
1841 for (n = node->rq_next; n != last_node; )
1842 {
1843 n = n->rq_next;
1844 vim_free(n->rq_prev);
1845 }
1846 node->rq_next = last_node->rq_next;
1847 if (last_node->rq_next == NULL)
1848 head->rq_prev = node;
1849 else
1850 last_node->rq_next->rq_prev = node;
1851 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001852 return OK;
1853}
1854
1855/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001856 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 * Returns OK or FAIL.
1859 */
1860 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001861channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863{
1864 readq_T *node;
1865 readq_T *head = &channel->ch_part[part].ch_head;
1866 char_u *p;
1867 int i;
1868
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001869 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 if (node == NULL)
1871 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001872 /* A NUL is added at the end, because netbeans code expects that.
1873 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001874 node->rq_buffer = alloc(len + 1);
1875 if (node->rq_buffer == NULL)
1876 {
1877 vim_free(node);
1878 return FAIL; /* out of memory */
1879 }
1880
1881 if (channel->ch_part[part].ch_mode == MODE_NL)
1882 {
1883 /* Drop any CR before a NL. */
1884 p = node->rq_buffer;
1885 for (i = 0; i < len; ++i)
1886 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1887 *p++ = buf[i];
1888 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001889 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890 }
1891 else
1892 {
1893 mch_memmove(node->rq_buffer, buf, len);
1894 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001895 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001896 }
1897
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001898 if (prepend)
1899 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001900 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001901 node->rq_next = head->rq_next;
1902 node->rq_prev = NULL;
1903 if (head->rq_next == NULL)
1904 head->rq_prev = node;
1905 else
1906 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001907 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001908 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001909 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001911 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001912 node->rq_next = NULL;
1913 node->rq_prev = head->rq_prev;
1914 if (head->rq_prev == NULL)
1915 head->rq_next = node;
1916 else
1917 head->rq_prev->rq_next = node;
1918 head->rq_prev = node;
1919 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001920
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001921 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001922 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001923 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001924 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001925 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926 fprintf(log_fd, "'\n");
1927 }
1928 return OK;
1929}
1930
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001931/*
1932 * Try to fill the buffer of "reader".
1933 * Returns FALSE when nothing was added.
1934 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001935 static int
1936channel_fill(js_read_T *reader)
1937{
1938 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001939 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001941 int keeplen;
1942 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001943 char_u *p;
1944
1945 if (next == NULL)
1946 return FALSE;
1947
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001948 keeplen = reader->js_end - reader->js_buf;
1949 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001950 {
1951 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001952 addlen = (int)STRLEN(next);
1953 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001954 if (p == NULL)
1955 {
1956 vim_free(next);
1957 return FALSE;
1958 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001959 mch_memmove(p, reader->js_buf, keeplen);
1960 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001961 vim_free(next);
1962 next = p;
1963 }
1964
1965 vim_free(reader->js_buf);
1966 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001967 return TRUE;
1968}
1969
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001970/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001971 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001972 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001974 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001975 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001976channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977{
1978 js_read_T reader;
1979 typval_T listtv;
1980 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001981 chanpart_T *chanpart = &channel->ch_part[part];
1982 jsonq_T *head = &chanpart->ch_json_head;
1983 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001984 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001985
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001986 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001987 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001988
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001989 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001990 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001991 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001992 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001993 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994
1995 /* When a message is incomplete we wait for a short while for more to
1996 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001997 * or list will make us hang.
1998 * Do not generate error messages, they will be written in a channel log. */
1999 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002000 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002001 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002002 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002003 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002005 /* Only accept the response when it is a list with at least two
2006 * items. */
2007 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002008 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002009 if (listtv.v_type != VAR_LIST)
2010 ch_error(channel, "Did not receive a list, discarding");
2011 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002012 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002013 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002014 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002015 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 else
2017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002018 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002019 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 else
2022 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002023 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002024 item->jq_value = alloc_tv();
2025 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002026 {
2027 vim_free(item);
2028 clear_tv(&listtv);
2029 }
2030 else
2031 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 *item->jq_value = listtv;
2033 item->jq_prev = head->jq_prev;
2034 head->jq_prev = item;
2035 item->jq_next = NULL;
2036 if (item->jq_prev == NULL)
2037 head->jq_next = item;
2038 else
2039 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002040 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 }
2042 }
2043 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002044
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002046 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002047 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002048 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002049 size_t buflen = STRLEN(reader.js_buf);
2050
2051 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002052 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002053 /* First time encountering incomplete message or after receiving
2054 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002055 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002056 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002057 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002058 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002059 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002060#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002061 chanpart->ch_deadline = GetTickCount() + 100L;
2062#else
2063 gettimeofday(&chanpart->ch_deadline, NULL);
2064 chanpart->ch_deadline.tv_usec += 100 * 1000;
2065 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2066 {
2067 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2068 ++chanpart->ch_deadline.tv_sec;
2069 }
2070#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002071 }
2072 else
2073 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002074 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002075#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002076 timeout = GetTickCount() > chanpart->ch_deadline;
2077#else
2078 {
2079 struct timeval now_tv;
2080
2081 gettimeofday(&now_tv, NULL);
2082 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2083 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2084 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2085 }
2086#endif
2087 if (timeout)
2088 {
2089 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002090 chanpart->ch_wait_len = 0;
2091 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002092 }
2093 else
2094 {
2095 reader.js_used = 0;
2096 ch_log(channel, "still waiting on incomplete message");
2097 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002099 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002100
2101 if (status == FAIL)
2102 {
2103 ch_error(channel, "Decoding failed - discarding input");
2104 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002105 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002106 }
2107 else if (reader.js_buf[reader.js_used] != NUL)
2108 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002109 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002110 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002111 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2112 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002113 ret = status == MAYBE ? FALSE: TRUE;
2114 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002115 else
2116 ret = FALSE;
2117
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002118 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002119 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002120}
2121
2122/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002123 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002124 */
2125 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002126remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002127{
Bram Moolenaar77073442016-02-13 23:23:53 +01002128 if (node->cq_prev == NULL)
2129 head->cq_next = node->cq_next;
2130 else
2131 node->cq_prev->cq_next = node->cq_next;
2132 if (node->cq_next == NULL)
2133 head->cq_prev = node->cq_prev;
2134 else
2135 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002136}
2137
2138/*
2139 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002140 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002141 */
2142 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002143remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002144{
Bram Moolenaar77073442016-02-13 23:23:53 +01002145 if (node->jq_prev == NULL)
2146 head->jq_next = node->jq_next;
2147 else
2148 node->jq_prev->jq_next = node->jq_next;
2149 if (node->jq_next == NULL)
2150 head->jq_prev = node->jq_prev;
2151 else
2152 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002153 vim_free(node);
2154}
2155
2156/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002157 * Add "id" to the list of JSON message IDs we are waiting on.
2158 */
2159 static void
2160channel_add_block_id(chanpart_T *chanpart, int id)
2161{
2162 garray_T *gap = &chanpart->ch_block_ids;
2163
2164 if (gap->ga_growsize == 0)
2165 ga_init2(gap, (int)sizeof(int), 10);
2166 if (ga_grow(gap, 1) == OK)
2167 {
2168 ((int *)gap->ga_data)[gap->ga_len] = id;
2169 ++gap->ga_len;
2170 }
2171}
2172
2173/*
2174 * Remove "id" from the list of JSON message IDs we are waiting on.
2175 */
2176 static void
2177channel_remove_block_id(chanpart_T *chanpart, int id)
2178{
2179 garray_T *gap = &chanpart->ch_block_ids;
2180 int i;
2181
2182 for (i = 0; i < gap->ga_len; ++i)
2183 if (((int *)gap->ga_data)[i] == id)
2184 {
2185 --gap->ga_len;
2186 if (i < gap->ga_len)
2187 {
2188 int *p = ((int *)gap->ga_data) + i;
2189
2190 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2191 }
2192 return;
2193 }
2194 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2195}
2196
2197/*
2198 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2199 */
2200 static int
2201channel_has_block_id(chanpart_T *chanpart, int id)
2202{
2203 garray_T *gap = &chanpart->ch_block_ids;
2204 int i;
2205
2206 for (i = 0; i < gap->ga_len; ++i)
2207 if (((int *)gap->ga_data)[i] == id)
2208 return TRUE;
2209 return FALSE;
2210}
2211
2212/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002213 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002214 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002215 * When "id" is zero or negative jut get the first message. But not one
2216 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002217 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002218 * Return OK when found and return the value in "rettv".
2219 * Return FAIL otherwise.
2220 */
2221 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002222channel_get_json(
2223 channel_T *channel,
2224 ch_part_T part,
2225 int id,
2226 int without_callback,
2227 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002228{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002229 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002230 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002231
Bram Moolenaar77073442016-02-13 23:23:53 +01002232 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002233 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002234 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002235 typval_T *tv = &l->lv_first->li_tv;
2236
Bram Moolenaar958dc692016-12-01 15:34:12 +01002237 if ((without_callback || !item->jq_no_callback)
2238 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002239 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002241 || !channel_has_block_id(
2242 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002243 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002244 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002245 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002246 ch_log(channel, "Getting JSON message %ld",
2247 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002248 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002249 return OK;
2250 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002251 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002252 }
2253 return FAIL;
2254}
2255
Bram Moolenaar958dc692016-12-01 15:34:12 +01002256/*
2257 * Put back "rettv" into the JSON queue, there was no callback for it.
2258 * Takes over the values in "rettv".
2259 */
2260 static void
2261channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2262{
2263 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2264 jsonq_T *item = head->jq_next;
2265 jsonq_T *newitem;
2266
2267 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2268 /* last item was pushed back, append to the end */
2269 item = NULL;
2270 else while (item != NULL && item->jq_no_callback)
2271 /* append after the last item that was pushed back */
2272 item = item->jq_next;
2273
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002274 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002275 if (newitem == NULL)
2276 clear_tv(rettv);
2277 else
2278 {
2279 newitem->jq_value = alloc_tv();
2280 if (newitem->jq_value == NULL)
2281 {
2282 vim_free(newitem);
2283 clear_tv(rettv);
2284 }
2285 else
2286 {
2287 newitem->jq_no_callback = FALSE;
2288 *newitem->jq_value = *rettv;
2289 if (item == NULL)
2290 {
2291 /* append to the end */
2292 newitem->jq_prev = head->jq_prev;
2293 head->jq_prev = newitem;
2294 newitem->jq_next = NULL;
2295 if (newitem->jq_prev == NULL)
2296 head->jq_next = newitem;
2297 else
2298 newitem->jq_prev->jq_next = newitem;
2299 }
2300 else
2301 {
2302 /* append after "item" */
2303 newitem->jq_prev = item;
2304 newitem->jq_next = item->jq_next;
2305 item->jq_next = newitem;
2306 if (newitem->jq_next == NULL)
2307 head->jq_prev = newitem;
2308 else
2309 newitem->jq_next->jq_prev = newitem;
2310 }
2311 }
2312 }
2313}
2314
Bram Moolenaarece61b02016-02-20 21:39:05 +01002315#define CH_JSON_MAX_ARGS 4
2316
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002317/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002318 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002319 * "argv[0]" is the command string.
2320 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002321 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002322 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002323channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002324{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 char_u *cmd = argv[0].vval.v_string;
2326 char_u *arg;
2327 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002328
Bram Moolenaarece61b02016-02-20 21:39:05 +01002329 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002330 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002332 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002334 return;
2335 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002336 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002337 if (arg == NULL)
2338 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002339
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002340 if (STRCMP(cmd, "ex") == 0)
2341 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002342 int save_called_emsg = called_emsg;
2343
2344 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002345 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002346 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002347 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002348 --emsg_silent;
2349 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002350 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002351 (char *)get_vim_var_str(VV_ERRMSG));
2352 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002353 }
2354 else if (STRCMP(cmd, "normal") == 0)
2355 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002356 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002357
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002358 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002359 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002360 ea.arg = arg;
2361 ea.addr_count = 0;
2362 ea.forceit = TRUE; /* no mapping */
2363 ex_normal(&ea);
2364 }
2365 else if (STRCMP(cmd, "redraw") == 0)
2366 {
2367 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002368
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002369 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002370 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002371 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002372 ex_redraw(&ea);
2373 showruler(FALSE);
2374 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002375 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002376 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002377 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002378 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002379 int is_call = cmd[0] == 'c';
2380 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002381
Bram Moolenaarece61b02016-02-20 21:39:05 +01002382 if (argv[id_idx].v_type != VAR_UNKNOWN
2383 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002384 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002385 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002386 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 }
2389 else if (is_call && argv[2].v_type != VAR_LIST)
2390 {
2391 ch_error(channel, "third argument for call must be a list");
2392 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002394 }
2395 else
2396 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002397 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002398 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002399 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002400 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002401
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002402 /* Don't pollute the display with errors. */
2403 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002405 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002406 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002407 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002408 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002410 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002411 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002412 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2413 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002414 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415
2416 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002417 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002418 int id = argv[id_idx].vval.v_number;
2419
Bram Moolenaar55fab432016-02-07 16:53:13 +01002420 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002421 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002422 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002423 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002424 /* If evaluation failed or the result can't be encoded
2425 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002426 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002427 err_tv.v_type = VAR_STRING;
2428 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002429 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002430 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002431 if (json != NULL)
2432 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002433 channel_send(channel,
2434 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002435 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002436 vim_free(json);
2437 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002438 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002439 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002440 if (tv == &res_tv)
2441 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002442 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002443 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002444 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002445 }
2446 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002447 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002448 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002450 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002451}
2452
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002453/*
2454 * Invoke the callback at "cbhead".
2455 * Does not redraw but sets channel_need_redraw.
2456 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002457 static void
2458invoke_one_time_callback(
2459 channel_T *channel,
2460 cbq_T *cbhead,
2461 cbq_T *item,
2462 typval_T *argv)
2463{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002464 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002465 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002466 /* Remove the item from the list first, if the callback
2467 * invokes ch_close() the list will be cleared. */
2468 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002469 invoke_callback(channel, &item->cq_callback, argv);
2470 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002471 vim_free(item);
2472}
2473
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002474 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002475append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002476{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002477 bufref_T save_curbuf = {NULL, 0, 0};
2478 win_T *save_curwin = NULL;
2479 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002480 linenr_T lnum = buffer->b_ml.ml_line_count;
2481 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002482 chanpart_T *ch_part = &channel->ch_part[part];
2483 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002484 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002485
2486 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2487 {
2488 if (!ch_part->ch_nomod_error)
2489 {
2490 ch_error(channel, "Buffer is not modifiable, cannot append");
2491 ch_part->ch_nomod_error = TRUE;
2492 }
2493 return;
2494 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002495
2496 /* If the buffer is also used as input insert above the last
2497 * line. Don't write these lines. */
2498 if (save_write_to)
2499 {
2500 --lnum;
2501 buffer->b_write_to_channel = FALSE;
2502 }
2503
2504 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002505 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002506
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002507 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002508
2509 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2510 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2511
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002512 u_sync(TRUE);
2513 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002514 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002515
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002516 if (empty)
2517 {
2518 /* The buffer is empty, replace the first (dummy) line. */
2519 ml_replace(lnum, msg, TRUE);
2520 lnum = 0;
2521 }
2522 else
2523 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002524 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002525
2526 /* Restore curbuf/curwin/curtab */
2527 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2528
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002529 if (ch_part->ch_nomodifiable)
2530 buffer->b_p_ma = FALSE;
2531 else
2532 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002533
2534 if (buffer->b_nwindows > 0)
2535 {
2536 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002537
2538 FOR_ALL_WINDOWS(wp)
2539 {
2540 if (wp->w_buffer == buffer
2541 && (save_write_to
2542 ? wp->w_cursor.lnum == lnum + 1
2543 : (wp->w_cursor.lnum == lnum
2544 && wp->w_cursor.col == 0)))
2545 {
2546 ++wp->w_cursor.lnum;
2547 save_curwin = curwin;
2548 curwin = wp;
2549 curbuf = curwin->w_buffer;
2550 scroll_cursor_bot(0, FALSE);
2551 curwin = save_curwin;
2552 curbuf = curwin->w_buffer;
2553 }
2554 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002555 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002556 channel_need_redraw = TRUE;
2557 }
2558
2559 if (save_write_to)
2560 {
2561 channel_T *ch;
2562
2563 /* Find channels reading from this buffer and adjust their
2564 * next-to-read line number. */
2565 buffer->b_write_to_channel = TRUE;
2566 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2567 {
2568 chanpart_T *in_part = &ch->ch_part[PART_IN];
2569
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002570 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002571 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2572 }
2573 }
2574}
2575
Bram Moolenaar437905c2016-04-26 19:01:05 +02002576 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002577drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002578{
2579 char_u *msg;
2580
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002581 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002582 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002583 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002584 vim_free(msg);
2585 }
2586}
2587
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002588/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002589 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002590 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002591 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002592 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002593 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002594may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002595{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002596 char_u *msg = NULL;
2597 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002598 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002599 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002600 chanpart_T *ch_part = &channel->ch_part[part];
2601 ch_mode_T ch_mode = ch_part->ch_mode;
2602 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002603 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002604 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002605 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002606 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002607
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002608 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002609 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002610 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002611
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002612 /* Use a message-specific callback, part callback or channel callback */
2613 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2614 if (cbitem->cq_seq_nr == 0)
2615 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002616 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002617 callback = &cbitem->cq_callback;
2618 else if (ch_part->ch_callback.cb_name != NULL)
2619 callback = &ch_part->ch_callback;
2620 else if (channel->ch_callback.cb_name != NULL)
2621 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002622
Bram Moolenaarec68a992016-10-03 21:37:41 +02002623 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002624 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2625 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002626 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002627 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002628 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002629 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002630 buffer = NULL;
2631 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002632
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002633 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002634 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002635 listitem_T *item;
2636 int argc = 0;
2637
Bram Moolenaard7ece102016-02-02 23:23:02 +01002638 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002639 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002640 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002641 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002642 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002643 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002644 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002645 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002646
Bram Moolenaarece61b02016-02-20 21:39:05 +01002647 for (item = listtv->vval.v_list->lv_first;
2648 item != NULL && argc < CH_JSON_MAX_ARGS;
2649 item = item->li_next)
2650 argv[argc++] = item->li_tv;
2651 while (argc < CH_JSON_MAX_ARGS)
2652 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002653
Bram Moolenaarece61b02016-02-20 21:39:05 +01002654 if (argv[0].v_type == VAR_STRING)
2655 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002656 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002657 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002658 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002659 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002660 }
2661
Bram Moolenaarece61b02016-02-20 21:39:05 +01002662 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002663 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002664 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002665 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002666 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002667 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002668 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002669 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002670 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002671 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002672 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002673 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002674 return FALSE;
2675 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002676 else
2677 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002678 /* If there is no callback or buffer drop the message. */
2679 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002680 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002681 /* If there is a close callback it may use ch_read() to get the
2682 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002683 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002684 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002685 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002686 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002687
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002688 if (ch_mode == MODE_NL)
2689 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002690 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002691 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002692 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002693
2694 /* See if we have a message ending in NL in the first buffer. If
2695 * not try to concatenate the first and the second buffer. */
2696 while (TRUE)
2697 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002698 node = channel_peek(channel, part);
2699 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002700 if (nl != NULL)
2701 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002702 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002703 {
2704 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2705 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002706 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002707 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002708 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002709 buf = node->rq_buffer;
2710
Bram Moolenaar772153f2019-03-04 12:09:49 +01002711 // Convert NUL to NL, the internal representation.
2712 for (p = buf; (nl == NULL || p < nl)
2713 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002714 if (*p == NUL)
2715 *p = NL;
2716
Bram Moolenaar772153f2019-03-04 12:09:49 +01002717 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002718 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002719 // get the whole buffer, drop the NL
2720 msg = channel_get(channel, part, NULL);
2721 }
2722 else if (nl + 1 == buf + node->rq_buflen)
2723 {
2724 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002725 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002726 *nl = NUL;
2727 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002728 else
2729 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002730 /* Copy the message into allocated memory (excluding the NL)
2731 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002732 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002733 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002734 }
2735 }
2736 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002737 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002738 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002739 * get everything we have.
2740 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002741 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002742 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002743
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002744 if (msg == NULL)
2745 return FALSE; /* out of memory (and avoids Coverity warning) */
2746
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002747 argv[1].v_type = VAR_STRING;
2748 argv[1].vval.v_string = msg;
2749 }
2750
Bram Moolenaara07fec92016-02-05 21:04:08 +01002751 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002752 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002753 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002754
Bram Moolenaar958dc692016-12-01 15:34:12 +01002755 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002756 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002757 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002758 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002759 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002760 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002761 break;
2762 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002763 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002764 {
2765 if (channel->ch_drop_never)
2766 {
2767 /* message must be read with ch_read() */
2768 channel_push_json(channel, part, listtv);
2769 listtv = NULL;
2770 }
2771 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002772 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002773 seq_nr);
2774 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002775 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002776 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002777 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002778 if (buffer != NULL)
2779 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002780 if (msg == NULL)
2781 /* JSON or JS mode: re-encode the message. */
2782 msg = json_encode(listtv, ch_mode);
2783 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002784 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002785#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002786 if (buffer->b_term != NULL)
2787 write_to_term(buffer, msg, channel);
2788 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002789#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002790 append_to_buffer(buffer, msg, channel, part);
2791 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002792 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002793
Bram Moolenaar187db502016-02-27 14:44:26 +01002794 if (callback != NULL)
2795 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002796 if (cbitem != NULL)
2797 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2798 else
2799 {
2800 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002801 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002802 (char *)callback->cb_name);
2803 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002804 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002805 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002806 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002807 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002808 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002809
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002810 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002811 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002812 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002813
2814 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002815}
2816
Bram Moolenaar113e1072019-01-20 15:30:40 +01002817#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002818/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002819 * Return TRUE when channel "channel" is open for writing to.
2820 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002821 */
2822 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002823channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002824{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002825 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002826 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002827}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002828#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002829
2830/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002831 * Return TRUE when channel "channel" is open for reading or writing.
2832 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002833 */
2834 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002835channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002836{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002837 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002838 || channel->CH_IN_FD != INVALID_FD
2839 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002840 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002841}
2842
2843/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002844 * Return TRUE if "channel" has JSON or other typeahead.
2845 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002846 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002847channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002848{
2849 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2850
2851 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2852 {
2853 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2854 jsonq_T *item = head->jq_next;
2855
2856 return item != NULL;
2857 }
2858 return channel_peek(channel, part) != NULL;
2859}
2860
2861/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002862 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002863 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002864 */
2865 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002866channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002867{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002868 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002869 int has_readahead = FALSE;
2870
Bram Moolenaar77073442016-02-13 23:23:53 +01002871 if (channel == NULL)
2872 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002873 if (req_part == PART_OUT)
2874 {
2875 if (channel->CH_OUT_FD != INVALID_FD)
2876 return "open";
2877 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002878 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002879 }
2880 else if (req_part == PART_ERR)
2881 {
2882 if (channel->CH_ERR_FD != INVALID_FD)
2883 return "open";
2884 if (channel_has_readahead(channel, PART_ERR))
2885 has_readahead = TRUE;
2886 }
2887 else
2888 {
2889 if (channel_is_open(channel))
2890 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002891 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002892 if (channel_has_readahead(channel, part))
2893 {
2894 has_readahead = TRUE;
2895 break;
2896 }
2897 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002898
2899 if (has_readahead)
2900 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002901 return "closed";
2902}
2903
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002904 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002905channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002906{
2907 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002908 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002909 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002910 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002911 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002912
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002913 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002914 STRCAT(namebuf, "_");
2915 tail = STRLEN(namebuf);
2916
2917 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002918 if (chanpart->ch_fd != INVALID_FD)
2919 status = "open";
2920 else if (channel_has_readahead(channel, part))
2921 status = "buffered";
2922 else
2923 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002924 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002925
2926 STRCPY(namebuf + tail, "mode");
2927 switch (chanpart->ch_mode)
2928 {
2929 case MODE_NL: s = "NL"; break;
2930 case MODE_RAW: s = "RAW"; break;
2931 case MODE_JSON: s = "JSON"; break;
2932 case MODE_JS: s = "JS"; break;
2933 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002934 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002935
2936 STRCPY(namebuf + tail, "io");
2937 if (part == PART_SOCK)
2938 s = "socket";
2939 else switch (chanpart->ch_io)
2940 {
2941 case JIO_NULL: s = "null"; break;
2942 case JIO_PIPE: s = "pipe"; break;
2943 case JIO_FILE: s = "file"; break;
2944 case JIO_BUFFER: s = "buffer"; break;
2945 case JIO_OUT: s = "out"; break;
2946 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002947 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002948
2949 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002950 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002951}
2952
2953 void
2954channel_info(channel_T *channel, dict_T *dict)
2955{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002956 dict_add_number(dict, "id", channel->ch_id);
2957 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002958
2959 if (channel->ch_hostname != NULL)
2960 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002961 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2962 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002963 channel_part_info(channel, dict, "sock", PART_SOCK);
2964 }
2965 else
2966 {
2967 channel_part_info(channel, dict, "out", PART_OUT);
2968 channel_part_info(channel, dict, "err", PART_ERR);
2969 channel_part_info(channel, dict, "in", PART_IN);
2970 }
2971}
2972
Bram Moolenaar77073442016-02-13 23:23:53 +01002973/*
2974 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002975 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002976 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002977 */
2978 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002979channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002980{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002981 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002982
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002983#ifdef FEAT_GUI
2984 channel_gui_unregister(channel);
2985#endif
2986
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002987 ch_close_part(channel, PART_SOCK);
2988 ch_close_part(channel, PART_IN);
2989 ch_close_part(channel, PART_OUT);
2990 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002991
Bram Moolenaara9f02812017-08-05 17:40:38 +02002992 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002993 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002994 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002995
Bram Moolenaara9f02812017-08-05 17:40:38 +02002996 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002997 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02002998 ch_log(channel,
2999 "Invoking callbacks and flushing buffers before closing");
3000 for (part = PART_SOCK; part < PART_IN; ++part)
3001 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003002 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003003 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3004 {
3005 /* Increment the refcount to avoid the channel being freed
3006 * halfway. */
3007 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003008 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003009 ch_log(channel, "flushing %s buffers before closing",
3010 part_names[part]);
3011 while (may_invoke_callback(channel, part))
3012 ;
3013 --channel->ch_refcount;
3014 }
3015 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003016
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003017 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003018 {
3019 typval_T argv[1];
3020 typval_T rettv;
3021 int dummy;
3022
3023 /* Increment the refcount to avoid the channel being freed
3024 * halfway. */
3025 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003026 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003027 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003028 argv[0].v_type = VAR_CHANNEL;
3029 argv[0].vval.v_channel = channel;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003030 call_callback(&channel->ch_close_cb, -1,
3031 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003032 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003033 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003034
Bram Moolenaara9f02812017-08-05 17:40:38 +02003035 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003036 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003037
Bram Moolenaara9f02812017-08-05 17:40:38 +02003038 if (channel_need_redraw)
3039 {
3040 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003041 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003042 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003043
Bram Moolenaara9f02812017-08-05 17:40:38 +02003044 if (!channel->ch_drop_never)
3045 /* any remaining messages are useless now */
3046 for (part = PART_SOCK; part < PART_IN; ++part)
3047 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003048
3049 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003050 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003051 }
3052
3053 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003054
3055#ifdef FEAT_TERMINAL
3056 term_channel_closed(channel);
3057#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003058}
3059
Bram Moolenaard04a0202016-01-26 23:30:18 +01003060/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003061 * Close the "in" part channel "channel".
3062 */
3063 void
3064channel_close_in(channel_T *channel)
3065{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003066 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003067}
3068
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003069 static void
3070remove_from_writeque(writeq_T *wq, writeq_T *entry)
3071{
3072 ga_clear(&entry->wq_ga);
3073 wq->wq_next = entry->wq_next;
3074 if (wq->wq_next == NULL)
3075 wq->wq_prev = NULL;
3076 else
3077 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003078 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003079}
3080
Bram Moolenaar0874a832016-09-01 15:11:51 +02003081/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003082 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003083 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003084 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003085channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003086{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003087 chanpart_T *ch_part = &channel->ch_part[part];
3088 jsonq_T *json_head = &ch_part->ch_json_head;
3089 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003091 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003092 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003093
3094 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003095 {
3096 cbq_T *node = cb_head->cq_next;
3097
3098 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003099 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003100 vim_free(node);
3101 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003102
3103 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003104 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003105 free_tv(json_head->jq_next->jq_value);
3106 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003107 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003108
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003109 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003110 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003111
3112 while (ch_part->ch_writeque.wq_next != NULL)
3113 remove_from_writeque(&ch_part->ch_writeque,
3114 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003115}
3116
3117/*
3118 * Clear all the read buffers on "channel".
3119 */
3120 void
3121channel_clear(channel_T *channel)
3122{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003123 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003124 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003125 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003126 channel_clear_one(channel, PART_OUT);
3127 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003128 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003129 free_callback(&channel->ch_callback);
3130 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003131}
3132
Bram Moolenaar77073442016-02-13 23:23:53 +01003133#if defined(EXITFREE) || defined(PROTO)
3134 void
3135channel_free_all(void)
3136{
3137 channel_T *channel;
3138
Bram Moolenaard6051b52016-02-28 15:49:03 +01003139 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003140 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3141 channel_clear(channel);
3142}
3143#endif
3144
3145
Bram Moolenaar715d2852016-04-30 17:06:31 +02003146/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003147#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003148
3149/* Buffer size for reading incoming messages. */
3150#define MAXMSGSIZE 4096
3151
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003152#if defined(HAVE_SELECT)
3153/*
3154 * Add write fds where we are waiting for writing to be possible.
3155 */
3156 static int
3157channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3158{
3159 int maxfd = maxfd_arg;
3160 channel_T *ch;
3161
3162 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3163 {
3164 chanpart_T *in_part = &ch->ch_part[PART_IN];
3165
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003166 if (in_part->ch_fd != INVALID_FD
3167 && (in_part->ch_bufref.br_buf != NULL
3168 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003169 {
3170 FD_SET((int)in_part->ch_fd, wfds);
3171 if ((int)in_part->ch_fd >= maxfd)
3172 maxfd = (int)in_part->ch_fd + 1;
3173 }
3174 }
3175 return maxfd;
3176}
3177#else
3178/*
3179 * Add write fds where we are waiting for writing to be possible.
3180 */
3181 static int
3182channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3183{
3184 int nfd = nfd_in;
3185 channel_T *ch;
3186
3187 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3188 {
3189 chanpart_T *in_part = &ch->ch_part[PART_IN];
3190
Bram Moolenaar683b7962017-08-19 15:51:59 +02003191 if (in_part->ch_fd != INVALID_FD
3192 && (in_part->ch_bufref.br_buf != NULL
3193 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003194 {
3195 in_part->ch_poll_idx = nfd;
3196 fds[nfd].fd = in_part->ch_fd;
3197 fds[nfd].events = POLLOUT;
3198 ++nfd;
3199 }
3200 else
3201 in_part->ch_poll_idx = -1;
3202 }
3203 return nfd;
3204}
3205#endif
3206
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003207typedef enum {
3208 CW_READY,
3209 CW_NOT_READY,
3210 CW_ERROR
3211} channel_wait_result;
3212
Bram Moolenaard04a0202016-01-26 23:30:18 +01003213/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003214 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003215 * Return CW_READY when there is something to read.
3216 * Return CW_NOT_READY when there is nothing to read.
3217 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003218 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003219 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003220channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003221{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003222 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003223 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003224
Bram Moolenaar4f974752019-02-17 17:44:42 +01003225# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003226 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003227 {
3228 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003229 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003230 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003231 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003232
3233 /* reading from a pipe, not a socket */
3234 while (TRUE)
3235 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003236 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3237
3238 if (r && nread > 0)
3239 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003240
3241 if (channel->ch_named_pipe)
3242 {
3243 DisconnectNamedPipe((HANDLE)fd);
3244 ConnectNamedPipe((HANDLE)fd, NULL);
3245 }
3246 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003247 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003248
3249 /* perhaps write some buffer lines */
3250 channel_write_any_lines();
3251
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003252 sleep_time = deadline - GetTickCount();
3253 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003254 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003255 /* Wait for a little while. Very short at first, up to 10 msec
3256 * after looping a few times. */
3257 if (sleep_time > delay)
3258 sleep_time = delay;
3259 Sleep(sleep_time);
3260 delay = delay * 2;
3261 if (delay > 10)
3262 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003263 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003264 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003265 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003266#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003267 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003268#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003269 struct timeval tval;
3270 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003271 fd_set wfds;
3272 int ret;
3273 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003274
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003275 tval.tv_sec = timeout / 1000;
3276 tval.tv_usec = (timeout % 1000) * 1000;
3277 for (;;)
3278 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003279 FD_ZERO(&rfds);
3280 FD_SET((int)fd, &rfds);
3281
3282 /* Write lines to a pipe when a pipe can be written to. Need to
3283 * set this every time, some buffers may be done. */
3284 maxfd = (int)fd + 1;
3285 FD_ZERO(&wfds);
3286 maxfd = channel_fill_wfds(maxfd, &wfds);
3287
3288 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003289# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003290 SOCK_ERRNO;
3291 if (ret == -1 && errno == EINTR)
3292 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003293# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003294 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003295 {
3296 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003297 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003298 channel_write_any_lines();
3299 continue;
3300 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003301 break;
3302 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003303#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003304 for (;;)
3305 {
3306 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3307 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003308
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003309 fds[0].fd = fd;
3310 fds[0].events = POLLIN;
3311 nfd = channel_fill_poll_write(nfd, fds);
3312 if (poll(fds, nfd, timeout) > 0)
3313 {
3314 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003315 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003316 channel_write_any_lines();
3317 continue;
3318 }
3319 break;
3320 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003321#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003322 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003323 return CW_NOT_READY;
3324}
3325
3326 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003327ch_close_part_on_error(
3328 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003329{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003330 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003331
3332 if (is_err)
3333 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003334 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003335 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003336 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003337
3338 /* Queue a "DETACH" netbeans message in the command queue in order to
3339 * terminate the netbeans session later. Do not end the session here
3340 * directly as we may be running in the context of a call to
3341 * netbeans_parse_messages():
3342 * netbeans_parse_messages
3343 * -> autocmd triggered while processing the netbeans cmd
3344 * -> ui_breakcheck
3345 * -> gui event loop or select loop
3346 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003347 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003348 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003349 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003350 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003351 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3352
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003353 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003354 * close the channel yet, there may be something to read on another part.
3355 * When stdout and stderr use the same FD we get the error only on one of
3356 * them, also close the other. */
3357 if (part == PART_OUT || part == PART_ERR)
3358 {
3359 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3360
3361 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3362 ch_close_part(channel, other);
3363 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003364 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003365
3366#ifdef FEAT_GUI
3367 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003368 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003369#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003370}
3371
3372 static void
3373channel_close_now(channel_T *channel)
3374{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003375 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003376 if (channel->ch_nb_close_cb != NULL)
3377 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003378 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003379}
3380
3381/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003382 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003383 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003384 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003385 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003386 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003387channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003388{
3389 static char_u *buf = NULL;
3390 int len = 0;
3391 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003392 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003393 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003394
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003395 fd = channel->ch_part[part].ch_fd;
3396 if (fd == INVALID_FD)
3397 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003398 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003399 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003400 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003401 }
3402 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003403
3404 /* Allocate a buffer to read into. */
3405 if (buf == NULL)
3406 {
3407 buf = alloc(MAXMSGSIZE);
3408 if (buf == NULL)
3409 return; /* out of memory! */
3410 }
3411
3412 /* Keep on reading for as long as there is something to read.
3413 * Use select() or poll() to avoid blocking on a message that is exactly
3414 * MAXMSGSIZE long. */
3415 for (;;)
3416 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003417 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003418 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003419 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003420 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003421 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003422 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003423 if (len <= 0)
3424 break; /* error or nothing more to read */
3425
3426 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003427 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003428 readlen += len;
3429 if (len < MAXMSGSIZE)
3430 break; /* did read everything that's available */
3431 }
3432
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003433 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003434 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003435 {
3436 if (!channel->ch_keep_open)
3437 ch_close_part_on_error(channel, part, (len < 0), func);
3438 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003439#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003440 else if (CH_HAS_GUI && gtk_main_level() > 0)
3441 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003442 gtk_main_quit();
3443#endif
3444}
3445
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003446/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003447 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003448 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003449 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003450 * Returns what was read in allocated memory.
3451 * Returns NULL in case of error or timeout.
3452 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003453 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003454channel_read_block(
3455 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003456{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003457 char_u *buf;
3458 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003459 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003460 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003461 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003462 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003463
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003464 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003465 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003466
3467 while (TRUE)
3468 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003469 node = channel_peek(channel, part);
3470 if (node != NULL)
3471 {
3472 if (mode == MODE_RAW || (mode == MODE_NL
3473 && channel_first_nl(node) != NULL))
3474 /* got a complete message */
3475 break;
3476 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3477 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003478 /* If not blocking or nothing more is coming then return what we
3479 * have. */
3480 if (raw || fd == INVALID_FD)
3481 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003482 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003483
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003484 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003485 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003486 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003487 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003488 {
3489 ch_log(channel, "Timed out");
3490 return NULL;
3491 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003492 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003493 }
3494
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003495 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003496 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003497 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003498 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003499 }
3500 else
3501 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003502 char_u *p;
3503
3504 buf = node->rq_buffer;
3505 nl = channel_first_nl(node);
3506
3507 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003508 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003509 if (*p == NUL)
3510 *p = NL;
3511
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003512 if (nl == NULL)
3513 {
3514 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003515 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003516 }
3517 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003518 {
3519 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003520 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003521 *nl = NUL;
3522 }
3523 else
3524 {
3525 /* Copy the message into allocated memory and remove it from the
3526 * buffer. */
3527 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003528 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003529 }
3530 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003531 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003532 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003533 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003534}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003535
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003536/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003538 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003539 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003540 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003541 * In corner cases this can be called recursively, that is why ch_block_ids is
3542 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003543 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003544 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003545channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003546 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003547 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003548 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003549 int id,
3550 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003551{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003552 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003553 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003554 int timeout;
3555 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003556
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003557 ch_log(channel, "Blocking read JSON for id %d", id);
3558 if (id >= 0)
3559 channel_add_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003560 for (;;)
3561 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003562 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003563
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003564 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003565 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003566 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003567 if (id >= 0)
3568 channel_remove_block_id(chanpart, id);
3569 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003570 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003571 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003572
Bram Moolenaard7ece102016-02-02 23:23:02 +01003573 if (!more)
3574 {
3575 /* Handle any other messages in the queue. If done some more
3576 * messages may have arrived. */
3577 if (channel_parse_messages())
3578 continue;
3579
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003580 /* Wait for up to the timeout. If there was an incomplete message
3581 * use the deadline for that. */
3582 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003583 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003584 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003585#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003586 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3587#else
3588 {
3589 struct timeval now_tv;
3590
3591 gettimeofday(&now_tv, NULL);
3592 timeout = (chanpart->ch_deadline.tv_sec
3593 - now_tv.tv_sec) * 1000
3594 + (chanpart->ch_deadline.tv_usec
3595 - now_tv.tv_usec) / 1000
3596 + 1;
3597 }
3598#endif
3599 if (timeout < 0)
3600 {
3601 /* Something went wrong, channel_parse_json() didn't
3602 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003603 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003604 timeout = timeout_arg;
3605 }
3606 else if (timeout > timeout_arg)
3607 timeout = timeout_arg;
3608 }
3609 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003610 if (fd == INVALID_FD
3611 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003612 {
3613 if (timeout == timeout_arg)
3614 {
3615 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003616 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003617 break;
3618 }
3619 }
3620 else
3621 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003622 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003623 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003624 if (id >= 0)
3625 channel_remove_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003626 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003627}
3628
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003629/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003630 * Get the channel from the argument.
3631 * Returns NULL if the handle is invalid.
3632 * When "check_open" is TRUE check that the channel can be used.
3633 * When "reading" is TRUE "check_open" considers typeahead useful.
3634 * "part" is used to check typeahead, when PART_COUNT use the default part.
3635 */
3636 static channel_T *
3637get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3638{
3639 channel_T *channel = NULL;
3640 int has_readahead = FALSE;
3641
3642 if (tv->v_type == VAR_JOB)
3643 {
3644 if (tv->vval.v_job != NULL)
3645 channel = tv->vval.v_job->jv_channel;
3646 }
3647 else if (tv->v_type == VAR_CHANNEL)
3648 {
3649 channel = tv->vval.v_channel;
3650 }
3651 else
3652 {
3653 semsg(_(e_invarg2), tv_get_string(tv));
3654 return NULL;
3655 }
3656 if (channel != NULL && reading)
3657 has_readahead = channel_has_readahead(channel,
3658 part != PART_COUNT ? part : channel_part_read(channel));
3659
3660 if (check_open && (channel == NULL || (!channel_is_open(channel)
3661 && !(reading && has_readahead))))
3662 {
3663 emsg(_("E906: not an open channel"));
3664 return NULL;
3665 }
3666 return channel;
3667}
3668
3669/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003670 * Common for ch_read() and ch_readraw().
3671 */
3672 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003673common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003674{
3675 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003676 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003677 jobopt_T opt;
3678 int mode;
3679 int timeout;
3680 int id = -1;
3681 typval_T *listtv = NULL;
3682
3683 /* return an empty string by default */
3684 rettv->v_type = VAR_STRING;
3685 rettv->vval.v_string = NULL;
3686
3687 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003688 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003689 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003690 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003691
Bram Moolenaar437905c2016-04-26 19:01:05 +02003692 if (opt.jo_set & JO_PART)
3693 part = opt.jo_part;
3694 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003695 if (channel != NULL)
3696 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003697 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698 part = channel_part_read(channel);
3699 mode = channel_get_mode(channel, part);
3700 timeout = channel_get_timeout(channel, part);
3701 if (opt.jo_set & JO_TIMEOUT)
3702 timeout = opt.jo_timeout;
3703
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003704 if (blob)
3705 {
3706 int outlen = 0;
3707 char_u *p = channel_read_block(channel, part,
3708 timeout, TRUE, &outlen);
3709 if (p != NULL)
3710 {
3711 blob_T *b = blob_alloc();
3712
3713 if (b != NULL)
3714 {
3715 b->bv_ga.ga_len = outlen;
3716 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3717 blob_free(b);
3718 else
3719 {
3720 memcpy(b->bv_ga.ga_data, p, outlen);
3721 rettv_blob_set(rettv, b);
3722 }
3723 }
3724 vim_free(p);
3725 }
3726 }
3727 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003728 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003729 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003730 else
3731 {
3732 if (opt.jo_set & JO_ID)
3733 id = opt.jo_id;
3734 channel_read_json_block(channel, part, timeout, id, &listtv);
3735 if (listtv != NULL)
3736 {
3737 *rettv = *listtv;
3738 vim_free(listtv);
3739 }
3740 else
3741 {
3742 rettv->v_type = VAR_SPECIAL;
3743 rettv->vval.v_number = VVAL_NONE;
3744 }
3745 }
3746 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003747
3748theend:
3749 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003750}
3751
Bram Moolenaar4f974752019-02-17 17:44:42 +01003752# if defined(MSWIN) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003753 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003754/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003755 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003756 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003757 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003758 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003759channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003760{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003761 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003762 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003763
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003764 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003765 for (channel = first_channel; channel != NULL;
3766 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003767 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003768 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003769 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003770 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003771 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003772 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003773 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003774 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003775 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003776}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003777# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003778
Bram Moolenaar4f974752019-02-17 17:44:42 +01003779# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003780/*
3781 * Check the channels for anything that is ready to be read.
3782 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003783 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003784 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003785 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003786channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003787{
3788 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003789 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003790 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003791
3792 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3793 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003794 if (only_keep_open && !channel->ch_keep_open)
3795 continue;
3796
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003797 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003798 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003799 {
3800 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003801 if (fd != INVALID_FD)
3802 {
3803 int r = channel_wait(channel, fd, 0);
3804
3805 if (r == CW_READY)
3806 channel_read(channel, part, "channel_handle_events");
3807 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003808 ch_close_part_on_error(channel, part, TRUE,
3809 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003810 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003811 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003812 }
3813}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003814# endif
3815
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003816# if defined(FEAT_GUI) || defined(PROTO)
3817/*
3818 * Return TRUE when there is any channel with a keep_open flag.
3819 */
3820 int
3821channel_any_keep_open()
3822{
3823 channel_T *channel;
3824
3825 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3826 if (channel->ch_keep_open)
3827 return TRUE;
3828 return FALSE;
3829}
3830# endif
3831
Bram Moolenaard04a0202016-01-26 23:30:18 +01003832/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003833 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003834 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003835 */
3836 void
3837channel_set_nonblock(channel_T *channel, ch_part_T part)
3838{
3839 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003840 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003841
3842 if (fd != INVALID_FD)
3843 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003844#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003845 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003846
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003847 ioctlsocket(fd, FIONBIO, &val);
3848#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003849 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003850#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003851 ch_part->ch_nonblocking = TRUE;
3852 }
3853}
3854
3855/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003856 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003857 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003858 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003859 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003860 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003861channel_send(
3862 channel_T *channel,
3863 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003864 char_u *buf_arg,
3865 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003866 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003867{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003868 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003869 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003870 chanpart_T *ch_part = &channel->ch_part[part];
3871 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003872
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003873 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003874 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003875 {
3876 if (!channel->ch_error && fun != NULL)
3877 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003878 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003879 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003880 }
3881 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003882 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003883 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003884
Bram Moolenaar0b146882018-09-06 16:27:24 +02003885 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3886 channel_set_nonblock(channel, part);
3887
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003888 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003889 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003890 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003891 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003892 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003893 fprintf(log_fd, "'\n");
3894 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003895 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003896 }
3897
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003898 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003899 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003900 writeq_T *wq = &ch_part->ch_writeque;
3901 char_u *buf;
3902 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003903
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003904 if (wq->wq_next != NULL)
3905 {
3906 /* first write what was queued */
3907 buf = wq->wq_next->wq_ga.ga_data;
3908 len = wq->wq_next->wq_ga.ga_len;
3909 did_use_queue = TRUE;
3910 }
3911 else
3912 {
3913 if (len_arg == 0)
3914 /* nothing to write, called from channel_select_check() */
3915 return OK;
3916 buf = buf_arg;
3917 len = len_arg;
3918 }
3919
3920 if (part == PART_SOCK)
3921 res = sock_write(fd, (char *)buf, len);
3922 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003923 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003924 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003925#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003926 if (channel->ch_named_pipe && res < 0)
3927 {
3928 DisconnectNamedPipe((HANDLE)fd);
3929 ConnectNamedPipe((HANDLE)fd, NULL);
3930 }
3931#endif
3932 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003933 if (res < 0 && (errno == EWOULDBLOCK
3934#ifdef EAGAIN
3935 || errno == EAGAIN
3936#endif
3937 ))
3938 res = 0; /* nothing got written */
3939
3940 if (res >= 0 && ch_part->ch_nonblocking)
3941 {
3942 writeq_T *entry = wq->wq_next;
3943
3944 if (did_use_queue)
3945 ch_log(channel, "Sent %d bytes now", res);
3946 if (res == len)
3947 {
3948 /* Wrote all the buf[len] bytes. */
3949 if (entry != NULL)
3950 {
3951 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003952 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003953 continue;
3954 }
3955 if (did_use_queue)
3956 ch_log(channel, "Write queue empty");
3957 }
3958 else
3959 {
3960 /* Wrote only buf[res] bytes, can't write more now. */
3961 if (entry != NULL)
3962 {
3963 if (res > 0)
3964 {
3965 /* Remove the bytes that were written. */
3966 mch_memmove(entry->wq_ga.ga_data,
3967 (char *)entry->wq_ga.ga_data + res,
3968 len - res);
3969 entry->wq_ga.ga_len -= res;
3970 }
3971 buf = buf_arg;
3972 len = len_arg;
3973 }
3974 else
3975 {
3976 buf += res;
3977 len -= res;
3978 }
3979 ch_log(channel, "Adding %d bytes to the write queue", len);
3980
3981 /* Append the not written bytes of the argument to the write
3982 * buffer. Limit entries to 4000 bytes. */
3983 if (wq->wq_prev != NULL
3984 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3985 {
3986 writeq_T *last = wq->wq_prev;
3987
3988 /* append to the last entry */
3989 if (ga_grow(&last->wq_ga, len) == OK)
3990 {
3991 mch_memmove((char *)last->wq_ga.ga_data
3992 + last->wq_ga.ga_len,
3993 buf, len);
3994 last->wq_ga.ga_len += len;
3995 }
3996 }
3997 else
3998 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003999 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004000
4001 if (last != NULL)
4002 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004003 last->wq_prev = wq->wq_prev;
4004 last->wq_next = NULL;
4005 if (wq->wq_prev == NULL)
4006 wq->wq_next = last;
4007 else
4008 wq->wq_prev->wq_next = last;
4009 wq->wq_prev = last;
4010 ga_init2(&last->wq_ga, 1, 1000);
4011 if (ga_grow(&last->wq_ga, len) == OK)
4012 {
4013 mch_memmove(last->wq_ga.ga_data, buf, len);
4014 last->wq_ga.ga_len = len;
4015 }
4016 }
4017 }
4018 }
4019 }
4020 else if (res != len)
4021 {
4022 if (!channel->ch_error && fun != NULL)
4023 {
4024 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004025 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004026 }
4027 channel->ch_error = TRUE;
4028 return FAIL;
4029 }
4030
4031 channel->ch_error = FALSE;
4032 return OK;
4033 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004034}
4035
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004036/*
4037 * Common for "ch_sendexpr()" and "ch_sendraw()".
4038 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004039 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004040 * Otherwise returns NULL.
4041 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004042 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004043send_common(
4044 typval_T *argvars,
4045 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004046 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004047 int id,
4048 int eval,
4049 jobopt_T *opt,
4050 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004051 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004052{
4053 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004054 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004056 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004057 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004058 if (channel == NULL)
4059 return NULL;
4060 part_send = channel_part_send(channel);
4061 *part_read = channel_part_read(channel);
4062
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004063 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 return NULL;
4065
4066 /* Set the callback. An empty callback means no callback and not reading
4067 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4068 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004069 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070 {
4071 if (eval)
4072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004073 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004074 return NULL;
4075 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004076 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077 }
4078
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004079 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004080 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081 return channel;
4082 return NULL;
4083}
4084
4085/*
4086 * common for "ch_evalexpr()" and "ch_sendexpr()"
4087 */
4088 void
4089ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4090{
4091 char_u *text;
4092 typval_T *listtv;
4093 channel_T *channel;
4094 int id;
4095 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004096 ch_part_T part_send;
4097 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004098 jobopt_T opt;
4099 int timeout;
4100
4101 /* return an empty string by default */
4102 rettv->v_type = VAR_STRING;
4103 rettv->vval.v_string = NULL;
4104
Bram Moolenaar437905c2016-04-26 19:01:05 +02004105 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004106 if (channel == NULL)
4107 return;
4108 part_send = channel_part_send(channel);
4109
4110 ch_mode = channel_get_mode(channel, part_send);
4111 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004113 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114 return;
4115 }
4116
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004117 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004119 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004120 if (text == NULL)
4121 return;
4122
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004123 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004124 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4125 vim_free(text);
4126 if (channel != NULL && eval)
4127 {
4128 if (opt.jo_set & JO_TIMEOUT)
4129 timeout = opt.jo_timeout;
4130 else
4131 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4133 == OK)
4134 {
4135 list_T *list = listtv->vval.v_list;
4136
4137 /* Move the item from the list and then change the type to
4138 * avoid the value being freed. */
4139 *rettv = list->lv_last->li_tv;
4140 list->lv_last->li_tv.v_type = VAR_NUMBER;
4141 free_tv(listtv);
4142 }
4143 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004144 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004145}
4146
4147/*
4148 * common for "ch_evalraw()" and "ch_sendraw()"
4149 */
4150 void
4151ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4152{
4153 char_u buf[NUMBUFLEN];
4154 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004155 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004157 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004158 jobopt_T opt;
4159 int timeout;
4160
4161 /* return an empty string by default */
4162 rettv->v_type = VAR_STRING;
4163 rettv->vval.v_string = NULL;
4164
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004165 if (argvars[1].v_type == VAR_BLOB)
4166 {
4167 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4168 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4169 }
4170 else
4171 {
4172 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004173 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004174 }
4175 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004176 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4177 if (channel != NULL && eval)
4178 {
4179 if (opt.jo_set & JO_TIMEOUT)
4180 timeout = opt.jo_timeout;
4181 else
4182 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004183 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004184 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004185 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004186 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004187}
4188
Bram Moolenaarf3360612017-10-01 16:21:31 +02004189# define KEEP_OPEN_TIME 20 /* msec */
4190
Bram Moolenaard04a0202016-01-26 23:30:18 +01004191# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004192/*
4193 * Add open channels to the poll struct.
4194 * Return the adjusted struct index.
4195 * The type of "fds" is hidden to avoid problems with the function proto.
4196 */
4197 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004198channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004199{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004200 int nfd = nfd_in;
4201 channel_T *channel;
4202 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004203 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004204
Bram Moolenaar77073442016-02-13 23:23:53 +01004205 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004206 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004207 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004208 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004209 chanpart_T *ch_part = &channel->ch_part[part];
4210
4211 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004212 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004213 if (channel->ch_keep_open)
4214 {
4215 /* For unknown reason poll() returns immediately for a
4216 * keep-open channel. Instead of adding it to the fds add
4217 * a short timeout and check, like polling. */
4218 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4219 *towait = KEEP_OPEN_TIME;
4220 }
4221 else
4222 {
4223 ch_part->ch_poll_idx = nfd;
4224 fds[nfd].fd = ch_part->ch_fd;
4225 fds[nfd].events = POLLIN;
4226 nfd++;
4227 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004228 }
4229 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004230 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004231 }
4232 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004233
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004234 nfd = channel_fill_poll_write(nfd, fds);
4235
Bram Moolenaare0874f82016-01-24 20:36:41 +01004236 return nfd;
4237}
4238
4239/*
4240 * The type of "fds" is hidden to avoid problems with the function proto.
4241 */
4242 int
4243channel_poll_check(int ret_in, void *fds_in)
4244{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004245 int ret = ret_in;
4246 channel_T *channel;
4247 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004248 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004249 int idx;
4250 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004251
Bram Moolenaar77073442016-02-13 23:23:53 +01004252 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004253 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004254 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004255 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004256 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004257
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004258 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004259 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004260 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004261 --ret;
4262 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004263 else if (channel->ch_part[part].ch_fd != INVALID_FD
4264 && channel->ch_keep_open)
4265 {
4266 /* polling a keep-open channel */
4267 channel_read(channel, part, "channel_poll_check_keep_open");
4268 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004269 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004270
4271 in_part = &channel->ch_part[PART_IN];
4272 idx = in_part->ch_poll_idx;
4273 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4274 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004275 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004276 --ret;
4277 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004278 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004279
4280 return ret;
4281}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004282# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004283
Bram Moolenaar4f974752019-02-17 17:44:42 +01004284# if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004285
Bram Moolenaare0874f82016-01-24 20:36:41 +01004286/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004287 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004288 */
4289 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004290channel_select_setup(
4291 int maxfd_in,
4292 void *rfds_in,
4293 void *wfds_in,
4294 struct timeval *tv,
4295 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004296{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004297 int maxfd = maxfd_in;
4298 channel_T *channel;
4299 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004300 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004301 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004302
Bram Moolenaar77073442016-02-13 23:23:53 +01004303 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004304 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004305 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004306 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004307 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004308
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004309 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004310 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004311 if (channel->ch_keep_open)
4312 {
4313 /* For unknown reason select() returns immediately for a
4314 * keep-open channel. Instead of adding it to the rfds add
4315 * a short timeout and check, like polling. */
4316 if (*tvp == NULL || tv->tv_sec > 0
4317 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4318 {
4319 *tvp = tv;
4320 tv->tv_sec = 0;
4321 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4322 }
4323 }
4324 else
4325 {
4326 FD_SET((int)fd, rfds);
4327 if (maxfd < (int)fd)
4328 maxfd = (int)fd;
4329 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004330 }
4331 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004332 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004333
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004334 maxfd = channel_fill_wfds(maxfd, wfds);
4335
Bram Moolenaare0874f82016-01-24 20:36:41 +01004336 return maxfd;
4337}
4338
4339/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004340 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004341 */
4342 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004343channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004344{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004345 int ret = ret_in;
4346 channel_T *channel;
4347 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004348 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004349 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004350 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004351
Bram Moolenaar77073442016-02-13 23:23:53 +01004352 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004353 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004354 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004355 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004356 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004357
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004358 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004359 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004360 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004361 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004362 --ret;
4363 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004364 else if (fd != INVALID_FD && channel->ch_keep_open)
4365 {
4366 /* polling a keep-open channel */
4367 channel_read(channel, part, "channel_select_check_keep_open");
4368 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004369 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004370
4371 in_part = &channel->ch_part[PART_IN];
4372 if (ret > 0 && in_part->ch_fd != INVALID_FD
4373 && FD_ISSET(in_part->ch_fd, wfds))
4374 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004375 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004376 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004377 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004378 --ret;
4379 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004380 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004381
4382 return ret;
4383}
Bram Moolenaar4f974752019-02-17 17:44:42 +01004384# endif /* !MSWIN && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004385
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004386/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004387 * Execute queued up commands.
4388 * Invoked from the main loop when it's safe to execute received commands.
4389 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004390 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004391 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004392channel_parse_messages(void)
4393{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004394 channel_T *channel = first_channel;
4395 int ret = FALSE;
4396 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004397 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004398#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004399 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004400
4401 ELAPSED_INIT(start_tv);
4402#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004403
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004404 ++safe_to_invoke_callback;
4405
Bram Moolenaard0b65022016-03-06 21:50:33 +01004406 /* Only do this message when another message was given, otherwise we get
4407 * lots of them. */
4408 if (did_log_msg)
4409 {
4410 ch_log(NULL, "looking for messages on channels");
4411 did_log_msg = FALSE;
4412 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004413 while (channel != NULL)
4414 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004415 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004416 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004417 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004418 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004419 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004420 channel = first_channel;
4421 continue;
4422 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004423 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004424 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004425 if (channel->ch_killing)
4426 {
4427 channel_free_contents(channel);
4428 channel->ch_job->jv_channel = NULL;
4429 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004430 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004431 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004432 channel = first_channel;
4433 continue;
4434 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004435 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004436 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004437 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004438 channel_free(channel);
4439 channel = first_channel;
4440 part = PART_SOCK;
4441 continue;
4442 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004443 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004444 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004445 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004446 /* Increase the refcount, in case the handler causes the channel
4447 * to be unreferenced or closed. */
4448 ++channel->ch_refcount;
4449 r = may_invoke_callback(channel, part);
4450 if (r == OK)
4451 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004452 if (channel_unref(channel) || (r == OK
4453#ifdef ELAPSED_FUNC
4454 /* Limit the time we loop here to 100 msec, otherwise
4455 * Vim becomes unresponsive when the callback takes
4456 * more than a bit of time. */
4457 && ELAPSED_FUNC(start_tv) < 100L
4458#endif
4459 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004460 {
4461 /* channel was freed or something was done, start over */
4462 channel = first_channel;
4463 part = PART_SOCK;
4464 continue;
4465 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004466 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004467 if (part < PART_ERR)
4468 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004469 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004470 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004471 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004472 part = PART_SOCK;
4473 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004474 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004475
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004476 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004477 {
4478 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004479 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004480 }
4481
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004482 --safe_to_invoke_callback;
4483
Bram Moolenaard7ece102016-02-02 23:23:02 +01004484 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004485}
4486
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004487/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004488 * Return TRUE if any channel has readahead. That means we should not block on
4489 * waiting for input.
4490 */
4491 int
4492channel_any_readahead(void)
4493{
4494 channel_T *channel = first_channel;
4495 ch_part_T part = PART_SOCK;
4496
4497 while (channel != NULL)
4498 {
4499 if (channel_has_readahead(channel, part))
4500 return TRUE;
4501 if (part < PART_ERR)
4502 ++part;
4503 else
4504 {
4505 channel = channel->ch_next;
4506 part = PART_SOCK;
4507 }
4508 }
4509 return FALSE;
4510}
4511
4512/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004513 * Mark references to lists used in channels.
4514 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004515 int
4516set_ref_in_channel(int copyID)
4517{
Bram Moolenaar77073442016-02-13 23:23:53 +01004518 int abort = FALSE;
4519 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004520 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004521
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004522 for (channel = first_channel; !abort && channel != NULL;
4523 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004524 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004525 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004526 tv.v_type = VAR_CHANNEL;
4527 tv.vval.v_channel = channel;
4528 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004529 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004530 return abort;
4531}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004532
4533/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004534 * Return the "part" to write to for "channel".
4535 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004536 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004537channel_part_send(channel_T *channel)
4538{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004539 if (channel->CH_SOCK_FD == INVALID_FD)
4540 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004541 return PART_SOCK;
4542}
4543
4544/*
4545 * Return the default "part" to read from for "channel".
4546 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004547 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004548channel_part_read(channel_T *channel)
4549{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004550 if (channel->CH_SOCK_FD == INVALID_FD)
4551 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004552 return PART_SOCK;
4553}
4554
4555/*
4556 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004557 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004558 */
4559 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004560channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004561{
Bram Moolenaar77073442016-02-13 23:23:53 +01004562 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004563 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004564 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004565}
4566
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004567/*
4568 * Return the timeout of "channel"/"part"
4569 */
4570 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004571channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004572{
4573 return channel->ch_part[part].ch_timeout;
4574}
4575
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004576 static int
4577handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4578{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004579 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004580
4581 opt->jo_set |= jo;
4582 if (STRCMP(val, "nl") == 0)
4583 *modep = MODE_NL;
4584 else if (STRCMP(val, "raw") == 0)
4585 *modep = MODE_RAW;
4586 else if (STRCMP(val, "js") == 0)
4587 *modep = MODE_JS;
4588 else if (STRCMP(val, "json") == 0)
4589 *modep = MODE_JSON;
4590 else
4591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004592 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004593 return FAIL;
4594 }
4595 return OK;
4596}
4597
4598 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004599handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004601 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004602
4603 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4604 if (STRCMP(val, "null") == 0)
4605 opt->jo_io[part] = JIO_NULL;
4606 else if (STRCMP(val, "pipe") == 0)
4607 opt->jo_io[part] = JIO_PIPE;
4608 else if (STRCMP(val, "file") == 0)
4609 opt->jo_io[part] = JIO_FILE;
4610 else if (STRCMP(val, "buffer") == 0)
4611 opt->jo_io[part] = JIO_BUFFER;
4612 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4613 opt->jo_io[part] = JIO_OUT;
4614 else
4615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004616 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004617 return FAIL;
4618 }
4619 return OK;
4620}
4621
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004622/*
4623 * Clear a jobopt_T before using it.
4624 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004625 void
4626clear_job_options(jobopt_T *opt)
4627{
4628 vim_memset(opt, 0, sizeof(jobopt_T));
4629}
4630
4631/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004632 * Free any members of a jobopt_T.
4633 */
4634 void
4635free_job_options(jobopt_T *opt)
4636{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004637 if (opt->jo_callback.cb_partial != NULL)
4638 partial_unref(opt->jo_callback.cb_partial);
4639 else if (opt->jo_callback.cb_name != NULL)
4640 func_unref(opt->jo_callback.cb_name);
4641 if (opt->jo_out_cb.cb_partial != NULL)
4642 partial_unref(opt->jo_out_cb.cb_partial);
4643 else if (opt->jo_out_cb.cb_name != NULL)
4644 func_unref(opt->jo_out_cb.cb_name);
4645 if (opt->jo_err_cb.cb_partial != NULL)
4646 partial_unref(opt->jo_err_cb.cb_partial);
4647 else if (opt->jo_err_cb.cb_name != NULL)
4648 func_unref(opt->jo_err_cb.cb_name);
4649 if (opt->jo_close_cb.cb_partial != NULL)
4650 partial_unref(opt->jo_close_cb.cb_partial);
4651 else if (opt->jo_close_cb.cb_name != NULL)
4652 func_unref(opt->jo_close_cb.cb_name);
4653 if (opt->jo_exit_cb.cb_partial != NULL)
4654 partial_unref(opt->jo_exit_cb.cb_partial);
4655 else if (opt->jo_exit_cb.cb_name != NULL)
4656 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004657 if (opt->jo_env != NULL)
4658 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004659}
4660
4661/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004662 * Get the PART_ number from the first character of an option name.
4663 */
4664 static int
4665part_from_char(int c)
4666{
4667 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4668}
4669
4670/*
4671 * Get the option entries from the dict in "tv", parse them and put the result
4672 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004673 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004674 * If an option value is invalid return FAIL.
4675 */
4676 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004677get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678{
4679 typval_T *item;
4680 char_u *val;
4681 dict_T *dict;
4682 int todo;
4683 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004684 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004685
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004686 if (tv->v_type == VAR_UNKNOWN)
4687 return OK;
4688 if (tv->v_type != VAR_DICT)
4689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004690 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691 return FAIL;
4692 }
4693 dict = tv->vval.v_dict;
4694 if (dict == NULL)
4695 return OK;
4696
4697 todo = (int)dict->dv_hashtab.ht_used;
4698 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4699 if (!HASHITEM_EMPTY(hi))
4700 {
4701 item = &dict_lookup(hi)->di_tv;
4702
4703 if (STRCMP(hi->hi_key, "mode") == 0)
4704 {
4705 if (!(supported & JO_MODE))
4706 break;
4707 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4708 return FAIL;
4709 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004710 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004711 {
4712 if (!(supported & JO_IN_MODE))
4713 break;
4714 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4715 == FAIL)
4716 return FAIL;
4717 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004718 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004719 {
4720 if (!(supported & JO_OUT_MODE))
4721 break;
4722 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4723 == FAIL)
4724 return FAIL;
4725 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004726 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 {
4728 if (!(supported & JO_ERR_MODE))
4729 break;
4730 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4731 == FAIL)
4732 return FAIL;
4733 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004734 else if (STRCMP(hi->hi_key, "noblock") == 0)
4735 {
4736 if (!(supported & JO_MODE))
4737 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004738 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004739 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004740 else if (STRCMP(hi->hi_key, "in_io") == 0
4741 || STRCMP(hi->hi_key, "out_io") == 0
4742 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004743 {
4744 if (!(supported & JO_OUT_IO))
4745 break;
4746 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4747 return FAIL;
4748 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004749 else if (STRCMP(hi->hi_key, "in_name") == 0
4750 || STRCMP(hi->hi_key, "out_name") == 0
4751 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004752 {
4753 part = part_from_char(*hi->hi_key);
4754
4755 if (!(supported & JO_OUT_IO))
4756 break;
4757 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4758 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004759 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004760 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004761 else if (STRCMP(hi->hi_key, "pty") == 0)
4762 {
4763 if (!(supported & JO_MODE))
4764 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004765 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004766 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004767 else if (STRCMP(hi->hi_key, "in_buf") == 0
4768 || STRCMP(hi->hi_key, "out_buf") == 0
4769 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004770 {
4771 part = part_from_char(*hi->hi_key);
4772
4773 if (!(supported & JO_OUT_IO))
4774 break;
4775 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004776 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004777 if (opt->jo_io_buf[part] <= 0)
4778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004780 return FAIL;
4781 }
4782 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004784 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004785 return FAIL;
4786 }
4787 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004788 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4789 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4790 {
4791 part = part_from_char(*hi->hi_key);
4792
4793 if (!(supported & JO_OUT_IO))
4794 break;
4795 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004796 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004797 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004798 else if (STRCMP(hi->hi_key, "out_msg") == 0
4799 || STRCMP(hi->hi_key, "err_msg") == 0)
4800 {
4801 part = part_from_char(*hi->hi_key);
4802
4803 if (!(supported & JO_OUT_IO))
4804 break;
4805 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004806 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004807 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004808 else if (STRCMP(hi->hi_key, "in_top") == 0
4809 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004810 {
4811 linenr_T *lp;
4812
4813 if (!(supported & JO_OUT_IO))
4814 break;
4815 if (hi->hi_key[3] == 't')
4816 {
4817 lp = &opt->jo_in_top;
4818 opt->jo_set |= JO_IN_TOP;
4819 }
4820 else
4821 {
4822 lp = &opt->jo_in_bot;
4823 opt->jo_set |= JO_IN_BOT;
4824 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004825 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004826 if (*lp < 0)
4827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004828 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004829 return FAIL;
4830 }
4831 }
4832 else if (STRCMP(hi->hi_key, "channel") == 0)
4833 {
4834 if (!(supported & JO_OUT_IO))
4835 break;
4836 opt->jo_set |= JO_CHANNEL;
4837 if (item->v_type != VAR_CHANNEL)
4838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004840 return FAIL;
4841 }
4842 opt->jo_channel = item->vval.v_channel;
4843 }
4844 else if (STRCMP(hi->hi_key, "callback") == 0)
4845 {
4846 if (!(supported & JO_CALLBACK))
4847 break;
4848 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004849 opt->jo_callback = get_callback(item);
4850 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004853 return FAIL;
4854 }
4855 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004856 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004857 {
4858 if (!(supported & JO_OUT_CALLBACK))
4859 break;
4860 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004861 opt->jo_out_cb = get_callback(item);
4862 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004865 return FAIL;
4866 }
4867 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004868 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004869 {
4870 if (!(supported & JO_ERR_CALLBACK))
4871 break;
4872 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004873 opt->jo_err_cb = get_callback(item);
4874 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004876 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004877 return FAIL;
4878 }
4879 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004880 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004881 {
4882 if (!(supported & JO_CLOSE_CALLBACK))
4883 break;
4884 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 opt->jo_close_cb = get_callback(item);
4886 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004888 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004889 return FAIL;
4890 }
4891 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004892 else if (STRCMP(hi->hi_key, "drop") == 0)
4893 {
4894 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004895 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004896
4897 if (STRCMP(val, "never") == 0)
4898 never = TRUE;
4899 else if (STRCMP(val, "auto") != 0)
4900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004901 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004902 return FAIL;
4903 }
4904 opt->jo_drop_never = never;
4905 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004906 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4907 {
4908 if (!(supported & JO_EXIT_CB))
4909 break;
4910 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004911 opt->jo_exit_cb = get_callback(item);
4912 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004914 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004915 return FAIL;
4916 }
4917 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004918#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004919 else if (STRCMP(hi->hi_key, "term_name") == 0)
4920 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004921 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004922 break;
4923 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004924 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004925 if (opt->jo_term_name == NULL)
4926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004927 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004928 return FAIL;
4929 }
4930 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004931 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4932 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004933 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004934 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004935 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004936 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004938 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004939 return FAIL;
4940 }
4941 opt->jo_set2 |= JO2_TERM_FINISH;
4942 opt->jo_term_finish = *val;
4943 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004944 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4945 {
4946 char_u *p;
4947
4948 if (!(supported2 & JO2_TERM_OPENCMD))
4949 break;
4950 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004951 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004952 if (p != NULL)
4953 {
4954 /* Must have %d and no other %. */
4955 p = vim_strchr(p, '%');
4956 if (p != NULL && (p[1] != 'd'
4957 || vim_strchr(p + 2, '%') != NULL))
4958 p = NULL;
4959 }
4960 if (p == NULL)
4961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004962 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004963 return FAIL;
4964 }
4965 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004966 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4967 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004968 char_u *p;
4969
4970 if (!(supported2 & JO2_EOF_CHARS))
4971 break;
4972 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004973 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004974 if (p == NULL)
4975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004976 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004977 return FAIL;
4978 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004979 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004980 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4981 {
4982 if (!(supported2 & JO2_TERM_ROWS))
4983 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004984 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004985 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004986 }
4987 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4988 {
4989 if (!(supported2 & JO2_TERM_COLS))
4990 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004991 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004992 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004993 }
4994 else if (STRCMP(hi->hi_key, "vertical") == 0)
4995 {
4996 if (!(supported2 & JO2_VERTICAL))
4997 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004998 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004999 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005000 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005001 else if (STRCMP(hi->hi_key, "curwin") == 0)
5002 {
5003 if (!(supported2 & JO2_CURWIN))
5004 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005005 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005006 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005007 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005008 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5009 {
5010 int nr;
5011
5012 if (!(supported2 & JO2_CURWIN))
5013 break;
5014 opt->jo_set2 |= JO2_BUFNR;
5015 nr = tv_get_number(item);
5016 if (nr <= 0)
5017 {
5018 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5019 return FAIL;
5020 }
5021 opt->jo_bufnr_buf = buflist_findnr(nr);
5022 if (opt->jo_bufnr_buf == NULL)
5023 {
5024 semsg(_(e_nobufnr), (long)nr);
5025 return FAIL;
5026 }
5027 if (opt->jo_bufnr_buf->b_nwindows == 0
5028 || opt->jo_bufnr_buf->b_term == NULL)
5029 {
5030 semsg(_(e_invarg2), "bufnr");
5031 return FAIL;
5032 }
5033 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005034 else if (STRCMP(hi->hi_key, "hidden") == 0)
5035 {
5036 if (!(supported2 & JO2_HIDDEN))
5037 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005038 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005039 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005040 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005041 else if (STRCMP(hi->hi_key, "norestore") == 0)
5042 {
5043 if (!(supported2 & JO2_NORESTORE))
5044 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005045 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005046 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005047 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005048 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5049 {
5050 if (!(supported2 & JO2_TERM_KILL))
5051 break;
5052 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005053 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005054 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005055 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005056 {
5057 char_u *p;
5058
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005059 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005060 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005061 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005062 p = tv_get_string_chk(item);
5063 if (p == NULL)
5064 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005065 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005066 return FAIL;
5067 }
5068 // Allow empty string, "winpty", "conpty".
5069 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5070 || STRCMP(p, "conpty") == 0))
5071 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005072 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005073 return FAIL;
5074 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005075 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005076 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005077# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5078 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5079 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005080 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005081 listitem_T *li;
5082 long_u rgb[16];
5083
5084 if (!(supported2 & JO2_ANSI_COLORS))
5085 break;
5086
5087 if (item == NULL || item->v_type != VAR_LIST
5088 || item->vval.v_list == NULL)
5089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005090 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005091 return FAIL;
5092 }
5093
5094 li = item->vval.v_list->lv_first;
5095 for (; li != NULL && n < 16; li = li->li_next, n++)
5096 {
5097 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005098 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005099
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005100 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005101 if (color_name == NULL)
5102 return FAIL;
5103
5104 guicolor = GUI_GET_COLOR(color_name);
5105 if (guicolor == INVALCOLOR)
5106 return FAIL;
5107
5108 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5109 }
5110
5111 if (n != 16 || li != NULL)
5112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005113 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005114 return FAIL;
5115 }
5116
5117 opt->jo_set2 |= JO2_ANSI_COLORS;
5118 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5119 }
5120# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005121#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005122 else if (STRCMP(hi->hi_key, "env") == 0)
5123 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005124 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005125 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005126 if (item->v_type != VAR_DICT)
5127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005128 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005129 return FAIL;
5130 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005131 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005132 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005133 if (opt->jo_env != NULL)
5134 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005135 }
5136 else if (STRCMP(hi->hi_key, "cwd") == 0)
5137 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005138 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005139 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005140 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005141 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005142#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005143 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5144#endif
5145 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005147 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005148 return FAIL;
5149 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005150 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005151 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005152 else if (STRCMP(hi->hi_key, "waittime") == 0)
5153 {
5154 if (!(supported & JO_WAITTIME))
5155 break;
5156 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005157 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005158 }
5159 else if (STRCMP(hi->hi_key, "timeout") == 0)
5160 {
5161 if (!(supported & JO_TIMEOUT))
5162 break;
5163 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005164 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005165 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005166 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005167 {
5168 if (!(supported & JO_OUT_TIMEOUT))
5169 break;
5170 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005171 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005172 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005173 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005174 {
5175 if (!(supported & JO_ERR_TIMEOUT))
5176 break;
5177 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005178 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005179 }
5180 else if (STRCMP(hi->hi_key, "part") == 0)
5181 {
5182 if (!(supported & JO_PART))
5183 break;
5184 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005185 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005186 if (STRCMP(val, "err") == 0)
5187 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005188 else if (STRCMP(val, "out") == 0)
5189 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005190 else
5191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005192 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005193 return FAIL;
5194 }
5195 }
5196 else if (STRCMP(hi->hi_key, "id") == 0)
5197 {
5198 if (!(supported & JO_ID))
5199 break;
5200 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005201 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005202 }
5203 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5204 {
5205 if (!(supported & JO_STOPONEXIT))
5206 break;
5207 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005208 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005209 opt->jo_soe_buf);
5210 if (opt->jo_stoponexit == NULL)
5211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005212 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 return FAIL;
5214 }
5215 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005216 else if (STRCMP(hi->hi_key, "block_write") == 0)
5217 {
5218 if (!(supported & JO_BLOCK_WRITE))
5219 break;
5220 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005221 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005222 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005223 else
5224 break;
5225 --todo;
5226 }
5227 if (todo > 0)
5228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005229 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005230 return FAIL;
5231 }
5232
5233 return OK;
5234}
5235
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005236static job_T *first_job = NULL;
5237
5238 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005239job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005240{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005241 int i;
5242
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005243 ch_log(job->jv_channel, "Freeing job");
5244 if (job->jv_channel != NULL)
5245 {
5246 /* The link from the channel to the job doesn't count as a reference,
5247 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005248 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005249 * NULL the reference. We don't set ch_job_killed, unreferencing the
5250 * job doesn't mean it stops running. */
5251 job->jv_channel->ch_job = NULL;
5252 channel_unref(job->jv_channel);
5253 }
5254 mch_clear_job(job);
5255
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005256 vim_free(job->jv_tty_in);
5257 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005258 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005259#ifdef UNIX
5260 vim_free(job->jv_termsig);
5261#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005262#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005263 vim_free(job->jv_tty_type);
5264#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005265 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005266 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005267 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005268 for (i = 0; job->jv_argv[i] != NULL; i++)
5269 vim_free(job->jv_argv[i]);
5270 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005271 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005272}
5273
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005274/*
5275 * Remove "job" from the list of jobs.
5276 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005277 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005278job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005279{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005280 if (job->jv_next != NULL)
5281 job->jv_next->jv_prev = job->jv_prev;
5282 if (job->jv_prev == NULL)
5283 first_job = job->jv_next;
5284 else
5285 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005286}
5287
5288 static void
5289job_free_job(job_T *job)
5290{
5291 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005292 vim_free(job);
5293}
5294
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005295 static void
5296job_free(job_T *job)
5297{
5298 if (!in_free_unref_items)
5299 {
5300 job_free_contents(job);
5301 job_free_job(job);
5302 }
5303}
5304
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005305job_T *jobs_to_free = NULL;
5306
5307/*
5308 * Put "job" in a list to be freed later, when it's no longer referenced.
5309 */
5310 static void
5311job_free_later(job_T *job)
5312{
5313 job_unlink(job);
5314 job->jv_next = jobs_to_free;
5315 jobs_to_free = job;
5316}
5317
5318 static void
5319free_jobs_to_free_later(void)
5320{
5321 job_T *job;
5322
5323 while (jobs_to_free != NULL)
5324 {
5325 job = jobs_to_free;
5326 jobs_to_free = job->jv_next;
5327 job_free_contents(job);
5328 vim_free(job);
5329 }
5330}
5331
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005332#if defined(EXITFREE) || defined(PROTO)
5333 void
5334job_free_all(void)
5335{
5336 while (first_job != NULL)
5337 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005338 free_jobs_to_free_later();
5339
5340# ifdef FEAT_TERMINAL
5341 free_unused_terminals();
5342# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005343}
5344#endif
5345
5346/*
5347 * Return TRUE if we need to check if the process of "job" has ended.
5348 */
5349 static int
5350job_need_end_check(job_T *job)
5351{
5352 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005353 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005354}
5355
5356/*
5357 * Return TRUE if the channel of "job" is still useful.
5358 */
5359 static int
5360job_channel_still_useful(job_T *job)
5361{
5362 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5363}
5364
5365/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005366 * Return TRUE if the channel of "job" is closeable.
5367 */
5368 static int
5369job_channel_can_close(job_T *job)
5370{
5371 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5372}
5373
5374/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005375 * Return TRUE if the job should not be freed yet. Do not free the job when
5376 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5377 * or when the associated channel will do something with the job output.
5378 */
5379 static int
5380job_still_useful(job_T *job)
5381{
5382 return job_need_end_check(job) || job_channel_still_useful(job);
5383}
5384
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005385#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005386/*
5387 * Return TRUE when there is any running job that we care about.
5388 */
5389 int
5390job_any_running()
5391{
5392 job_T *job;
5393
5394 for (job = first_job; job != NULL; job = job->jv_next)
5395 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005396 {
5397 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005398 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005399 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005400 return FALSE;
5401}
5402#endif
5403
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005404#if !defined(USE_ARGV) || defined(PROTO)
5405/*
5406 * Escape one argument for an external command.
5407 * Returns the escaped string in allocated memory. NULL when out of memory.
5408 */
5409 static char_u *
5410win32_escape_arg(char_u *arg)
5411{
5412 int slen, dlen;
5413 int escaping = 0;
5414 int i;
5415 char_u *s, *d;
5416 char_u *escaped_arg;
5417 int has_spaces = FALSE;
5418
5419 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005420 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005421 dlen = slen;
5422 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5423 {
5424 if (*s == '"' || *s == '\\')
5425 ++dlen;
5426 if (*s == ' ' || *s == '\t')
5427 has_spaces = TRUE;
5428 }
5429
5430 if (has_spaces)
5431 dlen += 2;
5432
5433 if (dlen == slen)
5434 return vim_strsave(arg);
5435
5436 /* Allocate memory for the result and fill it. */
5437 escaped_arg = alloc(dlen + 1);
5438 if (escaped_arg == NULL)
5439 return NULL;
5440 memset(escaped_arg, 0, dlen+1);
5441
5442 d = escaped_arg;
5443
5444 if (has_spaces)
5445 *d++ = '"';
5446
5447 for (s = arg; *s != NUL;)
5448 {
5449 switch (*s)
5450 {
5451 case '"':
5452 for (i = 0; i < escaping; i++)
5453 *d++ = '\\';
5454 escaping = 0;
5455 *d++ = '\\';
5456 *d++ = *s++;
5457 break;
5458 case '\\':
5459 escaping++;
5460 *d++ = *s++;
5461 break;
5462 default:
5463 escaping = 0;
5464 MB_COPY_CHAR(s, d);
5465 break;
5466 }
5467 }
5468
5469 /* add terminating quote and finish with a NUL */
5470 if (has_spaces)
5471 {
5472 for (i = 0; i < escaping; i++)
5473 *d++ = '\\';
5474 *d++ = '"';
5475 }
5476 *d = NUL;
5477
5478 return escaped_arg;
5479}
5480
5481/*
5482 * Build a command line from a list, taking care of escaping.
5483 * The result is put in gap->ga_data.
5484 * Returns FAIL when out of memory.
5485 */
5486 int
5487win32_build_cmd(list_T *l, garray_T *gap)
5488{
5489 listitem_T *li;
5490 char_u *s;
5491
5492 for (li = l->lv_first; li != NULL; li = li->li_next)
5493 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005494 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005495 if (s == NULL)
5496 return FAIL;
5497 s = win32_escape_arg(s);
5498 if (s == NULL)
5499 return FAIL;
5500 ga_concat(gap, s);
5501 vim_free(s);
5502 if (li->li_next != NULL)
5503 ga_append(gap, ' ');
5504 }
5505 return OK;
5506}
5507#endif
5508
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005509/*
5510 * NOTE: Must call job_cleanup() only once right after the status of "job"
5511 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5512 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005513 * If the job is no longer used it will be removed from the list of jobs, and
5514 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005515 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005516 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005517job_cleanup(job_T *job)
5518{
5519 if (job->jv_status != JOB_ENDED)
5520 return;
5521
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005522 /* Ready to cleanup the job. */
5523 job->jv_status = JOB_FINISHED;
5524
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005525 /* When only channel-in is kept open, close explicitly. */
5526 if (job->jv_channel != NULL)
5527 ch_close_part(job->jv_channel, PART_IN);
5528
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005529 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005530 {
5531 typval_T argv[3];
5532 typval_T rettv;
5533 int dummy;
5534
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005535 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005536 ch_log(job->jv_channel, "Invoking exit callback %s",
5537 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005538 ++job->jv_refcount;
5539 argv[0].v_type = VAR_JOB;
5540 argv[0].vval.v_job = job;
5541 argv[1].v_type = VAR_NUMBER;
5542 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005543 call_callback(&job->jv_exit_cb, -1,
5544 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005545 clear_tv(&rettv);
5546 --job->jv_refcount;
5547 channel_need_redraw = TRUE;
5548 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005549
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005550 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5551 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005552
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005553 // Do not free the job in case the close callback of the associated channel
5554 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005555 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005556 // The job was already unreferenced and the associated channel was
5557 // detached, now that it ended it can be freed. However, a caller might
5558 // still use it, thus free it a bit later.
5559 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005560}
5561
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005562/*
5563 * Mark references in jobs that are still useful.
5564 */
5565 int
5566set_ref_in_job(int copyID)
5567{
5568 int abort = FALSE;
5569 job_T *job;
5570 typval_T tv;
5571
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005572 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005573 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005574 {
5575 tv.v_type = VAR_JOB;
5576 tv.vval.v_job = job;
5577 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5578 }
5579 return abort;
5580}
5581
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005582/*
5583 * Dereference "job". Note that after this "job" may have been freed.
5584 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005585 void
5586job_unref(job_T *job)
5587{
5588 if (job != NULL && --job->jv_refcount <= 0)
5589 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005590 /* Do not free the job if there is a channel where the close callback
5591 * may get the job info. */
5592 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005593 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005594 /* Do not free the job when it has not ended yet and there is a
5595 * "stoponexit" flag or an exit callback. */
5596 if (!job_need_end_check(job))
5597 {
5598 job_free(job);
5599 }
5600 else if (job->jv_channel != NULL)
5601 {
5602 /* Do remove the link to the channel, otherwise it hangs
5603 * around until Vim exits. See job_free() for refcount. */
5604 ch_log(job->jv_channel, "detaching channel from job");
5605 job->jv_channel->ch_job = NULL;
5606 channel_unref(job->jv_channel);
5607 job->jv_channel = NULL;
5608 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005609 }
5610 }
5611}
5612
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005613 int
5614free_unused_jobs_contents(int copyID, int mask)
5615{
5616 int did_free = FALSE;
5617 job_T *job;
5618
5619 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005620 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005621 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005622 {
5623 /* Free the channel and ordinary items it contains, but don't
5624 * recurse into Lists, Dictionaries etc. */
5625 job_free_contents(job);
5626 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005627 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005628 return did_free;
5629}
5630
5631 void
5632free_unused_jobs(int copyID, int mask)
5633{
5634 job_T *job;
5635 job_T *job_next;
5636
5637 for (job = first_job; job != NULL; job = job_next)
5638 {
5639 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005640 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005641 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005642 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005643 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005644 job_free_job(job);
5645 }
5646 }
5647}
5648
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005649/*
5650 * Allocate a job. Sets the refcount to one and sets options default.
5651 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005652 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005653job_alloc(void)
5654{
5655 job_T *job;
5656
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005657 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005658 if (job != NULL)
5659 {
5660 job->jv_refcount = 1;
5661 job->jv_stoponexit = vim_strsave((char_u *)"term");
5662
5663 if (first_job != NULL)
5664 {
5665 first_job->jv_prev = job;
5666 job->jv_next = first_job;
5667 }
5668 first_job = job;
5669 }
5670 return job;
5671}
5672
5673 void
5674job_set_options(job_T *job, jobopt_T *opt)
5675{
5676 if (opt->jo_set & JO_STOPONEXIT)
5677 {
5678 vim_free(job->jv_stoponexit);
5679 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5680 job->jv_stoponexit = NULL;
5681 else
5682 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5683 }
5684 if (opt->jo_set & JO_EXIT_CB)
5685 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005686 free_callback(&job->jv_exit_cb);
5687 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005688 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005689 job->jv_exit_cb.cb_name = NULL;
5690 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005691 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005692 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005693 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005694 }
5695}
5696
5697/*
5698 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5699 */
5700 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005701job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005702{
5703 job_T *job;
5704
5705 for (job = first_job; job != NULL; job = job->jv_next)
5706 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005707 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005708}
5709
5710/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005711 * Return TRUE when there is any job that has an exit callback and might exit,
5712 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005713 */
5714 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005715has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005716{
5717 job_T *job;
5718
5719 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005720 /* Only should check if the channel has been closed, if the channel is
5721 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005722 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5723 || (job->jv_status == JOB_FINISHED
5724 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005725 return TRUE;
5726 return FALSE;
5727}
5728
Bram Moolenaar97792de2016-10-15 18:36:49 +02005729#define MAX_CHECK_ENDED 8
5730
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005731/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005732 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005733 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005734 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005735 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005736job_check_ended(void)
5737{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005738 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005739 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005740
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005741 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005742 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005743 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005744
Bram Moolenaar97792de2016-10-15 18:36:49 +02005745 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005746 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005747 // NOTE: mch_detect_ended_job() must only return a job of which the
5748 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005749 job_T *job = mch_detect_ended_job(first_job);
5750
5751 if (job == NULL)
5752 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005753 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005754 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005755 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005756
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005757 // Actually free jobs that were cleaned up.
5758 free_jobs_to_free_later();
5759
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005760 if (channel_need_redraw)
5761 {
5762 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005763 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005764 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005765 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005766}
5767
5768/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005769 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005770 * "argv_arg" is only for Unix.
5771 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005772 * The returned job has a refcount of one.
5773 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005774 */
5775 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005776job_start(
5777 typval_T *argvars,
5778 char **argv_arg,
5779 jobopt_T *opt_arg,
5780 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005781{
5782 job_T *job;
5783 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005784 char **argv = NULL;
5785 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005786#if defined(UNIX)
5787# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005788 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005789#else
5790 garray_T ga;
5791#endif
5792 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005793 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005794
5795 job = job_alloc();
5796 if (job == NULL)
5797 return NULL;
5798
5799 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005800#ifndef USE_ARGV
5801 ga_init2(&ga, (int)sizeof(char*), 20);
5802#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005803
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005804 if (opt_arg != NULL)
5805 opt = *opt_arg;
5806 else
5807 {
5808 /* Default mode is NL. */
5809 clear_job_options(&opt);
5810 opt.jo_mode = MODE_NL;
5811 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005812 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5813 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5814 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005815 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005816 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005817
5818 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005819 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005820 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5821 && opt.jo_io[part] == JIO_FILE
5822 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5823 || *opt.jo_io_name[part] == NUL))
5824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005825 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005826 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005827 }
5828
5829 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5830 {
5831 buf_T *buf = NULL;
5832
5833 /* check that we can find the buffer before starting the job */
5834 if (opt.jo_set & JO_IN_BUF)
5835 {
5836 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5837 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005838 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005839 }
5840 else if (!(opt.jo_set & JO_IN_NAME))
5841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005842 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005843 }
5844 else
5845 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5846 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005847 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005848 if (buf->b_ml.ml_mfp == NULL)
5849 {
5850 char_u numbuf[NUMBUFLEN];
5851 char_u *s;
5852
5853 if (opt.jo_set & JO_IN_BUF)
5854 {
5855 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5856 s = numbuf;
5857 }
5858 else
5859 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005860 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005861 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005862 }
5863 job->jv_in_buf = buf;
5864 }
5865
5866 job_set_options(job, &opt);
5867
Bram Moolenaar13568252018-03-16 20:46:58 +01005868#ifdef USE_ARGV
5869 if (argv_arg != NULL)
5870 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005871 /* Make a copy of argv_arg for job->jv_argv. */
5872 for (i = 0; argv_arg[i] != NULL; i++)
5873 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005874 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005875 if (argv == NULL)
5876 goto theend;
5877 for (i = 0; i < argc; i++)
5878 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5879 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005880 }
5881 else
5882#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005883 if (argvars[0].v_type == VAR_STRING)
5884 {
5885 /* Command is a string. */
5886 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005887 if (cmd == NULL || *cmd == NUL)
5888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005889 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005890 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005891 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005892
5893 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005894 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005895 }
5896 else if (argvars[0].v_type != VAR_LIST
5897 || argvars[0].vval.v_list == NULL
5898 || argvars[0].vval.v_list->lv_len < 1)
5899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005900 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005901 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005902 }
5903 else
5904 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005905 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005906
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005907 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005908 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005909#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005910 if (win32_build_cmd(l, &ga) == FAIL)
5911 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005912 cmd = ga.ga_data;
5913#endif
5914 }
5915
Bram Moolenaar20608922018-04-21 22:30:08 +02005916 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005917 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005918
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005919#ifdef USE_ARGV
5920 if (ch_log_active())
5921 {
5922 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005923
5924 ga_init2(&ga, (int)sizeof(char), 200);
5925 for (i = 0; i < argc; ++i)
5926 {
5927 if (i > 0)
5928 ga_concat(&ga, (char_u *)" ");
5929 ga_concat(&ga, (char_u *)argv[i]);
5930 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005931 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005932 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005933 ga_clear(&ga);
5934 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005935 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005936#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005937 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005938 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005939#endif
5940
5941 /* If the channel is reading from a buffer, write lines now. */
5942 if (job->jv_channel != NULL)
5943 channel_write_in(job->jv_channel);
5944
5945theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005946#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005947 vim_free(ga.ga_data);
5948#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005949 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005950 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005951 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005952 return job;
5953}
5954
5955/*
5956 * Get the status of "job" and invoke the exit callback when needed.
5957 * The returned string is not allocated.
5958 */
5959 char *
5960job_status(job_T *job)
5961{
5962 char *result;
5963
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005964 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005965 /* No need to check, dead is dead. */
5966 result = "dead";
5967 else if (job->jv_status == JOB_FAILED)
5968 result = "fail";
5969 else
5970 {
5971 result = mch_job_status(job);
5972 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005973 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005974 }
5975 return result;
5976}
5977
Bram Moolenaar8950a562016-03-12 15:22:55 +01005978/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005979 * Send a signal to "job". Implements job_stop().
5980 * When "type" is not NULL use this for the type.
5981 * Otherwise use argvars[1] for the type.
5982 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005983 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005984job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005985{
5986 char_u *arg;
5987
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005988 if (type != NULL)
5989 arg = (char_u *)type;
5990 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005991 arg = (char_u *)"";
5992 else
5993 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005994 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005995 if (arg == NULL)
5996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005997 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005998 return 0;
5999 }
6000 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006001 if (job->jv_status == JOB_FAILED)
6002 {
6003 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6004 return 0;
6005 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006006 if (job->jv_status == JOB_ENDED)
6007 {
6008 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6009 return 0;
6010 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006011 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006012 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006013 return 0;
6014
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006015 /* Assume that only "kill" will kill the job. */
6016 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006017 job->jv_channel->ch_job_killed = TRUE;
6018
6019 /* We don't try freeing the job, obviously the caller still has a
6020 * reference to it. */
6021 return 1;
6022}
6023
Bram Moolenaarf2732452018-06-03 14:47:35 +02006024 void
6025invoke_prompt_callback(void)
6026{
6027 typval_T rettv;
6028 int dummy;
6029 typval_T argv[2];
6030 char_u *text;
6031 char_u *prompt;
6032 linenr_T lnum = curbuf->b_ml.ml_line_count;
6033
6034 // Add a new line for the prompt before invoking the callback, so that
6035 // text can always be inserted above the last line.
6036 ml_append(lnum, (char_u *)"", 0, FALSE);
6037 curwin->w_cursor.lnum = lnum + 1;
6038 curwin->w_cursor.col = 0;
6039
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006040 if (curbuf->b_prompt_callback.cb_name == NULL
6041 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006042 return;
6043 text = ml_get(lnum);
6044 prompt = prompt_text();
6045 if (STRLEN(text) >= STRLEN(prompt))
6046 text += STRLEN(prompt);
6047 argv[0].v_type = VAR_STRING;
6048 argv[0].vval.v_string = vim_strsave(text);
6049 argv[1].v_type = VAR_UNKNOWN;
6050
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006051 call_callback(&curbuf->b_prompt_callback, -1,
6052 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006053 clear_tv(&argv[0]);
6054 clear_tv(&rettv);
6055}
6056
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006057/*
6058 * Return TRUE when the interrupt callback was invoked.
6059 */
6060 int
6061invoke_prompt_interrupt(void)
6062{
6063 typval_T rettv;
6064 int dummy;
6065 typval_T argv[1];
6066
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006067 if (curbuf->b_prompt_interrupt.cb_name == NULL
6068 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006069 return FALSE;
6070 argv[0].v_type = VAR_UNKNOWN;
6071
6072 got_int = FALSE; // don't skip executing commands
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006073 call_callback(&curbuf->b_prompt_interrupt, -1,
6074 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006075 clear_tv(&rettv);
6076 return TRUE;
6077}
6078
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006079/*
6080 * "prompt_setcallback({buffer}, {callback})" function
6081 */
6082 void
6083f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6084{
6085 buf_T *buf;
6086 callback_T callback;
6087
6088 if (check_secure())
6089 return;
6090 buf = tv_get_buf(&argvars[0], FALSE);
6091 if (buf == NULL)
6092 return;
6093
6094 callback = get_callback(&argvars[1]);
6095 if (callback.cb_name == NULL)
6096 return;
6097
6098 free_callback(&buf->b_prompt_callback);
6099 set_callback(&buf->b_prompt_callback, &callback);
6100}
6101
6102/*
6103 * "prompt_setinterrupt({buffer}, {callback})" function
6104 */
6105 void
6106f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6107{
6108 buf_T *buf;
6109 callback_T callback;
6110
6111 if (check_secure())
6112 return;
6113 buf = tv_get_buf(&argvars[0], FALSE);
6114 if (buf == NULL)
6115 return;
6116
6117 callback = get_callback(&argvars[1]);
6118 if (callback.cb_name == NULL)
6119 return;
6120
6121 free_callback(&buf->b_prompt_interrupt);
6122 set_callback(&buf->b_prompt_interrupt, &callback);
6123}
6124
6125/*
6126 * "prompt_setprompt({buffer}, {text})" function
6127 */
6128 void
6129f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6130{
6131 buf_T *buf;
6132 char_u *text;
6133
6134 if (check_secure())
6135 return;
6136 buf = tv_get_buf(&argvars[0], FALSE);
6137 if (buf == NULL)
6138 return;
6139
6140 text = tv_get_string(&argvars[1]);
6141 vim_free(buf->b_prompt_text);
6142 buf->b_prompt_text = vim_strsave(text);
6143}
6144
6145/*
6146 * "ch_canread()" function
6147 */
6148 void
6149f_ch_canread(typval_T *argvars, typval_T *rettv)
6150{
6151 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6152
6153 rettv->vval.v_number = 0;
6154 if (channel != NULL)
6155 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6156 || channel_has_readahead(channel, PART_OUT)
6157 || channel_has_readahead(channel, PART_ERR);
6158}
6159
6160/*
6161 * "ch_close()" function
6162 */
6163 void
6164f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6165{
6166 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6167
6168 if (channel != NULL)
6169 {
6170 channel_close(channel, FALSE);
6171 channel_clear(channel);
6172 }
6173}
6174
6175/*
6176 * "ch_close()" function
6177 */
6178 void
6179f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6180{
6181 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6182
6183 if (channel != NULL)
6184 channel_close_in(channel);
6185}
6186
6187/*
6188 * "ch_getbufnr()" function
6189 */
6190 void
6191f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6192{
6193 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6194
6195 rettv->vval.v_number = -1;
6196 if (channel != NULL)
6197 {
6198 char_u *what = tv_get_string(&argvars[1]);
6199 int part;
6200
6201 if (STRCMP(what, "err") == 0)
6202 part = PART_ERR;
6203 else if (STRCMP(what, "out") == 0)
6204 part = PART_OUT;
6205 else if (STRCMP(what, "in") == 0)
6206 part = PART_IN;
6207 else
6208 part = PART_SOCK;
6209 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6210 rettv->vval.v_number =
6211 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6212 }
6213}
6214
6215/*
6216 * "ch_getjob()" function
6217 */
6218 void
6219f_ch_getjob(typval_T *argvars, typval_T *rettv)
6220{
6221 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6222
6223 if (channel != NULL)
6224 {
6225 rettv->v_type = VAR_JOB;
6226 rettv->vval.v_job = channel->ch_job;
6227 if (channel->ch_job != NULL)
6228 ++channel->ch_job->jv_refcount;
6229 }
6230}
6231
6232/*
6233 * "ch_info()" function
6234 */
6235 void
6236f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6237{
6238 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6239
6240 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6241 channel_info(channel, rettv->vval.v_dict);
6242}
6243
6244/*
6245 * "ch_log()" function
6246 */
6247 void
6248f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6249{
6250 char_u *msg = tv_get_string(&argvars[0]);
6251 channel_T *channel = NULL;
6252
6253 if (argvars[1].v_type != VAR_UNKNOWN)
6254 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6255
6256 ch_log(channel, "%s", msg);
6257}
6258
6259/*
6260 * "ch_logfile()" function
6261 */
6262 void
6263f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6264{
6265 char_u *fname;
6266 char_u *opt = (char_u *)"";
6267 char_u buf[NUMBUFLEN];
6268
6269 /* Don't open a file in restricted mode. */
6270 if (check_restricted() || check_secure())
6271 return;
6272 fname = tv_get_string(&argvars[0]);
6273 if (argvars[1].v_type == VAR_STRING)
6274 opt = tv_get_string_buf(&argvars[1], buf);
6275 ch_logfile(fname, opt);
6276}
6277
6278/*
6279 * "ch_open()" function
6280 */
6281 void
6282f_ch_open(typval_T *argvars, typval_T *rettv)
6283{
6284 rettv->v_type = VAR_CHANNEL;
6285 if (check_restricted() || check_secure())
6286 return;
6287 rettv->vval.v_channel = channel_open_func(argvars);
6288}
6289
6290/*
6291 * "ch_read()" function
6292 */
6293 void
6294f_ch_read(typval_T *argvars, typval_T *rettv)
6295{
6296 common_channel_read(argvars, rettv, FALSE, FALSE);
6297}
6298
6299/*
6300 * "ch_readblob()" function
6301 */
6302 void
6303f_ch_readblob(typval_T *argvars, typval_T *rettv)
6304{
6305 common_channel_read(argvars, rettv, TRUE, TRUE);
6306}
6307
6308/*
6309 * "ch_readraw()" function
6310 */
6311 void
6312f_ch_readraw(typval_T *argvars, typval_T *rettv)
6313{
6314 common_channel_read(argvars, rettv, TRUE, FALSE);
6315}
6316
6317/*
6318 * "ch_evalexpr()" function
6319 */
6320 void
6321f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6322{
6323 ch_expr_common(argvars, rettv, TRUE);
6324}
6325
6326/*
6327 * "ch_sendexpr()" function
6328 */
6329 void
6330f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6331{
6332 ch_expr_common(argvars, rettv, FALSE);
6333}
6334
6335/*
6336 * "ch_evalraw()" function
6337 */
6338 void
6339f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6340{
6341 ch_raw_common(argvars, rettv, TRUE);
6342}
6343
6344/*
6345 * "ch_sendraw()" function
6346 */
6347 void
6348f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6349{
6350 ch_raw_common(argvars, rettv, FALSE);
6351}
6352
6353/*
6354 * "ch_setoptions()" function
6355 */
6356 void
6357f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6358{
6359 channel_T *channel;
6360 jobopt_T opt;
6361
6362 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6363 if (channel == NULL)
6364 return;
6365 clear_job_options(&opt);
6366 if (get_job_options(&argvars[1], &opt,
6367 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6368 channel_set_options(channel, &opt);
6369 free_job_options(&opt);
6370}
6371
6372/*
6373 * "ch_status()" function
6374 */
6375 void
6376f_ch_status(typval_T *argvars, typval_T *rettv)
6377{
6378 channel_T *channel;
6379 jobopt_T opt;
6380 int part = -1;
6381
6382 /* return an empty string by default */
6383 rettv->v_type = VAR_STRING;
6384 rettv->vval.v_string = NULL;
6385
6386 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6387
6388 if (argvars[1].v_type != VAR_UNKNOWN)
6389 {
6390 clear_job_options(&opt);
6391 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6392 && (opt.jo_set & JO_PART))
6393 part = opt.jo_part;
6394 }
6395
6396 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6397}
6398
6399/*
6400 * Get the job from the argument.
6401 * Returns NULL if the job is invalid.
6402 */
6403 static job_T *
6404get_job_arg(typval_T *tv)
6405{
6406 job_T *job;
6407
6408 if (tv->v_type != VAR_JOB)
6409 {
6410 semsg(_(e_invarg2), tv_get_string(tv));
6411 return NULL;
6412 }
6413 job = tv->vval.v_job;
6414
6415 if (job == NULL)
6416 emsg(_("E916: not a valid job"));
6417 return job;
6418}
6419
6420/*
6421 * "job_getchannel()" function
6422 */
6423 void
6424f_job_getchannel(typval_T *argvars, typval_T *rettv)
6425{
6426 job_T *job = get_job_arg(&argvars[0]);
6427
6428 if (job != NULL)
6429 {
6430 rettv->v_type = VAR_CHANNEL;
6431 rettv->vval.v_channel = job->jv_channel;
6432 if (job->jv_channel != NULL)
6433 ++job->jv_channel->ch_refcount;
6434 }
6435}
6436
6437/*
6438 * Implementation of job_info().
6439 */
6440 static void
6441job_info(job_T *job, dict_T *dict)
6442{
6443 dictitem_T *item;
6444 varnumber_T nr;
6445 list_T *l;
6446 int i;
6447
6448 dict_add_string(dict, "status", (char_u *)job_status(job));
6449
6450 item = dictitem_alloc((char_u *)"channel");
6451 if (item == NULL)
6452 return;
6453 item->di_tv.v_type = VAR_CHANNEL;
6454 item->di_tv.vval.v_channel = job->jv_channel;
6455 if (job->jv_channel != NULL)
6456 ++job->jv_channel->ch_refcount;
6457 if (dict_add(dict, item) == FAIL)
6458 dictitem_free(item);
6459
6460#ifdef UNIX
6461 nr = job->jv_pid;
6462#else
6463 nr = job->jv_proc_info.dwProcessId;
6464#endif
6465 dict_add_number(dict, "process", nr);
6466 dict_add_string(dict, "tty_in", job->jv_tty_in);
6467 dict_add_string(dict, "tty_out", job->jv_tty_out);
6468
6469 dict_add_number(dict, "exitval", job->jv_exitval);
6470 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6471 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6472#ifdef UNIX
6473 dict_add_string(dict, "termsig", job->jv_termsig);
6474#endif
6475#ifdef MSWIN
6476 dict_add_string(dict, "tty_type", job->jv_tty_type);
6477#endif
6478
6479 l = list_alloc();
6480 if (l != NULL)
6481 {
6482 dict_add_list(dict, "cmd", l);
6483 if (job->jv_argv != NULL)
6484 for (i = 0; job->jv_argv[i] != NULL; i++)
6485 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6486 }
6487}
6488
6489/*
6490 * Implementation of job_info() to return info for all jobs.
6491 */
6492 static void
6493job_info_all(list_T *l)
6494{
6495 job_T *job;
6496 typval_T tv;
6497
6498 for (job = first_job; job != NULL; job = job->jv_next)
6499 {
6500 tv.v_type = VAR_JOB;
6501 tv.vval.v_job = job;
6502
6503 if (list_append_tv(l, &tv) != OK)
6504 return;
6505 }
6506}
6507
6508/*
6509 * "job_info()" function
6510 */
6511 void
6512f_job_info(typval_T *argvars, typval_T *rettv)
6513{
6514 if (argvars[0].v_type != VAR_UNKNOWN)
6515 {
6516 job_T *job = get_job_arg(&argvars[0]);
6517
6518 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6519 job_info(job, rettv->vval.v_dict);
6520 }
6521 else if (rettv_list_alloc(rettv) == OK)
6522 job_info_all(rettv->vval.v_list);
6523}
6524
6525/*
6526 * "job_setoptions()" function
6527 */
6528 void
6529f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6530{
6531 job_T *job = get_job_arg(&argvars[0]);
6532 jobopt_T opt;
6533
6534 if (job == NULL)
6535 return;
6536 clear_job_options(&opt);
6537 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6538 job_set_options(job, &opt);
6539 free_job_options(&opt);
6540}
6541
6542/*
6543 * "job_start()" function
6544 */
6545 void
6546f_job_start(typval_T *argvars, typval_T *rettv)
6547{
6548 rettv->v_type = VAR_JOB;
6549 if (check_restricted() || check_secure())
6550 return;
6551 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6552}
6553
6554/*
6555 * "job_status()" function
6556 */
6557 void
6558f_job_status(typval_T *argvars, typval_T *rettv)
6559{
6560 job_T *job = get_job_arg(&argvars[0]);
6561
6562 if (job != NULL)
6563 {
6564 rettv->v_type = VAR_STRING;
6565 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6566 }
6567}
6568
6569/*
6570 * "job_stop()" function
6571 */
6572 void
6573f_job_stop(typval_T *argvars, typval_T *rettv)
6574{
6575 job_T *job = get_job_arg(&argvars[0]);
6576
6577 if (job != NULL)
6578 rettv->vval.v_number = job_stop(job, argvars, NULL);
6579}
6580
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006581#endif /* FEAT_JOB_CHANNEL */