blob: f6ffcd67d2a1b32066c439d2a5dbb484abc3634a [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 Moolenaard04a0202016-01-26 23:30:18 +010023#ifdef WIN32
24/* 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 Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 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 Moolenaarb091f302019-01-19 14:37:00 +010083 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010086
Bram Moolenaarb091f302019-01-19 14:37:00 +010087 // If the pipe overflows while the job does not read the data, WriteFile
88 // will block forever. This abandons the write.
89 memset(&ov, 0, sizeof(ov));
90 if (!WriteFile(h, buf, (DWORD)len, &nwrite, &ov))
91 {
92 DWORD err = GetLastError();
93
94 if (err != ERROR_IO_PENDING)
95 return -1;
96 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
97 return -1;
98 FlushFileBuffers(h);
99 }
Bram Moolenaard8070362016-02-15 21:56:54 +0100100 return (int)nwrite;
101}
102
103 static void
104fd_close(sock_T fd)
105{
106 HANDLE h = (HANDLE)fd;
107
108 CloseHandle(h);
109}
110#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100111
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100112/* Log file opened with ch_logfile(). */
113static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100114#ifdef FEAT_RELTIME
115static proftime_T log_start;
116#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100117
118 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100119ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100121 FILE *file = NULL;
122
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 if (log_fd != NULL)
124 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100125
126 if (*fname != NUL)
127 {
128 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
129 if (file == NULL)
130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100131 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100132 return;
133 }
134 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100136
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100138 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100139 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100140#ifdef FEAT_RELTIME
141 profile_start(&log_start);
142#endif
143 }
144}
145
146 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200147ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100148{
149 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100150}
151
152 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200153ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154{
155 if (log_fd != NULL)
156 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100157#ifdef FEAT_RELTIME
158 proftime_T log_now;
159
160 profile_start(&log_now);
161 profile_sub(&log_now, &log_start);
162 fprintf(log_fd, "%s ", profile_msg(&log_now));
163#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100164 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200165 {
166 if (part < PART_COUNT)
167 fprintf(log_fd, "%son %d(%s): ",
168 what, ch->ch_id, part_names[part]);
169 else
170 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
171 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100172 else
173 fprintf(log_fd, "%s: ", what);
174 }
175}
176
Bram Moolenaard0b65022016-03-06 21:50:33 +0100177static int did_log_msg = TRUE;
178
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200179#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100180 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200181ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182{
183 if (log_fd != NULL)
184 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200185 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200187 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200188 va_start(ap, fmt);
189 vfprintf(log_fd, fmt, ap);
190 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100191 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100193 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100194 }
195}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200196#endif
197
198 static void
199ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200200#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
201 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200202#endif
203 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100204
205 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200206ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100207{
208 if (log_fd != NULL)
209 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200210 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100211
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200212 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200213 va_start(ap, fmt);
214 vfprintf(log_fd, fmt, ap);
215 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100216 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100217 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100218 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 }
220}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100221
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100222#ifdef _WIN32
223# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100224# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100225
226 static char *
227strerror_win32(int eno)
228{
229 static LPVOID msgbuf = NULL;
230 char_u *ptr;
231
232 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200233 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100234 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200235 msgbuf = NULL;
236 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100237 FormatMessage(
238 FORMAT_MESSAGE_ALLOCATE_BUFFER |
239 FORMAT_MESSAGE_FROM_SYSTEM |
240 FORMAT_MESSAGE_IGNORE_INSERTS,
241 NULL,
242 eno,
243 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
244 (LPTSTR) &msgbuf,
245 0,
246 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200247 if (msgbuf != NULL)
248 /* chomp \r or \n */
249 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
250 switch (*ptr)
251 {
252 case '\r':
253 STRMOVE(ptr, ptr + 1);
254 ptr--;
255 break;
256 case '\n':
257 if (*(ptr + 1) == '\0')
258 *ptr = '\0';
259 else
260 *ptr = ' ';
261 break;
262 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100263 return msgbuf;
264}
265#endif
266
Bram Moolenaar77073442016-02-13 23:23:53 +0100267/*
268 * The list of all allocated channels.
269 */
270static channel_T *first_channel = NULL;
271static int next_ch_id = 0;
272
273/*
274 * Allocate a new channel. The refcount is set to 1.
275 * The channel isn't actually used until it is opened.
276 * Returns NULL if out of memory.
277 */
278 channel_T *
279add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100280{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200281 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100282 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100283
Bram Moolenaar77073442016-02-13 23:23:53 +0100284 if (channel == NULL)
285 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100286
Bram Moolenaar77073442016-02-13 23:23:53 +0100287 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100288 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100289
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200290 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100291 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100292 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100293#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100294 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100295#endif
296#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100297 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100298#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100299 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100300 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100301
Bram Moolenaar77073442016-02-13 23:23:53 +0100302 if (first_channel != NULL)
303 {
304 first_channel->ch_prev = channel;
305 channel->ch_next = first_channel;
306 }
307 first_channel = channel;
308
309 channel->ch_refcount = 1;
310 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100311}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100312
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200313 int
314has_any_channel(void)
315{
316 return first_channel != NULL;
317}
318
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100319/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100320 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100321 * Return TRUE if "channel" has a callback and the associated job wasn't
322 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100323 */
324 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100325channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100326{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100327 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100328 int has_out_msg;
329 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100330
331 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100332 if (channel->ch_job_killed && channel->ch_job == NULL)
333 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100334
335 /* If there is a close callback it may still need to be invoked. */
336 if (channel->ch_close_cb != NULL)
337 return TRUE;
338
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200339 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200341 return TRUE;
342
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 /* If there is no callback then nobody can get readahead. If the fd is
344 * closed and there is no readahead then the callback won't be called. */
345 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100346 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
347 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
349 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
350 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
351 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
352 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
353 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100354 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100355 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200356 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200357 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
358 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200359 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200360 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
361 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100362}
363
364/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200365 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
366 */
367 static int
368channel_can_close(channel_T *channel)
369{
370 return channel->ch_to_be_closed == 0;
371}
372
373/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200374 * Close a channel and free all its resources.
375 */
376 static void
377channel_free_contents(channel_T *channel)
378{
379 channel_close(channel, TRUE);
380 channel_clear(channel);
381 ch_log(channel, "Freeing channel");
382}
383
384 static void
385channel_free_channel(channel_T *channel)
386{
387 if (channel->ch_next != NULL)
388 channel->ch_next->ch_prev = channel->ch_prev;
389 if (channel->ch_prev == NULL)
390 first_channel = channel->ch_next;
391 else
392 channel->ch_prev->ch_next = channel->ch_next;
393 vim_free(channel);
394}
395
396 static void
397channel_free(channel_T *channel)
398{
399 if (!in_free_unref_items)
400 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200401 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200402 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200403 else
404 {
405 channel_free_contents(channel);
406 channel_free_channel(channel);
407 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200408 }
409}
410
411/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100413 * possible, there is no callback to be invoked or the associated job was
414 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100417 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100418channel_may_free(channel_T *channel)
419{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100420 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100421 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100422 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100423 return TRUE;
424 }
425 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100426}
427
428/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100429 * Decrement the reference count on "channel" and maybe free it when it goes
430 * down to zero. Don't free it if there is a pending action.
431 * Returns TRUE when the channel is no longer referenced.
432 */
433 int
434channel_unref(channel_T *channel)
435{
436 if (channel != NULL && --channel->ch_refcount <= 0)
437 return channel_may_free(channel);
438 return FALSE;
439}
440
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 int
442free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100443{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200444 int did_free = FALSE;
445 channel_T *ch;
446
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200447 /* This is invoked from the garbage collector, which only runs at a safe
448 * point. */
449 ++safe_to_invoke_callback;
450
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200451 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200452 if (!channel_still_useful(ch)
453 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200454 {
455 /* Free the channel and ordinary items it contains, but don't
456 * recurse into Lists, Dictionaries etc. */
457 channel_free_contents(ch);
458 did_free = TRUE;
459 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200460
461 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 return did_free;
463}
464
465 void
466free_unused_channels(int copyID, int mask)
467{
468 channel_T *ch;
469 channel_T *ch_next;
470
471 for (ch = first_channel; ch != NULL; ch = ch_next)
472 {
473 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200474 if (!channel_still_useful(ch)
475 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200476 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200477 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200478 channel_free_channel(ch);
479 }
480 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100481}
482
Bram Moolenaard04a0202016-01-26 23:30:18 +0100483#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100484
485#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
486 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100487channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100488{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100489 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200490 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100491
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100492 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100493 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200494 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100495 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200496 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100497}
498#endif
499
Bram Moolenaare0874f82016-01-24 20:36:41 +0100500/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100502 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100503#ifdef FEAT_GUI_X11
504 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200505messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200506 int *unused1 UNUSED,
507 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100508{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100509 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100510}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100514# if GTK_CHECK_VERSION(3,0,0)
515 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200516messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200517 GIOCondition unused2 UNUSED,
518 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100519{
520 channel_read_fd(GPOINTER_TO_INT(clientData));
521 return TRUE; /* Return FALSE instead in case the event source is to
522 * be removed after this function returns. */
523}
524# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200526messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527 gint unused1 UNUSED,
528 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100529{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100530 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100531}
Bram Moolenaar98921892016-02-23 17:14:37 +0100532# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100533#endif
534
535 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200536channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100537{
Bram Moolenaarde279892016-03-11 22:19:44 +0100538 if (!CH_HAS_GUI)
539 return;
540
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200541 /* gets stuck in handling events for a not connected channel */
542 if (channel->ch_keep_open)
543 return;
544
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100545# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200546 /* Tell notifier we are interested in being called when there is input on
547 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100548 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200549 {
550 ch_log(channel, "Registering part %s with fd %d",
551 part_names[part], channel->ch_part[part].ch_fd);
552
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100553 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100554 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100555 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100556 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200557 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100558 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200559 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560# else
561# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200562 /* Tell gdk we are interested in being called when there is input on the
563 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100564 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100565 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200566 ch_log(channel, "Registering part %s with fd %d",
567 part_names[part], channel->ch_part[part].ch_fd);
568# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100569 GIOChannel *chnnl = g_io_channel_unix_new(
570 (gint)channel->ch_part[part].ch_fd);
571
572 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
573 chnnl,
574 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200575 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100576 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
577
578 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100579# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100580 channel->ch_part[part].ch_inputHandler = gdk_input_add(
581 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100582 (GdkInputCondition)
583 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200584 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100585 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100586# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200587 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100588# endif
589# endif
590}
591
Bram Moolenaarde279892016-03-11 22:19:44 +0100592 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100593channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100594{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100595 if (channel->CH_SOCK_FD != INVALID_FD)
596 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200597 if (channel->CH_OUT_FD != INVALID_FD
598 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100599 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200600 if (channel->CH_ERR_FD != INVALID_FD
601 && channel->CH_ERR_FD != channel->CH_SOCK_FD
602 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100603 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100604}
605
606/*
607 * Register any of our file descriptors with the GUI event handling system.
608 * Called when the GUI has started.
609 */
610 void
611channel_gui_register_all(void)
612{
Bram Moolenaar77073442016-02-13 23:23:53 +0100613 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614
Bram Moolenaar77073442016-02-13 23:23:53 +0100615 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100616 channel_gui_register(channel);
617}
618
619 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200620channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100621{
622# ifdef FEAT_GUI_X11
623 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
624 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200625 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100626 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
627 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
628 }
629# else
630# ifdef FEAT_GUI_GTK
631 if (channel->ch_part[part].ch_inputHandler != 0)
632 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200633 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100634# if GTK_CHECK_VERSION(3,0,0)
635 g_source_remove(channel->ch_part[part].ch_inputHandler);
636# else
637 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
638# endif
639 channel->ch_part[part].ch_inputHandler = 0;
640 }
641# endif
642# endif
643}
644
645 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100646channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100647{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200648 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100649
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100650 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100651 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652}
653
654#endif
655
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100656static char *e_cannot_connect = N_("E902: Cannot connect to port");
657
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100659 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100660 * "waittime" is the time in msec to wait for the connection.
661 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100662 * Returns the channel for success.
663 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100665 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100666channel_open(
667 char *hostname,
668 int port_in,
669 int waittime,
670 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100672 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100675#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100676 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100677 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678#else
679 int port = port_in;
680#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100681 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100682 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100683
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100684#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100685 channel_init_winsock();
686#endif
687
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 channel = add_channel();
689 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100690 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100692 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693 }
694
695 /* Get the server internet address and put into addr structure */
696 /* fill in the socket address structure and connect to server */
697 vim_memset((char *)&server, 0, sizeof(server));
698 server.sin_family = AF_INET;
699 server.sin_port = htons(port);
700 if ((host = gethostbyname(hostname)) == NULL)
701 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100702 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200703 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100704 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100705 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100706 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100707 {
708 char *p;
709
Bram Moolenaar2e324952018-04-14 14:37:07 +0200710 /* When using host->h_addr_list[0] directly ubsan warns for it to not
711 * be aligned. First copy the pointer to avoid that. */
712 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100713 memcpy((char *)&server.sin_addr, p, host->h_length);
714 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100715
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100716 /* On Mac and Solaris a zero timeout almost never works. At least wait
717 * one millisecond. Let's do it for all systems, because we don't know why
718 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100719 if (waittime == 0)
720 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100721
722 /*
723 * For Unix we need to call connect() again after connect() failed.
724 * On Win32 one time is sufficient.
725 */
726 while (TRUE)
727 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100728 long elapsed_msec = 0;
729 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100732 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100733 sd = socket(AF_INET, SOCK_STREAM, 0);
734 if (sd == -1)
735 {
736 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200737 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100738 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100739 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100740 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100741
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100742 if (waittime >= 0)
743 {
744 /* Make connect() non-blocking. */
745 if (
746#ifdef _WIN32
747 ioctlsocket(sd, FIONBIO, &val) < 0
748#else
749 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
750#endif
751 )
752 {
753 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200754 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100755 "channel_open: Connect failed with errno %d", errno);
756 sock_close(sd);
757 channel_free(channel);
758 return NULL;
759 }
760 }
761
762 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200763 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100764 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
765
Bram Moolenaar045a2842016-03-08 22:33:07 +0100766 if (ret == 0)
767 /* The connection could be established. */
768 break;
769
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100771 if (waittime < 0 || (errno != EWOULDBLOCK
772 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100773#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100774 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100775#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100776 ))
777 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100779 "channel_open: Connect failed with errno %d", errno);
780 PERROR(_(e_cannot_connect));
781 sock_close(sd);
782 channel_free(channel);
783 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100784 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100785
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100786 /* Limit the waittime to 50 msec. If it doesn't work within this
787 * time we close the socket and try creating it again. */
788 waitnow = waittime > 50 ? 50 : waittime;
789
Bram Moolenaar045a2842016-03-08 22:33:07 +0100790 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100791 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100792#ifndef WIN32
793 if (errno != ECONNREFUSED)
794#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100795 {
796 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100797 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100798 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100799#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100800 int so_error = 0;
801 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100802 struct timeval start_tv;
803 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100804#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100805 FD_ZERO(&rfds);
806 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100807 FD_ZERO(&wfds);
808 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100809
Bram Moolenaar562ca712016-03-09 21:50:05 +0100810 tv.tv_sec = waitnow / 1000;
811 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100812#ifndef WIN32
813 gettimeofday(&start_tv, NULL);
814#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200815 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100816 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100817 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100818
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100819 if (ret < 0)
820 {
821 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200822 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100823 "channel_open: Connect failed with errno %d", errno);
824 PERROR(_(e_cannot_connect));
825 sock_close(sd);
826 channel_free(channel);
827 return NULL;
828 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100829
Bram Moolenaare081e212016-02-28 22:33:46 +0100830#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100831 /* On Win32: select() is expected to work and wait for up to
832 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100833 if (FD_ISSET(sd, &wfds))
834 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100835 elapsed_msec = waitnow;
836 if (waittime > 1 && elapsed_msec < waittime)
837 {
838 waittime -= elapsed_msec;
839 continue;
840 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100841#else
842 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100843 * After putting the socket in non-blocking mode, connect() will
844 * return EINPROGRESS, select() will not wait (as if writing is
845 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100846 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100847 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100849 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100850 {
851 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100852 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100853 if (ret < 0 || (so_error != 0
854 && so_error != EWOULDBLOCK
855 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100856# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100857 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100858# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100859 ))
860 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200861 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100862 "channel_open: Connect failed with errno %d",
863 so_error);
864 PERROR(_(e_cannot_connect));
865 sock_close(sd);
866 channel_free(channel);
867 return NULL;
868 }
869 }
870
Bram Moolenaar045a2842016-03-08 22:33:07 +0100871 if (FD_ISSET(sd, &wfds) && so_error == 0)
872 /* Did not detect an error, connection is established. */
873 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100874
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875 gettimeofday(&end_tv, NULL);
876 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
877 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100878#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100879 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100880
881#ifndef WIN32
882 if (waittime > 1 && elapsed_msec < waittime)
883 {
884 /* The port isn't ready but we also didn't get an error.
885 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100886 * yet. Select() may return early, wait until the remaining
887 * "waitnow" and try again. */
888 waitnow -= elapsed_msec;
889 waittime -= elapsed_msec;
890 if (waitnow > 0)
891 {
892 mch_delay((long)waitnow, TRUE);
893 ui_breakcheck();
894 waittime -= waitnow;
895 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100896 if (!got_int)
897 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100898 if (waittime <= 0)
899 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100900 waittime = 1;
901 continue;
902 }
903 /* we were interrupted, behave as if timed out */
904 }
905#endif
906
907 /* We timed out. */
908 ch_error(channel, "Connection timed out");
909 sock_close(sd);
910 channel_free(channel);
911 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100912 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100913
Bram Moolenaar045a2842016-03-08 22:33:07 +0100914 ch_log(channel, "Connection made");
915
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100916 if (waittime >= 0)
917 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100918#ifdef _WIN32
919 val = 0;
920 ioctlsocket(sd, FIONBIO, &val);
921#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100922 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100923#endif
924 }
925
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100926 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100927 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100928 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
929 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200930 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100931
932#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100933 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100934#endif
935
Bram Moolenaar77073442016-02-13 23:23:53 +0100936 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100937}
938
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939/*
940 * Implements ch_open().
941 */
942 channel_T *
943channel_open_func(typval_T *argvars)
944{
945 char_u *address;
946 char_u *p;
947 char *rest;
948 int port;
949 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200950 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100951
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100952 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100953 if (argvars[1].v_type != VAR_UNKNOWN
954 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100956 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957 return NULL;
958 }
959
960 /* parse address */
961 p = vim_strchr(address, ':');
962 if (p == NULL)
963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100964 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100965 return NULL;
966 }
967 *p++ = NUL;
968 port = strtol((char *)p, &rest, 10);
969 if (*address == NUL || port <= 0 || *rest != NUL)
970 {
971 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100973 return NULL;
974 }
975
976 /* parse options */
977 clear_job_options(&opt);
978 opt.jo_mode = MODE_JSON;
979 opt.jo_timeout = 2000;
980 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200981 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200982 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983 if (opt.jo_timeout < 0)
984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100985 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200986 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100987 }
988
989 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
990 if (channel != NULL)
991 {
992 opt.jo_set = JO_ALL;
993 channel_set_options(channel, &opt);
994 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200995theend:
996 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100997 return channel;
998}
999
Bram Moolenaarde279892016-03-11 22:19:44 +01001000 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001001ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001002{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001003 sock_T *fd = &channel->ch_part[part].ch_fd;
1004
Bram Moolenaarde279892016-03-11 22:19:44 +01001005 if (*fd != INVALID_FD)
1006 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001007 if (part == PART_SOCK)
1008 sock_close(*fd);
1009 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001010 {
1011 /* When using a pty the same FD is set on multiple parts, only
1012 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001013 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1014 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1015 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001016 {
1017#ifdef WIN32
1018 if (channel->ch_named_pipe)
1019 DisconnectNamedPipe((HANDLE)fd);
1020#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001021 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001022 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001023 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001024 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001025
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001026 /* channel is closed, may want to end the job if it was the last */
1027 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001028 }
1029}
1030
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001031 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001032channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001033{
Bram Moolenaarde279892016-03-11 22:19:44 +01001034 if (in != INVALID_FD)
1035 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001036 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001037 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001038# if defined(UNIX)
1039 /* Do not end the job when all output channels are closed, wait until
1040 * the job ended. */
1041 if (isatty(in))
1042 channel->ch_to_be_closed |= (1U << PART_IN);
1043# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001044 }
1045 if (out != INVALID_FD)
1046 {
1047# if defined(FEAT_GUI)
1048 channel_gui_unregister_one(channel, PART_OUT);
1049# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001050 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001051 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001052 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001053# if defined(FEAT_GUI)
1054 channel_gui_register_one(channel, PART_OUT);
1055# endif
1056 }
1057 if (err != INVALID_FD)
1058 {
1059# if defined(FEAT_GUI)
1060 channel_gui_unregister_one(channel, PART_ERR);
1061# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001062 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001063 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001064 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001065# if defined(FEAT_GUI)
1066 channel_gui_register_one(channel, PART_ERR);
1067# endif
1068 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001069}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001070
Bram Moolenaard6051b52016-02-28 15:49:03 +01001071/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001072 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001073 * This does not keep a refcount, when the job is freed ch_job is cleared.
1074 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001075 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001076channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001077{
Bram Moolenaar77073442016-02-13 23:23:53 +01001078 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001079
1080 channel_set_options(channel, options);
1081
1082 if (job->jv_in_buf != NULL)
1083 {
1084 chanpart_T *in_part = &channel->ch_part[PART_IN];
1085
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001086 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001087 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001088 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001089 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001090 {
1091 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1092 {
1093 /* Special mode: send last-but-one line when appending a line
1094 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001095 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001096 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001097 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001098 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001099 }
1100 else
1101 in_part->ch_buf_top = options->jo_in_top;
1102 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001103 else
1104 in_part->ch_buf_top = 1;
1105 if (options->jo_set & JO_IN_BOT)
1106 in_part->ch_buf_bot = options->jo_in_bot;
1107 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001109 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001110}
1111
1112/*
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001113 * Prepare buffer "buf" for writing channel output to.
1114 */
1115 static void
1116prepare_buffer(buf_T *buf)
1117{
1118 buf_T *save_curbuf = curbuf;
1119
1120 buf_copy_options(buf, BCO_ENTER);
1121 curbuf = buf;
1122#ifdef FEAT_QUICKFIX
1123 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1124 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1125#endif
1126 if (curbuf->b_ml.ml_mfp == NULL)
1127 ml_open(curbuf);
1128 curbuf = save_curbuf;
1129}
1130
1131/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001132 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001133 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001134 */
1135 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001136find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001137{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001138 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001139 buf_T *save_curbuf = curbuf;
1140
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001141 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001142 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001143 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001144 if (buf == NULL)
1145 buf = buflist_findname_exp(name);
1146 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001147 if (buf == NULL)
1148 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001149 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001150 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001151 if (buf == NULL)
1152 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001153 prepare_buffer(buf);
1154
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001155 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001156 if (msg)
1157 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001158 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001159 changed_bytes(1, 0);
1160 curbuf = save_curbuf;
1161 }
1162
1163 return buf;
1164}
1165
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001166 static void
1167set_callback(
1168 char_u **cbp,
1169 partial_T **pp,
1170 char_u *callback,
1171 partial_T *partial)
1172{
1173 free_callback(*cbp, *pp);
1174 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001175 {
1176 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001177 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001178 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001179 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001180 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001181 func_ref(*cbp);
1182 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001183 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001184 else
1185 *cbp = NULL;
1186 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001187 if (partial != NULL)
1188 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001189}
1190
Bram Moolenaar187db502016-02-27 14:44:26 +01001191/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001192 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001193 */
1194 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001195channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001196{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001197 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001198
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001199 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001200 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001201 channel->ch_part[part].ch_mode = opt->jo_mode;
1202 if (opt->jo_set & JO_IN_MODE)
1203 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1204 if (opt->jo_set & JO_OUT_MODE)
1205 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1206 if (opt->jo_set & JO_ERR_MODE)
1207 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001208 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001209
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001210 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001211 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001212 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1213 if (opt->jo_set & JO_OUT_TIMEOUT)
1214 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1215 if (opt->jo_set & JO_ERR_TIMEOUT)
1216 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001217 if (opt->jo_set & JO_BLOCK_WRITE)
1218 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001219
1220 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001221 set_callback(&channel->ch_callback, &channel->ch_partial,
1222 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001223 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001224 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1225 &channel->ch_part[PART_OUT].ch_partial,
1226 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001227 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001228 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1229 &channel->ch_part[PART_ERR].ch_partial,
1230 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001231 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001232 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1233 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001234 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001235
1236 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1237 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001238 buf_T *buf;
1239
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001240 /* writing output to a buffer. Default mode is NL. */
1241 if (!(opt->jo_set & JO_OUT_MODE))
1242 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001243 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 {
1245 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1246 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001247 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001249 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001250 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001251 int msg = TRUE;
1252
1253 if (opt->jo_set2 & JO2_OUT_MSG)
1254 msg = opt->jo_message[PART_OUT];
1255 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001256 }
1257 if (buf != NULL)
1258 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001259 if (opt->jo_set & JO_OUT_MODIFIABLE)
1260 channel->ch_part[PART_OUT].ch_nomodifiable =
1261 !opt->jo_modifiable[PART_OUT];
1262
1263 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001266 }
1267 else
1268 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001269 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001270 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001271 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001272 // if the buffer was deleted or unloaded resurrect it
1273 if (buf->b_ml.ml_mfp == NULL)
1274 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001275 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001276 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001277 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001278
1279 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1280 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1281 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1282 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001283 buf_T *buf;
1284
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001285 /* writing err to a buffer. Default mode is NL. */
1286 if (!(opt->jo_set & JO_ERR_MODE))
1287 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1288 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001289 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001290 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001291 {
1292 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1293 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001294 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001295 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001296 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001297 {
1298 int msg = TRUE;
1299
1300 if (opt->jo_set2 & JO2_ERR_MSG)
1301 msg = opt->jo_message[PART_ERR];
1302 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1303 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001304 if (buf != NULL)
1305 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001306 if (opt->jo_set & JO_ERR_MODIFIABLE)
1307 channel->ch_part[PART_ERR].ch_nomodifiable =
1308 !opt->jo_modifiable[PART_ERR];
1309 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001311 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001312 }
1313 else
1314 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001315 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001316 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001317 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001318 // if the buffer was deleted or unloaded resurrect it
1319 if (buf->b_ml.ml_mfp == NULL)
1320 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001321 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001322 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001323 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001324
1325 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1326 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1327 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001328}
1329
1330/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001331 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001332 */
1333 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001334channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001335 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001336 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001337 char_u *callback,
1338 partial_T *partial,
1339 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001340{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001341 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001342 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1343
1344 if (item != NULL)
1345 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001346 item->cq_partial = partial;
1347 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001348 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001349 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001350 item->cq_callback = callback;
1351 }
1352 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001353 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001354 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001355 func_ref(item->cq_callback);
1356 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001357 item->cq_seq_nr = id;
1358 item->cq_prev = head->cq_prev;
1359 head->cq_prev = item;
1360 item->cq_next = NULL;
1361 if (item->cq_prev == NULL)
1362 head->cq_next = item;
1363 else
1364 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001365 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001366}
1367
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001368 static void
1369write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1370{
1371 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001372 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001373 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001374 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375
Bram Moolenaar655da312016-05-28 22:22:34 +02001376 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001377 if ((p = alloc(len + 2)) == NULL)
1378 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001379 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001380
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001381 if (channel->ch_write_text_mode)
1382 p[len] = CAR;
1383 else
1384 {
1385 for (i = 0; i < len; ++i)
1386 if (p[i] == NL)
1387 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001388
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001389 p[len] = NL;
1390 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001392 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001393 vim_free(p);
1394}
1395
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001396/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001397 * Return TRUE if "channel" can be written to.
1398 * Returns FALSE if the input is closed or the write would block.
1399 */
1400 static int
1401can_write_buf_line(channel_T *channel)
1402{
1403 chanpart_T *in_part = &channel->ch_part[PART_IN];
1404
1405 if (in_part->ch_fd == INVALID_FD)
1406 return FALSE; /* pipe was closed */
1407
1408 /* for testing: block every other attempt to write */
1409 if (in_part->ch_block_write == 1)
1410 in_part->ch_block_write = -1;
1411 else if (in_part->ch_block_write == -1)
1412 in_part->ch_block_write = 1;
1413
1414 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1415#ifndef WIN32
1416 {
1417# if defined(HAVE_SELECT)
1418 struct timeval tval;
1419 fd_set wfds;
1420 int ret;
1421
1422 FD_ZERO(&wfds);
1423 FD_SET((int)in_part->ch_fd, &wfds);
1424 tval.tv_sec = 0;
1425 tval.tv_usec = 0;
1426 for (;;)
1427 {
1428 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1429# ifdef EINTR
1430 SOCK_ERRNO;
1431 if (ret == -1 && errno == EINTR)
1432 continue;
1433# endif
1434 if (ret <= 0 || in_part->ch_block_write == 1)
1435 {
1436 if (ret > 0)
1437 ch_log(channel, "FAKED Input not ready for writing");
1438 else
1439 ch_log(channel, "Input not ready for writing");
1440 return FALSE;
1441 }
1442 break;
1443 }
1444# else
1445 struct pollfd fds;
1446
1447 fds.fd = in_part->ch_fd;
1448 fds.events = POLLOUT;
1449 if (poll(&fds, 1, 0) <= 0)
1450 {
1451 ch_log(channel, "Input not ready for writing");
1452 return FALSE;
1453 }
1454 if (in_part->ch_block_write == 1)
1455 {
1456 ch_log(channel, "FAKED Input not ready for writing");
1457 return FALSE;
1458 }
1459# endif
1460 }
1461#endif
1462 return TRUE;
1463}
1464
1465/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001466 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001467 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001469channel_write_in(channel_T *channel)
1470{
1471 chanpart_T *in_part = &channel->ch_part[PART_IN];
1472 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001473 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001474 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001475
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001476 if (buf == NULL || in_part->ch_buf_append)
1477 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001478 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001479 {
1480 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001481 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001482 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001483 return;
1484 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001485
1486 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1487 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1488 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001489 if (!can_write_buf_line(channel))
1490 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001491 write_buf_line(buf, lnum, channel);
1492 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001493 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001494
1495 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001496 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001497 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001498 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001499
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001501 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001503#if defined(FEAT_TERMINAL)
1504 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001505 if (channel->ch_job != NULL)
1506 term_send_eof(channel);
1507#endif
1508
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001509 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001510 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001511 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001512
1513 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001514 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001515 }
1516 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001517 ch_log(channel, "Still %ld more lines to write",
1518 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001519}
1520
1521/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001522 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001523 */
1524 void
1525channel_buffer_free(buf_T *buf)
1526{
1527 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001528 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001529
1530 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001531 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001532 {
1533 chanpart_T *ch_part = &channel->ch_part[part];
1534
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001535 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001536 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001537 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001538 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001539 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001540 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001541 }
1542}
1543
1544/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001545 * Write any lines waiting to be written to "channel".
1546 */
1547 static void
1548channel_write_input(channel_T *channel)
1549{
1550 chanpart_T *in_part = &channel->ch_part[PART_IN];
1551
1552 if (in_part->ch_writeque.wq_next != NULL)
1553 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1554 else if (in_part->ch_bufref.br_buf != NULL)
1555 {
1556 if (in_part->ch_buf_append)
1557 channel_write_new_lines(in_part->ch_bufref.br_buf);
1558 else
1559 channel_write_in(channel);
1560 }
1561}
1562
1563/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001564 * Write any lines waiting to be written to a channel.
1565 */
1566 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001567channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001568{
1569 channel_T *channel;
1570
1571 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001572 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001573}
1574
1575/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001576 * Write appended lines above the last one in "buf" to the channel.
1577 */
1578 void
1579channel_write_new_lines(buf_T *buf)
1580{
1581 channel_T *channel;
1582 int found_one = FALSE;
1583
1584 /* There could be more than one channel for the buffer, loop over all of
1585 * them. */
1586 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1587 {
1588 chanpart_T *in_part = &channel->ch_part[PART_IN];
1589 linenr_T lnum;
1590 int written = 0;
1591
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001592 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001593 {
1594 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001595 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001596 found_one = TRUE;
1597 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1598 ++lnum)
1599 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001600 if (!can_write_buf_line(channel))
1601 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001602 write_buf_line(buf, lnum, channel);
1603 ++written;
1604 }
1605
1606 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001607 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001608 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001609 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001610 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001611 ch_log(channel, "Still %ld more lines to write",
1612 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001613
1614 in_part->ch_buf_bot = lnum;
1615 }
1616 }
1617 if (!found_one)
1618 buf->b_write_to_channel = FALSE;
1619}
1620
1621/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001622 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001623 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001624 */
1625 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001626invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1627 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001628{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001629 typval_T rettv;
1630 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001631
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001632 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001633 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001634
Bram Moolenaar77073442016-02-13 23:23:53 +01001635 argv[0].v_type = VAR_CHANNEL;
1636 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001637
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001638 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1639 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001640 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001641 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001642}
1643
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001644/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001645 * Return the first node from "channel"/"part" without removing it.
1646 * Returns NULL if there is nothing.
1647 */
1648 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001649channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001650{
1651 readq_T *head = &channel->ch_part[part].ch_head;
1652
1653 return head->rq_next;
1654}
1655
1656/*
1657 * Return a pointer to the first NL in "node".
1658 * Skips over NUL characters.
1659 * Returns NULL if there is no NL.
1660 */
1661 char_u *
1662channel_first_nl(readq_T *node)
1663{
1664 char_u *buffer = node->rq_buffer;
1665 long_u i;
1666
1667 for (i = 0; i < node->rq_buflen; ++i)
1668 if (buffer[i] == NL)
1669 return buffer + i;
1670 return NULL;
1671}
1672
1673/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001674 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675 * The caller must free it.
1676 * Returns NULL if there is nothing.
1677 */
1678 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001680{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001682 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 char_u *p;
1684
Bram Moolenaar77073442016-02-13 23:23:53 +01001685 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001687 if (outlen != NULL)
1688 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001690 p = node->rq_buffer;
1691 head->rq_next = node->rq_next;
1692 if (node->rq_next == NULL)
1693 head->rq_prev = NULL;
1694 else
1695 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 vim_free(node);
1697 return p;
1698}
1699
1700/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001701 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001702 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 */
1704 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001705channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001707 readq_T *head = &channel->ch_part[part].ch_head;
1708 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001709 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001710 char_u *res;
1711 char_u *p;
1712
1713 /* If there is only one buffer just get that one. */
1714 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 return channel_get(channel, part, outlen);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001716
1717 /* Concatenate everything into one buffer. */
1718 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001719 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001720 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001721 if (res == NULL)
1722 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001724 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001726 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001727 p += node->rq_buflen;
1728 }
1729 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001730
1731 /* Free all buffers */
1732 do
1733 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001735 vim_free(p);
1736 } while (p != NULL);
1737
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738 if (outlen != NULL)
1739 {
1740 *outlen += len;
1741 return res;
1742 }
1743
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001744 /* turn all NUL into NL */
1745 while (len > 0)
1746 {
1747 --len;
1748 if (res[len] == NUL)
1749 res[len] = NL;
1750 }
1751
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001752 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001753}
1754
1755/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001756 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001757 * Caller must check these bytes are available.
1758 */
1759 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001760channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001761{
1762 readq_T *head = &channel->ch_part[part].ch_head;
1763 readq_T *node = head->rq_next;
1764 char_u *buf = node->rq_buffer;
1765
1766 mch_memmove(buf, buf + len, node->rq_buflen - len);
1767 node->rq_buflen -= len;
1768}
1769
1770/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001771 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001772 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001773 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774 */
1775 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001776channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001777{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001779 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001780 readq_T *last_node;
1781 readq_T *n;
1782 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001783 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001784 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001785
Bram Moolenaar77073442016-02-13 23:23:53 +01001786 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001787 return FAIL;
1788
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001789 last_node = node->rq_next;
1790 len = node->rq_buflen + last_node->rq_buflen + 1;
1791 if (want_nl)
1792 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001793 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001794 {
1795 last_node = last_node->rq_next;
1796 len += last_node->rq_buflen;
1797 }
1798
1799 p = newbuf = alloc(len);
1800 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001802 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001803 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001804 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001805 node->rq_buffer = newbuf;
1806 for (n = node; n != last_node; )
1807 {
1808 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001809 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001810 p += n->rq_buflen;
1811 vim_free(n->rq_buffer);
1812 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001813 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001814
1815 /* dispose of the collapsed nodes and their buffers */
1816 for (n = node->rq_next; n != last_node; )
1817 {
1818 n = n->rq_next;
1819 vim_free(n->rq_prev);
1820 }
1821 node->rq_next = last_node->rq_next;
1822 if (last_node->rq_next == NULL)
1823 head->rq_prev = node;
1824 else
1825 last_node->rq_next->rq_prev = node;
1826 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001827 return OK;
1828}
1829
1830/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001831 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001832 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001833 * Returns OK or FAIL.
1834 */
1835 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001836channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001837 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001838{
1839 readq_T *node;
1840 readq_T *head = &channel->ch_part[part].ch_head;
1841 char_u *p;
1842 int i;
1843
1844 node = (readq_T *)alloc(sizeof(readq_T));
1845 if (node == NULL)
1846 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001847 /* A NUL is added at the end, because netbeans code expects that.
1848 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849 node->rq_buffer = alloc(len + 1);
1850 if (node->rq_buffer == NULL)
1851 {
1852 vim_free(node);
1853 return FAIL; /* out of memory */
1854 }
1855
1856 if (channel->ch_part[part].ch_mode == MODE_NL)
1857 {
1858 /* Drop any CR before a NL. */
1859 p = node->rq_buffer;
1860 for (i = 0; i < len; ++i)
1861 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1862 *p++ = buf[i];
1863 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001864 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001865 }
1866 else
1867 {
1868 mch_memmove(node->rq_buffer, buf, len);
1869 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001870 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001871 }
1872
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001873 if (prepend)
1874 {
1875 /* preend node to the head of the queue */
1876 node->rq_next = head->rq_next;
1877 node->rq_prev = NULL;
1878 if (head->rq_next == NULL)
1879 head->rq_prev = node;
1880 else
1881 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001882 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001884 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001885 {
1886 /* append node to the tail of the queue */
1887 node->rq_next = NULL;
1888 node->rq_prev = head->rq_prev;
1889 if (head->rq_prev == NULL)
1890 head->rq_next = node;
1891 else
1892 head->rq_prev->rq_next = node;
1893 head->rq_prev = node;
1894 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001895
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001896 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001897 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001898 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001899 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001900 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001901 fprintf(log_fd, "'\n");
1902 }
1903 return OK;
1904}
1905
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001906/*
1907 * Try to fill the buffer of "reader".
1908 * Returns FALSE when nothing was added.
1909 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 static int
1911channel_fill(js_read_T *reader)
1912{
1913 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001914 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001915 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001916 int keeplen;
1917 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001918 char_u *p;
1919
1920 if (next == NULL)
1921 return FALSE;
1922
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001923 keeplen = reader->js_end - reader->js_buf;
1924 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001925 {
1926 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001927 addlen = (int)STRLEN(next);
1928 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001929 if (p == NULL)
1930 {
1931 vim_free(next);
1932 return FALSE;
1933 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001934 mch_memmove(p, reader->js_buf, keeplen);
1935 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001936 vim_free(next);
1937 next = p;
1938 }
1939
1940 vim_free(reader->js_buf);
1941 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001942 return TRUE;
1943}
1944
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001945/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001946 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001947 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001948 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001950 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001951channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001952{
1953 js_read_T reader;
1954 typval_T listtv;
1955 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001956 chanpart_T *chanpart = &channel->ch_part[part];
1957 jsonq_T *head = &chanpart->ch_json_head;
1958 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001959 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001960
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001961 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001962 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001963
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001964 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001965 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001966 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001967 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001968 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969
1970 /* When a message is incomplete we wait for a short while for more to
1971 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001972 * or list will make us hang.
1973 * Do not generate error messages, they will be written in a channel log. */
1974 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001975 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001976 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001977 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001979 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001980 /* Only accept the response when it is a list with at least two
1981 * items. */
1982 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001983 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001984 if (listtv.v_type != VAR_LIST)
1985 ch_error(channel, "Did not receive a list, discarding");
1986 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001987 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001988 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001989 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001990 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001991 else
1992 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1994 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001995 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001996 else
1997 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001998 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001999 item->jq_value = alloc_tv();
2000 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002001 {
2002 vim_free(item);
2003 clear_tv(&listtv);
2004 }
2005 else
2006 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002007 *item->jq_value = listtv;
2008 item->jq_prev = head->jq_prev;
2009 head->jq_prev = item;
2010 item->jq_next = NULL;
2011 if (item->jq_prev == NULL)
2012 head->jq_next = item;
2013 else
2014 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002015 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 }
2017 }
2018 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002019
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002020 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002021 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002022 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002023 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002024 size_t buflen = STRLEN(reader.js_buf);
2025
2026 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002027 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002028 /* First time encountering incomplete message or after receiving
2029 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002030 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002031 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002032 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002033 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002034 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002035#ifdef WIN32
2036 chanpart->ch_deadline = GetTickCount() + 100L;
2037#else
2038 gettimeofday(&chanpart->ch_deadline, NULL);
2039 chanpart->ch_deadline.tv_usec += 100 * 1000;
2040 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2041 {
2042 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2043 ++chanpart->ch_deadline.tv_sec;
2044 }
2045#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002046 }
2047 else
2048 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002049 int timeout;
2050#ifdef WIN32
2051 timeout = GetTickCount() > chanpart->ch_deadline;
2052#else
2053 {
2054 struct timeval now_tv;
2055
2056 gettimeofday(&now_tv, NULL);
2057 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2058 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2059 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2060 }
2061#endif
2062 if (timeout)
2063 {
2064 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002065 chanpart->ch_wait_len = 0;
2066 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002067 }
2068 else
2069 {
2070 reader.js_used = 0;
2071 ch_log(channel, "still waiting on incomplete message");
2072 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002073 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002074 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002075
2076 if (status == FAIL)
2077 {
2078 ch_error(channel, "Decoding failed - discarding input");
2079 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002080 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002081 }
2082 else if (reader.js_buf[reader.js_used] != NUL)
2083 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002084 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002085 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002086 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2087 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002088 ret = status == MAYBE ? FALSE: TRUE;
2089 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002090 else
2091 ret = FALSE;
2092
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002093 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002094 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002095}
2096
2097/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002098 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002099 */
2100 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002101remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002102{
Bram Moolenaar77073442016-02-13 23:23:53 +01002103 if (node->cq_prev == NULL)
2104 head->cq_next = node->cq_next;
2105 else
2106 node->cq_prev->cq_next = node->cq_next;
2107 if (node->cq_next == NULL)
2108 head->cq_prev = node->cq_prev;
2109 else
2110 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002111}
2112
2113/*
2114 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002115 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002116 */
2117 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002118remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002119{
Bram Moolenaar77073442016-02-13 23:23:53 +01002120 if (node->jq_prev == NULL)
2121 head->jq_next = node->jq_next;
2122 else
2123 node->jq_prev->jq_next = node->jq_next;
2124 if (node->jq_next == NULL)
2125 head->jq_prev = node->jq_prev;
2126 else
2127 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 vim_free(node);
2129}
2130
2131/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002132 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002133 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002134 * When "id" is zero or negative jut get the first message. But not the one
2135 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002136 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002137 * Return OK when found and return the value in "rettv".
2138 * Return FAIL otherwise.
2139 */
2140 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002141channel_get_json(
2142 channel_T *channel,
2143 ch_part_T part,
2144 int id,
2145 int without_callback,
2146 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002147{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002148 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002149 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002150
Bram Moolenaar77073442016-02-13 23:23:53 +01002151 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002152 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002153 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002154 typval_T *tv = &l->lv_first->li_tv;
2155
Bram Moolenaar958dc692016-12-01 15:34:12 +01002156 if ((without_callback || !item->jq_no_callback)
2157 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002158 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002159 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002160 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002161 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002162 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002163 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002164 ch_log(channel, "Getting JSON message %ld",
2165 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002166 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002167 return OK;
2168 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002169 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002170 }
2171 return FAIL;
2172}
2173
Bram Moolenaar958dc692016-12-01 15:34:12 +01002174/*
2175 * Put back "rettv" into the JSON queue, there was no callback for it.
2176 * Takes over the values in "rettv".
2177 */
2178 static void
2179channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2180{
2181 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2182 jsonq_T *item = head->jq_next;
2183 jsonq_T *newitem;
2184
2185 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2186 /* last item was pushed back, append to the end */
2187 item = NULL;
2188 else while (item != NULL && item->jq_no_callback)
2189 /* append after the last item that was pushed back */
2190 item = item->jq_next;
2191
2192 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2193 if (newitem == NULL)
2194 clear_tv(rettv);
2195 else
2196 {
2197 newitem->jq_value = alloc_tv();
2198 if (newitem->jq_value == NULL)
2199 {
2200 vim_free(newitem);
2201 clear_tv(rettv);
2202 }
2203 else
2204 {
2205 newitem->jq_no_callback = FALSE;
2206 *newitem->jq_value = *rettv;
2207 if (item == NULL)
2208 {
2209 /* append to the end */
2210 newitem->jq_prev = head->jq_prev;
2211 head->jq_prev = newitem;
2212 newitem->jq_next = NULL;
2213 if (newitem->jq_prev == NULL)
2214 head->jq_next = newitem;
2215 else
2216 newitem->jq_prev->jq_next = newitem;
2217 }
2218 else
2219 {
2220 /* append after "item" */
2221 newitem->jq_prev = item;
2222 newitem->jq_next = item->jq_next;
2223 item->jq_next = newitem;
2224 if (newitem->jq_next == NULL)
2225 head->jq_prev = newitem;
2226 else
2227 newitem->jq_next->jq_prev = newitem;
2228 }
2229 }
2230 }
2231}
2232
Bram Moolenaarece61b02016-02-20 21:39:05 +01002233#define CH_JSON_MAX_ARGS 4
2234
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002235/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002236 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 * "argv[0]" is the command string.
2238 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002239 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002240 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002241channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002242{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 char_u *cmd = argv[0].vval.v_string;
2244 char_u *arg;
2245 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246
Bram Moolenaarece61b02016-02-20 21:39:05 +01002247 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002248 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002250 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002252 return;
2253 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002254 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002255 if (arg == NULL)
2256 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002257
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002258 if (STRCMP(cmd, "ex") == 0)
2259 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002260 int save_called_emsg = called_emsg;
2261
2262 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002263 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002264 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002265 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002266 --emsg_silent;
2267 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002268 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002269 (char *)get_vim_var_str(VV_ERRMSG));
2270 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002271 }
2272 else if (STRCMP(cmd, "normal") == 0)
2273 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002274 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002275
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002276 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002277 ea.arg = arg;
2278 ea.addr_count = 0;
2279 ea.forceit = TRUE; /* no mapping */
2280 ex_normal(&ea);
2281 }
2282 else if (STRCMP(cmd, "redraw") == 0)
2283 {
2284 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002285
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002286 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002287 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002288 ex_redraw(&ea);
2289 showruler(FALSE);
2290 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002291 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002292 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002293 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002294 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002295 int is_call = cmd[0] == 'c';
2296 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002297
Bram Moolenaarece61b02016-02-20 21:39:05 +01002298 if (argv[id_idx].v_type != VAR_UNKNOWN
2299 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002300 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002301 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002302 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002303 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002304 }
2305 else if (is_call && argv[2].v_type != VAR_LIST)
2306 {
2307 ch_error(channel, "third argument for call must be a list");
2308 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002310 }
2311 else
2312 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002313 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002314 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002315 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002316 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002317
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002318 /* Don't pollute the display with errors. */
2319 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002321 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002322 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002323 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002324 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002326 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002327 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002328 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2329 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002330 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331
2332 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002333 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002334 int id = argv[id_idx].vval.v_number;
2335
Bram Moolenaar55fab432016-02-07 16:53:13 +01002336 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002337 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002338 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002339 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002340 /* If evaluation failed or the result can't be encoded
2341 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002342 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002343 err_tv.v_type = VAR_STRING;
2344 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002345 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002346 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002347 if (json != NULL)
2348 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002349 channel_send(channel,
2350 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002351 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002352 vim_free(json);
2353 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002354 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002355 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002356 if (tv == &res_tv)
2357 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002358 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002359 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002360 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002361 }
2362 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002363 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002364 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002365 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002366 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002367}
2368
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002369/*
2370 * Invoke the callback at "cbhead".
2371 * Does not redraw but sets channel_need_redraw.
2372 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002373 static void
2374invoke_one_time_callback(
2375 channel_T *channel,
2376 cbq_T *cbhead,
2377 cbq_T *item,
2378 typval_T *argv)
2379{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002380 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002381 (char *)item->cq_callback);
2382 /* Remove the item from the list first, if the callback
2383 * invokes ch_close() the list will be cleared. */
2384 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002385 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002386 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002387 vim_free(item);
2388}
2389
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002390 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002391append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002392{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002393 bufref_T save_curbuf = {NULL, 0, 0};
2394 win_T *save_curwin = NULL;
2395 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002396 linenr_T lnum = buffer->b_ml.ml_line_count;
2397 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002398 chanpart_T *ch_part = &channel->ch_part[part];
2399 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002400 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002401
2402 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2403 {
2404 if (!ch_part->ch_nomod_error)
2405 {
2406 ch_error(channel, "Buffer is not modifiable, cannot append");
2407 ch_part->ch_nomod_error = TRUE;
2408 }
2409 return;
2410 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002411
2412 /* If the buffer is also used as input insert above the last
2413 * line. Don't write these lines. */
2414 if (save_write_to)
2415 {
2416 --lnum;
2417 buffer->b_write_to_channel = FALSE;
2418 }
2419
2420 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002421 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002422
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002423 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002424
2425 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2426 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2427
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002428 u_sync(TRUE);
2429 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002430 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002431
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002432 if (empty)
2433 {
2434 /* The buffer is empty, replace the first (dummy) line. */
2435 ml_replace(lnum, msg, TRUE);
2436 lnum = 0;
2437 }
2438 else
2439 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002440 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002441
2442 /* Restore curbuf/curwin/curtab */
2443 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2444
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002445 if (ch_part->ch_nomodifiable)
2446 buffer->b_p_ma = FALSE;
2447 else
2448 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002449
2450 if (buffer->b_nwindows > 0)
2451 {
2452 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002453
2454 FOR_ALL_WINDOWS(wp)
2455 {
2456 if (wp->w_buffer == buffer
2457 && (save_write_to
2458 ? wp->w_cursor.lnum == lnum + 1
2459 : (wp->w_cursor.lnum == lnum
2460 && wp->w_cursor.col == 0)))
2461 {
2462 ++wp->w_cursor.lnum;
2463 save_curwin = curwin;
2464 curwin = wp;
2465 curbuf = curwin->w_buffer;
2466 scroll_cursor_bot(0, FALSE);
2467 curwin = save_curwin;
2468 curbuf = curwin->w_buffer;
2469 }
2470 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002471 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002472 channel_need_redraw = TRUE;
2473 }
2474
2475 if (save_write_to)
2476 {
2477 channel_T *ch;
2478
2479 /* Find channels reading from this buffer and adjust their
2480 * next-to-read line number. */
2481 buffer->b_write_to_channel = TRUE;
2482 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2483 {
2484 chanpart_T *in_part = &ch->ch_part[PART_IN];
2485
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002486 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002487 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2488 }
2489 }
2490}
2491
Bram Moolenaar437905c2016-04-26 19:01:05 +02002492 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002493drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002494{
2495 char_u *msg;
2496
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002497 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002498 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002499 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002500 vim_free(msg);
2501 }
2502}
2503
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002504/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002505 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002506 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002507 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002508 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002509 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002510may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002511{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002512 char_u *msg = NULL;
2513 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002514 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002515 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002516 chanpart_T *ch_part = &channel->ch_part[part];
2517 ch_mode_T ch_mode = ch_part->ch_mode;
2518 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002519 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002520 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002521 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002522 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002523 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002524
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002525 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002526 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002527 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002528
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002529 /* Use a message-specific callback, part callback or channel callback */
2530 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2531 if (cbitem->cq_seq_nr == 0)
2532 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002533 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002534 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002535 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002536 partial = cbitem->cq_partial;
2537 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002538 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002539 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002540 callback = ch_part->ch_callback;
2541 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002542 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002543 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002544 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002545 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002546 partial = channel->ch_partial;
2547 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002548
Bram Moolenaarec68a992016-10-03 21:37:41 +02002549 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002550 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2551 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002552 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002553 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002554 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002555 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002556 buffer = NULL;
2557 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002558
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002559 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002560 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002561 listitem_T *item;
2562 int argc = 0;
2563
Bram Moolenaard7ece102016-02-02 23:23:02 +01002564 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002565 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002566 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002567 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002568 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002569 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002570 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002571 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002572
Bram Moolenaarece61b02016-02-20 21:39:05 +01002573 for (item = listtv->vval.v_list->lv_first;
2574 item != NULL && argc < CH_JSON_MAX_ARGS;
2575 item = item->li_next)
2576 argv[argc++] = item->li_tv;
2577 while (argc < CH_JSON_MAX_ARGS)
2578 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002579
Bram Moolenaarece61b02016-02-20 21:39:05 +01002580 if (argv[0].v_type == VAR_STRING)
2581 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002582 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002583 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002584 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002585 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002586 }
2587
Bram Moolenaarece61b02016-02-20 21:39:05 +01002588 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002589 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002590 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002591 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002592 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002593 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002594 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002595 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002596 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002597 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002598 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002599 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002600 return FALSE;
2601 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002602 else
2603 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002604 /* If there is no callback or buffer drop the message. */
2605 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002606 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002607 /* If there is a close callback it may use ch_read() to get the
2608 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002609 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002610 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002611 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002612 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002613
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002614 if (ch_mode == MODE_NL)
2615 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002616 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002617 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002618 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002619
2620 /* See if we have a message ending in NL in the first buffer. If
2621 * not try to concatenate the first and the second buffer. */
2622 while (TRUE)
2623 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002624 node = channel_peek(channel, part);
2625 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002626 if (nl != NULL)
2627 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002628 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002629 {
2630 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2631 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002632 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002633 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002634 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002635 buf = node->rq_buffer;
2636
Bram Moolenaarec68a992016-10-03 21:37:41 +02002637 if (nl == NULL)
2638 {
2639 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002640 char_u *new_buf;
2641
2642 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2643 if (new_buf == NULL)
2644 /* This might fail over and over again, should the message
2645 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002646 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002647 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002648 node->rq_buffer = buf;
2649 nl = buf + node->rq_buflen++;
2650 *nl = NUL;
2651 }
2652
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002653 /* Convert NUL to NL, the internal representation. */
2654 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2655 if (*p == NUL)
2656 *p = NL;
2657
2658 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002659 {
2660 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002661 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002662 *nl = NUL;
2663 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002664 else
2665 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002666 /* Copy the message into allocated memory (excluding the NL)
2667 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002668 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002669 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002670 }
2671 }
2672 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002673 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002674 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002675 * get everything we have.
2676 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002677 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002678 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002679
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002680 if (msg == NULL)
2681 return FALSE; /* out of memory (and avoids Coverity warning) */
2682
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002683 argv[1].v_type = VAR_STRING;
2684 argv[1].vval.v_string = msg;
2685 }
2686
Bram Moolenaara07fec92016-02-05 21:04:08 +01002687 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002688 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002689 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002690
Bram Moolenaar958dc692016-12-01 15:34:12 +01002691 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002692 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002693 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002694 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002695 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002696 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002697 break;
2698 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002699 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002700 {
2701 if (channel->ch_drop_never)
2702 {
2703 /* message must be read with ch_read() */
2704 channel_push_json(channel, part, listtv);
2705 listtv = NULL;
2706 }
2707 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002708 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002709 seq_nr);
2710 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002711 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002712 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002713 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002714 if (buffer != NULL)
2715 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002716 if (msg == NULL)
2717 /* JSON or JS mode: re-encode the message. */
2718 msg = json_encode(listtv, ch_mode);
2719 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002720 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002721#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002722 if (buffer->b_term != NULL)
2723 write_to_term(buffer, msg, channel);
2724 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002725#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002726 append_to_buffer(buffer, msg, channel, part);
2727 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002728 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002729
Bram Moolenaar187db502016-02-27 14:44:26 +01002730 if (callback != NULL)
2731 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002732 if (cbitem != NULL)
2733 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2734 else
2735 {
2736 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002737 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002738 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002739 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002740 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002741 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002742 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002743 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002744 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002745
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002746 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002747 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002748 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002749
2750 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002751}
2752
2753/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002754 * Return TRUE when channel "channel" is open for writing to.
2755 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002756 */
2757 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002758channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002759{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002760 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002761 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002762}
2763
2764/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002765 * Return TRUE when channel "channel" is open for reading or writing.
2766 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002767 */
2768 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002769channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002770{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002771 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002772 || channel->CH_IN_FD != INVALID_FD
2773 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002774 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002775}
2776
2777/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002778 * Return TRUE if "channel" has JSON or other typeahead.
2779 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002780 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002781channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002782{
2783 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2784
2785 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2786 {
2787 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2788 jsonq_T *item = head->jq_next;
2789
2790 return item != NULL;
2791 }
2792 return channel_peek(channel, part) != NULL;
2793}
2794
2795/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002796 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002797 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002798 */
2799 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002800channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002801{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002802 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002803 int has_readahead = FALSE;
2804
Bram Moolenaar77073442016-02-13 23:23:53 +01002805 if (channel == NULL)
2806 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002807 if (req_part == PART_OUT)
2808 {
2809 if (channel->CH_OUT_FD != INVALID_FD)
2810 return "open";
2811 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002812 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002813 }
2814 else if (req_part == PART_ERR)
2815 {
2816 if (channel->CH_ERR_FD != INVALID_FD)
2817 return "open";
2818 if (channel_has_readahead(channel, PART_ERR))
2819 has_readahead = TRUE;
2820 }
2821 else
2822 {
2823 if (channel_is_open(channel))
2824 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002825 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002826 if (channel_has_readahead(channel, part))
2827 {
2828 has_readahead = TRUE;
2829 break;
2830 }
2831 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002832
2833 if (has_readahead)
2834 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002835 return "closed";
2836}
2837
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002838 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002839channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002840{
2841 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002842 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002843 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002844 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002845 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002846
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002847 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002848 STRCAT(namebuf, "_");
2849 tail = STRLEN(namebuf);
2850
2851 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002852 if (chanpart->ch_fd != INVALID_FD)
2853 status = "open";
2854 else if (channel_has_readahead(channel, part))
2855 status = "buffered";
2856 else
2857 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002858 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002859
2860 STRCPY(namebuf + tail, "mode");
2861 switch (chanpart->ch_mode)
2862 {
2863 case MODE_NL: s = "NL"; break;
2864 case MODE_RAW: s = "RAW"; break;
2865 case MODE_JSON: s = "JSON"; break;
2866 case MODE_JS: s = "JS"; break;
2867 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002868 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002869
2870 STRCPY(namebuf + tail, "io");
2871 if (part == PART_SOCK)
2872 s = "socket";
2873 else switch (chanpart->ch_io)
2874 {
2875 case JIO_NULL: s = "null"; break;
2876 case JIO_PIPE: s = "pipe"; break;
2877 case JIO_FILE: s = "file"; break;
2878 case JIO_BUFFER: s = "buffer"; break;
2879 case JIO_OUT: s = "out"; break;
2880 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002881 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002882
2883 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002884 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002885}
2886
2887 void
2888channel_info(channel_T *channel, dict_T *dict)
2889{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002890 dict_add_number(dict, "id", channel->ch_id);
2891 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002892
2893 if (channel->ch_hostname != NULL)
2894 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002895 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2896 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002897 channel_part_info(channel, dict, "sock", PART_SOCK);
2898 }
2899 else
2900 {
2901 channel_part_info(channel, dict, "out", PART_OUT);
2902 channel_part_info(channel, dict, "err", PART_ERR);
2903 channel_part_info(channel, dict, "in", PART_IN);
2904 }
2905}
2906
Bram Moolenaar77073442016-02-13 23:23:53 +01002907/*
2908 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002909 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002910 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002911 */
2912 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002913channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002914{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002915 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002917#ifdef FEAT_GUI
2918 channel_gui_unregister(channel);
2919#endif
2920
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002921 ch_close_part(channel, PART_SOCK);
2922 ch_close_part(channel, PART_IN);
2923 ch_close_part(channel, PART_OUT);
2924 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002925
Bram Moolenaara9f02812017-08-05 17:40:38 +02002926 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002927 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002928 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002929
Bram Moolenaara9f02812017-08-05 17:40:38 +02002930 /* Invoke callbacks and flush buffers before the close callback. */
2931 if (channel->ch_close_cb != NULL)
2932 ch_log(channel,
2933 "Invoking callbacks and flushing buffers before closing");
2934 for (part = PART_SOCK; part < PART_IN; ++part)
2935 {
2936 if (channel->ch_close_cb != NULL
2937 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2938 {
2939 /* Increment the refcount to avoid the channel being freed
2940 * halfway. */
2941 ++channel->ch_refcount;
2942 if (channel->ch_close_cb == NULL)
2943 ch_log(channel, "flushing %s buffers before closing",
2944 part_names[part]);
2945 while (may_invoke_callback(channel, part))
2946 ;
2947 --channel->ch_refcount;
2948 }
2949 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002950
Bram Moolenaara9f02812017-08-05 17:40:38 +02002951 if (channel->ch_close_cb != NULL)
2952 {
2953 typval_T argv[1];
2954 typval_T rettv;
2955 int dummy;
2956
2957 /* Increment the refcount to avoid the channel being freed
2958 * halfway. */
2959 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002960 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002961 (char *)channel->ch_close_cb);
2962 argv[0].v_type = VAR_CHANNEL;
2963 argv[0].vval.v_channel = channel;
2964 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002965 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002966 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002967 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002968 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002969
Bram Moolenaara9f02812017-08-05 17:40:38 +02002970 /* the callback is only called once */
2971 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2972 channel->ch_close_cb = NULL;
2973 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002974
Bram Moolenaara9f02812017-08-05 17:40:38 +02002975 if (channel_need_redraw)
2976 {
2977 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002978 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002979 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002980
Bram Moolenaara9f02812017-08-05 17:40:38 +02002981 if (!channel->ch_drop_never)
2982 /* any remaining messages are useless now */
2983 for (part = PART_SOCK; part < PART_IN; ++part)
2984 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002985
2986 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002987 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002988 }
2989
2990 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002991
2992#ifdef FEAT_TERMINAL
2993 term_channel_closed(channel);
2994#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002995}
2996
Bram Moolenaard04a0202016-01-26 23:30:18 +01002997/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002998 * Close the "in" part channel "channel".
2999 */
3000 void
3001channel_close_in(channel_T *channel)
3002{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003003 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003004}
3005
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003006 static void
3007remove_from_writeque(writeq_T *wq, writeq_T *entry)
3008{
3009 ga_clear(&entry->wq_ga);
3010 wq->wq_next = entry->wq_next;
3011 if (wq->wq_next == NULL)
3012 wq->wq_prev = NULL;
3013 else
3014 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003015 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003016}
3017
Bram Moolenaar0874a832016-09-01 15:11:51 +02003018/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003019 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003020 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003021 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003022channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003023{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003024 chanpart_T *ch_part = &channel->ch_part[part];
3025 jsonq_T *json_head = &ch_part->ch_json_head;
3026 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003027
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003028 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003029 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003030
3031 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003032 {
3033 cbq_T *node = cb_head->cq_next;
3034
3035 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003036 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003037 vim_free(node);
3038 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003039
3040 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003041 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003042 free_tv(json_head->jq_next->jq_value);
3043 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003045
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003046 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3047 ch_part->ch_callback = NULL;
3048 ch_part->ch_partial = NULL;
3049
3050 while (ch_part->ch_writeque.wq_next != NULL)
3051 remove_from_writeque(&ch_part->ch_writeque,
3052 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003053}
3054
3055/*
3056 * Clear all the read buffers on "channel".
3057 */
3058 void
3059channel_clear(channel_T *channel)
3060{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003061 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003062 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003063 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003064 channel_clear_one(channel, PART_OUT);
3065 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003066 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003067 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003068 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003069 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003070 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003071 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003072 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003073}
3074
Bram Moolenaar77073442016-02-13 23:23:53 +01003075#if defined(EXITFREE) || defined(PROTO)
3076 void
3077channel_free_all(void)
3078{
3079 channel_T *channel;
3080
Bram Moolenaard6051b52016-02-28 15:49:03 +01003081 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003082 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3083 channel_clear(channel);
3084}
3085#endif
3086
3087
Bram Moolenaar715d2852016-04-30 17:06:31 +02003088/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003089#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090
3091/* Buffer size for reading incoming messages. */
3092#define MAXMSGSIZE 4096
3093
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003094#if defined(HAVE_SELECT)
3095/*
3096 * Add write fds where we are waiting for writing to be possible.
3097 */
3098 static int
3099channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3100{
3101 int maxfd = maxfd_arg;
3102 channel_T *ch;
3103
3104 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3105 {
3106 chanpart_T *in_part = &ch->ch_part[PART_IN];
3107
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003108 if (in_part->ch_fd != INVALID_FD
3109 && (in_part->ch_bufref.br_buf != NULL
3110 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003111 {
3112 FD_SET((int)in_part->ch_fd, wfds);
3113 if ((int)in_part->ch_fd >= maxfd)
3114 maxfd = (int)in_part->ch_fd + 1;
3115 }
3116 }
3117 return maxfd;
3118}
3119#else
3120/*
3121 * Add write fds where we are waiting for writing to be possible.
3122 */
3123 static int
3124channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3125{
3126 int nfd = nfd_in;
3127 channel_T *ch;
3128
3129 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3130 {
3131 chanpart_T *in_part = &ch->ch_part[PART_IN];
3132
Bram Moolenaar683b7962017-08-19 15:51:59 +02003133 if (in_part->ch_fd != INVALID_FD
3134 && (in_part->ch_bufref.br_buf != NULL
3135 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003136 {
3137 in_part->ch_poll_idx = nfd;
3138 fds[nfd].fd = in_part->ch_fd;
3139 fds[nfd].events = POLLOUT;
3140 ++nfd;
3141 }
3142 else
3143 in_part->ch_poll_idx = -1;
3144 }
3145 return nfd;
3146}
3147#endif
3148
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003149typedef enum {
3150 CW_READY,
3151 CW_NOT_READY,
3152 CW_ERROR
3153} channel_wait_result;
3154
Bram Moolenaard04a0202016-01-26 23:30:18 +01003155/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003156 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003157 * Return CW_READY when there is something to read.
3158 * Return CW_NOT_READY when there is nothing to read.
3159 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003160 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003161 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003162channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003163{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003164 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003165 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003166
Bram Moolenaard8070362016-02-15 21:56:54 +01003167# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003168 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003169 {
3170 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003171 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003172 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003173 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003174
3175 /* reading from a pipe, not a socket */
3176 while (TRUE)
3177 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003178 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3179
3180 if (r && nread > 0)
3181 return CW_READY;
3182 if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003183 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003184
3185 /* perhaps write some buffer lines */
3186 channel_write_any_lines();
3187
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003188 sleep_time = deadline - GetTickCount();
3189 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003190 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003191 /* Wait for a little while. Very short at first, up to 10 msec
3192 * after looping a few times. */
3193 if (sleep_time > delay)
3194 sleep_time = delay;
3195 Sleep(sleep_time);
3196 delay = delay * 2;
3197 if (delay > 10)
3198 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003199 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003200 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003201 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003202#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003203 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003204#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003205 struct timeval tval;
3206 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003207 fd_set wfds;
3208 int ret;
3209 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003210
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003211 tval.tv_sec = timeout / 1000;
3212 tval.tv_usec = (timeout % 1000) * 1000;
3213 for (;;)
3214 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003215 FD_ZERO(&rfds);
3216 FD_SET((int)fd, &rfds);
3217
3218 /* Write lines to a pipe when a pipe can be written to. Need to
3219 * set this every time, some buffers may be done. */
3220 maxfd = (int)fd + 1;
3221 FD_ZERO(&wfds);
3222 maxfd = channel_fill_wfds(maxfd, &wfds);
3223
3224 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003225# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003226 SOCK_ERRNO;
3227 if (ret == -1 && errno == EINTR)
3228 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003229# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003230 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003231 {
3232 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003233 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003234 channel_write_any_lines();
3235 continue;
3236 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003237 break;
3238 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003239#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003240 for (;;)
3241 {
3242 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3243 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003244
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003245 fds[0].fd = fd;
3246 fds[0].events = POLLIN;
3247 nfd = channel_fill_poll_write(nfd, fds);
3248 if (poll(fds, nfd, timeout) > 0)
3249 {
3250 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003251 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003252 channel_write_any_lines();
3253 continue;
3254 }
3255 break;
3256 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003257#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003258 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003259 return CW_NOT_READY;
3260}
3261
3262 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003263ch_close_part_on_error(
3264 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003265{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003266 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003267
3268 if (is_err)
3269 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003270 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003271 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003272 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003273
3274 /* Queue a "DETACH" netbeans message in the command queue in order to
3275 * terminate the netbeans session later. Do not end the session here
3276 * directly as we may be running in the context of a call to
3277 * netbeans_parse_messages():
3278 * netbeans_parse_messages
3279 * -> autocmd triggered while processing the netbeans cmd
3280 * -> ui_breakcheck
3281 * -> gui event loop or select loop
3282 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003283 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003284 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003285 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003286 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003287 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3288
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003289 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003290 * close the channel yet, there may be something to read on another part.
3291 * When stdout and stderr use the same FD we get the error only on one of
3292 * them, also close the other. */
3293 if (part == PART_OUT || part == PART_ERR)
3294 {
3295 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3296
3297 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3298 ch_close_part(channel, other);
3299 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003300 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003301
3302#ifdef FEAT_GUI
3303 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003304 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003305#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003306}
3307
3308 static void
3309channel_close_now(channel_T *channel)
3310{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003311 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003312 if (channel->ch_nb_close_cb != NULL)
3313 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003314 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003315}
3316
3317/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003318 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003319 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003320 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003321 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003322 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003323channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003324{
3325 static char_u *buf = NULL;
3326 int len = 0;
3327 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003328 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003329 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003330
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003331 fd = channel->ch_part[part].ch_fd;
3332 if (fd == INVALID_FD)
3333 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003334 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003335 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003336 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 }
3338 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003339
3340 /* Allocate a buffer to read into. */
3341 if (buf == NULL)
3342 {
3343 buf = alloc(MAXMSGSIZE);
3344 if (buf == NULL)
3345 return; /* out of memory! */
3346 }
3347
3348 /* Keep on reading for as long as there is something to read.
3349 * Use select() or poll() to avoid blocking on a message that is exactly
3350 * MAXMSGSIZE long. */
3351 for (;;)
3352 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003353 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003354 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003355 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003356 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003357 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003358 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003359 if (len <= 0)
3360 break; /* error or nothing more to read */
3361
3362 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003363 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003364 readlen += len;
3365 if (len < MAXMSGSIZE)
3366 break; /* did read everything that's available */
3367 }
3368
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003369 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003370 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003371 {
3372 if (!channel->ch_keep_open)
3373 ch_close_part_on_error(channel, part, (len < 0), func);
3374 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003375#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003376 else if (CH_HAS_GUI && gtk_main_level() > 0)
3377 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003378 gtk_main_quit();
3379#endif
3380}
3381
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003382/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003383 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003384 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003385 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003386 * Returns what was read in allocated memory.
3387 * Returns NULL in case of error or timeout.
3388 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003389 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003390channel_read_block(
3391 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003392{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003393 char_u *buf;
3394 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003395 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003396 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003397 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003398 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003399
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003400 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003401 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003402
3403 while (TRUE)
3404 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003405 node = channel_peek(channel, part);
3406 if (node != NULL)
3407 {
3408 if (mode == MODE_RAW || (mode == MODE_NL
3409 && channel_first_nl(node) != NULL))
3410 /* got a complete message */
3411 break;
3412 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3413 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003414 /* If not blocking or nothing more is coming then return what we
3415 * have. */
3416 if (raw || fd == INVALID_FD)
3417 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003418 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003419
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003420 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003421 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003422 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003423 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003424 {
3425 ch_log(channel, "Timed out");
3426 return NULL;
3427 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003428 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003429 }
3430
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003431 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003432 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003433 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003434 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003435 }
3436 else
3437 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003438 char_u *p;
3439
3440 buf = node->rq_buffer;
3441 nl = channel_first_nl(node);
3442
3443 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003444 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003445 if (*p == NUL)
3446 *p = NL;
3447
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003448 if (nl == NULL)
3449 {
3450 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003451 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003452 }
3453 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003454 {
3455 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003456 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003457 *nl = NUL;
3458 }
3459 else
3460 {
3461 /* Copy the message into allocated memory and remove it from the
3462 * buffer. */
3463 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003464 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003465 }
3466 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003467 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003468 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003469 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003470}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003471
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003472/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003473 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003474 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003475 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003476 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003477 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003478 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003479channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003480 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003481 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003482 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003483 int id,
3484 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003485{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003486 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003487 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003488 int timeout;
3489 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003490
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003491 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003492 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003493 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003494 for (;;)
3495 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003497
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003498 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003499 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003500 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003501 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003502 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003503 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003504
Bram Moolenaard7ece102016-02-02 23:23:02 +01003505 if (!more)
3506 {
3507 /* Handle any other messages in the queue. If done some more
3508 * messages may have arrived. */
3509 if (channel_parse_messages())
3510 continue;
3511
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003512 /* Wait for up to the timeout. If there was an incomplete message
3513 * use the deadline for that. */
3514 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003515 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003516 {
3517#ifdef WIN32
3518 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3519#else
3520 {
3521 struct timeval now_tv;
3522
3523 gettimeofday(&now_tv, NULL);
3524 timeout = (chanpart->ch_deadline.tv_sec
3525 - now_tv.tv_sec) * 1000
3526 + (chanpart->ch_deadline.tv_usec
3527 - now_tv.tv_usec) / 1000
3528 + 1;
3529 }
3530#endif
3531 if (timeout < 0)
3532 {
3533 /* Something went wrong, channel_parse_json() didn't
3534 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003535 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003536 timeout = timeout_arg;
3537 }
3538 else if (timeout > timeout_arg)
3539 timeout = timeout_arg;
3540 }
3541 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003542 if (fd == INVALID_FD
3543 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003544 {
3545 if (timeout == timeout_arg)
3546 {
3547 if (fd != INVALID_FD)
3548 ch_log(channel, "Timed out");
3549 break;
3550 }
3551 }
3552 else
3553 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003554 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003555 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003556 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003557 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003558}
3559
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003560/*
3561 * Common for ch_read() and ch_readraw().
3562 */
3563 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003564common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003565{
3566 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003567 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003568 jobopt_T opt;
3569 int mode;
3570 int timeout;
3571 int id = -1;
3572 typval_T *listtv = NULL;
3573
3574 /* return an empty string by default */
3575 rettv->v_type = VAR_STRING;
3576 rettv->vval.v_string = NULL;
3577
3578 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003579 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003580 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003581 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003582
Bram Moolenaar437905c2016-04-26 19:01:05 +02003583 if (opt.jo_set & JO_PART)
3584 part = opt.jo_part;
3585 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003586 if (channel != NULL)
3587 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003588 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003589 part = channel_part_read(channel);
3590 mode = channel_get_mode(channel, part);
3591 timeout = channel_get_timeout(channel, part);
3592 if (opt.jo_set & JO_TIMEOUT)
3593 timeout = opt.jo_timeout;
3594
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003595 if (blob)
3596 {
3597 int outlen = 0;
3598 char_u *p = channel_read_block(channel, part,
3599 timeout, TRUE, &outlen);
3600 if (p != NULL)
3601 {
3602 blob_T *b = blob_alloc();
3603
3604 if (b != NULL)
3605 {
3606 b->bv_ga.ga_len = outlen;
3607 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3608 blob_free(b);
3609 else
3610 {
3611 memcpy(b->bv_ga.ga_data, p, outlen);
3612 rettv_blob_set(rettv, b);
3613 }
3614 }
3615 vim_free(p);
3616 }
3617 }
3618 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003619 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003620 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003621 else
3622 {
3623 if (opt.jo_set & JO_ID)
3624 id = opt.jo_id;
3625 channel_read_json_block(channel, part, timeout, id, &listtv);
3626 if (listtv != NULL)
3627 {
3628 *rettv = *listtv;
3629 vim_free(listtv);
3630 }
3631 else
3632 {
3633 rettv->v_type = VAR_SPECIAL;
3634 rettv->vval.v_number = VVAL_NONE;
3635 }
3636 }
3637 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003638
3639theend:
3640 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003641}
3642
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003643# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3644 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003645/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003646 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003647 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003648 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003649 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003650channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003651{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003652 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003653 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003654
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003655 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003656 for (channel = first_channel; channel != NULL;
3657 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003658 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003659 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003660 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003661 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003662 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003663 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003664 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003665 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003666 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003667}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003668# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003669
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003670# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003671/*
3672 * Check the channels for anything that is ready to be read.
3673 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003674 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003675 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003676 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003677channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003678{
3679 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003680 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003681 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003682
3683 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3684 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003685 if (only_keep_open && !channel->ch_keep_open)
3686 continue;
3687
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003688 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003689 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003690 {
3691 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003692 if (fd != INVALID_FD)
3693 {
3694 int r = channel_wait(channel, fd, 0);
3695
3696 if (r == CW_READY)
3697 channel_read(channel, part, "channel_handle_events");
3698 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003699 ch_close_part_on_error(channel, part, TRUE,
3700 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003701 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003702 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003703 }
3704}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003705# endif
3706
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003707# if defined(FEAT_GUI) || defined(PROTO)
3708/*
3709 * Return TRUE when there is any channel with a keep_open flag.
3710 */
3711 int
3712channel_any_keep_open()
3713{
3714 channel_T *channel;
3715
3716 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3717 if (channel->ch_keep_open)
3718 return TRUE;
3719 return FALSE;
3720}
3721# endif
3722
Bram Moolenaard04a0202016-01-26 23:30:18 +01003723/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003724 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003725 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003726 */
3727 void
3728channel_set_nonblock(channel_T *channel, ch_part_T part)
3729{
3730 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003731 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003732
3733 if (fd != INVALID_FD)
3734 {
3735#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003736 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003737
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003738 ioctlsocket(fd, FIONBIO, &val);
3739#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003740 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003741#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003742 ch_part->ch_nonblocking = TRUE;
3743 }
3744}
3745
3746/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003747 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003748 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003749 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003750 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003751 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003752channel_send(
3753 channel_T *channel,
3754 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003755 char_u *buf_arg,
3756 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003757 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003758{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003759 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003760 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003761 chanpart_T *ch_part = &channel->ch_part[part];
3762 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003763
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003764 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003765 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003766 {
3767 if (!channel->ch_error && fun != NULL)
3768 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003769 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003770 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003771 }
3772 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003773 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003774 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003775
Bram Moolenaar0b146882018-09-06 16:27:24 +02003776 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3777 channel_set_nonblock(channel, part);
3778
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003779 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003780 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003781 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003782 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003783 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003784 fprintf(log_fd, "'\n");
3785 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003786 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003787 }
3788
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003789 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003790 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003791 writeq_T *wq = &ch_part->ch_writeque;
3792 char_u *buf;
3793 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003794
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003795 if (wq->wq_next != NULL)
3796 {
3797 /* first write what was queued */
3798 buf = wq->wq_next->wq_ga.ga_data;
3799 len = wq->wq_next->wq_ga.ga_len;
3800 did_use_queue = TRUE;
3801 }
3802 else
3803 {
3804 if (len_arg == 0)
3805 /* nothing to write, called from channel_select_check() */
3806 return OK;
3807 buf = buf_arg;
3808 len = len_arg;
3809 }
3810
3811 if (part == PART_SOCK)
3812 res = sock_write(fd, (char *)buf, len);
3813 else
3814 res = fd_write(fd, (char *)buf, len);
3815 if (res < 0 && (errno == EWOULDBLOCK
3816#ifdef EAGAIN
3817 || errno == EAGAIN
3818#endif
3819 ))
3820 res = 0; /* nothing got written */
3821
3822 if (res >= 0 && ch_part->ch_nonblocking)
3823 {
3824 writeq_T *entry = wq->wq_next;
3825
3826 if (did_use_queue)
3827 ch_log(channel, "Sent %d bytes now", res);
3828 if (res == len)
3829 {
3830 /* Wrote all the buf[len] bytes. */
3831 if (entry != NULL)
3832 {
3833 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003834 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003835 continue;
3836 }
3837 if (did_use_queue)
3838 ch_log(channel, "Write queue empty");
3839 }
3840 else
3841 {
3842 /* Wrote only buf[res] bytes, can't write more now. */
3843 if (entry != NULL)
3844 {
3845 if (res > 0)
3846 {
3847 /* Remove the bytes that were written. */
3848 mch_memmove(entry->wq_ga.ga_data,
3849 (char *)entry->wq_ga.ga_data + res,
3850 len - res);
3851 entry->wq_ga.ga_len -= res;
3852 }
3853 buf = buf_arg;
3854 len = len_arg;
3855 }
3856 else
3857 {
3858 buf += res;
3859 len -= res;
3860 }
3861 ch_log(channel, "Adding %d bytes to the write queue", len);
3862
3863 /* Append the not written bytes of the argument to the write
3864 * buffer. Limit entries to 4000 bytes. */
3865 if (wq->wq_prev != NULL
3866 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3867 {
3868 writeq_T *last = wq->wq_prev;
3869
3870 /* append to the last entry */
3871 if (ga_grow(&last->wq_ga, len) == OK)
3872 {
3873 mch_memmove((char *)last->wq_ga.ga_data
3874 + last->wq_ga.ga_len,
3875 buf, len);
3876 last->wq_ga.ga_len += len;
3877 }
3878 }
3879 else
3880 {
3881 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3882
3883 if (last != NULL)
3884 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003885 last->wq_prev = wq->wq_prev;
3886 last->wq_next = NULL;
3887 if (wq->wq_prev == NULL)
3888 wq->wq_next = last;
3889 else
3890 wq->wq_prev->wq_next = last;
3891 wq->wq_prev = last;
3892 ga_init2(&last->wq_ga, 1, 1000);
3893 if (ga_grow(&last->wq_ga, len) == OK)
3894 {
3895 mch_memmove(last->wq_ga.ga_data, buf, len);
3896 last->wq_ga.ga_len = len;
3897 }
3898 }
3899 }
3900 }
3901 }
3902 else if (res != len)
3903 {
3904 if (!channel->ch_error && fun != NULL)
3905 {
3906 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003907 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003908 }
3909 channel->ch_error = TRUE;
3910 return FAIL;
3911 }
3912
3913 channel->ch_error = FALSE;
3914 return OK;
3915 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003916}
3917
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003918/*
3919 * Common for "ch_sendexpr()" and "ch_sendraw()".
3920 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003921 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003922 * Otherwise returns NULL.
3923 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003924 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003925send_common(
3926 typval_T *argvars,
3927 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003928 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003929 int id,
3930 int eval,
3931 jobopt_T *opt,
3932 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003933 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003934{
3935 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003936 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003937
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003938 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003939 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003940 if (channel == NULL)
3941 return NULL;
3942 part_send = channel_part_send(channel);
3943 *part_read = channel_part_read(channel);
3944
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003945 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003946 return NULL;
3947
3948 /* Set the callback. An empty callback means no callback and not reading
3949 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3950 * allowed. */
3951 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3952 {
3953 if (eval)
3954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003956 return NULL;
3957 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003958 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003959 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 }
3961
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003962 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003963 && opt->jo_callback == NULL)
3964 return channel;
3965 return NULL;
3966}
3967
3968/*
3969 * common for "ch_evalexpr()" and "ch_sendexpr()"
3970 */
3971 void
3972ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3973{
3974 char_u *text;
3975 typval_T *listtv;
3976 channel_T *channel;
3977 int id;
3978 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003979 ch_part_T part_send;
3980 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003981 jobopt_T opt;
3982 int timeout;
3983
3984 /* return an empty string by default */
3985 rettv->v_type = VAR_STRING;
3986 rettv->vval.v_string = NULL;
3987
Bram Moolenaar437905c2016-04-26 19:01:05 +02003988 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 if (channel == NULL)
3990 return;
3991 part_send = channel_part_send(channel);
3992
3993 ch_mode = channel_get_mode(channel, part_send);
3994 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003996 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003997 return;
3998 }
3999
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004000 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004001 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004002 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004003 if (text == NULL)
4004 return;
4005
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004006 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4008 vim_free(text);
4009 if (channel != NULL && eval)
4010 {
4011 if (opt.jo_set & JO_TIMEOUT)
4012 timeout = opt.jo_timeout;
4013 else
4014 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004015 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4016 == OK)
4017 {
4018 list_T *list = listtv->vval.v_list;
4019
4020 /* Move the item from the list and then change the type to
4021 * avoid the value being freed. */
4022 *rettv = list->lv_last->li_tv;
4023 list->lv_last->li_tv.v_type = VAR_NUMBER;
4024 free_tv(listtv);
4025 }
4026 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004027 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004028}
4029
4030/*
4031 * common for "ch_evalraw()" and "ch_sendraw()"
4032 */
4033 void
4034ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4035{
4036 char_u buf[NUMBUFLEN];
4037 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004038 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004039 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004040 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004041 jobopt_T opt;
4042 int timeout;
4043
4044 /* return an empty string by default */
4045 rettv->v_type = VAR_STRING;
4046 rettv->vval.v_string = NULL;
4047
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004048 if (argvars[1].v_type == VAR_BLOB)
4049 {
4050 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4051 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4052 }
4053 else
4054 {
4055 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004056 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004057 }
4058 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4060 if (channel != NULL && eval)
4061 {
4062 if (opt.jo_set & JO_TIMEOUT)
4063 timeout = opt.jo_timeout;
4064 else
4065 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004066 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004067 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004069 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070}
4071
Bram Moolenaarf3360612017-10-01 16:21:31 +02004072# define KEEP_OPEN_TIME 20 /* msec */
4073
Bram Moolenaard04a0202016-01-26 23:30:18 +01004074# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004075/*
4076 * Add open channels to the poll struct.
4077 * Return the adjusted struct index.
4078 * The type of "fds" is hidden to avoid problems with the function proto.
4079 */
4080 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004081channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004082{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004083 int nfd = nfd_in;
4084 channel_T *channel;
4085 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004086 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004087
Bram Moolenaar77073442016-02-13 23:23:53 +01004088 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004089 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004090 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004091 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004092 chanpart_T *ch_part = &channel->ch_part[part];
4093
4094 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004095 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004096 if (channel->ch_keep_open)
4097 {
4098 /* For unknown reason poll() returns immediately for a
4099 * keep-open channel. Instead of adding it to the fds add
4100 * a short timeout and check, like polling. */
4101 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4102 *towait = KEEP_OPEN_TIME;
4103 }
4104 else
4105 {
4106 ch_part->ch_poll_idx = nfd;
4107 fds[nfd].fd = ch_part->ch_fd;
4108 fds[nfd].events = POLLIN;
4109 nfd++;
4110 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004111 }
4112 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004113 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004114 }
4115 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004116
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004117 nfd = channel_fill_poll_write(nfd, fds);
4118
Bram Moolenaare0874f82016-01-24 20:36:41 +01004119 return nfd;
4120}
4121
4122/*
4123 * The type of "fds" is hidden to avoid problems with the function proto.
4124 */
4125 int
4126channel_poll_check(int ret_in, void *fds_in)
4127{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004128 int ret = ret_in;
4129 channel_T *channel;
4130 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004131 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004132 int idx;
4133 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004134
Bram Moolenaar77073442016-02-13 23:23:53 +01004135 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004136 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004137 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004138 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004139 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004140
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004141 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004142 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004143 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004144 --ret;
4145 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004146 else if (channel->ch_part[part].ch_fd != INVALID_FD
4147 && channel->ch_keep_open)
4148 {
4149 /* polling a keep-open channel */
4150 channel_read(channel, part, "channel_poll_check_keep_open");
4151 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004152 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004153
4154 in_part = &channel->ch_part[PART_IN];
4155 idx = in_part->ch_poll_idx;
4156 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4157 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004158 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004159 --ret;
4160 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004161 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004162
4163 return ret;
4164}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004165# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004166
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004167# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004168
Bram Moolenaare0874f82016-01-24 20:36:41 +01004169/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004170 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004171 */
4172 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004173channel_select_setup(
4174 int maxfd_in,
4175 void *rfds_in,
4176 void *wfds_in,
4177 struct timeval *tv,
4178 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004179{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004180 int maxfd = maxfd_in;
4181 channel_T *channel;
4182 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004183 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004184 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004185
Bram Moolenaar77073442016-02-13 23:23:53 +01004186 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004187 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004188 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004189 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004190 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004191
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004192 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004193 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004194 if (channel->ch_keep_open)
4195 {
4196 /* For unknown reason select() returns immediately for a
4197 * keep-open channel. Instead of adding it to the rfds add
4198 * a short timeout and check, like polling. */
4199 if (*tvp == NULL || tv->tv_sec > 0
4200 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4201 {
4202 *tvp = tv;
4203 tv->tv_sec = 0;
4204 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4205 }
4206 }
4207 else
4208 {
4209 FD_SET((int)fd, rfds);
4210 if (maxfd < (int)fd)
4211 maxfd = (int)fd;
4212 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004213 }
4214 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004215 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004216
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004217 maxfd = channel_fill_wfds(maxfd, wfds);
4218
Bram Moolenaare0874f82016-01-24 20:36:41 +01004219 return maxfd;
4220}
4221
4222/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004223 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004224 */
4225 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004226channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004227{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004228 int ret = ret_in;
4229 channel_T *channel;
4230 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004231 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004232 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004233 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004234
Bram Moolenaar77073442016-02-13 23:23:53 +01004235 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004236 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004237 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004238 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004239 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004240
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004241 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004242 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004243 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004244 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004245 --ret;
4246 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004247 else if (fd != INVALID_FD && channel->ch_keep_open)
4248 {
4249 /* polling a keep-open channel */
4250 channel_read(channel, part, "channel_select_check_keep_open");
4251 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004252 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004253
4254 in_part = &channel->ch_part[PART_IN];
4255 if (ret > 0 && in_part->ch_fd != INVALID_FD
4256 && FD_ISSET(in_part->ch_fd, wfds))
4257 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004258 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004259 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004260 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004261 --ret;
4262 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004263 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004264
4265 return ret;
4266}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004267# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004268
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004269/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004270 * Execute queued up commands.
4271 * Invoked from the main loop when it's safe to execute received commands.
4272 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004273 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004274 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004275channel_parse_messages(void)
4276{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004277 channel_T *channel = first_channel;
4278 int ret = FALSE;
4279 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004280 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004281#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004282 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004283
4284 ELAPSED_INIT(start_tv);
4285#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004286
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004287 ++safe_to_invoke_callback;
4288
Bram Moolenaard0b65022016-03-06 21:50:33 +01004289 /* Only do this message when another message was given, otherwise we get
4290 * lots of them. */
4291 if (did_log_msg)
4292 {
4293 ch_log(NULL, "looking for messages on channels");
4294 did_log_msg = FALSE;
4295 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004296 while (channel != NULL)
4297 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004298 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004299 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004300 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004301 channel_close_now(channel);
4302 /* channel may have been freed, start over */
4303 channel = first_channel;
4304 continue;
4305 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004306 if (channel->ch_to_be_freed)
4307 {
4308 channel_free(channel);
4309 /* channel has been freed, start over */
4310 channel = first_channel;
4311 continue;
4312 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004313 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004314 {
4315 /* channel is no longer useful, free it */
4316 channel_free(channel);
4317 channel = first_channel;
4318 part = PART_SOCK;
4319 continue;
4320 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004321 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004322 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004323 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004324 /* Increase the refcount, in case the handler causes the channel
4325 * to be unreferenced or closed. */
4326 ++channel->ch_refcount;
4327 r = may_invoke_callback(channel, part);
4328 if (r == OK)
4329 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004330 if (channel_unref(channel) || (r == OK
4331#ifdef ELAPSED_FUNC
4332 /* Limit the time we loop here to 100 msec, otherwise
4333 * Vim becomes unresponsive when the callback takes
4334 * more than a bit of time. */
4335 && ELAPSED_FUNC(start_tv) < 100L
4336#endif
4337 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004338 {
4339 /* channel was freed or something was done, start over */
4340 channel = first_channel;
4341 part = PART_SOCK;
4342 continue;
4343 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004344 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004345 if (part < PART_ERR)
4346 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004347 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004348 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004349 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004350 part = PART_SOCK;
4351 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004352 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004353
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004354 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004355 {
4356 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004357 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004358 }
4359
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004360 --safe_to_invoke_callback;
4361
Bram Moolenaard7ece102016-02-02 23:23:02 +01004362 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004363}
4364
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004365/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004366 * Return TRUE if any channel has readahead. That means we should not block on
4367 * waiting for input.
4368 */
4369 int
4370channel_any_readahead(void)
4371{
4372 channel_T *channel = first_channel;
4373 ch_part_T part = PART_SOCK;
4374
4375 while (channel != NULL)
4376 {
4377 if (channel_has_readahead(channel, part))
4378 return TRUE;
4379 if (part < PART_ERR)
4380 ++part;
4381 else
4382 {
4383 channel = channel->ch_next;
4384 part = PART_SOCK;
4385 }
4386 }
4387 return FALSE;
4388}
4389
4390/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004391 * Mark references to lists used in channels.
4392 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004393 int
4394set_ref_in_channel(int copyID)
4395{
Bram Moolenaar77073442016-02-13 23:23:53 +01004396 int abort = FALSE;
4397 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004398 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004399
Bram Moolenaar77073442016-02-13 23:23:53 +01004400 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004401 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004402 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004403 tv.v_type = VAR_CHANNEL;
4404 tv.vval.v_channel = channel;
4405 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004406 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004407 return abort;
4408}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004409
4410/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004411 * Return the "part" to write to for "channel".
4412 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004413 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004414channel_part_send(channel_T *channel)
4415{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004416 if (channel->CH_SOCK_FD == INVALID_FD)
4417 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004418 return PART_SOCK;
4419}
4420
4421/*
4422 * Return the default "part" to read from for "channel".
4423 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004424 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004425channel_part_read(channel_T *channel)
4426{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004427 if (channel->CH_SOCK_FD == INVALID_FD)
4428 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004429 return PART_SOCK;
4430}
4431
4432/*
4433 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004434 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004435 */
4436 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004437channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004438{
Bram Moolenaar77073442016-02-13 23:23:53 +01004439 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004440 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004441 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004442}
4443
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004444/*
4445 * Return the timeout of "channel"/"part"
4446 */
4447 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004448channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004449{
4450 return channel->ch_part[part].ch_timeout;
4451}
4452
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004453 static int
4454handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4455{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004456 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004457
4458 opt->jo_set |= jo;
4459 if (STRCMP(val, "nl") == 0)
4460 *modep = MODE_NL;
4461 else if (STRCMP(val, "raw") == 0)
4462 *modep = MODE_RAW;
4463 else if (STRCMP(val, "js") == 0)
4464 *modep = MODE_JS;
4465 else if (STRCMP(val, "json") == 0)
4466 *modep = MODE_JSON;
4467 else
4468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004469 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004470 return FAIL;
4471 }
4472 return OK;
4473}
4474
4475 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004476handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004477{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004478 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004479
4480 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4481 if (STRCMP(val, "null") == 0)
4482 opt->jo_io[part] = JIO_NULL;
4483 else if (STRCMP(val, "pipe") == 0)
4484 opt->jo_io[part] = JIO_PIPE;
4485 else if (STRCMP(val, "file") == 0)
4486 opt->jo_io[part] = JIO_FILE;
4487 else if (STRCMP(val, "buffer") == 0)
4488 opt->jo_io[part] = JIO_BUFFER;
4489 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4490 opt->jo_io[part] = JIO_OUT;
4491 else
4492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004493 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494 return FAIL;
4495 }
4496 return OK;
4497}
4498
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004499/*
4500 * Clear a jobopt_T before using it.
4501 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004502 void
4503clear_job_options(jobopt_T *opt)
4504{
4505 vim_memset(opt, 0, sizeof(jobopt_T));
4506}
4507
4508/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004509 * Free any members of a jobopt_T.
4510 */
4511 void
4512free_job_options(jobopt_T *opt)
4513{
4514 if (opt->jo_partial != NULL)
4515 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004516 else if (opt->jo_callback != NULL)
4517 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004518 if (opt->jo_out_partial != NULL)
4519 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004520 else if (opt->jo_out_cb != NULL)
4521 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004522 if (opt->jo_err_partial != NULL)
4523 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004524 else if (opt->jo_err_cb != NULL)
4525 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004526 if (opt->jo_close_partial != NULL)
4527 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004528 else if (opt->jo_close_cb != NULL)
4529 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004530 if (opt->jo_exit_partial != NULL)
4531 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004532 else if (opt->jo_exit_cb != NULL)
4533 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004534 if (opt->jo_env != NULL)
4535 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004536}
4537
4538/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004539 * Get the PART_ number from the first character of an option name.
4540 */
4541 static int
4542part_from_char(int c)
4543{
4544 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4545}
4546
4547/*
4548 * Get the option entries from the dict in "tv", parse them and put the result
4549 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004550 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 * If an option value is invalid return FAIL.
4552 */
4553 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004554get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004555{
4556 typval_T *item;
4557 char_u *val;
4558 dict_T *dict;
4559 int todo;
4560 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004561 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004562
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563 if (tv->v_type == VAR_UNKNOWN)
4564 return OK;
4565 if (tv->v_type != VAR_DICT)
4566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004567 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568 return FAIL;
4569 }
4570 dict = tv->vval.v_dict;
4571 if (dict == NULL)
4572 return OK;
4573
4574 todo = (int)dict->dv_hashtab.ht_used;
4575 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4576 if (!HASHITEM_EMPTY(hi))
4577 {
4578 item = &dict_lookup(hi)->di_tv;
4579
4580 if (STRCMP(hi->hi_key, "mode") == 0)
4581 {
4582 if (!(supported & JO_MODE))
4583 break;
4584 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4585 return FAIL;
4586 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004587 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004588 {
4589 if (!(supported & JO_IN_MODE))
4590 break;
4591 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4592 == FAIL)
4593 return FAIL;
4594 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004595 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 {
4597 if (!(supported & JO_OUT_MODE))
4598 break;
4599 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4600 == FAIL)
4601 return FAIL;
4602 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004603 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004604 {
4605 if (!(supported & JO_ERR_MODE))
4606 break;
4607 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4608 == FAIL)
4609 return FAIL;
4610 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004611 else if (STRCMP(hi->hi_key, "noblock") == 0)
4612 {
4613 if (!(supported & JO_MODE))
4614 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004615 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004616 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004617 else if (STRCMP(hi->hi_key, "in_io") == 0
4618 || STRCMP(hi->hi_key, "out_io") == 0
4619 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004620 {
4621 if (!(supported & JO_OUT_IO))
4622 break;
4623 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4624 return FAIL;
4625 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004626 else if (STRCMP(hi->hi_key, "in_name") == 0
4627 || STRCMP(hi->hi_key, "out_name") == 0
4628 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004629 {
4630 part = part_from_char(*hi->hi_key);
4631
4632 if (!(supported & JO_OUT_IO))
4633 break;
4634 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4635 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004636 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004637 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004638 else if (STRCMP(hi->hi_key, "pty") == 0)
4639 {
4640 if (!(supported & JO_MODE))
4641 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004643 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004644 else if (STRCMP(hi->hi_key, "in_buf") == 0
4645 || STRCMP(hi->hi_key, "out_buf") == 0
4646 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004647 {
4648 part = part_from_char(*hi->hi_key);
4649
4650 if (!(supported & JO_OUT_IO))
4651 break;
4652 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004653 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004654 if (opt->jo_io_buf[part] <= 0)
4655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004656 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004657 return FAIL;
4658 }
4659 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004661 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004662 return FAIL;
4663 }
4664 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004665 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4666 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4667 {
4668 part = part_from_char(*hi->hi_key);
4669
4670 if (!(supported & JO_OUT_IO))
4671 break;
4672 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004673 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004674 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004675 else if (STRCMP(hi->hi_key, "out_msg") == 0
4676 || STRCMP(hi->hi_key, "err_msg") == 0)
4677 {
4678 part = part_from_char(*hi->hi_key);
4679
4680 if (!(supported & JO_OUT_IO))
4681 break;
4682 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004683 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004684 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004685 else if (STRCMP(hi->hi_key, "in_top") == 0
4686 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004687 {
4688 linenr_T *lp;
4689
4690 if (!(supported & JO_OUT_IO))
4691 break;
4692 if (hi->hi_key[3] == 't')
4693 {
4694 lp = &opt->jo_in_top;
4695 opt->jo_set |= JO_IN_TOP;
4696 }
4697 else
4698 {
4699 lp = &opt->jo_in_bot;
4700 opt->jo_set |= JO_IN_BOT;
4701 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004702 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004703 if (*lp < 0)
4704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004705 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 return FAIL;
4707 }
4708 }
4709 else if (STRCMP(hi->hi_key, "channel") == 0)
4710 {
4711 if (!(supported & JO_OUT_IO))
4712 break;
4713 opt->jo_set |= JO_CHANNEL;
4714 if (item->v_type != VAR_CHANNEL)
4715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004717 return FAIL;
4718 }
4719 opt->jo_channel = item->vval.v_channel;
4720 }
4721 else if (STRCMP(hi->hi_key, "callback") == 0)
4722 {
4723 if (!(supported & JO_CALLBACK))
4724 break;
4725 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004726 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 if (opt->jo_callback == NULL)
4728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004729 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004730 return FAIL;
4731 }
4732 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004733 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004734 {
4735 if (!(supported & JO_OUT_CALLBACK))
4736 break;
4737 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004738 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004739 if (opt->jo_out_cb == NULL)
4740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004741 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004742 return FAIL;
4743 }
4744 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004745 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004746 {
4747 if (!(supported & JO_ERR_CALLBACK))
4748 break;
4749 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004750 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004751 if (opt->jo_err_cb == NULL)
4752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004753 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004754 return FAIL;
4755 }
4756 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004757 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004758 {
4759 if (!(supported & JO_CLOSE_CALLBACK))
4760 break;
4761 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004762 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 if (opt->jo_close_cb == NULL)
4764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004765 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004766 return FAIL;
4767 }
4768 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004769 else if (STRCMP(hi->hi_key, "drop") == 0)
4770 {
4771 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004772 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004773
4774 if (STRCMP(val, "never") == 0)
4775 never = TRUE;
4776 else if (STRCMP(val, "auto") != 0)
4777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004778 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004779 return FAIL;
4780 }
4781 opt->jo_drop_never = never;
4782 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004783 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4784 {
4785 if (!(supported & JO_EXIT_CB))
4786 break;
4787 opt->jo_set |= JO_EXIT_CB;
4788 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4789 if (opt->jo_exit_cb == NULL)
4790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004791 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004792 return FAIL;
4793 }
4794 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004795#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004796 else if (STRCMP(hi->hi_key, "term_name") == 0)
4797 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004798 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004799 break;
4800 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004801 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004802 if (opt->jo_term_name == NULL)
4803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004804 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004805 return FAIL;
4806 }
4807 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004808 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4809 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004810 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004811 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004812 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004813 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004815 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004816 return FAIL;
4817 }
4818 opt->jo_set2 |= JO2_TERM_FINISH;
4819 opt->jo_term_finish = *val;
4820 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004821 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4822 {
4823 char_u *p;
4824
4825 if (!(supported2 & JO2_TERM_OPENCMD))
4826 break;
4827 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004828 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004829 if (p != NULL)
4830 {
4831 /* Must have %d and no other %. */
4832 p = vim_strchr(p, '%');
4833 if (p != NULL && (p[1] != 'd'
4834 || vim_strchr(p + 2, '%') != NULL))
4835 p = NULL;
4836 }
4837 if (p == NULL)
4838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004840 return FAIL;
4841 }
4842 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004843 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4844 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004845 char_u *p;
4846
4847 if (!(supported2 & JO2_EOF_CHARS))
4848 break;
4849 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004851 if (p == NULL)
4852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004854 return FAIL;
4855 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004856 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004857 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4858 {
4859 if (!(supported2 & JO2_TERM_ROWS))
4860 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004861 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004862 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004863 }
4864 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4865 {
4866 if (!(supported2 & JO2_TERM_COLS))
4867 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004868 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004869 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004870 }
4871 else if (STRCMP(hi->hi_key, "vertical") == 0)
4872 {
4873 if (!(supported2 & JO2_VERTICAL))
4874 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004875 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004876 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004877 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004878 else if (STRCMP(hi->hi_key, "curwin") == 0)
4879 {
4880 if (!(supported2 & JO2_CURWIN))
4881 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004882 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004883 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004884 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004885 else if (STRCMP(hi->hi_key, "hidden") == 0)
4886 {
4887 if (!(supported2 & JO2_HIDDEN))
4888 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004889 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004890 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004891 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004892 else if (STRCMP(hi->hi_key, "norestore") == 0)
4893 {
4894 if (!(supported2 & JO2_NORESTORE))
4895 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004896 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004897 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004898 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004899 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4900 {
4901 if (!(supported2 & JO2_TERM_KILL))
4902 break;
4903 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004904 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004905 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004906# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4907 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004909 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004910 listitem_T *li;
4911 long_u rgb[16];
4912
4913 if (!(supported2 & JO2_ANSI_COLORS))
4914 break;
4915
4916 if (item == NULL || item->v_type != VAR_LIST
4917 || item->vval.v_list == NULL)
4918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004919 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004920 return FAIL;
4921 }
4922
4923 li = item->vval.v_list->lv_first;
4924 for (; li != NULL && n < 16; li = li->li_next, n++)
4925 {
4926 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004927 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004929 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004930 if (color_name == NULL)
4931 return FAIL;
4932
4933 guicolor = GUI_GET_COLOR(color_name);
4934 if (guicolor == INVALCOLOR)
4935 return FAIL;
4936
4937 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4938 }
4939
4940 if (n != 16 || li != NULL)
4941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004942 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004943 return FAIL;
4944 }
4945
4946 opt->jo_set2 |= JO2_ANSI_COLORS;
4947 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4948 }
4949# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004950#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004951 else if (STRCMP(hi->hi_key, "env") == 0)
4952 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004953 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004954 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004955 if (item->v_type != VAR_DICT)
4956 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004957 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02004958 return FAIL;
4959 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004960 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004961 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004962 if (opt->jo_env != NULL)
4963 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004964 }
4965 else if (STRCMP(hi->hi_key, "cwd") == 0)
4966 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004967 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004968 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004969 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02004970 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02004971#ifndef WIN32 // Win32 directories don't have the concept of "executable"
4972 || mch_access((char *)opt->jo_cwd, X_OK) != 0
4973#endif
4974 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004976 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004977 return FAIL;
4978 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004979 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004980 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004981 else if (STRCMP(hi->hi_key, "waittime") == 0)
4982 {
4983 if (!(supported & JO_WAITTIME))
4984 break;
4985 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004986 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004987 }
4988 else if (STRCMP(hi->hi_key, "timeout") == 0)
4989 {
4990 if (!(supported & JO_TIMEOUT))
4991 break;
4992 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004993 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004994 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004995 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004996 {
4997 if (!(supported & JO_OUT_TIMEOUT))
4998 break;
4999 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005000 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005001 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005002 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005003 {
5004 if (!(supported & JO_ERR_TIMEOUT))
5005 break;
5006 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005007 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005008 }
5009 else if (STRCMP(hi->hi_key, "part") == 0)
5010 {
5011 if (!(supported & JO_PART))
5012 break;
5013 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005014 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005015 if (STRCMP(val, "err") == 0)
5016 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005017 else if (STRCMP(val, "out") == 0)
5018 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005019 else
5020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005021 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005022 return FAIL;
5023 }
5024 }
5025 else if (STRCMP(hi->hi_key, "id") == 0)
5026 {
5027 if (!(supported & JO_ID))
5028 break;
5029 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005030 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005031 }
5032 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5033 {
5034 if (!(supported & JO_STOPONEXIT))
5035 break;
5036 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005037 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005038 opt->jo_soe_buf);
5039 if (opt->jo_stoponexit == NULL)
5040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005041 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005042 return FAIL;
5043 }
5044 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005045 else if (STRCMP(hi->hi_key, "block_write") == 0)
5046 {
5047 if (!(supported & JO_BLOCK_WRITE))
5048 break;
5049 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005050 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005051 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005052 else
5053 break;
5054 --todo;
5055 }
5056 if (todo > 0)
5057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005058 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005059 return FAIL;
5060 }
5061
5062 return OK;
5063}
5064
5065/*
5066 * Get the channel from the argument.
5067 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005068 * When "check_open" is TRUE check that the channel can be used.
5069 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005070 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005071 */
5072 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005073get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005074{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005075 channel_T *channel = NULL;
5076 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005077
5078 if (tv->v_type == VAR_JOB)
5079 {
5080 if (tv->vval.v_job != NULL)
5081 channel = tv->vval.v_job->jv_channel;
5082 }
5083 else if (tv->v_type == VAR_CHANNEL)
5084 {
5085 channel = tv->vval.v_channel;
5086 }
5087 else
5088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005089 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005090 return NULL;
5091 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005092 if (channel != NULL && reading)
5093 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005094 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005095
Bram Moolenaar437905c2016-04-26 19:01:05 +02005096 if (check_open && (channel == NULL || (!channel_is_open(channel)
5097 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005099 emsg(_("E906: not an open channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005100 return NULL;
5101 }
5102 return channel;
5103}
5104
5105static job_T *first_job = NULL;
5106
5107 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005108job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005109{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005110 int i;
5111
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005112 ch_log(job->jv_channel, "Freeing job");
5113 if (job->jv_channel != NULL)
5114 {
5115 /* The link from the channel to the job doesn't count as a reference,
5116 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005117 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005118 * NULL the reference. We don't set ch_job_killed, unreferencing the
5119 * job doesn't mean it stops running. */
5120 job->jv_channel->ch_job = NULL;
5121 channel_unref(job->jv_channel);
5122 }
5123 mch_clear_job(job);
5124
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005125 vim_free(job->jv_tty_in);
5126 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005127 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005128 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005129 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005130 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005131 for (i = 0; job->jv_argv[i] != NULL; i++)
5132 vim_free(job->jv_argv[i]);
5133 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005134 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005135}
5136
5137 static void
5138job_free_job(job_T *job)
5139{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005140 if (job->jv_next != NULL)
5141 job->jv_next->jv_prev = job->jv_prev;
5142 if (job->jv_prev == NULL)
5143 first_job = job->jv_next;
5144 else
5145 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005146 vim_free(job);
5147}
5148
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149 static void
5150job_free(job_T *job)
5151{
5152 if (!in_free_unref_items)
5153 {
5154 job_free_contents(job);
5155 job_free_job(job);
5156 }
5157}
5158
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005159#if defined(EXITFREE) || defined(PROTO)
5160 void
5161job_free_all(void)
5162{
5163 while (first_job != NULL)
5164 job_free(first_job);
5165}
5166#endif
5167
5168/*
5169 * Return TRUE if we need to check if the process of "job" has ended.
5170 */
5171 static int
5172job_need_end_check(job_T *job)
5173{
5174 return job->jv_status == JOB_STARTED
5175 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5176}
5177
5178/*
5179 * Return TRUE if the channel of "job" is still useful.
5180 */
5181 static int
5182job_channel_still_useful(job_T *job)
5183{
5184 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5185}
5186
5187/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005188 * Return TRUE if the channel of "job" is closeable.
5189 */
5190 static int
5191job_channel_can_close(job_T *job)
5192{
5193 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5194}
5195
5196/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005197 * Return TRUE if the job should not be freed yet. Do not free the job when
5198 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5199 * or when the associated channel will do something with the job output.
5200 */
5201 static int
5202job_still_useful(job_T *job)
5203{
5204 return job_need_end_check(job) || job_channel_still_useful(job);
5205}
5206
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005207#if defined(GUI_MAY_FORK) || defined(PROTO)
5208/*
5209 * Return TRUE when there is any running job that we care about.
5210 */
5211 int
5212job_any_running()
5213{
5214 job_T *job;
5215
5216 for (job = first_job; job != NULL; job = job->jv_next)
5217 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005218 {
5219 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005220 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005221 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005222 return FALSE;
5223}
5224#endif
5225
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005226#if !defined(USE_ARGV) || defined(PROTO)
5227/*
5228 * Escape one argument for an external command.
5229 * Returns the escaped string in allocated memory. NULL when out of memory.
5230 */
5231 static char_u *
5232win32_escape_arg(char_u *arg)
5233{
5234 int slen, dlen;
5235 int escaping = 0;
5236 int i;
5237 char_u *s, *d;
5238 char_u *escaped_arg;
5239 int has_spaces = FALSE;
5240
5241 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005242 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005243 dlen = slen;
5244 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5245 {
5246 if (*s == '"' || *s == '\\')
5247 ++dlen;
5248 if (*s == ' ' || *s == '\t')
5249 has_spaces = TRUE;
5250 }
5251
5252 if (has_spaces)
5253 dlen += 2;
5254
5255 if (dlen == slen)
5256 return vim_strsave(arg);
5257
5258 /* Allocate memory for the result and fill it. */
5259 escaped_arg = alloc(dlen + 1);
5260 if (escaped_arg == NULL)
5261 return NULL;
5262 memset(escaped_arg, 0, dlen+1);
5263
5264 d = escaped_arg;
5265
5266 if (has_spaces)
5267 *d++ = '"';
5268
5269 for (s = arg; *s != NUL;)
5270 {
5271 switch (*s)
5272 {
5273 case '"':
5274 for (i = 0; i < escaping; i++)
5275 *d++ = '\\';
5276 escaping = 0;
5277 *d++ = '\\';
5278 *d++ = *s++;
5279 break;
5280 case '\\':
5281 escaping++;
5282 *d++ = *s++;
5283 break;
5284 default:
5285 escaping = 0;
5286 MB_COPY_CHAR(s, d);
5287 break;
5288 }
5289 }
5290
5291 /* add terminating quote and finish with a NUL */
5292 if (has_spaces)
5293 {
5294 for (i = 0; i < escaping; i++)
5295 *d++ = '\\';
5296 *d++ = '"';
5297 }
5298 *d = NUL;
5299
5300 return escaped_arg;
5301}
5302
5303/*
5304 * Build a command line from a list, taking care of escaping.
5305 * The result is put in gap->ga_data.
5306 * Returns FAIL when out of memory.
5307 */
5308 int
5309win32_build_cmd(list_T *l, garray_T *gap)
5310{
5311 listitem_T *li;
5312 char_u *s;
5313
5314 for (li = l->lv_first; li != NULL; li = li->li_next)
5315 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005316 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005317 if (s == NULL)
5318 return FAIL;
5319 s = win32_escape_arg(s);
5320 if (s == NULL)
5321 return FAIL;
5322 ga_concat(gap, s);
5323 vim_free(s);
5324 if (li->li_next != NULL)
5325 ga_append(gap, ' ');
5326 }
5327 return OK;
5328}
5329#endif
5330
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005331/*
5332 * NOTE: Must call job_cleanup() only once right after the status of "job"
5333 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5334 * mch_detect_ended_job() returned non-NULL).
5335 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005336 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005337job_cleanup(job_T *job)
5338{
5339 if (job->jv_status != JOB_ENDED)
5340 return;
5341
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005342 /* Ready to cleanup the job. */
5343 job->jv_status = JOB_FINISHED;
5344
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005345 /* When only channel-in is kept open, close explicitly. */
5346 if (job->jv_channel != NULL)
5347 ch_close_part(job->jv_channel, PART_IN);
5348
Bram Moolenaar97792de2016-10-15 18:36:49 +02005349 if (job->jv_exit_cb != NULL)
5350 {
5351 typval_T argv[3];
5352 typval_T rettv;
5353 int dummy;
5354
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005355 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005356 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005357 ++job->jv_refcount;
5358 argv[0].v_type = VAR_JOB;
5359 argv[0].vval.v_job = job;
5360 argv[1].v_type = VAR_NUMBER;
5361 argv[1].vval.v_number = job->jv_exitval;
5362 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5363 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5364 job->jv_exit_partial, NULL);
5365 clear_tv(&rettv);
5366 --job->jv_refcount;
5367 channel_need_redraw = TRUE;
5368 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005369
5370 /* Do not free the job in case the close callback of the associated channel
5371 * isn't invoked yet and may get information by job_info(). */
5372 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005373 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005374 /* The job was already unreferenced and the associated channel was
5375 * detached, now that it ended it can be freed. Careful: caller must
5376 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005377 job_free(job);
5378 }
5379}
5380
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005381/*
5382 * Mark references in jobs that are still useful.
5383 */
5384 int
5385set_ref_in_job(int copyID)
5386{
5387 int abort = FALSE;
5388 job_T *job;
5389 typval_T tv;
5390
5391 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005392 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005393 {
5394 tv.v_type = VAR_JOB;
5395 tv.vval.v_job = job;
5396 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5397 }
5398 return abort;
5399}
5400
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005401/*
5402 * Dereference "job". Note that after this "job" may have been freed.
5403 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005404 void
5405job_unref(job_T *job)
5406{
5407 if (job != NULL && --job->jv_refcount <= 0)
5408 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005409 /* Do not free the job if there is a channel where the close callback
5410 * may get the job info. */
5411 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005412 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005413 /* Do not free the job when it has not ended yet and there is a
5414 * "stoponexit" flag or an exit callback. */
5415 if (!job_need_end_check(job))
5416 {
5417 job_free(job);
5418 }
5419 else if (job->jv_channel != NULL)
5420 {
5421 /* Do remove the link to the channel, otherwise it hangs
5422 * around until Vim exits. See job_free() for refcount. */
5423 ch_log(job->jv_channel, "detaching channel from job");
5424 job->jv_channel->ch_job = NULL;
5425 channel_unref(job->jv_channel);
5426 job->jv_channel = NULL;
5427 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005428 }
5429 }
5430}
5431
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005432 int
5433free_unused_jobs_contents(int copyID, int mask)
5434{
5435 int did_free = FALSE;
5436 job_T *job;
5437
5438 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005439 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005440 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005441 {
5442 /* Free the channel and ordinary items it contains, but don't
5443 * recurse into Lists, Dictionaries etc. */
5444 job_free_contents(job);
5445 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005446 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005447 return did_free;
5448}
5449
5450 void
5451free_unused_jobs(int copyID, int mask)
5452{
5453 job_T *job;
5454 job_T *job_next;
5455
5456 for (job = first_job; job != NULL; job = job_next)
5457 {
5458 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005459 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005460 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005461 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005462 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005463 job_free_job(job);
5464 }
5465 }
5466}
5467
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005468/*
5469 * Allocate a job. Sets the refcount to one and sets options default.
5470 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005471 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005472job_alloc(void)
5473{
5474 job_T *job;
5475
5476 job = (job_T *)alloc_clear(sizeof(job_T));
5477 if (job != NULL)
5478 {
5479 job->jv_refcount = 1;
5480 job->jv_stoponexit = vim_strsave((char_u *)"term");
5481
5482 if (first_job != NULL)
5483 {
5484 first_job->jv_prev = job;
5485 job->jv_next = first_job;
5486 }
5487 first_job = job;
5488 }
5489 return job;
5490}
5491
5492 void
5493job_set_options(job_T *job, jobopt_T *opt)
5494{
5495 if (opt->jo_set & JO_STOPONEXIT)
5496 {
5497 vim_free(job->jv_stoponexit);
5498 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5499 job->jv_stoponexit = NULL;
5500 else
5501 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5502 }
5503 if (opt->jo_set & JO_EXIT_CB)
5504 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005505 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005506 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005507 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005508 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005509 job->jv_exit_partial = NULL;
5510 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005511 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005512 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005513 job->jv_exit_partial = opt->jo_exit_partial;
5514 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005515 {
5516 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005517 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005518 }
5519 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005520 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005521 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005522 func_ref(job->jv_exit_cb);
5523 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005524 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005525 }
5526}
5527
5528/*
5529 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5530 */
5531 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005532job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005533{
5534 job_T *job;
5535
5536 for (job = first_job; job != NULL; job = job->jv_next)
5537 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005538 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005539}
5540
5541/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005542 * Return TRUE when there is any job that has an exit callback and might exit,
5543 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005544 */
5545 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005546has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005547{
5548 job_T *job;
5549
5550 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005551 /* Only should check if the channel has been closed, if the channel is
5552 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005553 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5554 || (job->jv_status == JOB_FINISHED
5555 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005556 return TRUE;
5557 return FALSE;
5558}
5559
Bram Moolenaar97792de2016-10-15 18:36:49 +02005560#define MAX_CHECK_ENDED 8
5561
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005562/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005563 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005564 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005565 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005566 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005567job_check_ended(void)
5568{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005569 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005570 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005571
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005572 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005573 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005574 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005575
Bram Moolenaar97792de2016-10-15 18:36:49 +02005576 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005577 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005578 // NOTE: mch_detect_ended_job() must only return a job of which the
5579 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005580 job_T *job = mch_detect_ended_job(first_job);
5581
5582 if (job == NULL)
5583 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005584 did_end = TRUE;
5585 job_cleanup(job); // may free "job"
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005586 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005587
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005588 if (channel_need_redraw)
5589 {
5590 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005591 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005592 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005593 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005594}
5595
5596/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005597 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005598 * "argv_arg" is only for Unix.
5599 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005600 * The returned job has a refcount of one.
5601 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005602 */
5603 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005604job_start(
5605 typval_T *argvars,
5606 char **argv_arg,
5607 jobopt_T *opt_arg,
5608 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005609{
5610 job_T *job;
5611 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005612 char **argv = NULL;
5613 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005614#if defined(UNIX)
5615# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005616 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005617#else
5618 garray_T ga;
5619#endif
5620 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005621 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005622
5623 job = job_alloc();
5624 if (job == NULL)
5625 return NULL;
5626
5627 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005628#ifndef USE_ARGV
5629 ga_init2(&ga, (int)sizeof(char*), 20);
5630#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005631
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005632 if (opt_arg != NULL)
5633 opt = *opt_arg;
5634 else
5635 {
5636 /* Default mode is NL. */
5637 clear_job_options(&opt);
5638 opt.jo_mode = MODE_NL;
5639 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005640 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5641 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5642 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005643 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005644 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005645
5646 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005647 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005648 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5649 && opt.jo_io[part] == JIO_FILE
5650 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5651 || *opt.jo_io_name[part] == NUL))
5652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005653 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005654 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005655 }
5656
5657 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5658 {
5659 buf_T *buf = NULL;
5660
5661 /* check that we can find the buffer before starting the job */
5662 if (opt.jo_set & JO_IN_BUF)
5663 {
5664 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5665 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005666 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005667 }
5668 else if (!(opt.jo_set & JO_IN_NAME))
5669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005670 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005671 }
5672 else
5673 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5674 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005675 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005676 if (buf->b_ml.ml_mfp == NULL)
5677 {
5678 char_u numbuf[NUMBUFLEN];
5679 char_u *s;
5680
5681 if (opt.jo_set & JO_IN_BUF)
5682 {
5683 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5684 s = numbuf;
5685 }
5686 else
5687 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005688 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005689 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005690 }
5691 job->jv_in_buf = buf;
5692 }
5693
5694 job_set_options(job, &opt);
5695
Bram Moolenaar13568252018-03-16 20:46:58 +01005696#ifdef USE_ARGV
5697 if (argv_arg != NULL)
5698 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005699 /* Make a copy of argv_arg for job->jv_argv. */
5700 for (i = 0; argv_arg[i] != NULL; i++)
5701 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005702 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005703 if (argv == NULL)
5704 goto theend;
5705 for (i = 0; i < argc; i++)
5706 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5707 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005708 }
5709 else
5710#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005711 if (argvars[0].v_type == VAR_STRING)
5712 {
5713 /* Command is a string. */
5714 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005715 if (cmd == NULL || *cmd == NUL)
5716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005717 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005718 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005719 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005720
5721 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005722 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005723 }
5724 else if (argvars[0].v_type != VAR_LIST
5725 || argvars[0].vval.v_list == NULL
5726 || argvars[0].vval.v_list->lv_len < 1)
5727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005728 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005729 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005730 }
5731 else
5732 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005733 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005734
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005735 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005736 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005737#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005738 if (win32_build_cmd(l, &ga) == FAIL)
5739 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005740 cmd = ga.ga_data;
5741#endif
5742 }
5743
Bram Moolenaar20608922018-04-21 22:30:08 +02005744 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005745 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005746
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005747#ifdef USE_ARGV
5748 if (ch_log_active())
5749 {
5750 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005751
5752 ga_init2(&ga, (int)sizeof(char), 200);
5753 for (i = 0; i < argc; ++i)
5754 {
5755 if (i > 0)
5756 ga_concat(&ga, (char_u *)" ");
5757 ga_concat(&ga, (char_u *)argv[i]);
5758 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005759 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005760 ga_clear(&ga);
5761 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005762 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005763#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005764 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005765 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005766#endif
5767
5768 /* If the channel is reading from a buffer, write lines now. */
5769 if (job->jv_channel != NULL)
5770 channel_write_in(job->jv_channel);
5771
5772theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005773#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005774 vim_free(ga.ga_data);
5775#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005776 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005777 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005778 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005779 return job;
5780}
5781
5782/*
5783 * Get the status of "job" and invoke the exit callback when needed.
5784 * The returned string is not allocated.
5785 */
5786 char *
5787job_status(job_T *job)
5788{
5789 char *result;
5790
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005791 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005792 /* No need to check, dead is dead. */
5793 result = "dead";
5794 else if (job->jv_status == JOB_FAILED)
5795 result = "fail";
5796 else
5797 {
5798 result = mch_job_status(job);
5799 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005800 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005801 }
5802 return result;
5803}
5804
Bram Moolenaar8950a562016-03-12 15:22:55 +01005805/*
5806 * Implementation of job_info().
5807 */
5808 void
5809job_info(job_T *job, dict_T *dict)
5810{
5811 dictitem_T *item;
5812 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005813 list_T *l;
5814 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005815
Bram Moolenaare0be1672018-07-08 16:50:37 +02005816 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005817
5818 item = dictitem_alloc((char_u *)"channel");
5819 if (item == NULL)
5820 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005821 item->di_tv.v_type = VAR_CHANNEL;
5822 item->di_tv.vval.v_channel = job->jv_channel;
5823 if (job->jv_channel != NULL)
5824 ++job->jv_channel->ch_refcount;
5825 if (dict_add(dict, item) == FAIL)
5826 dictitem_free(item);
5827
5828#ifdef UNIX
5829 nr = job->jv_pid;
5830#else
5831 nr = job->jv_proc_info.dwProcessId;
5832#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005833 dict_add_number(dict, "process", nr);
5834 dict_add_string(dict, "tty_in", job->jv_tty_in);
5835 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005836
Bram Moolenaare0be1672018-07-08 16:50:37 +02005837 dict_add_number(dict, "exitval", job->jv_exitval);
5838 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5839 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005840
5841 l = list_alloc();
5842 if (l != NULL)
5843 {
5844 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005845 if (job->jv_argv != NULL)
5846 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005847 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005848 }
5849}
5850
5851/*
5852 * Implementation of job_info() to return info for all jobs.
5853 */
5854 void
5855job_info_all(list_T *l)
5856{
5857 job_T *job;
5858 typval_T tv;
5859
5860 for (job = first_job; job != NULL; job = job->jv_next)
5861 {
5862 tv.v_type = VAR_JOB;
5863 tv.vval.v_job = job;
5864
5865 if (list_append_tv(l, &tv) != OK)
5866 return;
5867 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005868}
5869
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005870/*
5871 * Send a signal to "job". Implements job_stop().
5872 * When "type" is not NULL use this for the type.
5873 * Otherwise use argvars[1] for the type.
5874 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005875 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005876job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005877{
5878 char_u *arg;
5879
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005880 if (type != NULL)
5881 arg = (char_u *)type;
5882 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005883 arg = (char_u *)"";
5884 else
5885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005886 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005887 if (arg == NULL)
5888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005889 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005890 return 0;
5891 }
5892 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005893 if (job->jv_status == JOB_FAILED)
5894 {
5895 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5896 return 0;
5897 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005898 if (job->jv_status == JOB_ENDED)
5899 {
5900 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5901 return 0;
5902 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005903 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005904 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005905 return 0;
5906
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005907 /* Assume that only "kill" will kill the job. */
5908 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005909 job->jv_channel->ch_job_killed = TRUE;
5910
5911 /* We don't try freeing the job, obviously the caller still has a
5912 * reference to it. */
5913 return 1;
5914}
5915
Bram Moolenaarf2732452018-06-03 14:47:35 +02005916 void
5917invoke_prompt_callback(void)
5918{
5919 typval_T rettv;
5920 int dummy;
5921 typval_T argv[2];
5922 char_u *text;
5923 char_u *prompt;
5924 linenr_T lnum = curbuf->b_ml.ml_line_count;
5925
5926 // Add a new line for the prompt before invoking the callback, so that
5927 // text can always be inserted above the last line.
5928 ml_append(lnum, (char_u *)"", 0, FALSE);
5929 curwin->w_cursor.lnum = lnum + 1;
5930 curwin->w_cursor.col = 0;
5931
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005932 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005933 return;
5934 text = ml_get(lnum);
5935 prompt = prompt_text();
5936 if (STRLEN(text) >= STRLEN(prompt))
5937 text += STRLEN(prompt);
5938 argv[0].v_type = VAR_STRING;
5939 argv[0].vval.v_string = vim_strsave(text);
5940 argv[1].v_type = VAR_UNKNOWN;
5941
5942 call_func(curbuf->b_prompt_callback,
5943 (int)STRLEN(curbuf->b_prompt_callback),
5944 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5945 curbuf->b_prompt_partial, NULL);
5946 clear_tv(&argv[0]);
5947 clear_tv(&rettv);
5948}
5949
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005950/*
5951 * Return TRUE when the interrupt callback was invoked.
5952 */
5953 int
5954invoke_prompt_interrupt(void)
5955{
5956 typval_T rettv;
5957 int dummy;
5958 typval_T argv[1];
5959
5960 if (curbuf->b_prompt_interrupt == NULL
5961 || *curbuf->b_prompt_interrupt == NUL)
5962 return FALSE;
5963 argv[0].v_type = VAR_UNKNOWN;
5964
5965 got_int = FALSE; // don't skip executing commands
5966 call_func(curbuf->b_prompt_interrupt,
5967 (int)STRLEN(curbuf->b_prompt_interrupt),
5968 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5969 curbuf->b_prompt_int_partial, NULL);
5970 clear_tv(&rettv);
5971 return TRUE;
5972}
5973
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005974#endif /* FEAT_JOB_CHANNEL */