blob: 892c09ab7e42c382fab9ffa7a7393f4edec6227f [file] [log] [blame]
Bram Moolenaare0874f82016-01-24 20:36:41 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
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
22/* Note: when making changes here also adjust configure.in. */
23#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 Moolenaar187db502016-02-27 14:44:26 +010057/* Whether a redraw is needed for appending a line to a buffer. */
58static int channel_need_redraw = FALSE;
59
60
Bram Moolenaard8070362016-02-15 21:56:54 +010061#ifdef WIN32
62 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010063fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010064{
65 HANDLE h = (HANDLE)fd;
66 DWORD nread;
67
68 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
69 return -1;
70 return (int)nread;
71}
72
73 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010074fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010075{
76 HANDLE h = (HANDLE)fd;
77 DWORD nwrite;
78
79 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
80 return -1;
81 return (int)nwrite;
82}
83
84 static void
85fd_close(sock_T fd)
86{
87 HANDLE h = (HANDLE)fd;
88
89 CloseHandle(h);
90}
91#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010092
Bram Moolenaar6463ca22016-02-13 17:04:46 +010093/* Log file opened with ch_logfile(). */
94static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010095#ifdef FEAT_RELTIME
96static proftime_T log_start;
97#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010098
99 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100100ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100101{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102 FILE *file = NULL;
103
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100104 if (log_fd != NULL)
105 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100106
107 if (*fname != NUL)
108 {
109 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
110 if (file == NULL)
111 {
112 EMSG2(_(e_notopen), fname);
113 return;
114 }
115 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100116 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100117
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100119 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121#ifdef FEAT_RELTIME
122 profile_start(&log_start);
123#endif
124 }
125}
126
127 int
128ch_log_active()
129{
130 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100131}
132
133 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100134ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135{
136 if (log_fd != NULL)
137 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100138#ifdef FEAT_RELTIME
139 proftime_T log_now;
140
141 profile_start(&log_now);
142 profile_sub(&log_now, &log_start);
143 fprintf(log_fd, "%s ", profile_msg(&log_now));
144#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100145 if (ch != NULL)
146 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100147 else
148 fprintf(log_fd, "%s: ", what);
149 }
150}
151
Bram Moolenaard0b65022016-03-06 21:50:33 +0100152static int did_log_msg = TRUE;
153
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100154 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100155ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100156{
157 if (log_fd != NULL)
158 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100159 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100163 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 }
165}
166
167 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100168ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169{
170 if (log_fd != NULL)
171 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100172 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100174 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100176 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 }
178}
179
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100180 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100181ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182{
183 if (log_fd != NULL)
184 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100189 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 }
191}
192
193 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100194ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195{
196 if (log_fd != NULL)
197 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100198 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100199 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100200 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100202 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 }
204}
205
206 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100207ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208{
209 if (log_fd != NULL)
210 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100211 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100212 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100213 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100215 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 }
217}
218
219 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100220ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221{
222 if (log_fd != NULL)
223 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100224 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100225 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100226 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100228 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 }
230}
231
232 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100233ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234{
235 if (log_fd != NULL)
236 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100237 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100238 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100239 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100241 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 }
243}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100244
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100245#ifdef _WIN32
246# undef PERROR
247# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
248 (char_u *)msg, (char_u *)strerror_win32(errno))
249
250 static char *
251strerror_win32(int eno)
252{
253 static LPVOID msgbuf = NULL;
254 char_u *ptr;
255
256 if (msgbuf)
257 LocalFree(msgbuf);
258 FormatMessage(
259 FORMAT_MESSAGE_ALLOCATE_BUFFER |
260 FORMAT_MESSAGE_FROM_SYSTEM |
261 FORMAT_MESSAGE_IGNORE_INSERTS,
262 NULL,
263 eno,
264 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
265 (LPTSTR) &msgbuf,
266 0,
267 NULL);
268 /* chomp \r or \n */
269 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
270 switch (*ptr)
271 {
272 case '\r':
273 STRMOVE(ptr, ptr + 1);
274 ptr--;
275 break;
276 case '\n':
277 if (*(ptr + 1) == '\0')
278 *ptr = '\0';
279 else
280 *ptr = ' ';
281 break;
282 }
283 return msgbuf;
284}
285#endif
286
Bram Moolenaar77073442016-02-13 23:23:53 +0100287/*
288 * The list of all allocated channels.
289 */
290static channel_T *first_channel = NULL;
291static int next_ch_id = 0;
292
293/*
294 * Allocate a new channel. The refcount is set to 1.
295 * The channel isn't actually used until it is opened.
296 * Returns NULL if out of memory.
297 */
298 channel_T *
299add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100300{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100301 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100302 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100303
Bram Moolenaar77073442016-02-13 23:23:53 +0100304 if (channel == NULL)
305 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100306
Bram Moolenaar77073442016-02-13 23:23:53 +0100307 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100308 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100309
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100310 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100311 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#endif
316#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100318#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100320 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100321
Bram Moolenaar77073442016-02-13 23:23:53 +0100322 if (first_channel != NULL)
323 {
324 first_channel->ch_prev = channel;
325 channel->ch_next = first_channel;
326 }
327 first_channel = channel;
328
329 channel->ch_refcount = 1;
330 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100331}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100332
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100334 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100335 * Return TRUE if "channel" has a callback and the associated job wasn't
336 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100337 */
338 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100339channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100340{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100341 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 int has_out_msg;
343 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344
345 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346 if (channel->ch_job_killed && channel->ch_job == NULL)
347 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348
349 /* If there is a close callback it may still need to be invoked. */
350 if (channel->ch_close_cb != NULL)
351 return TRUE;
352
353 /* If there is no callback then nobody can get readahead. If the fd is
354 * closed and there is no readahead then the callback won't be called. */
355 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
356 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
357 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
359 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
360 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
361 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
362 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
363 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100365 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100368}
369
370/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200371 * Close a channel and free all its resources.
372 */
373 static void
374channel_free_contents(channel_T *channel)
375{
376 channel_close(channel, TRUE);
377 channel_clear(channel);
378 ch_log(channel, "Freeing channel");
379}
380
381 static void
382channel_free_channel(channel_T *channel)
383{
384 if (channel->ch_next != NULL)
385 channel->ch_next->ch_prev = channel->ch_prev;
386 if (channel->ch_prev == NULL)
387 first_channel = channel->ch_next;
388 else
389 channel->ch_prev->ch_next = channel->ch_next;
390 vim_free(channel);
391}
392
393 static void
394channel_free(channel_T *channel)
395{
396 if (!in_free_unref_items)
397 {
398 channel_free_contents(channel);
399 channel_free_channel(channel);
400 }
401}
402
403/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100404 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100405 * possible, there is no callback to be invoked or the associated job was
406 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100407 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100408 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100409 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410channel_may_free(channel_T *channel)
411{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100412 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100413 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100414 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 return TRUE;
416 }
417 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100418}
419
420/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100421 * Decrement the reference count on "channel" and maybe free it when it goes
422 * down to zero. Don't free it if there is a pending action.
423 * Returns TRUE when the channel is no longer referenced.
424 */
425 int
426channel_unref(channel_T *channel)
427{
428 if (channel != NULL && --channel->ch_refcount <= 0)
429 return channel_may_free(channel);
430 return FALSE;
431}
432
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200433 int
434free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100435{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200436 int did_free = FALSE;
437 channel_T *ch;
438
439 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
440 if ((ch->ch_copyID & mask) != (copyID & mask))
441 {
442 /* Free the channel and ordinary items it contains, but don't
443 * recurse into Lists, Dictionaries etc. */
444 channel_free_contents(ch);
445 did_free = TRUE;
446 }
447 return did_free;
448}
449
450 void
451free_unused_channels(int copyID, int mask)
452{
453 channel_T *ch;
454 channel_T *ch_next;
455
456 for (ch = first_channel; ch != NULL; ch = ch_next)
457 {
458 ch_next = ch->ch_next;
459 if ((ch->ch_copyID & mask) != (copyID & mask))
460 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200461 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 channel_free_channel(ch);
463 }
464 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465}
466
Bram Moolenaard04a0202016-01-26 23:30:18 +0100467#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100468
469#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
470 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100471channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100472{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100473 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100474 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100475
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100476 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100477 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100478 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100479 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100480 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100481}
482#endif
483
Bram Moolenaare0874f82016-01-24 20:36:41 +0100484/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100485 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100486 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100487#ifdef FEAT_GUI_X11
488 static void
489messageFromNetbeans(XtPointer clientData,
490 int *unused1 UNUSED,
491 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100492{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100493 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100495#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496
Bram Moolenaard04a0202016-01-26 23:30:18 +0100497#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100498# if GTK_CHECK_VERSION(3,0,0)
499 static gboolean
500messageFromNetbeans(GIOChannel *unused1 UNUSED,
501 GIOCondition unused2 UNUSED,
502 gpointer clientData)
503{
504 channel_read_fd(GPOINTER_TO_INT(clientData));
505 return TRUE; /* Return FALSE instead in case the event source is to
506 * be removed after this function returns. */
507}
508# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100509 static void
510messageFromNetbeans(gpointer clientData,
511 gint unused1 UNUSED,
512 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100513{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100514 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515}
Bram Moolenaar98921892016-02-23 17:14:37 +0100516# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517#endif
518
519 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100520channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100521{
Bram Moolenaarde279892016-03-11 22:19:44 +0100522 if (!CH_HAS_GUI)
523 return;
524
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100525# ifdef FEAT_GUI_X11
526 /* Tell notifier we are interested in being called
527 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100528 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
529 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100530 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100531 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100532 (XtPointer)(XtInputReadMask + XtInputExceptMask),
533 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100534 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100535# else
536# ifdef FEAT_GUI_GTK
537 /* Tell gdk we are interested in being called when there
538 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100539 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100540# if GTK_CHECK_VERSION(3,0,0)
541 {
542 GIOChannel *chnnl = g_io_channel_unix_new(
543 (gint)channel->ch_part[part].ch_fd);
544
545 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
546 chnnl,
547 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
548 messageFromNetbeans,
549 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
550
551 g_io_channel_unref(chnnl);
552 }
553# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100554 channel->ch_part[part].ch_inputHandler = gdk_input_add(
555 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100556 (GdkInputCondition)
557 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100558 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100559 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100560# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100561# endif
562# endif
563}
564
Bram Moolenaarde279892016-03-11 22:19:44 +0100565 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100566channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100567{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 if (channel->CH_SOCK_FD != INVALID_FD)
569 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 if (channel->CH_OUT_FD != INVALID_FD)
571 channel_gui_register_one(channel, PART_OUT);
572 if (channel->CH_ERR_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100574}
575
576/*
577 * Register any of our file descriptors with the GUI event handling system.
578 * Called when the GUI has started.
579 */
580 void
581channel_gui_register_all(void)
582{
Bram Moolenaar77073442016-02-13 23:23:53 +0100583 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100584
Bram Moolenaar77073442016-02-13 23:23:53 +0100585 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100586 channel_gui_register(channel);
587}
588
589 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100590channel_gui_unregister_one(channel_T *channel, int part)
591{
592# ifdef FEAT_GUI_X11
593 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
594 {
595 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
596 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
597 }
598# else
599# ifdef FEAT_GUI_GTK
600 if (channel->ch_part[part].ch_inputHandler != 0)
601 {
602# if GTK_CHECK_VERSION(3,0,0)
603 g_source_remove(channel->ch_part[part].ch_inputHandler);
604# else
605 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
606# endif
607 channel->ch_part[part].ch_inputHandler = 0;
608 }
609# endif
610# endif
611}
612
613 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100614channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100615{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100616 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100617
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100619 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100620}
621
622#endif
623
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100624static char *e_cannot_connect = N_("E902: Cannot connect to port");
625
Bram Moolenaard04a0202016-01-26 23:30:18 +0100626/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100627 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100628 * "waittime" is the time in msec to wait for the connection.
629 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 * Returns the channel for success.
631 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100632 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100633 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100634channel_open(
635 char *hostname,
636 int port_in,
637 int waittime,
638 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100639{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100640 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100642 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100643#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100644 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100645 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646#else
647 int port = port_in;
648#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100649 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100650 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100651
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100652#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653 channel_init_winsock();
654#endif
655
Bram Moolenaar77073442016-02-13 23:23:53 +0100656 channel = add_channel();
657 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100659 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100660 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100661 }
662
663 /* Get the server internet address and put into addr structure */
664 /* fill in the socket address structure and connect to server */
665 vim_memset((char *)&server, 0, sizeof(server));
666 server.sin_family = AF_INET;
667 server.sin_port = htons(port);
668 if ((host = gethostbyname(hostname)) == NULL)
669 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100671 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100672 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100673 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674 }
675 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
676
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100677 /* On Mac and Solaris a zero timeout almost never works. At least wait
678 * one millisecond. Let's do it for all systems, because we don't know why
679 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 if (waittime == 0)
681 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682
683 /*
684 * For Unix we need to call connect() again after connect() failed.
685 * On Win32 one time is sufficient.
686 */
687 while (TRUE)
688 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100689 long elapsed_msec = 0;
690 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100691
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100692 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100694 sd = socket(AF_INET, SOCK_STREAM, 0);
695 if (sd == -1)
696 {
697 ch_error(channel, "in socket() in channel_open().");
698 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100699 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100700 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100701 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100702
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100703 if (waittime >= 0)
704 {
705 /* Make connect() non-blocking. */
706 if (
707#ifdef _WIN32
708 ioctlsocket(sd, FIONBIO, &val) < 0
709#else
710 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
711#endif
712 )
713 {
714 SOCK_ERRNO;
715 ch_errorn(channel,
716 "channel_open: Connect failed with errno %d", errno);
717 sock_close(sd);
718 channel_free(channel);
719 return NULL;
720 }
721 }
722
723 /* Try connecting to the server. */
724 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
725 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
726
Bram Moolenaar045a2842016-03-08 22:33:07 +0100727 if (ret == 0)
728 /* The connection could be established. */
729 break;
730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100732 if (waittime < 0 || (errno != EWOULDBLOCK
733 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100734#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100735 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100736#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100737 ))
738 {
739 ch_errorn(channel,
740 "channel_open: Connect failed with errno %d", errno);
741 PERROR(_(e_cannot_connect));
742 sock_close(sd);
743 channel_free(channel);
744 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100745 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100746
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100747 /* Limit the waittime to 50 msec. If it doesn't work within this
748 * time we close the socket and try creating it again. */
749 waitnow = waittime > 50 ? 50 : waittime;
750
Bram Moolenaar045a2842016-03-08 22:33:07 +0100751 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100752 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100753#ifndef WIN32
754 if (errno != ECONNREFUSED)
755#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 {
757 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100758 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100759 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100760#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100761 int so_error = 0;
762 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 struct timeval start_tv;
764 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100765#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100766 FD_ZERO(&rfds);
767 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768 FD_ZERO(&wfds);
769 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100770
Bram Moolenaar562ca712016-03-09 21:50:05 +0100771 tv.tv_sec = waitnow / 1000;
772 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100773#ifndef WIN32
774 gettimeofday(&start_tv, NULL);
775#endif
776 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100777 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100778 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100779
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100780 if (ret < 0)
781 {
782 SOCK_ERRNO;
783 ch_errorn(channel,
784 "channel_open: Connect failed with errno %d", errno);
785 PERROR(_(e_cannot_connect));
786 sock_close(sd);
787 channel_free(channel);
788 return NULL;
789 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100790
Bram Moolenaare081e212016-02-28 22:33:46 +0100791#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100792 /* On Win32: select() is expected to work and wait for up to
793 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100794 if (FD_ISSET(sd, &wfds))
795 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100796 elapsed_msec = waitnow;
797 if (waittime > 1 && elapsed_msec < waittime)
798 {
799 waittime -= elapsed_msec;
800 continue;
801 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100802#else
803 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 * After putting the socket in non-blocking mode, connect() will
805 * return EINPROGRESS, select() will not wait (as if writing is
806 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100807 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100809 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100810 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100811 {
812 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100813 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100814 if (ret < 0 || (so_error != 0
815 && so_error != EWOULDBLOCK
816 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100817# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100819# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100820 ))
821 {
822 ch_errorn(channel,
823 "channel_open: Connect failed with errno %d",
824 so_error);
825 PERROR(_(e_cannot_connect));
826 sock_close(sd);
827 channel_free(channel);
828 return NULL;
829 }
830 }
831
Bram Moolenaar045a2842016-03-08 22:33:07 +0100832 if (FD_ISSET(sd, &wfds) && so_error == 0)
833 /* Did not detect an error, connection is established. */
834 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100835
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 gettimeofday(&end_tv, NULL);
837 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
838 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100840 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100841
842#ifndef WIN32
843 if (waittime > 1 && elapsed_msec < waittime)
844 {
845 /* The port isn't ready but we also didn't get an error.
846 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100847 * yet. Select() may return early, wait until the remaining
848 * "waitnow" and try again. */
849 waitnow -= elapsed_msec;
850 waittime -= elapsed_msec;
851 if (waitnow > 0)
852 {
853 mch_delay((long)waitnow, TRUE);
854 ui_breakcheck();
855 waittime -= waitnow;
856 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100857 if (!got_int)
858 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100859 if (waittime <= 0)
860 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100861 waittime = 1;
862 continue;
863 }
864 /* we were interrupted, behave as if timed out */
865 }
866#endif
867
868 /* We timed out. */
869 ch_error(channel, "Connection timed out");
870 sock_close(sd);
871 channel_free(channel);
872 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100873 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100874
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875 ch_log(channel, "Connection made");
876
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100877 if (waittime >= 0)
878 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100879#ifdef _WIN32
880 val = 0;
881 ioctlsocket(sd, FIONBIO, &val);
882#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100883 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100884#endif
885 }
886
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100887 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100888 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100889 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
890 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100891
892#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100893 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100894#endif
895
Bram Moolenaar77073442016-02-13 23:23:53 +0100896 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100897}
898
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100899/*
900 * Implements ch_open().
901 */
902 channel_T *
903channel_open_func(typval_T *argvars)
904{
905 char_u *address;
906 char_u *p;
907 char *rest;
908 int port;
909 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200910 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100911
912 address = get_tv_string(&argvars[0]);
913 if (argvars[1].v_type != VAR_UNKNOWN
914 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
915 {
916 EMSG(_(e_invarg));
917 return NULL;
918 }
919
920 /* parse address */
921 p = vim_strchr(address, ':');
922 if (p == NULL)
923 {
924 EMSG2(_(e_invarg2), address);
925 return NULL;
926 }
927 *p++ = NUL;
928 port = strtol((char *)p, &rest, 10);
929 if (*address == NUL || port <= 0 || *rest != NUL)
930 {
931 p[-1] = ':';
932 EMSG2(_(e_invarg2), address);
933 return NULL;
934 }
935
936 /* parse options */
937 clear_job_options(&opt);
938 opt.jo_mode = MODE_JSON;
939 opt.jo_timeout = 2000;
940 if (get_job_options(&argvars[1], &opt,
941 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200942 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100943 if (opt.jo_timeout < 0)
944 {
945 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200946 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100947 }
948
949 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
950 if (channel != NULL)
951 {
952 opt.jo_set = JO_ALL;
953 channel_set_options(channel, &opt);
954 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200955theend:
956 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957 return channel;
958}
959
Bram Moolenaarde279892016-03-11 22:19:44 +0100960 static void
961may_close_part(sock_T *fd)
962{
963 if (*fd != INVALID_FD)
964 {
965 fd_close(*fd);
966 *fd = INVALID_FD;
967 }
968}
969
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100970 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100971channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100972{
Bram Moolenaarde279892016-03-11 22:19:44 +0100973 if (in != INVALID_FD)
974 {
975 may_close_part(&channel->CH_IN_FD);
976 channel->CH_IN_FD = in;
977 }
978 if (out != INVALID_FD)
979 {
980# if defined(FEAT_GUI)
981 channel_gui_unregister_one(channel, PART_OUT);
982# endif
983 may_close_part(&channel->CH_OUT_FD);
984 channel->CH_OUT_FD = out;
985# if defined(FEAT_GUI)
986 channel_gui_register_one(channel, PART_OUT);
987# endif
988 }
989 if (err != INVALID_FD)
990 {
991# if defined(FEAT_GUI)
992 channel_gui_unregister_one(channel, PART_ERR);
993# endif
994 may_close_part(&channel->CH_ERR_FD);
995 channel->CH_ERR_FD = err;
996# if defined(FEAT_GUI)
997 channel_gui_register_one(channel, PART_ERR);
998# endif
999 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001000}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001001
Bram Moolenaard6051b52016-02-28 15:49:03 +01001002/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001003 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001004 * This does not keep a refcount, when the job is freed ch_job is cleared.
1005 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001006 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001007channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001008{
Bram Moolenaar77073442016-02-13 23:23:53 +01001009 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001010
1011 channel_set_options(channel, options);
1012
1013 if (job->jv_in_buf != NULL)
1014 {
1015 chanpart_T *in_part = &channel->ch_part[PART_IN];
1016
1017 in_part->ch_buffer = job->jv_in_buf;
1018 ch_logs(channel, "reading from buffer '%s'",
1019 (char *)in_part->ch_buffer->b_ffname);
1020 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001021 {
1022 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1023 {
1024 /* Special mode: send last-but-one line when appending a line
1025 * to the buffer. */
1026 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001027 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001028 in_part->ch_buf_top =
1029 in_part->ch_buffer->b_ml.ml_line_count + 1;
1030 }
1031 else
1032 in_part->ch_buf_top = options->jo_in_top;
1033 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001034 else
1035 in_part->ch_buf_top = 1;
1036 if (options->jo_set & JO_IN_BOT)
1037 in_part->ch_buf_bot = options->jo_in_bot;
1038 else
1039 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1040 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001041}
1042
1043/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001044 * Find a buffer matching "name" or create a new one.
1045 */
1046 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001047find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001048{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001049 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001050 buf_T *save_curbuf = curbuf;
1051
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001052 if (name != NULL && *name != NUL)
1053 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001054 if (buf == NULL)
1055 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001056 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001057 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001058 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001059 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001060#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001061 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1062 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001063#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001064 if (curbuf->b_ml.ml_mfp == NULL)
1065 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001066 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1067 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001068 changed_bytes(1, 0);
1069 curbuf = save_curbuf;
1070 }
1071
1072 return buf;
1073}
1074
1075/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001076 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001077 */
1078 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001079channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001080{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001081 int part;
1082 char_u **cbp;
1083 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001084
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001085 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001086 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001087 channel->ch_part[part].ch_mode = opt->jo_mode;
1088 if (opt->jo_set & JO_IN_MODE)
1089 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1090 if (opt->jo_set & JO_OUT_MODE)
1091 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1092 if (opt->jo_set & JO_ERR_MODE)
1093 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001094
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001095 if (opt->jo_set & JO_TIMEOUT)
1096 for (part = PART_SOCK; part <= PART_IN; ++part)
1097 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1098 if (opt->jo_set & JO_OUT_TIMEOUT)
1099 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1100 if (opt->jo_set & JO_ERR_TIMEOUT)
1101 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001102 if (opt->jo_set & JO_BLOCK_WRITE)
1103 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001104
1105 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001106 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001107 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001108 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001109 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001110 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1112 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001113 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001115 *pp = opt->jo_partial;
1116 if (*pp != NULL)
1117 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 }
1119 if (opt->jo_set & JO_OUT_CALLBACK)
1120 {
1121 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001122 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001123 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001124 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001125 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1126 *cbp = vim_strsave(opt->jo_out_cb);
1127 else
1128 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001129 *pp = opt->jo_out_partial;
1130 if (*pp != NULL)
1131 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001132 }
1133 if (opt->jo_set & JO_ERR_CALLBACK)
1134 {
1135 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001136 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001137 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001138 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1140 *cbp = vim_strsave(opt->jo_err_cb);
1141 else
1142 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001143 *pp = opt->jo_err_partial;
1144 if (*pp != NULL)
1145 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001146 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001147 if (opt->jo_set & JO_CLOSE_CALLBACK)
1148 {
1149 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001150 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001151 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001152 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001153 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1154 *cbp = vim_strsave(opt->jo_close_cb);
1155 else
1156 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001157 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001158 if (*pp != NULL)
1159 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001160 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001161
1162 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1163 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001164 /* writing output to a buffer. Default mode is NL. */
1165 if (!(opt->jo_set & JO_OUT_MODE))
1166 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001167 if (opt->jo_set & JO_OUT_BUF)
1168 channel->ch_part[PART_OUT].ch_buffer =
1169 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1170 else
1171 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001172 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1173 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001174 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1175 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001176
1177 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1178 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1179 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1180 {
1181 /* writing err to a buffer. Default mode is NL. */
1182 if (!(opt->jo_set & JO_ERR_MODE))
1183 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1184 if (opt->jo_io[PART_ERR] == JIO_OUT)
1185 channel->ch_part[PART_ERR].ch_buffer =
1186 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001187 else if (opt->jo_set & JO_ERR_BUF)
1188 channel->ch_part[PART_ERR].ch_buffer =
1189 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001190 else
1191 channel->ch_part[PART_ERR].ch_buffer =
1192 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1193 ch_logs(channel, "writing err to buffer '%s'",
1194 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1195 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001196
1197 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1198 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1199 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001200}
1201
1202/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001203 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001204 */
1205 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001206channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001207 channel_T *channel,
1208 int part,
1209 char_u *callback,
1210 partial_T *partial,
1211 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001212{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001213 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001214 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1215
1216 if (item != NULL)
1217 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001218 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001219 item->cq_partial = partial;
1220 if (partial != NULL)
1221 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001222 item->cq_seq_nr = id;
1223 item->cq_prev = head->cq_prev;
1224 head->cq_prev = item;
1225 item->cq_next = NULL;
1226 if (item->cq_prev == NULL)
1227 head->cq_next = item;
1228 else
1229 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001230 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001231}
1232
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001233 static void
1234write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1235{
1236 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001237 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001238 char_u *p;
1239
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001240 if ((p = alloc(len + 2)) == NULL)
1241 return;
1242 STRCPY(p, line);
1243 p[len] = NL;
1244 p[len + 1] = NUL;
1245 channel_send(channel, PART_IN, p, "write_buf_line()");
1246 vim_free(p);
1247}
1248
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001249/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001250 * Return TRUE if "channel" can be written to.
1251 * Returns FALSE if the input is closed or the write would block.
1252 */
1253 static int
1254can_write_buf_line(channel_T *channel)
1255{
1256 chanpart_T *in_part = &channel->ch_part[PART_IN];
1257
1258 if (in_part->ch_fd == INVALID_FD)
1259 return FALSE; /* pipe was closed */
1260
1261 /* for testing: block every other attempt to write */
1262 if (in_part->ch_block_write == 1)
1263 in_part->ch_block_write = -1;
1264 else if (in_part->ch_block_write == -1)
1265 in_part->ch_block_write = 1;
1266
1267 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1268#ifndef WIN32
1269 {
1270# if defined(HAVE_SELECT)
1271 struct timeval tval;
1272 fd_set wfds;
1273 int ret;
1274
1275 FD_ZERO(&wfds);
1276 FD_SET((int)in_part->ch_fd, &wfds);
1277 tval.tv_sec = 0;
1278 tval.tv_usec = 0;
1279 for (;;)
1280 {
1281 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1282# ifdef EINTR
1283 SOCK_ERRNO;
1284 if (ret == -1 && errno == EINTR)
1285 continue;
1286# endif
1287 if (ret <= 0 || in_part->ch_block_write == 1)
1288 {
1289 if (ret > 0)
1290 ch_log(channel, "FAKED Input not ready for writing");
1291 else
1292 ch_log(channel, "Input not ready for writing");
1293 return FALSE;
1294 }
1295 break;
1296 }
1297# else
1298 struct pollfd fds;
1299
1300 fds.fd = in_part->ch_fd;
1301 fds.events = POLLOUT;
1302 if (poll(&fds, 1, 0) <= 0)
1303 {
1304 ch_log(channel, "Input not ready for writing");
1305 return FALSE;
1306 }
1307 if (in_part->ch_block_write == 1)
1308 {
1309 ch_log(channel, "FAKED Input not ready for writing");
1310 return FALSE;
1311 }
1312# endif
1313 }
1314#endif
1315 return TRUE;
1316}
1317
1318/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001319 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001320 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001321 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001322channel_write_in(channel_T *channel)
1323{
1324 chanpart_T *in_part = &channel->ch_part[PART_IN];
1325 linenr_T lnum;
1326 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001327 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001328
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001329 if (buf == NULL || in_part->ch_buf_append)
1330 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001331 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1332 {
1333 /* buffer was wiped out or unloaded */
1334 in_part->ch_buffer = NULL;
1335 return;
1336 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001337
1338 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1339 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1340 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001341 if (!can_write_buf_line(channel))
1342 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001343 write_buf_line(buf, lnum, channel);
1344 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001345 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001346
1347 if (written == 1)
1348 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1349 else if (written > 1)
1350 ch_logn(channel, "written %d lines to channel", written);
1351
Bram Moolenaar014069a2016-03-03 22:51:40 +01001352 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001353 if (lnum > buf->b_ml.ml_line_count)
1354 {
1355 /* Writing is done, no longer need the buffer. */
1356 in_part->ch_buffer = NULL;
1357 ch_log(channel, "Finished writing all lines to channel");
1358 }
1359 else
1360 ch_logn(channel, "Still %d more lines to write",
1361 buf->b_ml.ml_line_count - lnum + 1);
1362}
1363
1364/*
1365 * Write any lines waiting to be written to a channel.
1366 */
1367 void
1368channel_write_any_lines()
1369{
1370 channel_T *channel;
1371
1372 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1373 {
1374 chanpart_T *in_part = &channel->ch_part[PART_IN];
1375
1376 if (in_part->ch_buffer != NULL)
1377 {
1378 if (in_part->ch_buf_append)
1379 channel_write_new_lines(in_part->ch_buffer);
1380 else
1381 channel_write_in(channel);
1382 }
1383 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001384}
1385
1386/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001387 * Write appended lines above the last one in "buf" to the channel.
1388 */
1389 void
1390channel_write_new_lines(buf_T *buf)
1391{
1392 channel_T *channel;
1393 int found_one = FALSE;
1394
1395 /* There could be more than one channel for the buffer, loop over all of
1396 * them. */
1397 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1398 {
1399 chanpart_T *in_part = &channel->ch_part[PART_IN];
1400 linenr_T lnum;
1401 int written = 0;
1402
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001403 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001404 {
1405 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001406 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001407 found_one = TRUE;
1408 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1409 ++lnum)
1410 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001411 if (!can_write_buf_line(channel))
1412 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001413 write_buf_line(buf, lnum, channel);
1414 ++written;
1415 }
1416
1417 if (written == 1)
1418 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1419 else if (written > 1)
1420 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 if (lnum < buf->b_ml.ml_line_count)
1422 ch_logn(channel, "Still %d more lines to write",
1423 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001424
1425 in_part->ch_buf_bot = lnum;
1426 }
1427 }
1428 if (!found_one)
1429 buf->b_write_to_channel = FALSE;
1430}
1431
1432/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001433 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001434 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001435 */
1436 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001437invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1438 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001439{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001440 typval_T rettv;
1441 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001442
Bram Moolenaar77073442016-02-13 23:23:53 +01001443 argv[0].v_type = VAR_CHANNEL;
1444 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001445
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001446 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001447 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001448 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001449 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001450}
1451
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001452/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001453 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001454 * The caller must free it.
1455 * Returns NULL if there is nothing.
1456 */
1457 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001458channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001459{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001460 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001461 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001462 char_u *p;
1463
Bram Moolenaar77073442016-02-13 23:23:53 +01001464 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001465 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001467 p = node->rq_buffer;
1468 head->rq_next = node->rq_next;
1469 if (node->rq_next == NULL)
1470 head->rq_prev = NULL;
1471 else
1472 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001473 vim_free(node);
1474 return p;
1475}
1476
1477/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001478 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001479 */
1480 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001481channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001482{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001483 readq_T *head = &channel->ch_part[part].ch_head;
1484 readq_T *node = head->rq_next;
1485 long_u len = 1;
1486 char_u *res;
1487 char_u *p;
1488
1489 /* If there is only one buffer just get that one. */
1490 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1491 return channel_get(channel, part);
1492
1493 /* Concatenate everything into one buffer. */
1494 for (node = head->rq_next; node != NULL; node = node->rq_next)
1495 len += (long_u)STRLEN(node->rq_buffer);
1496 res = lalloc(len, TRUE);
1497 if (res == NULL)
1498 return NULL;
1499 *res = NUL;
1500 for (node = head->rq_next; node != NULL; node = node->rq_next)
1501 STRCAT(res, node->rq_buffer);
1502
1503 /* Free all buffers */
1504 do
1505 {
1506 p = channel_get(channel, part);
1507 vim_free(p);
1508 } while (p != NULL);
1509
1510 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001511}
1512
1513/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001514 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001515 * Returns FAIL if that is not possible.
1516 */
1517 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001518channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001519{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001520 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001521 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001522 char_u *p;
1523
Bram Moolenaar77073442016-02-13 23:23:53 +01001524 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001525 return FAIL;
1526
Bram Moolenaar77073442016-02-13 23:23:53 +01001527 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1528 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529 if (p == NULL)
1530 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 STRCPY(p, node->rq_buffer);
1532 STRCAT(p, node->rq_next->rq_buffer);
1533 vim_free(node->rq_next->rq_buffer);
1534 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001535
Bram Moolenaar77073442016-02-13 23:23:53 +01001536 /* dispose of the node and its buffer */
1537 head->rq_next = node->rq_next;
1538 head->rq_next->rq_prev = NULL;
1539 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540 vim_free(node);
1541 return OK;
1542}
1543
1544/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001545 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001546 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001547 * Returns OK or FAIL.
1548 */
1549 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001550channel_save(channel_T *channel, int part, char_u *buf, int len,
1551 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001552{
1553 readq_T *node;
1554 readq_T *head = &channel->ch_part[part].ch_head;
1555 char_u *p;
1556 int i;
1557
1558 node = (readq_T *)alloc(sizeof(readq_T));
1559 if (node == NULL)
1560 return FAIL; /* out of memory */
1561 node->rq_buffer = alloc(len + 1);
1562 if (node->rq_buffer == NULL)
1563 {
1564 vim_free(node);
1565 return FAIL; /* out of memory */
1566 }
1567
1568 if (channel->ch_part[part].ch_mode == MODE_NL)
1569 {
1570 /* Drop any CR before a NL. */
1571 p = node->rq_buffer;
1572 for (i = 0; i < len; ++i)
1573 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1574 *p++ = buf[i];
1575 *p = NUL;
1576 }
1577 else
1578 {
1579 mch_memmove(node->rq_buffer, buf, len);
1580 node->rq_buffer[len] = NUL;
1581 }
1582
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001583 if (prepend)
1584 {
1585 /* preend node to the head of the queue */
1586 node->rq_next = head->rq_next;
1587 node->rq_prev = NULL;
1588 if (head->rq_next == NULL)
1589 head->rq_prev = node;
1590 else
1591 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001593 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001595 {
1596 /* append node to the tail of the queue */
1597 node->rq_next = NULL;
1598 node->rq_prev = head->rq_prev;
1599 if (head->rq_prev == NULL)
1600 head->rq_next = node;
1601 else
1602 head->rq_prev->rq_next = node;
1603 head->rq_prev = node;
1604 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001605
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001606 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001608 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609 fprintf(log_fd, "'");
1610 if (fwrite(buf, len, 1, log_fd) != 1)
1611 return FAIL;
1612 fprintf(log_fd, "'\n");
1613 }
1614 return OK;
1615}
1616
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001617 static int
1618channel_fill(js_read_T *reader)
1619{
1620 channel_T *channel = (channel_T *)reader->js_cookie;
1621 int part = reader->js_cookie_arg;
1622 char_u *next = channel_get(channel, part);
1623 int unused;
1624 int len;
1625 char_u *p;
1626
1627 if (next == NULL)
1628 return FALSE;
1629
1630 unused = reader->js_end - reader->js_buf - reader->js_used;
1631 if (unused > 0)
1632 {
1633 /* Prepend unused text. */
1634 len = (int)STRLEN(next);
1635 p = alloc(unused + len + 1);
1636 if (p == NULL)
1637 {
1638 vim_free(next);
1639 return FALSE;
1640 }
1641 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1642 mch_memmove(p + unused, next, len + 1);
1643 vim_free(next);
1644 next = p;
1645 }
1646
1647 vim_free(reader->js_buf);
1648 reader->js_buf = next;
1649 reader->js_used = 0;
1650 return TRUE;
1651}
1652
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001653/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001654 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001655 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001656 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001657 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001658 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001659channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001660{
1661 js_read_T reader;
1662 typval_T listtv;
1663 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001664 chanpart_T *chanpart = &channel->ch_part[part];
1665 jsonq_T *head = &chanpart->ch_json_head;
1666 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001667 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001669 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001670 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001671
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001672 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001674 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001675 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001677
1678 /* When a message is incomplete we wait for a short while for more to
1679 * arrive. After the delay drop the input, otherwise a truncated string
1680 * or list will make us hang. */
1681 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001682 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001683 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001684 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001685 /* Only accept the response when it is a list with at least two
1686 * items. */
1687 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001688 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001689 if (listtv.v_type != VAR_LIST)
1690 ch_error(channel, "Did not receive a list, discarding");
1691 else
1692 ch_errorn(channel, "Expected list with two items, got %d",
1693 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001694 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001695 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 else
1697 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001698 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1699 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 else
1702 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001703 item->jq_value = alloc_tv();
1704 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001705 {
1706 vim_free(item);
1707 clear_tv(&listtv);
1708 }
1709 else
1710 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001711 *item->jq_value = listtv;
1712 item->jq_prev = head->jq_prev;
1713 head->jq_prev = item;
1714 item->jq_next = NULL;
1715 if (item->jq_prev == NULL)
1716 head->jq_next = item;
1717 else
1718 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001719 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001720 }
1721 }
1722 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001723
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001724 if (status == OK)
1725 chanpart->ch_waiting = FALSE;
1726 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001727 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001729 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001730 /* First time encountering incomplete message, set a deadline of
1731 * 100 msec. */
1732 ch_log(channel, "Incomplete message - wait for more");
1733 reader.js_used = 0;
1734 chanpart->ch_waiting = TRUE;
1735#ifdef WIN32
1736 chanpart->ch_deadline = GetTickCount() + 100L;
1737#else
1738 gettimeofday(&chanpart->ch_deadline, NULL);
1739 chanpart->ch_deadline.tv_usec += 100 * 1000;
1740 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1741 {
1742 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1743 ++chanpart->ch_deadline.tv_sec;
1744 }
1745#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001746 }
1747 else
1748 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001749 int timeout;
1750#ifdef WIN32
1751 timeout = GetTickCount() > chanpart->ch_deadline;
1752#else
1753 {
1754 struct timeval now_tv;
1755
1756 gettimeofday(&now_tv, NULL);
1757 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1758 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1759 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1760 }
1761#endif
1762 if (timeout)
1763 {
1764 status = FAIL;
1765 chanpart->ch_waiting = FALSE;
1766 }
1767 else
1768 {
1769 reader.js_used = 0;
1770 ch_log(channel, "still waiting on incomplete message");
1771 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001772 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001773 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001774
1775 if (status == FAIL)
1776 {
1777 ch_error(channel, "Decoding failed - discarding input");
1778 ret = FALSE;
1779 chanpart->ch_waiting = FALSE;
1780 }
1781 else if (reader.js_buf[reader.js_used] != NUL)
1782 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001783 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001784 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001785 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1786 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001787 ret = status == MAYBE ? FALSE: TRUE;
1788 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001789 else
1790 ret = FALSE;
1791
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001792 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794}
1795
1796/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001797 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001798 */
1799 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001800remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001801{
Bram Moolenaar77073442016-02-13 23:23:53 +01001802 if (node->cq_prev == NULL)
1803 head->cq_next = node->cq_next;
1804 else
1805 node->cq_prev->cq_next = node->cq_next;
1806 if (node->cq_next == NULL)
1807 head->cq_prev = node->cq_prev;
1808 else
1809 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001810}
1811
1812/*
1813 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001814 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001815 */
1816 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001817remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001818{
Bram Moolenaar77073442016-02-13 23:23:53 +01001819 if (node->jq_prev == NULL)
1820 head->jq_next = node->jq_next;
1821 else
1822 node->jq_prev->jq_next = node->jq_next;
1823 if (node->jq_next == NULL)
1824 head->jq_prev = node->jq_prev;
1825 else
1826 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001827 vim_free(node);
1828}
1829
1830/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001831 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001832 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001833 * When "id" is zero or negative jut get the first message. But not the one
1834 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835 * Return OK when found and return the value in "rettv".
1836 * Return FAIL otherwise.
1837 */
1838 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001839channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001840{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001842 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001843
Bram Moolenaar77073442016-02-13 23:23:53 +01001844 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001845 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847 typval_T *tv = &l->lv_first->li_tv;
1848
1849 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001850 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001851 || tv->vval.v_number == 0
1852 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001853 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001854 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001855 if (tv->v_type == VAR_NUMBER)
1856 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001857 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001858 return OK;
1859 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001860 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001861 }
1862 return FAIL;
1863}
1864
Bram Moolenaarece61b02016-02-20 21:39:05 +01001865#define CH_JSON_MAX_ARGS 4
1866
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001868 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001869 * "argv[0]" is the command string.
1870 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001871 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001872 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001873channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001874{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001875 char_u *cmd = argv[0].vval.v_string;
1876 char_u *arg;
1877 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001878
Bram Moolenaarece61b02016-02-20 21:39:05 +01001879 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001880 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001881 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 return;
1885 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001886 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001887 if (arg == NULL)
1888 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001889
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001890 if (STRCMP(cmd, "ex") == 0)
1891 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001892 int save_called_emsg = called_emsg;
1893
1894 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001895 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001896 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001897 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001898 --emsg_silent;
1899 if (called_emsg)
1900 ch_logs(channel, "Ex command error: '%s'",
1901 (char *)get_vim_var_str(VV_ERRMSG));
1902 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001903 }
1904 else if (STRCMP(cmd, "normal") == 0)
1905 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001906 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001907
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001908 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001909 ea.arg = arg;
1910 ea.addr_count = 0;
1911 ea.forceit = TRUE; /* no mapping */
1912 ex_normal(&ea);
1913 }
1914 else if (STRCMP(cmd, "redraw") == 0)
1915 {
1916 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001917
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001918 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001919 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001920 ex_redraw(&ea);
1921 showruler(FALSE);
1922 setcursor();
1923 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001924#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001925 if (gui.in_use)
1926 {
1927 gui_update_cursor(FALSE, FALSE);
1928 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001929 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001930#endif
1931 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001932 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001933 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001934 int is_call = cmd[0] == 'c';
1935 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001936
Bram Moolenaarece61b02016-02-20 21:39:05 +01001937 if (argv[id_idx].v_type != VAR_UNKNOWN
1938 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001939 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001940 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001941 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001942 EMSG("E904: last argument for expr/call must be a number");
1943 }
1944 else if (is_call && argv[2].v_type != VAR_LIST)
1945 {
1946 ch_error(channel, "third argument for call must be a list");
1947 if (p_verbose > 2)
1948 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001949 }
1950 else
1951 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001952 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001953 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001955 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001957 /* Don't pollute the display with errors. */
1958 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001959 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001960 {
1961 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001962 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001963 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001964 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001965 {
1966 ch_logs(channel, "Calling '%s'", (char *)arg);
1967 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1968 tv = &res_tv;
1969 else
1970 tv = NULL;
1971 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001972
1973 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001974 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001975 int id = argv[id_idx].vval.v_number;
1976
Bram Moolenaar55fab432016-02-07 16:53:13 +01001977 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001978 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001979 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 /* If evaluation failed or the result can't be encoded
1982 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001983 vim_free(json);
1984 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001985 err_tv.v_type = VAR_STRING;
1986 err_tv.vval.v_string = (char_u *)"ERROR";
1987 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001988 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001989 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001990 if (json != NULL)
1991 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001992 channel_send(channel,
1993 part == PART_SOCK ? PART_SOCK : PART_IN,
1994 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001995 vim_free(json);
1996 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001997 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001998 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001999 if (tv == &res_tv)
2000 clear_tv(tv);
2001 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002002 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002003 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002004 }
2005 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002006 {
2007 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002008 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002009 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002010}
2011
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002012/*
2013 * Invoke the callback at "cbhead".
2014 * Does not redraw but sets channel_need_redraw.
2015 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002016 static void
2017invoke_one_time_callback(
2018 channel_T *channel,
2019 cbq_T *cbhead,
2020 cbq_T *item,
2021 typval_T *argv)
2022{
2023 ch_logs(channel, "Invoking one-time callback %s",
2024 (char *)item->cq_callback);
2025 /* Remove the item from the list first, if the callback
2026 * invokes ch_close() the list will be cleared. */
2027 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002028 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002029 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002030 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002031 vim_free(item);
2032}
2033
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002034 static void
2035append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2036{
2037 buf_T *save_curbuf = curbuf;
2038 linenr_T lnum = buffer->b_ml.ml_line_count;
2039 int save_write_to = buffer->b_write_to_channel;
2040
2041 /* If the buffer is also used as input insert above the last
2042 * line. Don't write these lines. */
2043 if (save_write_to)
2044 {
2045 --lnum;
2046 buffer->b_write_to_channel = FALSE;
2047 }
2048
2049 /* Append to the buffer */
2050 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2051
2052 curbuf = buffer;
2053 u_sync(TRUE);
2054 /* ignore undo failure, undo is not very useful here */
2055 ignored = u_save(lnum, lnum + 1);
2056
2057 ml_append(lnum, msg, 0, FALSE);
2058 appended_lines_mark(lnum, 1L);
2059 curbuf = save_curbuf;
2060
2061 if (buffer->b_nwindows > 0)
2062 {
2063 win_T *wp;
2064 win_T *save_curwin;
2065
2066 FOR_ALL_WINDOWS(wp)
2067 {
2068 if (wp->w_buffer == buffer
2069 && (save_write_to
2070 ? wp->w_cursor.lnum == lnum + 1
2071 : (wp->w_cursor.lnum == lnum
2072 && wp->w_cursor.col == 0)))
2073 {
2074 ++wp->w_cursor.lnum;
2075 save_curwin = curwin;
2076 curwin = wp;
2077 curbuf = curwin->w_buffer;
2078 scroll_cursor_bot(0, FALSE);
2079 curwin = save_curwin;
2080 curbuf = curwin->w_buffer;
2081 }
2082 }
2083 redraw_buf_later(buffer, VALID);
2084 channel_need_redraw = TRUE;
2085 }
2086
2087 if (save_write_to)
2088 {
2089 channel_T *ch;
2090
2091 /* Find channels reading from this buffer and adjust their
2092 * next-to-read line number. */
2093 buffer->b_write_to_channel = TRUE;
2094 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2095 {
2096 chanpart_T *in_part = &ch->ch_part[PART_IN];
2097
2098 if (in_part->ch_buffer == buffer)
2099 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2100 }
2101 }
2102}
2103
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002104/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002105 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002106 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002107 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002108 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002109 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002110may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002111{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002112 char_u *msg = NULL;
2113 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002114 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002115 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002116 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002117 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002118 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002119 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002120 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002121 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002122
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002123 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002124 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002125 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002126
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002127 /* Use a message-specific callback, part callback or channel callback */
2128 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2129 if (cbitem->cq_seq_nr == 0)
2130 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002131 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002132 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002133 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002134 partial = cbitem->cq_partial;
2135 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002136 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002137 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002138 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002139 partial = channel->ch_part[part].ch_partial;
2140 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002141 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002142 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002143 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002144 partial = channel->ch_partial;
2145 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002146
Bram Moolenaar187db502016-02-27 14:44:26 +01002147 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002148 if (buffer != NULL && !buf_valid(buffer))
2149 {
2150 /* buffer was wiped out */
2151 channel->ch_part[part].ch_buffer = NULL;
2152 buffer = NULL;
2153 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002154
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002155 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002156 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002157 listitem_T *item;
2158 int argc = 0;
2159
Bram Moolenaard7ece102016-02-02 23:23:02 +01002160 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002161 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002162 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002163 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002164 channel_parse_json(channel, part);
2165 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002166 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002167 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002168
Bram Moolenaarece61b02016-02-20 21:39:05 +01002169 for (item = listtv->vval.v_list->lv_first;
2170 item != NULL && argc < CH_JSON_MAX_ARGS;
2171 item = item->li_next)
2172 argv[argc++] = item->li_tv;
2173 while (argc < CH_JSON_MAX_ARGS)
2174 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002175
Bram Moolenaarece61b02016-02-20 21:39:05 +01002176 if (argv[0].v_type == VAR_STRING)
2177 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002178 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002179 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002180 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002181 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002182 }
2183
Bram Moolenaarece61b02016-02-20 21:39:05 +01002184 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002185 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002186 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002187 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002188 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002189 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002190 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002191 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002192 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002193 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002194 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002195 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002196 return FALSE;
2197 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002198 else
2199 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002200 /* If there is no callback or buffer drop the message. */
2201 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002202 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002203 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002204 {
2205 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002206 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01002207 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002208 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002209 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002210
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002211 if (ch_mode == MODE_NL)
2212 {
2213 char_u *nl;
2214 char_u *buf;
2215
2216 /* See if we have a message ending in NL in the first buffer. If
2217 * not try to concatenate the first and the second buffer. */
2218 while (TRUE)
2219 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002220 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002221 nl = vim_strchr(buf, NL);
2222 if (nl != NULL)
2223 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002224 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002225 return FALSE; /* incomplete message */
2226 }
2227 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002228 {
2229 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002230 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002231 *nl = NUL;
2232 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002233 else
2234 {
2235 /* Copy the message into allocated memory and remove it from
2236 * the buffer. */
2237 msg = vim_strnsave(buf, (int)(nl - buf));
2238 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2239 }
2240 }
2241 else
2242 /* For a raw channel we don't know where the message ends, just
2243 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002244 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002245
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002246 if (msg == NULL)
2247 return FALSE; /* out of memory (and avoids Coverity warning) */
2248
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002249 argv[1].v_type = VAR_STRING;
2250 argv[1].vval.v_string = msg;
2251 }
2252
Bram Moolenaara07fec92016-02-05 21:04:08 +01002253 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002254 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002255 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002256
2257 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002258 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002259 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002260 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002261 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002262 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002263 break;
2264 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002265 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002266 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002267 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002268 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002269 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002270 if (buffer != NULL)
2271 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002272 if (msg == NULL)
2273 /* JSON or JS mode: re-encode the message. */
2274 msg = json_encode(listtv, ch_mode);
2275 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002276 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002277 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002278
Bram Moolenaar187db502016-02-27 14:44:26 +01002279 if (callback != NULL)
2280 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002281 if (cbitem != NULL)
2282 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2283 else
2284 {
2285 /* invoke the channel callback */
2286 ch_logs(channel, "Invoking channel callback %s",
2287 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002288 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002289 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002290 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002291 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002292 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002293 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002294
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002295 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002296 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002297 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002298
2299 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002300}
2301
2302/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002303 * Return TRUE when channel "channel" is open for writing to.
2304 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002305 */
2306 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002307channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002308{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002309 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002310 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002311}
2312
2313/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002314 * Return TRUE when channel "channel" is open for reading or writing.
2315 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002316 */
2317 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002318channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002319{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002320 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002321 || channel->CH_IN_FD != INVALID_FD
2322 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002323 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002324}
2325
2326/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002327 * Return a string indicating the status of the channel.
2328 */
2329 char *
2330channel_status(channel_T *channel)
2331{
2332 if (channel == NULL)
2333 return "fail";
2334 if (channel_is_open(channel))
2335 return "open";
2336 return "closed";
2337}
2338
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002339 static void
2340channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2341{
2342 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002343 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002344 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002345 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002346
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002347 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002348 STRCAT(namebuf, "_");
2349 tail = STRLEN(namebuf);
2350
2351 STRCPY(namebuf + tail, "status");
2352 dict_add_nr_str(dict, namebuf, 0,
2353 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2354
2355 STRCPY(namebuf + tail, "mode");
2356 switch (chanpart->ch_mode)
2357 {
2358 case MODE_NL: s = "NL"; break;
2359 case MODE_RAW: s = "RAW"; break;
2360 case MODE_JSON: s = "JSON"; break;
2361 case MODE_JS: s = "JS"; break;
2362 }
2363 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2364
2365 STRCPY(namebuf + tail, "io");
2366 if (part == PART_SOCK)
2367 s = "socket";
2368 else switch (chanpart->ch_io)
2369 {
2370 case JIO_NULL: s = "null"; break;
2371 case JIO_PIPE: s = "pipe"; break;
2372 case JIO_FILE: s = "file"; break;
2373 case JIO_BUFFER: s = "buffer"; break;
2374 case JIO_OUT: s = "out"; break;
2375 }
2376 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2377
2378 STRCPY(namebuf + tail, "timeout");
2379 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2380}
2381
2382 void
2383channel_info(channel_T *channel, dict_T *dict)
2384{
2385 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2386 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2387
2388 if (channel->ch_hostname != NULL)
2389 {
2390 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2391 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2392 channel_part_info(channel, dict, "sock", PART_SOCK);
2393 }
2394 else
2395 {
2396 channel_part_info(channel, dict, "out", PART_OUT);
2397 channel_part_info(channel, dict, "err", PART_ERR);
2398 channel_part_info(channel, dict, "in", PART_IN);
2399 }
2400}
2401
Bram Moolenaar77073442016-02-13 23:23:53 +01002402/*
2403 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002404 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002405 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002406 */
2407 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002408channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002409{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002410 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002411
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002412#ifdef FEAT_GUI
2413 channel_gui_unregister(channel);
2414#endif
2415
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002416 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002417 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002418 sock_close(channel->CH_SOCK_FD);
2419 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002420 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002421 may_close_part(&channel->CH_IN_FD);
2422 may_close_part(&channel->CH_OUT_FD);
2423 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002424
Bram Moolenaar8b374212016-02-24 20:43:06 +01002425 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002426 {
2427 typval_T argv[1];
2428 typval_T rettv;
2429 int dummy;
2430
2431 /* invoke the close callback; increment the refcount to avoid it
2432 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002433 ch_logs(channel, "Invoking close callback %s",
2434 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002435 argv[0].v_type = VAR_CHANNEL;
2436 argv[0].vval.v_channel = channel;
2437 ++channel->ch_refcount;
2438 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002439 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2440 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002441 clear_tv(&rettv);
2442 --channel->ch_refcount;
2443
2444 /* the callback is only called once */
2445 vim_free(channel->ch_close_cb);
2446 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002447 partial_unref(channel->ch_close_partial);
2448 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002449 }
2450
2451 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002452}
2453
Bram Moolenaard04a0202016-01-26 23:30:18 +01002454/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002455 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002456 * Returns NULL if there is nothing.
2457 */
2458 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002459channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002460{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002462
Bram Moolenaar77073442016-02-13 23:23:53 +01002463 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002464 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002465 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002466}
2467
2468/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002469 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002470 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471 static void
2472channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002473{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002474 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2475 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002476
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002477 while (channel_peek(channel, part) != NULL)
2478 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002479
2480 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002481 {
2482 cbq_T *node = cb_head->cq_next;
2483
2484 remove_cb_node(cb_head, node);
2485 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002486 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002487 vim_free(node);
2488 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002489
2490 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002491 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002492 free_tv(json_head->jq_next->jq_value);
2493 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002494 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002495
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002496 vim_free(channel->ch_part[part].ch_callback);
2497 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002498 partial_unref(channel->ch_part[part].ch_partial);
2499 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002500}
2501
2502/*
2503 * Clear all the read buffers on "channel".
2504 */
2505 void
2506channel_clear(channel_T *channel)
2507{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002508 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002509 vim_free(channel->ch_hostname);
2510 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002511 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002512 channel_clear_one(channel, PART_OUT);
2513 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002514 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002515 vim_free(channel->ch_callback);
2516 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002517 partial_unref(channel->ch_partial);
2518 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002519 vim_free(channel->ch_close_cb);
2520 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002521 partial_unref(channel->ch_close_partial);
2522 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002523}
2524
Bram Moolenaar77073442016-02-13 23:23:53 +01002525#if defined(EXITFREE) || defined(PROTO)
2526 void
2527channel_free_all(void)
2528{
2529 channel_T *channel;
2530
Bram Moolenaard6051b52016-02-28 15:49:03 +01002531 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002532 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2533 channel_clear(channel);
2534}
2535#endif
2536
2537
Bram Moolenaard04a0202016-01-26 23:30:18 +01002538/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002539#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002540
2541/* Buffer size for reading incoming messages. */
2542#define MAXMSGSIZE 4096
2543
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002544#if defined(HAVE_SELECT)
2545/*
2546 * Add write fds where we are waiting for writing to be possible.
2547 */
2548 static int
2549channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2550{
2551 int maxfd = maxfd_arg;
2552 channel_T *ch;
2553
2554 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2555 {
2556 chanpart_T *in_part = &ch->ch_part[PART_IN];
2557
2558 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2559 {
2560 FD_SET((int)in_part->ch_fd, wfds);
2561 if ((int)in_part->ch_fd >= maxfd)
2562 maxfd = (int)in_part->ch_fd + 1;
2563 }
2564 }
2565 return maxfd;
2566}
2567#else
2568/*
2569 * Add write fds where we are waiting for writing to be possible.
2570 */
2571 static int
2572channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2573{
2574 int nfd = nfd_in;
2575 channel_T *ch;
2576
2577 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2578 {
2579 chanpart_T *in_part = &ch->ch_part[PART_IN];
2580
2581 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2582 {
2583 in_part->ch_poll_idx = nfd;
2584 fds[nfd].fd = in_part->ch_fd;
2585 fds[nfd].events = POLLOUT;
2586 ++nfd;
2587 }
2588 else
2589 in_part->ch_poll_idx = -1;
2590 }
2591 return nfd;
2592}
2593#endif
2594
Bram Moolenaard04a0202016-01-26 23:30:18 +01002595/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002596 * Check for reading from "fd" with "timeout" msec.
2597 * Return FAIL when there is nothing to read.
2598 */
2599 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002600channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002601{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002602 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002603 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002604
Bram Moolenaard8070362016-02-15 21:56:54 +01002605# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002606 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002607 {
2608 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002609 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002610 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002611 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002612
2613 /* reading from a pipe, not a socket */
2614 while (TRUE)
2615 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002616 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2617 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002618 return OK;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002619
2620 /* perhaps write some buffer lines */
2621 channel_write_any_lines();
2622
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002623 sleep_time = deadline - GetTickCount();
2624 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002625 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002626 /* Wait for a little while. Very short at first, up to 10 msec
2627 * after looping a few times. */
2628 if (sleep_time > delay)
2629 sleep_time = delay;
2630 Sleep(sleep_time);
2631 delay = delay * 2;
2632 if (delay > 10)
2633 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002634 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002635 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002636 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002637#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002638 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002639#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002640 struct timeval tval;
2641 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002642 fd_set wfds;
2643 int ret;
2644 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002645
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002646 tval.tv_sec = timeout / 1000;
2647 tval.tv_usec = (timeout % 1000) * 1000;
2648 for (;;)
2649 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002650 FD_ZERO(&rfds);
2651 FD_SET((int)fd, &rfds);
2652
2653 /* Write lines to a pipe when a pipe can be written to. Need to
2654 * set this every time, some buffers may be done. */
2655 maxfd = (int)fd + 1;
2656 FD_ZERO(&wfds);
2657 maxfd = channel_fill_wfds(maxfd, &wfds);
2658
2659 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002660# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002661 SOCK_ERRNO;
2662 if (ret == -1 && errno == EINTR)
2663 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002664# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002665 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002666 {
2667 if (FD_ISSET(fd, &rfds))
2668 return OK;
2669 channel_write_any_lines();
2670 continue;
2671 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002672 break;
2673 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002674#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002675 for (;;)
2676 {
2677 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2678 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002679
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002680 fds[0].fd = fd;
2681 fds[0].events = POLLIN;
2682 nfd = channel_fill_poll_write(nfd, fds);
2683 if (poll(fds, nfd, timeout) > 0)
2684 {
2685 if (fds[0].revents & POLLIN)
2686 return OK;
2687 channel_write_any_lines();
2688 continue;
2689 }
2690 break;
2691 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002692#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002693 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002694 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002695}
2696
2697/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002698 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002699 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002700 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002701 */
2702 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002703channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002704{
2705 static char_u *buf = NULL;
2706 int len = 0;
2707 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002708 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002709 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002711 fd = channel->ch_part[part].ch_fd;
2712 if (fd == INVALID_FD)
2713 {
2714 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002715 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002716 }
2717 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002718
2719 /* Allocate a buffer to read into. */
2720 if (buf == NULL)
2721 {
2722 buf = alloc(MAXMSGSIZE);
2723 if (buf == NULL)
2724 return; /* out of memory! */
2725 }
2726
2727 /* Keep on reading for as long as there is something to read.
2728 * Use select() or poll() to avoid blocking on a message that is exactly
2729 * MAXMSGSIZE long. */
2730 for (;;)
2731 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002732 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002733 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002734 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002735 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002736 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002737 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002738 if (len <= 0)
2739 break; /* error or nothing more to read */
2740
2741 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002742 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002743 readlen += len;
2744 if (len < MAXMSGSIZE)
2745 break; /* did read everything that's available */
2746 }
2747
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002748 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002749 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002750 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002751 /* Do not give an error message, most likely the other end just
2752 * exited. */
2753 ch_errors(channel, "%s(): Cannot read from channel", func);
2754
Bram Moolenaard04a0202016-01-26 23:30:18 +01002755 /* Queue a "DETACH" netbeans message in the command queue in order to
2756 * terminate the netbeans session later. Do not end the session here
2757 * directly as we may be running in the context of a call to
2758 * netbeans_parse_messages():
2759 * netbeans_parse_messages
2760 * -> autocmd triggered while processing the netbeans cmd
2761 * -> ui_breakcheck
2762 * -> gui event loop or select loop
2763 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002764 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002765 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002766 if (channel->ch_part[part].ch_mode == MODE_RAW
2767 || channel->ch_part[part].ch_mode == MODE_NL)
2768 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002769 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002770
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02002771 /* When reading from stdout is not possible, assume the other side has
2772 * died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002773 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002774 if (channel->ch_nb_close_cb != NULL)
2775 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002776 }
2777
2778#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002779 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002780 if (CH_HAS_GUI && gtk_main_level() > 0)
2781 gtk_main_quit();
2782#endif
2783}
2784
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002785/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002786 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002787 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002788 * Returns what was read in allocated memory.
2789 * Returns NULL in case of error or timeout.
2790 */
2791 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002792channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002793{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002794 char_u *buf;
2795 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002796 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002797 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002798 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002799
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002800 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002801 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002802
2803 while (TRUE)
2804 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002805 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002806 if (buf != NULL && (mode == MODE_RAW
2807 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2808 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002809 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002810 continue;
2811
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002812 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002813 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002814 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002815 if (channel_wait(channel, fd, timeout) == FAIL)
2816 {
2817 ch_log(channel, "Timed out");
2818 return NULL;
2819 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002820 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002821 }
2822
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002823 if (mode == MODE_RAW)
2824 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002825 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002826 }
2827 else
2828 {
2829 nl = vim_strchr(buf, NL);
2830 if (nl[1] == NUL)
2831 {
2832 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002833 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002834 *nl = NUL;
2835 }
2836 else
2837 {
2838 /* Copy the message into allocated memory and remove it from the
2839 * buffer. */
2840 msg = vim_strnsave(buf, (int)(nl - buf));
2841 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2842 }
2843 }
2844 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002845 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002846 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002847}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002848
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002849/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002850 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002851 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002852 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002853 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002854 */
2855 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002856channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002857 channel_T *channel,
2858 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002859 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002860 int id,
2861 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002862{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002863 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002864 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002865 int timeout;
2866 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002867
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002868 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002869 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002870 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002871 for (;;)
2872 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002873 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002874
2875 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002876 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002877 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002878 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002879 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002880 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002881
Bram Moolenaard7ece102016-02-02 23:23:02 +01002882 if (!more)
2883 {
2884 /* Handle any other messages in the queue. If done some more
2885 * messages may have arrived. */
2886 if (channel_parse_messages())
2887 continue;
2888
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002889 /* Wait for up to the timeout. If there was an incomplete message
2890 * use the deadline for that. */
2891 timeout = timeout_arg;
2892 if (chanpart->ch_waiting)
2893 {
2894#ifdef WIN32
2895 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2896#else
2897 {
2898 struct timeval now_tv;
2899
2900 gettimeofday(&now_tv, NULL);
2901 timeout = (chanpart->ch_deadline.tv_sec
2902 - now_tv.tv_sec) * 1000
2903 + (chanpart->ch_deadline.tv_usec
2904 - now_tv.tv_usec) / 1000
2905 + 1;
2906 }
2907#endif
2908 if (timeout < 0)
2909 {
2910 /* Something went wrong, channel_parse_json() didn't
2911 * discard message. Cancel waiting. */
2912 chanpart->ch_waiting = FALSE;
2913 timeout = timeout_arg;
2914 }
2915 else if (timeout > timeout_arg)
2916 timeout = timeout_arg;
2917 }
2918 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002919 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002920 {
2921 if (timeout == timeout_arg)
2922 {
2923 if (fd != INVALID_FD)
2924 ch_log(channel, "Timed out");
2925 break;
2926 }
2927 }
2928 else
2929 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002930 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002931 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002932 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002933 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002934}
2935
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002936/*
2937 * Common for ch_read() and ch_readraw().
2938 */
2939 void
2940common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2941{
2942 channel_T *channel;
2943 int part;
2944 jobopt_T opt;
2945 int mode;
2946 int timeout;
2947 int id = -1;
2948 typval_T *listtv = NULL;
2949
2950 /* return an empty string by default */
2951 rettv->v_type = VAR_STRING;
2952 rettv->vval.v_string = NULL;
2953
2954 clear_job_options(&opt);
2955 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2956 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002957 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002958
2959 channel = get_channel_arg(&argvars[0], TRUE);
2960 if (channel != NULL)
2961 {
2962 if (opt.jo_set & JO_PART)
2963 part = opt.jo_part;
2964 else
2965 part = channel_part_read(channel);
2966 mode = channel_get_mode(channel, part);
2967 timeout = channel_get_timeout(channel, part);
2968 if (opt.jo_set & JO_TIMEOUT)
2969 timeout = opt.jo_timeout;
2970
2971 if (raw || mode == MODE_RAW || mode == MODE_NL)
2972 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2973 else
2974 {
2975 if (opt.jo_set & JO_ID)
2976 id = opt.jo_id;
2977 channel_read_json_block(channel, part, timeout, id, &listtv);
2978 if (listtv != NULL)
2979 {
2980 *rettv = *listtv;
2981 vim_free(listtv);
2982 }
2983 else
2984 {
2985 rettv->v_type = VAR_SPECIAL;
2986 rettv->vval.v_number = VVAL_NONE;
2987 }
2988 }
2989 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002990
2991theend:
2992 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002993}
2994
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002995# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2996 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002997/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002998 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002999 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003000 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003001 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003002channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003003{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003004 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003005 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003006
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003007 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003008 for (channel = first_channel; channel != NULL;
3009 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003010 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003011 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003012 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003013 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003014 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003015 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003016 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003017 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003018 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003019}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003020# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003021
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003022# if defined(WIN32) || defined(PROTO)
3023/*
3024 * Check the channels for anything that is ready to be read.
3025 * The data is put in the read queue.
3026 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003027 void
3028channel_handle_events(void)
3029{
3030 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003031 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003032 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003033
3034 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3035 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003036 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003037 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003038 {
3039 fd = channel->ch_part[part].ch_fd;
3040 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
3041 channel_read(channel, part, "channel_handle_events");
3042 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003043 }
3044}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003045# endif
3046
Bram Moolenaard04a0202016-01-26 23:30:18 +01003047/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003048 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003049 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003050 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003051 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003052 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003053channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003055 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003056 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003057 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003058
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003059 fd = channel->ch_part[part].ch_fd;
3060 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003061 {
3062 if (!channel->ch_error && fun != NULL)
3063 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003064 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003065 EMSG2("E630: %s(): write while not connected", fun);
3066 }
3067 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003068 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003069 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003070
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003071 if (log_fd != NULL)
3072 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003073 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003074 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003075 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003076 fprintf(log_fd, "'\n");
3077 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003078 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003079 }
3080
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003081 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003082 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003083 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003084 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003085 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003086 {
3087 if (!channel->ch_error && fun != NULL)
3088 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003089 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090 EMSG2("E631: %s(): write failed", fun);
3091 }
3092 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003093 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003094 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003095
3096 channel->ch_error = FALSE;
3097 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003098}
3099
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003100/*
3101 * Common for "ch_sendexpr()" and "ch_sendraw()".
3102 * Returns the channel if the caller should read the response.
3103 * Sets "part_read" to the the read fd.
3104 * Otherwise returns NULL.
3105 */
3106 channel_T *
3107send_common(
3108 typval_T *argvars,
3109 char_u *text,
3110 int id,
3111 int eval,
3112 jobopt_T *opt,
3113 char *fun,
3114 int *part_read)
3115{
3116 channel_T *channel;
3117 int part_send;
3118
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003119 clear_job_options(opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003120 channel = get_channel_arg(&argvars[0], TRUE);
3121 if (channel == NULL)
3122 return NULL;
3123 part_send = channel_part_send(channel);
3124 *part_read = channel_part_read(channel);
3125
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003126 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3127 return NULL;
3128
3129 /* Set the callback. An empty callback means no callback and not reading
3130 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3131 * allowed. */
3132 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3133 {
3134 if (eval)
3135 {
3136 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3137 return NULL;
3138 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003139 channel_set_req_callback(channel, part_send,
3140 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003141 }
3142
3143 if (channel_send(channel, part_send, text, fun) == OK
3144 && opt->jo_callback == NULL)
3145 return channel;
3146 return NULL;
3147}
3148
3149/*
3150 * common for "ch_evalexpr()" and "ch_sendexpr()"
3151 */
3152 void
3153ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3154{
3155 char_u *text;
3156 typval_T *listtv;
3157 channel_T *channel;
3158 int id;
3159 ch_mode_T ch_mode;
3160 int part_send;
3161 int part_read;
3162 jobopt_T opt;
3163 int timeout;
3164
3165 /* return an empty string by default */
3166 rettv->v_type = VAR_STRING;
3167 rettv->vval.v_string = NULL;
3168
3169 channel = get_channel_arg(&argvars[0], TRUE);
3170 if (channel == NULL)
3171 return;
3172 part_send = channel_part_send(channel);
3173
3174 ch_mode = channel_get_mode(channel, part_send);
3175 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3176 {
3177 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3178 return;
3179 }
3180
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003181 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003182 text = json_encode_nr_expr(id, &argvars[1],
3183 ch_mode == MODE_JS ? JSON_JS : 0);
3184 if (text == NULL)
3185 return;
3186
3187 channel = send_common(argvars, text, id, eval, &opt,
3188 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3189 vim_free(text);
3190 if (channel != NULL && eval)
3191 {
3192 if (opt.jo_set & JO_TIMEOUT)
3193 timeout = opt.jo_timeout;
3194 else
3195 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003196 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3197 == OK)
3198 {
3199 list_T *list = listtv->vval.v_list;
3200
3201 /* Move the item from the list and then change the type to
3202 * avoid the value being freed. */
3203 *rettv = list->lv_last->li_tv;
3204 list->lv_last->li_tv.v_type = VAR_NUMBER;
3205 free_tv(listtv);
3206 }
3207 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003208 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003209}
3210
3211/*
3212 * common for "ch_evalraw()" and "ch_sendraw()"
3213 */
3214 void
3215ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3216{
3217 char_u buf[NUMBUFLEN];
3218 char_u *text;
3219 channel_T *channel;
3220 int part_read;
3221 jobopt_T opt;
3222 int timeout;
3223
3224 /* return an empty string by default */
3225 rettv->v_type = VAR_STRING;
3226 rettv->vval.v_string = NULL;
3227
3228 text = get_tv_string_buf(&argvars[1], buf);
3229 channel = send_common(argvars, text, 0, eval, &opt,
3230 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3231 if (channel != NULL && eval)
3232 {
3233 if (opt.jo_set & JO_TIMEOUT)
3234 timeout = opt.jo_timeout;
3235 else
3236 timeout = channel_get_timeout(channel, part_read);
3237 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3238 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003239 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003240}
3241
Bram Moolenaard04a0202016-01-26 23:30:18 +01003242# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003243/*
3244 * Add open channels to the poll struct.
3245 * Return the adjusted struct index.
3246 * The type of "fds" is hidden to avoid problems with the function proto.
3247 */
3248 int
3249channel_poll_setup(int nfd_in, void *fds_in)
3250{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003251 int nfd = nfd_in;
3252 channel_T *channel;
3253 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003254 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003255
Bram Moolenaar77073442016-02-13 23:23:53 +01003256 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003257 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003258 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003259 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003260 chanpart_T *ch_part = &channel->ch_part[part];
3261
3262 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003263 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003264 ch_part->ch_poll_idx = nfd;
3265 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003266 fds[nfd].events = POLLIN;
3267 nfd++;
3268 }
3269 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003270 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003271 }
3272 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003273
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003274 nfd = channel_fill_poll_write(nfd, fds);
3275
Bram Moolenaare0874f82016-01-24 20:36:41 +01003276 return nfd;
3277}
3278
3279/*
3280 * The type of "fds" is hidden to avoid problems with the function proto.
3281 */
3282 int
3283channel_poll_check(int ret_in, void *fds_in)
3284{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003285 int ret = ret_in;
3286 channel_T *channel;
3287 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003288 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003289 int idx;
3290 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003291
Bram Moolenaar77073442016-02-13 23:23:53 +01003292 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003293 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003294 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003295 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003296 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003297
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003298 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003299 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003300 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003301 --ret;
3302 }
3303 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003304
3305 in_part = &channel->ch_part[PART_IN];
3306 idx = in_part->ch_poll_idx;
3307 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3308 {
3309 if (in_part->ch_buf_append)
3310 {
3311 if (in_part->ch_buffer != NULL)
3312 channel_write_new_lines(in_part->ch_buffer);
3313 }
3314 else
3315 channel_write_in(channel);
3316 --ret;
3317 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003318 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003319
3320 return ret;
3321}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003322# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003323
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003324# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003325/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003326 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003327 */
3328 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003329channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003330{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003331 int maxfd = maxfd_in;
3332 channel_T *channel;
3333 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003334 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003335 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003336
Bram Moolenaar77073442016-02-13 23:23:53 +01003337 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003338 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003339 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003340 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003341 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003342
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003343 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003344 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003345 FD_SET((int)fd, rfds);
3346 if (maxfd < (int)fd)
3347 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003348 }
3349 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003350 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003351
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003352 maxfd = channel_fill_wfds(maxfd, wfds);
3353
Bram Moolenaare0874f82016-01-24 20:36:41 +01003354 return maxfd;
3355}
3356
3357/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003358 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003359 */
3360 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003361channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003362{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003363 int ret = ret_in;
3364 channel_T *channel;
3365 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003366 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003367 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003368 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003369
Bram Moolenaar77073442016-02-13 23:23:53 +01003370 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003371 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003372 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003373 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003374 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003375
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003376 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003377 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003378 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003379 --ret;
3380 }
3381 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003382
3383 in_part = &channel->ch_part[PART_IN];
3384 if (ret > 0 && in_part->ch_fd != INVALID_FD
3385 && FD_ISSET(in_part->ch_fd, wfds))
3386 {
3387 if (in_part->ch_buf_append)
3388 {
3389 if (in_part->ch_buffer != NULL)
3390 channel_write_new_lines(in_part->ch_buffer);
3391 }
3392 else
3393 channel_write_in(channel);
3394 --ret;
3395 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003396 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003397
3398 return ret;
3399}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003400# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003401
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003402/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003403 * Return TRUE if "channel" has JSON or other typeahead.
3404 */
3405 static int
3406channel_has_readahead(channel_T *channel, int part)
3407{
3408 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3409
3410 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3411 {
3412 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3413 jsonq_T *item = head->jq_next;
3414
3415 return item != NULL;
3416 }
3417 return channel_peek(channel, part) != NULL;
3418}
3419
3420/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003421 * Execute queued up commands.
3422 * Invoked from the main loop when it's safe to execute received commands.
3423 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003424 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003425 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003426channel_parse_messages(void)
3427{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003428 channel_T *channel = first_channel;
3429 int ret = FALSE;
3430 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003431 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003432
Bram Moolenaard0b65022016-03-06 21:50:33 +01003433 /* Only do this message when another message was given, otherwise we get
3434 * lots of them. */
3435 if (did_log_msg)
3436 {
3437 ch_log(NULL, "looking for messages on channels");
3438 did_log_msg = FALSE;
3439 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003440 while (channel != NULL)
3441 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003442 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003443 {
3444 /* channel is no longer useful, free it */
3445 channel_free(channel);
3446 channel = first_channel;
3447 part = PART_SOCK;
3448 continue;
3449 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003450 if (channel->ch_part[part].ch_fd != INVALID_FD
3451 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003452 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003453 /* Increase the refcount, in case the handler causes the channel
3454 * to be unreferenced or closed. */
3455 ++channel->ch_refcount;
3456 r = may_invoke_callback(channel, part);
3457 if (r == OK)
3458 ret = TRUE;
3459 if (channel_unref(channel) || r == OK)
3460 {
3461 /* channel was freed or something was done, start over */
3462 channel = first_channel;
3463 part = PART_SOCK;
3464 continue;
3465 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003466 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003467 if (part < PART_ERR)
3468 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003469 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003470 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003471 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003472 part = PART_SOCK;
3473 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003474 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003475
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003476 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003477 {
3478 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003479 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003480 }
3481
Bram Moolenaard7ece102016-02-02 23:23:02 +01003482 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003483}
3484
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003485/*
3486 * Mark references to lists used in channels.
3487 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003488 int
3489set_ref_in_channel(int copyID)
3490{
Bram Moolenaar77073442016-02-13 23:23:53 +01003491 int abort = FALSE;
3492 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003493 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003494
Bram Moolenaar77073442016-02-13 23:23:53 +01003495 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003496 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003497 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003498 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003499 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3500 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003501
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003503 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003504 list_T *l = item->jq_value->vval.v_list;
3505
3506 if (l->lv_copyID != copyID)
3507 {
3508 l->lv_copyID = copyID;
3509 abort = abort || set_ref_in_list(l, copyID, NULL);
3510 }
3511 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003512 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003513 }
3514 }
3515 return abort;
3516}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003517
3518/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003519 * Return the "part" to write to for "channel".
3520 */
3521 int
3522channel_part_send(channel_T *channel)
3523{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003524 if (channel->CH_SOCK_FD == INVALID_FD)
3525 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003526 return PART_SOCK;
3527}
3528
3529/*
3530 * Return the default "part" to read from for "channel".
3531 */
3532 int
3533channel_part_read(channel_T *channel)
3534{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003535 if (channel->CH_SOCK_FD == INVALID_FD)
3536 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 return PART_SOCK;
3538}
3539
3540/*
3541 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003542 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003543 */
3544 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003545channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003546{
Bram Moolenaar77073442016-02-13 23:23:53 +01003547 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003548 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003549 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003550}
3551
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003552/*
3553 * Return the timeout of "channel"/"part"
3554 */
3555 int
3556channel_get_timeout(channel_T *channel, int part)
3557{
3558 return channel->ch_part[part].ch_timeout;
3559}
3560
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003561 static int
3562handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3563{
3564 char_u *val = get_tv_string(item);
3565
3566 opt->jo_set |= jo;
3567 if (STRCMP(val, "nl") == 0)
3568 *modep = MODE_NL;
3569 else if (STRCMP(val, "raw") == 0)
3570 *modep = MODE_RAW;
3571 else if (STRCMP(val, "js") == 0)
3572 *modep = MODE_JS;
3573 else if (STRCMP(val, "json") == 0)
3574 *modep = MODE_JSON;
3575 else
3576 {
3577 EMSG2(_(e_invarg2), val);
3578 return FAIL;
3579 }
3580 return OK;
3581}
3582
3583 static int
3584handle_io(typval_T *item, int part, jobopt_T *opt)
3585{
3586 char_u *val = get_tv_string(item);
3587
3588 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3589 if (STRCMP(val, "null") == 0)
3590 opt->jo_io[part] = JIO_NULL;
3591 else if (STRCMP(val, "pipe") == 0)
3592 opt->jo_io[part] = JIO_PIPE;
3593 else if (STRCMP(val, "file") == 0)
3594 opt->jo_io[part] = JIO_FILE;
3595 else if (STRCMP(val, "buffer") == 0)
3596 opt->jo_io[part] = JIO_BUFFER;
3597 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3598 opt->jo_io[part] = JIO_OUT;
3599 else
3600 {
3601 EMSG2(_(e_invarg2), val);
3602 return FAIL;
3603 }
3604 return OK;
3605}
3606
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003607/*
3608 * Clear a jobopt_T before using it.
3609 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003610 void
3611clear_job_options(jobopt_T *opt)
3612{
3613 vim_memset(opt, 0, sizeof(jobopt_T));
3614}
3615
3616/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003617 * Free any members of a jobopt_T.
3618 */
3619 void
3620free_job_options(jobopt_T *opt)
3621{
3622 if (opt->jo_partial != NULL)
3623 partial_unref(opt->jo_partial);
3624 if (opt->jo_out_partial != NULL)
3625 partial_unref(opt->jo_out_partial);
3626 if (opt->jo_err_partial != NULL)
3627 partial_unref(opt->jo_err_partial);
3628 if (opt->jo_close_partial != NULL)
3629 partial_unref(opt->jo_close_partial);
3630}
3631
3632/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003633 * Get the PART_ number from the first character of an option name.
3634 */
3635 static int
3636part_from_char(int c)
3637{
3638 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3639}
3640
3641/*
3642 * Get the option entries from the dict in "tv", parse them and put the result
3643 * in "opt".
3644 * Only accept options in "supported".
3645 * If an option value is invalid return FAIL.
3646 */
3647 int
3648get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3649{
3650 typval_T *item;
3651 char_u *val;
3652 dict_T *dict;
3653 int todo;
3654 hashitem_T *hi;
3655 int part;
3656
3657 opt->jo_set = 0;
3658 if (tv->v_type == VAR_UNKNOWN)
3659 return OK;
3660 if (tv->v_type != VAR_DICT)
3661 {
3662 EMSG(_(e_invarg));
3663 return FAIL;
3664 }
3665 dict = tv->vval.v_dict;
3666 if (dict == NULL)
3667 return OK;
3668
3669 todo = (int)dict->dv_hashtab.ht_used;
3670 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3671 if (!HASHITEM_EMPTY(hi))
3672 {
3673 item = &dict_lookup(hi)->di_tv;
3674
3675 if (STRCMP(hi->hi_key, "mode") == 0)
3676 {
3677 if (!(supported & JO_MODE))
3678 break;
3679 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3680 return FAIL;
3681 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003682 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003683 {
3684 if (!(supported & JO_IN_MODE))
3685 break;
3686 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3687 == FAIL)
3688 return FAIL;
3689 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003690 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003691 {
3692 if (!(supported & JO_OUT_MODE))
3693 break;
3694 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3695 == FAIL)
3696 return FAIL;
3697 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003698 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003699 {
3700 if (!(supported & JO_ERR_MODE))
3701 break;
3702 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3703 == FAIL)
3704 return FAIL;
3705 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003706 else if (STRCMP(hi->hi_key, "in_io") == 0
3707 || STRCMP(hi->hi_key, "out_io") == 0
3708 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003709 {
3710 if (!(supported & JO_OUT_IO))
3711 break;
3712 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3713 return FAIL;
3714 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003715 else if (STRCMP(hi->hi_key, "in_name") == 0
3716 || STRCMP(hi->hi_key, "out_name") == 0
3717 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003718 {
3719 part = part_from_char(*hi->hi_key);
3720
3721 if (!(supported & JO_OUT_IO))
3722 break;
3723 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3724 opt->jo_io_name[part] =
3725 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3726 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003727 else if (STRCMP(hi->hi_key, "in_buf") == 0
3728 || STRCMP(hi->hi_key, "out_buf") == 0
3729 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003730 {
3731 part = part_from_char(*hi->hi_key);
3732
3733 if (!(supported & JO_OUT_IO))
3734 break;
3735 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3736 opt->jo_io_buf[part] = get_tv_number(item);
3737 if (opt->jo_io_buf[part] <= 0)
3738 {
3739 EMSG2(_(e_invarg2), get_tv_string(item));
3740 return FAIL;
3741 }
3742 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3743 {
3744 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3745 return FAIL;
3746 }
3747 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003748 else if (STRCMP(hi->hi_key, "in_top") == 0
3749 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003750 {
3751 linenr_T *lp;
3752
3753 if (!(supported & JO_OUT_IO))
3754 break;
3755 if (hi->hi_key[3] == 't')
3756 {
3757 lp = &opt->jo_in_top;
3758 opt->jo_set |= JO_IN_TOP;
3759 }
3760 else
3761 {
3762 lp = &opt->jo_in_bot;
3763 opt->jo_set |= JO_IN_BOT;
3764 }
3765 *lp = get_tv_number(item);
3766 if (*lp < 0)
3767 {
3768 EMSG2(_(e_invarg2), get_tv_string(item));
3769 return FAIL;
3770 }
3771 }
3772 else if (STRCMP(hi->hi_key, "channel") == 0)
3773 {
3774 if (!(supported & JO_OUT_IO))
3775 break;
3776 opt->jo_set |= JO_CHANNEL;
3777 if (item->v_type != VAR_CHANNEL)
3778 {
3779 EMSG2(_(e_invarg2), "channel");
3780 return FAIL;
3781 }
3782 opt->jo_channel = item->vval.v_channel;
3783 }
3784 else if (STRCMP(hi->hi_key, "callback") == 0)
3785 {
3786 if (!(supported & JO_CALLBACK))
3787 break;
3788 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003789 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003790 if (opt->jo_callback == NULL)
3791 {
3792 EMSG2(_(e_invarg2), "callback");
3793 return FAIL;
3794 }
3795 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003796 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003797 {
3798 if (!(supported & JO_OUT_CALLBACK))
3799 break;
3800 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003801 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003802 if (opt->jo_out_cb == NULL)
3803 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003804 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003805 return FAIL;
3806 }
3807 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003808 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003809 {
3810 if (!(supported & JO_ERR_CALLBACK))
3811 break;
3812 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003813 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003814 if (opt->jo_err_cb == NULL)
3815 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003816 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003817 return FAIL;
3818 }
3819 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003820 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003821 {
3822 if (!(supported & JO_CLOSE_CALLBACK))
3823 break;
3824 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003825 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003826 if (opt->jo_close_cb == NULL)
3827 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003828 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003829 return FAIL;
3830 }
3831 }
3832 else if (STRCMP(hi->hi_key, "waittime") == 0)
3833 {
3834 if (!(supported & JO_WAITTIME))
3835 break;
3836 opt->jo_set |= JO_WAITTIME;
3837 opt->jo_waittime = get_tv_number(item);
3838 }
3839 else if (STRCMP(hi->hi_key, "timeout") == 0)
3840 {
3841 if (!(supported & JO_TIMEOUT))
3842 break;
3843 opt->jo_set |= JO_TIMEOUT;
3844 opt->jo_timeout = get_tv_number(item);
3845 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003846 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003847 {
3848 if (!(supported & JO_OUT_TIMEOUT))
3849 break;
3850 opt->jo_set |= JO_OUT_TIMEOUT;
3851 opt->jo_out_timeout = get_tv_number(item);
3852 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003853 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003854 {
3855 if (!(supported & JO_ERR_TIMEOUT))
3856 break;
3857 opt->jo_set |= JO_ERR_TIMEOUT;
3858 opt->jo_err_timeout = get_tv_number(item);
3859 }
3860 else if (STRCMP(hi->hi_key, "part") == 0)
3861 {
3862 if (!(supported & JO_PART))
3863 break;
3864 opt->jo_set |= JO_PART;
3865 val = get_tv_string(item);
3866 if (STRCMP(val, "err") == 0)
3867 opt->jo_part = PART_ERR;
3868 else
3869 {
3870 EMSG2(_(e_invarg2), val);
3871 return FAIL;
3872 }
3873 }
3874 else if (STRCMP(hi->hi_key, "id") == 0)
3875 {
3876 if (!(supported & JO_ID))
3877 break;
3878 opt->jo_set |= JO_ID;
3879 opt->jo_id = get_tv_number(item);
3880 }
3881 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3882 {
3883 if (!(supported & JO_STOPONEXIT))
3884 break;
3885 opt->jo_set |= JO_STOPONEXIT;
3886 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3887 opt->jo_soe_buf);
3888 if (opt->jo_stoponexit == NULL)
3889 {
3890 EMSG2(_(e_invarg2), "stoponexit");
3891 return FAIL;
3892 }
3893 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003894 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003895 {
3896 if (!(supported & JO_EXIT_CB))
3897 break;
3898 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003899 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3900 {
3901 opt->jo_exit_partial = item->vval.v_partial;
3902 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3903 }
3904 else
3905 opt->jo_exit_cb = get_tv_string_buf_chk(
3906 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003907 if (opt->jo_exit_cb == NULL)
3908 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003909 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003910 return FAIL;
3911 }
3912 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003913 else if (STRCMP(hi->hi_key, "block_write") == 0)
3914 {
3915 if (!(supported & JO_BLOCK_WRITE))
3916 break;
3917 opt->jo_set |= JO_BLOCK_WRITE;
3918 opt->jo_block_write = get_tv_number(item);
3919 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003920 else
3921 break;
3922 --todo;
3923 }
3924 if (todo > 0)
3925 {
3926 EMSG2(_(e_invarg2), hi->hi_key);
3927 return FAIL;
3928 }
3929
3930 return OK;
3931}
3932
3933/*
3934 * Get the channel from the argument.
3935 * Returns NULL if the handle is invalid.
3936 */
3937 channel_T *
3938get_channel_arg(typval_T *tv, int check_open)
3939{
3940 channel_T *channel = NULL;
3941
3942 if (tv->v_type == VAR_JOB)
3943 {
3944 if (tv->vval.v_job != NULL)
3945 channel = tv->vval.v_job->jv_channel;
3946 }
3947 else if (tv->v_type == VAR_CHANNEL)
3948 {
3949 channel = tv->vval.v_channel;
3950 }
3951 else
3952 {
3953 EMSG2(_(e_invarg2), get_tv_string(tv));
3954 return NULL;
3955 }
3956
3957 if (check_open && (channel == NULL || !channel_is_open(channel)))
3958 {
3959 EMSG(_("E906: not an open channel"));
3960 return NULL;
3961 }
3962 return channel;
3963}
3964
3965static job_T *first_job = NULL;
3966
3967 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003968job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003969{
3970 ch_log(job->jv_channel, "Freeing job");
3971 if (job->jv_channel != NULL)
3972 {
3973 /* The link from the channel to the job doesn't count as a reference,
3974 * thus don't decrement the refcount of the job. The reference from
3975 * the job to the channel does count the refrence, decrement it and
3976 * NULL the reference. We don't set ch_job_killed, unreferencing the
3977 * job doesn't mean it stops running. */
3978 job->jv_channel->ch_job = NULL;
3979 channel_unref(job->jv_channel);
3980 }
3981 mch_clear_job(job);
3982
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003983 vim_free(job->jv_stoponexit);
3984 vim_free(job->jv_exit_cb);
3985 partial_unref(job->jv_exit_partial);
3986}
3987
3988 static void
3989job_free_job(job_T *job)
3990{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003991 if (job->jv_next != NULL)
3992 job->jv_next->jv_prev = job->jv_prev;
3993 if (job->jv_prev == NULL)
3994 first_job = job->jv_next;
3995 else
3996 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003997 vim_free(job);
3998}
3999
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004000 static void
4001job_free(job_T *job)
4002{
4003 if (!in_free_unref_items)
4004 {
4005 job_free_contents(job);
4006 job_free_job(job);
4007 }
4008}
4009
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004010/*
4011 * Return TRUE if the job should not be freed yet. Do not free the job when
4012 * it has not ended yet and there is a "stoponexit" flag or an exit callback.
4013 */
4014 static int
4015job_still_useful(job_T *job)
4016{
4017 return job->jv_status == JOB_STARTED
4018 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4019}
4020
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004021 void
4022job_unref(job_T *job)
4023{
4024 if (job != NULL && --job->jv_refcount <= 0)
4025 {
4026 /* Do not free the job when it has not ended yet and there is a
4027 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004028 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004029 {
4030 job_free(job);
4031 }
4032 else if (job->jv_channel != NULL)
4033 {
4034 /* Do remove the link to the channel, otherwise it hangs
4035 * around until Vim exits. See job_free() for refcount. */
4036 job->jv_channel->ch_job = NULL;
4037 channel_unref(job->jv_channel);
4038 job->jv_channel = NULL;
4039 }
4040 }
4041}
4042
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004043 int
4044free_unused_jobs_contents(int copyID, int mask)
4045{
4046 int did_free = FALSE;
4047 job_T *job;
4048
4049 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004050 if ((job->jv_copyID & mask) != (copyID & mask)
4051 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004052 {
4053 /* Free the channel and ordinary items it contains, but don't
4054 * recurse into Lists, Dictionaries etc. */
4055 job_free_contents(job);
4056 did_free = TRUE;
4057 }
4058 return did_free;
4059}
4060
4061 void
4062free_unused_jobs(int copyID, int mask)
4063{
4064 job_T *job;
4065 job_T *job_next;
4066
4067 for (job = first_job; job != NULL; job = job_next)
4068 {
4069 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004070 if ((job->jv_copyID & mask) != (copyID & mask)
4071 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004073 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004074 job_free_job(job);
4075 }
4076 }
4077}
4078
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079/*
4080 * Allocate a job. Sets the refcount to one and sets options default.
4081 */
4082 static job_T *
4083job_alloc(void)
4084{
4085 job_T *job;
4086
4087 job = (job_T *)alloc_clear(sizeof(job_T));
4088 if (job != NULL)
4089 {
4090 job->jv_refcount = 1;
4091 job->jv_stoponexit = vim_strsave((char_u *)"term");
4092
4093 if (first_job != NULL)
4094 {
4095 first_job->jv_prev = job;
4096 job->jv_next = first_job;
4097 }
4098 first_job = job;
4099 }
4100 return job;
4101}
4102
4103 void
4104job_set_options(job_T *job, jobopt_T *opt)
4105{
4106 if (opt->jo_set & JO_STOPONEXIT)
4107 {
4108 vim_free(job->jv_stoponexit);
4109 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4110 job->jv_stoponexit = NULL;
4111 else
4112 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4113 }
4114 if (opt->jo_set & JO_EXIT_CB)
4115 {
4116 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004117 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004119 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004120 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004121 job->jv_exit_partial = NULL;
4122 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004123 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004124 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004126 job->jv_exit_partial = opt->jo_exit_partial;
4127 if (job->jv_exit_partial != NULL)
4128 ++job->jv_exit_partial->pt_refcount;
4129 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004130 }
4131}
4132
4133/*
4134 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4135 */
4136 void
4137job_stop_on_exit()
4138{
4139 job_T *job;
4140
4141 for (job = first_job; job != NULL; job = job->jv_next)
4142 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4143 mch_stop_job(job, job->jv_stoponexit);
4144}
4145
4146/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004147 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004148 */
4149 void
4150job_check_ended(void)
4151{
4152 static time_t last_check = 0;
4153 time_t now;
4154 job_T *job;
4155 job_T *next;
4156
4157 /* Only do this once in 10 seconds. */
4158 now = time(NULL);
4159 if (last_check + 10 < now)
4160 {
4161 last_check = now;
4162 for (job = first_job; job != NULL; job = next)
4163 {
4164 next = job->jv_next;
4165 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4166 job_status(job); /* may free "job" */
4167 }
4168 }
4169}
4170
4171/*
4172 * "job_start()" function
4173 */
4174 job_T *
4175job_start(typval_T *argvars)
4176{
4177 job_T *job;
4178 char_u *cmd = NULL;
4179#if defined(UNIX)
4180# define USE_ARGV
4181 char **argv = NULL;
4182 int argc = 0;
4183#else
4184 garray_T ga;
4185#endif
4186 jobopt_T opt;
4187 int part;
4188
4189 job = job_alloc();
4190 if (job == NULL)
4191 return NULL;
4192
4193 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004194#ifndef USE_ARGV
4195 ga_init2(&ga, (int)sizeof(char*), 20);
4196#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004197
4198 /* Default mode is NL. */
4199 clear_job_options(&opt);
4200 opt.jo_mode = MODE_NL;
4201 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004202 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4203 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004204 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004205
4206 /* Check that when io is "file" that there is a file name. */
4207 for (part = PART_OUT; part <= PART_IN; ++part)
4208 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4209 && opt.jo_io[part] == JIO_FILE
4210 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4211 || *opt.jo_io_name[part] == NUL))
4212 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004213 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004214 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004215 }
4216
4217 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4218 {
4219 buf_T *buf = NULL;
4220
4221 /* check that we can find the buffer before starting the job */
4222 if (opt.jo_set & JO_IN_BUF)
4223 {
4224 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4225 if (buf == NULL)
4226 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4227 }
4228 else if (!(opt.jo_set & JO_IN_NAME))
4229 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004230 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004231 }
4232 else
4233 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4234 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004235 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004236 if (buf->b_ml.ml_mfp == NULL)
4237 {
4238 char_u numbuf[NUMBUFLEN];
4239 char_u *s;
4240
4241 if (opt.jo_set & JO_IN_BUF)
4242 {
4243 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4244 s = numbuf;
4245 }
4246 else
4247 s = opt.jo_io_name[PART_IN];
4248 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004249 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004250 }
4251 job->jv_in_buf = buf;
4252 }
4253
4254 job_set_options(job, &opt);
4255
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004256 if (argvars[0].v_type == VAR_STRING)
4257 {
4258 /* Command is a string. */
4259 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004260 if (cmd == NULL || *cmd == NUL)
4261 {
4262 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004263 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004264 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004265#ifdef USE_ARGV
4266 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004267 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004268 argv[argc] = NULL;
4269#endif
4270 }
4271 else if (argvars[0].v_type != VAR_LIST
4272 || argvars[0].vval.v_list == NULL
4273 || argvars[0].vval.v_list->lv_len < 1)
4274 {
4275 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004276 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004277 }
4278 else
4279 {
4280 list_T *l = argvars[0].vval.v_list;
4281 listitem_T *li;
4282 char_u *s;
4283
4284#ifdef USE_ARGV
4285 /* Pass argv[] to mch_call_shell(). */
4286 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4287 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004288 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004289#endif
4290 for (li = l->lv_first; li != NULL; li = li->li_next)
4291 {
4292 s = get_tv_string_chk(&li->li_tv);
4293 if (s == NULL)
4294 goto theend;
4295#ifdef USE_ARGV
4296 argv[argc++] = (char *)s;
4297#else
4298 /* Only escape when needed, double quotes are not always allowed. */
4299 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4300 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004301# ifdef WIN32
4302 int old_ssl = p_ssl;
4303
4304 /* This is using CreateProcess, not cmd.exe. Always use
4305 * double quote and backslashes. */
4306 p_ssl = 0;
4307# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004308 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004309# ifdef WIN32
4310 p_ssl = old_ssl;
4311# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004312 if (s == NULL)
4313 goto theend;
4314 ga_concat(&ga, s);
4315 vim_free(s);
4316 }
4317 else
4318 ga_concat(&ga, s);
4319 if (li->li_next != NULL)
4320 ga_append(&ga, ' ');
4321#endif
4322 }
4323#ifdef USE_ARGV
4324 argv[argc] = NULL;
4325#else
4326 cmd = ga.ga_data;
4327#endif
4328 }
4329
4330#ifdef USE_ARGV
4331 if (ch_log_active())
4332 {
4333 garray_T ga;
4334 int i;
4335
4336 ga_init2(&ga, (int)sizeof(char), 200);
4337 for (i = 0; i < argc; ++i)
4338 {
4339 if (i > 0)
4340 ga_concat(&ga, (char_u *)" ");
4341 ga_concat(&ga, (char_u *)argv[i]);
4342 }
4343 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4344 ga_clear(&ga);
4345 }
4346 mch_start_job(argv, job, &opt);
4347#else
4348 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4349 mch_start_job((char *)cmd, job, &opt);
4350#endif
4351
4352 /* If the channel is reading from a buffer, write lines now. */
4353 if (job->jv_channel != NULL)
4354 channel_write_in(job->jv_channel);
4355
4356theend:
4357#ifdef USE_ARGV
4358 vim_free(argv);
4359#else
4360 vim_free(ga.ga_data);
4361#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004362 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004363 return job;
4364}
4365
4366/*
4367 * Get the status of "job" and invoke the exit callback when needed.
4368 * The returned string is not allocated.
4369 */
4370 char *
4371job_status(job_T *job)
4372{
4373 char *result;
4374
4375 if (job->jv_status == JOB_ENDED)
4376 /* No need to check, dead is dead. */
4377 result = "dead";
4378 else if (job->jv_status == JOB_FAILED)
4379 result = "fail";
4380 else
4381 {
4382 result = mch_job_status(job);
4383 if (job->jv_status == JOB_ENDED)
4384 ch_log(job->jv_channel, "Job ended");
4385 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4386 {
4387 typval_T argv[3];
4388 typval_T rettv;
4389 int dummy;
4390
4391 /* invoke the exit callback; make sure the refcount is > 0 */
4392 ++job->jv_refcount;
4393 argv[0].v_type = VAR_JOB;
4394 argv[0].vval.v_job = job;
4395 argv[1].v_type = VAR_NUMBER;
4396 argv[1].vval.v_number = job->jv_exitval;
4397 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004398 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4399 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004400 clear_tv(&rettv);
4401 --job->jv_refcount;
4402 }
4403 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4404 {
4405 /* The job was already unreferenced, now that it ended it can be
4406 * freed. Careful: caller must not use "job" after this! */
4407 job_free(job);
4408 }
4409 }
4410 return result;
4411}
4412
Bram Moolenaar8950a562016-03-12 15:22:55 +01004413/*
4414 * Implementation of job_info().
4415 */
4416 void
4417job_info(job_T *job, dict_T *dict)
4418{
4419 dictitem_T *item;
4420 varnumber_T nr;
4421
4422 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4423
4424 item = dictitem_alloc((char_u *)"channel");
4425 if (item == NULL)
4426 return;
4427 item->di_tv.v_lock = 0;
4428 item->di_tv.v_type = VAR_CHANNEL;
4429 item->di_tv.vval.v_channel = job->jv_channel;
4430 if (job->jv_channel != NULL)
4431 ++job->jv_channel->ch_refcount;
4432 if (dict_add(dict, item) == FAIL)
4433 dictitem_free(item);
4434
4435#ifdef UNIX
4436 nr = job->jv_pid;
4437#else
4438 nr = job->jv_proc_info.dwProcessId;
4439#endif
4440 dict_add_nr_str(dict, "process", nr, NULL);
4441
4442 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004443 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004444 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4445}
4446
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004447 int
4448job_stop(job_T *job, typval_T *argvars)
4449{
4450 char_u *arg;
4451
4452 if (argvars[1].v_type == VAR_UNKNOWN)
4453 arg = (char_u *)"";
4454 else
4455 {
4456 arg = get_tv_string_chk(&argvars[1]);
4457 if (arg == NULL)
4458 {
4459 EMSG(_(e_invarg));
4460 return 0;
4461 }
4462 }
4463 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4464 if (mch_stop_job(job, arg) == FAIL)
4465 return 0;
4466
4467 /* Assume that "hup" does not kill the job. */
4468 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4469 job->jv_channel->ch_job_killed = TRUE;
4470
4471 /* We don't try freeing the job, obviously the caller still has a
4472 * reference to it. */
4473 return 1;
4474}
4475
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004476#endif /* FEAT_JOB_CHANNEL */