blob: 83d057dbb089fd84859a35621edbda8e2e67c624 [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 {
461 /* Free the channel and ordinary items it contains, but don't
462 * recurse into Lists, Dictionaries etc. */
463 channel_free_channel(ch);
464 }
465 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100466}
467
Bram Moolenaard04a0202016-01-26 23:30:18 +0100468#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100469
470#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
471 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100472channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100473{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100474 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100475 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100476
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100477 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100478 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100479 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100480 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100481 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100482}
483#endif
484
Bram Moolenaare0874f82016-01-24 20:36:41 +0100485/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100486 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100487 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100488#ifdef FEAT_GUI_X11
489 static void
490messageFromNetbeans(XtPointer clientData,
491 int *unused1 UNUSED,
492 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100493{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100494 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100495}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100496#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100497
Bram Moolenaard04a0202016-01-26 23:30:18 +0100498#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100499# if GTK_CHECK_VERSION(3,0,0)
500 static gboolean
501messageFromNetbeans(GIOChannel *unused1 UNUSED,
502 GIOCondition unused2 UNUSED,
503 gpointer clientData)
504{
505 channel_read_fd(GPOINTER_TO_INT(clientData));
506 return TRUE; /* Return FALSE instead in case the event source is to
507 * be removed after this function returns. */
508}
509# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100510 static void
511messageFromNetbeans(gpointer clientData,
512 gint unused1 UNUSED,
513 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100514{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100515 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100516}
Bram Moolenaar98921892016-02-23 17:14:37 +0100517# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518#endif
519
520 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100521channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100522{
Bram Moolenaarde279892016-03-11 22:19:44 +0100523 if (!CH_HAS_GUI)
524 return;
525
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100526# ifdef FEAT_GUI_X11
527 /* Tell notifier we are interested in being called
528 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100529 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
530 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100531 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100532 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100533 (XtPointer)(XtInputReadMask + XtInputExceptMask),
534 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100535 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100536# else
537# ifdef FEAT_GUI_GTK
538 /* Tell gdk we are interested in being called when there
539 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100540 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100541# if GTK_CHECK_VERSION(3,0,0)
542 {
543 GIOChannel *chnnl = g_io_channel_unix_new(
544 (gint)channel->ch_part[part].ch_fd);
545
546 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
547 chnnl,
548 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
549 messageFromNetbeans,
550 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
551
552 g_io_channel_unref(chnnl);
553 }
554# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100555 channel->ch_part[part].ch_inputHandler = gdk_input_add(
556 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100557 (GdkInputCondition)
558 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100560 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100561# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100562# endif
563# endif
564}
565
Bram Moolenaarde279892016-03-11 22:19:44 +0100566 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100567channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100568{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 if (channel->CH_SOCK_FD != INVALID_FD)
570 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100571 if (channel->CH_OUT_FD != INVALID_FD)
572 channel_gui_register_one(channel, PART_OUT);
573 if (channel->CH_ERR_FD != INVALID_FD)
574 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100575}
576
577/*
578 * Register any of our file descriptors with the GUI event handling system.
579 * Called when the GUI has started.
580 */
581 void
582channel_gui_register_all(void)
583{
Bram Moolenaar77073442016-02-13 23:23:53 +0100584 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100585
Bram Moolenaar77073442016-02-13 23:23:53 +0100586 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100587 channel_gui_register(channel);
588}
589
590 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100591channel_gui_unregister_one(channel_T *channel, int part)
592{
593# ifdef FEAT_GUI_X11
594 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
595 {
596 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
597 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
598 }
599# else
600# ifdef FEAT_GUI_GTK
601 if (channel->ch_part[part].ch_inputHandler != 0)
602 {
603# if GTK_CHECK_VERSION(3,0,0)
604 g_source_remove(channel->ch_part[part].ch_inputHandler);
605# else
606 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
607# endif
608 channel->ch_part[part].ch_inputHandler = 0;
609 }
610# endif
611# endif
612}
613
614 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100615channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100616{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100617 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100618
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100619 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100620 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100621}
622
623#endif
624
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100625static char *e_cannot_connect = N_("E902: Cannot connect to port");
626
Bram Moolenaard04a0202016-01-26 23:30:18 +0100627/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100628 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100629 * "waittime" is the time in msec to wait for the connection.
630 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100631 * Returns the channel for success.
632 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100633 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100634 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100635channel_open(
636 char *hostname,
637 int port_in,
638 int waittime,
639 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100640{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100641 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100642 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100643 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100644#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100645 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100646 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100647#else
648 int port = port_in;
649#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100650 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100651 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100653#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654 channel_init_winsock();
655#endif
656
Bram Moolenaar77073442016-02-13 23:23:53 +0100657 channel = add_channel();
658 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100659 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100660 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100661 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 }
663
664 /* Get the server internet address and put into addr structure */
665 /* fill in the socket address structure and connect to server */
666 vim_memset((char *)&server, 0, sizeof(server));
667 server.sin_family = AF_INET;
668 server.sin_port = htons(port);
669 if ((host = gethostbyname(hostname)) == NULL)
670 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100671 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100672 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100673 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100674 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100675 }
676 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
677
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100678 /* On Mac and Solaris a zero timeout almost never works. At least wait
679 * one millisecond. Let's do it for all systems, because we don't know why
680 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100681 if (waittime == 0)
682 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100683
684 /*
685 * For Unix we need to call connect() again after connect() failed.
686 * On Win32 one time is sufficient.
687 */
688 while (TRUE)
689 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100690 long elapsed_msec = 0;
691 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100692
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100693 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100694 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100695 sd = socket(AF_INET, SOCK_STREAM, 0);
696 if (sd == -1)
697 {
698 ch_error(channel, "in socket() in channel_open().");
699 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100700 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100701 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100702 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100703
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704 if (waittime >= 0)
705 {
706 /* Make connect() non-blocking. */
707 if (
708#ifdef _WIN32
709 ioctlsocket(sd, FIONBIO, &val) < 0
710#else
711 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
712#endif
713 )
714 {
715 SOCK_ERRNO;
716 ch_errorn(channel,
717 "channel_open: Connect failed with errno %d", errno);
718 sock_close(sd);
719 channel_free(channel);
720 return NULL;
721 }
722 }
723
724 /* Try connecting to the server. */
725 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
726 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
727
Bram Moolenaar045a2842016-03-08 22:33:07 +0100728 if (ret == 0)
729 /* The connection could be established. */
730 break;
731
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100732 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100733 if (waittime < 0 || (errno != EWOULDBLOCK
734 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100735#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100737#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100738 ))
739 {
740 ch_errorn(channel,
741 "channel_open: Connect failed with errno %d", errno);
742 PERROR(_(e_cannot_connect));
743 sock_close(sd);
744 channel_free(channel);
745 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100746 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100747
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100748 /* Limit the waittime to 50 msec. If it doesn't work within this
749 * time we close the socket and try creating it again. */
750 waitnow = waittime > 50 ? 50 : waittime;
751
Bram Moolenaar045a2842016-03-08 22:33:07 +0100752 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100753 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100754#ifndef WIN32
755 if (errno != ECONNREFUSED)
756#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100757 {
758 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100759 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100760 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100761#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100762 int so_error = 0;
763 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100764 struct timeval start_tv;
765 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100766#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100767 FD_ZERO(&rfds);
768 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100769 FD_ZERO(&wfds);
770 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100771
Bram Moolenaar562ca712016-03-09 21:50:05 +0100772 tv.tv_sec = waitnow / 1000;
773 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100774#ifndef WIN32
775 gettimeofday(&start_tv, NULL);
776#endif
777 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100778 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100779 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100780
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100781 if (ret < 0)
782 {
783 SOCK_ERRNO;
784 ch_errorn(channel,
785 "channel_open: Connect failed with errno %d", errno);
786 PERROR(_(e_cannot_connect));
787 sock_close(sd);
788 channel_free(channel);
789 return NULL;
790 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100791
Bram Moolenaare081e212016-02-28 22:33:46 +0100792#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100793 /* On Win32: select() is expected to work and wait for up to
794 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100795 if (FD_ISSET(sd, &wfds))
796 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100797 elapsed_msec = waitnow;
798 if (waittime > 1 && elapsed_msec < waittime)
799 {
800 waittime -= elapsed_msec;
801 continue;
802 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100803#else
804 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100805 * After putting the socket in non-blocking mode, connect() will
806 * return EINPROGRESS, select() will not wait (as if writing is
807 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100808 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100809 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100810 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100811 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812 {
813 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100814 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 if (ret < 0 || (so_error != 0
816 && so_error != EWOULDBLOCK
817 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100818# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100819 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100820# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100821 ))
822 {
823 ch_errorn(channel,
824 "channel_open: Connect failed with errno %d",
825 so_error);
826 PERROR(_(e_cannot_connect));
827 sock_close(sd);
828 channel_free(channel);
829 return NULL;
830 }
831 }
832
Bram Moolenaar045a2842016-03-08 22:33:07 +0100833 if (FD_ISSET(sd, &wfds) && so_error == 0)
834 /* Did not detect an error, connection is established. */
835 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100836
Bram Moolenaar045a2842016-03-08 22:33:07 +0100837 gettimeofday(&end_tv, NULL);
838 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
839 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100840#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100841 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100842
843#ifndef WIN32
844 if (waittime > 1 && elapsed_msec < waittime)
845 {
846 /* The port isn't ready but we also didn't get an error.
847 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100848 * yet. Select() may return early, wait until the remaining
849 * "waitnow" and try again. */
850 waitnow -= elapsed_msec;
851 waittime -= elapsed_msec;
852 if (waitnow > 0)
853 {
854 mch_delay((long)waitnow, TRUE);
855 ui_breakcheck();
856 waittime -= waitnow;
857 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100858 if (!got_int)
859 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100860 if (waittime <= 0)
861 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 waittime = 1;
863 continue;
864 }
865 /* we were interrupted, behave as if timed out */
866 }
867#endif
868
869 /* We timed out. */
870 ch_error(channel, "Connection timed out");
871 sock_close(sd);
872 channel_free(channel);
873 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100874 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100875
Bram Moolenaar045a2842016-03-08 22:33:07 +0100876 ch_log(channel, "Connection made");
877
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100878 if (waittime >= 0)
879 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100880#ifdef _WIN32
881 val = 0;
882 ioctlsocket(sd, FIONBIO, &val);
883#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100884 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100885#endif
886 }
887
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100888 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100889 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100890 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
891 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100892
893#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100894 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100895#endif
896
Bram Moolenaar77073442016-02-13 23:23:53 +0100897 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100898}
899
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100900/*
901 * Implements ch_open().
902 */
903 channel_T *
904channel_open_func(typval_T *argvars)
905{
906 char_u *address;
907 char_u *p;
908 char *rest;
909 int port;
910 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200911 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100912
913 address = get_tv_string(&argvars[0]);
914 if (argvars[1].v_type != VAR_UNKNOWN
915 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
916 {
917 EMSG(_(e_invarg));
918 return NULL;
919 }
920
921 /* parse address */
922 p = vim_strchr(address, ':');
923 if (p == NULL)
924 {
925 EMSG2(_(e_invarg2), address);
926 return NULL;
927 }
928 *p++ = NUL;
929 port = strtol((char *)p, &rest, 10);
930 if (*address == NUL || port <= 0 || *rest != NUL)
931 {
932 p[-1] = ':';
933 EMSG2(_(e_invarg2), address);
934 return NULL;
935 }
936
937 /* parse options */
938 clear_job_options(&opt);
939 opt.jo_mode = MODE_JSON;
940 opt.jo_timeout = 2000;
941 if (get_job_options(&argvars[1], &opt,
942 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200943 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100944 if (opt.jo_timeout < 0)
945 {
946 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200947 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100948 }
949
950 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
951 if (channel != NULL)
952 {
953 opt.jo_set = JO_ALL;
954 channel_set_options(channel, &opt);
955 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200956theend:
957 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100958 return channel;
959}
960
Bram Moolenaarde279892016-03-11 22:19:44 +0100961 static void
962may_close_part(sock_T *fd)
963{
964 if (*fd != INVALID_FD)
965 {
966 fd_close(*fd);
967 *fd = INVALID_FD;
968 }
969}
970
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100971 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100972channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100973{
Bram Moolenaarde279892016-03-11 22:19:44 +0100974 if (in != INVALID_FD)
975 {
976 may_close_part(&channel->CH_IN_FD);
977 channel->CH_IN_FD = in;
978 }
979 if (out != INVALID_FD)
980 {
981# if defined(FEAT_GUI)
982 channel_gui_unregister_one(channel, PART_OUT);
983# endif
984 may_close_part(&channel->CH_OUT_FD);
985 channel->CH_OUT_FD = out;
986# if defined(FEAT_GUI)
987 channel_gui_register_one(channel, PART_OUT);
988# endif
989 }
990 if (err != INVALID_FD)
991 {
992# if defined(FEAT_GUI)
993 channel_gui_unregister_one(channel, PART_ERR);
994# endif
995 may_close_part(&channel->CH_ERR_FD);
996 channel->CH_ERR_FD = err;
997# if defined(FEAT_GUI)
998 channel_gui_register_one(channel, PART_ERR);
999# endif
1000 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001001}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001002
Bram Moolenaard6051b52016-02-28 15:49:03 +01001003/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001004 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001005 * This does not keep a refcount, when the job is freed ch_job is cleared.
1006 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001007 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001008channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001009{
Bram Moolenaar77073442016-02-13 23:23:53 +01001010 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001011
1012 channel_set_options(channel, options);
1013
1014 if (job->jv_in_buf != NULL)
1015 {
1016 chanpart_T *in_part = &channel->ch_part[PART_IN];
1017
1018 in_part->ch_buffer = job->jv_in_buf;
1019 ch_logs(channel, "reading from buffer '%s'",
1020 (char *)in_part->ch_buffer->b_ffname);
1021 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001022 {
1023 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1024 {
1025 /* Special mode: send last-but-one line when appending a line
1026 * to the buffer. */
1027 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001028 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001029 in_part->ch_buf_top =
1030 in_part->ch_buffer->b_ml.ml_line_count + 1;
1031 }
1032 else
1033 in_part->ch_buf_top = options->jo_in_top;
1034 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001035 else
1036 in_part->ch_buf_top = 1;
1037 if (options->jo_set & JO_IN_BOT)
1038 in_part->ch_buf_bot = options->jo_in_bot;
1039 else
1040 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1041 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001042}
1043
1044/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001045 * Find a buffer matching "name" or create a new one.
1046 */
1047 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001048find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001049{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001050 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001051 buf_T *save_curbuf = curbuf;
1052
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001053 if (name != NULL && *name != NUL)
1054 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001055 if (buf == NULL)
1056 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001057 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001058 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001059 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001060 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001061#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001062 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1063 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001064#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001065 if (curbuf->b_ml.ml_mfp == NULL)
1066 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001067 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1068 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001069 changed_bytes(1, 0);
1070 curbuf = save_curbuf;
1071 }
1072
1073 return buf;
1074}
1075
1076/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001077 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001078 */
1079 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001080channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001081{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001082 int part;
1083 char_u **cbp;
1084 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001085
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001086 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001087 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001088 channel->ch_part[part].ch_mode = opt->jo_mode;
1089 if (opt->jo_set & JO_IN_MODE)
1090 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1091 if (opt->jo_set & JO_OUT_MODE)
1092 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1093 if (opt->jo_set & JO_ERR_MODE)
1094 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001095
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001096 if (opt->jo_set & JO_TIMEOUT)
1097 for (part = PART_SOCK; part <= PART_IN; ++part)
1098 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1099 if (opt->jo_set & JO_OUT_TIMEOUT)
1100 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1101 if (opt->jo_set & JO_ERR_TIMEOUT)
1102 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001103 if (opt->jo_set & JO_BLOCK_WRITE)
1104 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001105
1106 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001107 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001108 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001109 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001110 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001111 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001112 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1113 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001114 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001115 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001116 *pp = opt->jo_partial;
1117 if (*pp != NULL)
1118 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001119 }
1120 if (opt->jo_set & JO_OUT_CALLBACK)
1121 {
1122 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001123 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001124 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001125 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001126 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1127 *cbp = vim_strsave(opt->jo_out_cb);
1128 else
1129 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001130 *pp = opt->jo_out_partial;
1131 if (*pp != NULL)
1132 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001133 }
1134 if (opt->jo_set & JO_ERR_CALLBACK)
1135 {
1136 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001137 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001138 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1141 *cbp = vim_strsave(opt->jo_err_cb);
1142 else
1143 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001144 *pp = opt->jo_err_partial;
1145 if (*pp != NULL)
1146 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001147 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001148 if (opt->jo_set & JO_CLOSE_CALLBACK)
1149 {
1150 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001151 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001152 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001153 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001154 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1155 *cbp = vim_strsave(opt->jo_close_cb);
1156 else
1157 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001158 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001159 if (*pp != NULL)
1160 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001161 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001162
1163 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1164 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001165 /* writing output to a buffer. Default mode is NL. */
1166 if (!(opt->jo_set & JO_OUT_MODE))
1167 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001168 if (opt->jo_set & JO_OUT_BUF)
1169 channel->ch_part[PART_OUT].ch_buffer =
1170 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1171 else
1172 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001173 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1174 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001175 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1176 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001177
1178 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1179 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1180 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1181 {
1182 /* writing err to a buffer. Default mode is NL. */
1183 if (!(opt->jo_set & JO_ERR_MODE))
1184 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1185 if (opt->jo_io[PART_ERR] == JIO_OUT)
1186 channel->ch_part[PART_ERR].ch_buffer =
1187 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001188 else if (opt->jo_set & JO_ERR_BUF)
1189 channel->ch_part[PART_ERR].ch_buffer =
1190 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001191 else
1192 channel->ch_part[PART_ERR].ch_buffer =
1193 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1194 ch_logs(channel, "writing err to buffer '%s'",
1195 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1196 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001197
1198 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1199 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1200 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001201}
1202
1203/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001204 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001205 */
1206 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001207channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001208 channel_T *channel,
1209 int part,
1210 char_u *callback,
1211 partial_T *partial,
1212 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001213{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001214 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001215 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1216
1217 if (item != NULL)
1218 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001219 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001220 item->cq_partial = partial;
1221 if (partial != NULL)
1222 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001223 item->cq_seq_nr = id;
1224 item->cq_prev = head->cq_prev;
1225 head->cq_prev = item;
1226 item->cq_next = NULL;
1227 if (item->cq_prev == NULL)
1228 head->cq_next = item;
1229 else
1230 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001231 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001232}
1233
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001234 static void
1235write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1236{
1237 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001238 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001239 char_u *p;
1240
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001241 if ((p = alloc(len + 2)) == NULL)
1242 return;
1243 STRCPY(p, line);
1244 p[len] = NL;
1245 p[len + 1] = NUL;
1246 channel_send(channel, PART_IN, p, "write_buf_line()");
1247 vim_free(p);
1248}
1249
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001250/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001251 * Return TRUE if "channel" can be written to.
1252 * Returns FALSE if the input is closed or the write would block.
1253 */
1254 static int
1255can_write_buf_line(channel_T *channel)
1256{
1257 chanpart_T *in_part = &channel->ch_part[PART_IN];
1258
1259 if (in_part->ch_fd == INVALID_FD)
1260 return FALSE; /* pipe was closed */
1261
1262 /* for testing: block every other attempt to write */
1263 if (in_part->ch_block_write == 1)
1264 in_part->ch_block_write = -1;
1265 else if (in_part->ch_block_write == -1)
1266 in_part->ch_block_write = 1;
1267
1268 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1269#ifndef WIN32
1270 {
1271# if defined(HAVE_SELECT)
1272 struct timeval tval;
1273 fd_set wfds;
1274 int ret;
1275
1276 FD_ZERO(&wfds);
1277 FD_SET((int)in_part->ch_fd, &wfds);
1278 tval.tv_sec = 0;
1279 tval.tv_usec = 0;
1280 for (;;)
1281 {
1282 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1283# ifdef EINTR
1284 SOCK_ERRNO;
1285 if (ret == -1 && errno == EINTR)
1286 continue;
1287# endif
1288 if (ret <= 0 || in_part->ch_block_write == 1)
1289 {
1290 if (ret > 0)
1291 ch_log(channel, "FAKED Input not ready for writing");
1292 else
1293 ch_log(channel, "Input not ready for writing");
1294 return FALSE;
1295 }
1296 break;
1297 }
1298# else
1299 struct pollfd fds;
1300
1301 fds.fd = in_part->ch_fd;
1302 fds.events = POLLOUT;
1303 if (poll(&fds, 1, 0) <= 0)
1304 {
1305 ch_log(channel, "Input not ready for writing");
1306 return FALSE;
1307 }
1308 if (in_part->ch_block_write == 1)
1309 {
1310 ch_log(channel, "FAKED Input not ready for writing");
1311 return FALSE;
1312 }
1313# endif
1314 }
1315#endif
1316 return TRUE;
1317}
1318
1319/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001320 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001321 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001322 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001323channel_write_in(channel_T *channel)
1324{
1325 chanpart_T *in_part = &channel->ch_part[PART_IN];
1326 linenr_T lnum;
1327 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001328 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001329
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001330 if (buf == NULL || in_part->ch_buf_append)
1331 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001332 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1333 {
1334 /* buffer was wiped out or unloaded */
1335 in_part->ch_buffer = NULL;
1336 return;
1337 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001338
1339 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1340 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1341 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001342 if (!can_write_buf_line(channel))
1343 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001344 write_buf_line(buf, lnum, channel);
1345 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001346 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001347
1348 if (written == 1)
1349 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1350 else if (written > 1)
1351 ch_logn(channel, "written %d lines to channel", written);
1352
Bram Moolenaar014069a2016-03-03 22:51:40 +01001353 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001354 if (lnum > buf->b_ml.ml_line_count)
1355 {
1356 /* Writing is done, no longer need the buffer. */
1357 in_part->ch_buffer = NULL;
1358 ch_log(channel, "Finished writing all lines to channel");
1359 }
1360 else
1361 ch_logn(channel, "Still %d more lines to write",
1362 buf->b_ml.ml_line_count - lnum + 1);
1363}
1364
1365/*
1366 * Write any lines waiting to be written to a channel.
1367 */
1368 void
1369channel_write_any_lines()
1370{
1371 channel_T *channel;
1372
1373 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1374 {
1375 chanpart_T *in_part = &channel->ch_part[PART_IN];
1376
1377 if (in_part->ch_buffer != NULL)
1378 {
1379 if (in_part->ch_buf_append)
1380 channel_write_new_lines(in_part->ch_buffer);
1381 else
1382 channel_write_in(channel);
1383 }
1384 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001385}
1386
1387/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001388 * Write appended lines above the last one in "buf" to the channel.
1389 */
1390 void
1391channel_write_new_lines(buf_T *buf)
1392{
1393 channel_T *channel;
1394 int found_one = FALSE;
1395
1396 /* There could be more than one channel for the buffer, loop over all of
1397 * them. */
1398 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1399 {
1400 chanpart_T *in_part = &channel->ch_part[PART_IN];
1401 linenr_T lnum;
1402 int written = 0;
1403
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001404 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001405 {
1406 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001408 found_one = TRUE;
1409 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1410 ++lnum)
1411 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 if (!can_write_buf_line(channel))
1413 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001414 write_buf_line(buf, lnum, channel);
1415 ++written;
1416 }
1417
1418 if (written == 1)
1419 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1420 else if (written > 1)
1421 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001422 if (lnum < buf->b_ml.ml_line_count)
1423 ch_logn(channel, "Still %d more lines to write",
1424 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001425
1426 in_part->ch_buf_bot = lnum;
1427 }
1428 }
1429 if (!found_one)
1430 buf->b_write_to_channel = FALSE;
1431}
1432
1433/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001434 * Invoke the "callback" on channel "channel".
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);
1449
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001450 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001451}
1452
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001453/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001454 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001455 * The caller must free it.
1456 * Returns NULL if there is nothing.
1457 */
1458 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001459channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001460{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001461 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001462 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001463 char_u *p;
1464
Bram Moolenaar77073442016-02-13 23:23:53 +01001465 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001467 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001468 p = node->rq_buffer;
1469 head->rq_next = node->rq_next;
1470 if (node->rq_next == NULL)
1471 head->rq_prev = NULL;
1472 else
1473 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001474 vim_free(node);
1475 return p;
1476}
1477
1478/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001479 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001480 */
1481 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001482channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001483{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001484 readq_T *head = &channel->ch_part[part].ch_head;
1485 readq_T *node = head->rq_next;
1486 long_u len = 1;
1487 char_u *res;
1488 char_u *p;
1489
1490 /* If there is only one buffer just get that one. */
1491 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1492 return channel_get(channel, part);
1493
1494 /* Concatenate everything into one buffer. */
1495 for (node = head->rq_next; node != NULL; node = node->rq_next)
1496 len += (long_u)STRLEN(node->rq_buffer);
1497 res = lalloc(len, TRUE);
1498 if (res == NULL)
1499 return NULL;
1500 *res = NUL;
1501 for (node = head->rq_next; node != NULL; node = node->rq_next)
1502 STRCAT(res, node->rq_buffer);
1503
1504 /* Free all buffers */
1505 do
1506 {
1507 p = channel_get(channel, part);
1508 vim_free(p);
1509 } while (p != NULL);
1510
1511 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001512}
1513
1514/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001515 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001516 * Returns FAIL if that is not possible.
1517 */
1518 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001519channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001520{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001521 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001522 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001523 char_u *p;
1524
Bram Moolenaar77073442016-02-13 23:23:53 +01001525 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001526 return FAIL;
1527
Bram Moolenaar77073442016-02-13 23:23:53 +01001528 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1529 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001530 if (p == NULL)
1531 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001532 STRCPY(p, node->rq_buffer);
1533 STRCAT(p, node->rq_next->rq_buffer);
1534 vim_free(node->rq_next->rq_buffer);
1535 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001536
Bram Moolenaar77073442016-02-13 23:23:53 +01001537 /* dispose of the node and its buffer */
1538 head->rq_next = node->rq_next;
1539 head->rq_next->rq_prev = NULL;
1540 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001541 vim_free(node);
1542 return OK;
1543}
1544
1545/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001546 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001547 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001548 * Returns OK or FAIL.
1549 */
1550 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001551channel_save(channel_T *channel, int part, char_u *buf, int len,
1552 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001553{
1554 readq_T *node;
1555 readq_T *head = &channel->ch_part[part].ch_head;
1556 char_u *p;
1557 int i;
1558
1559 node = (readq_T *)alloc(sizeof(readq_T));
1560 if (node == NULL)
1561 return FAIL; /* out of memory */
1562 node->rq_buffer = alloc(len + 1);
1563 if (node->rq_buffer == NULL)
1564 {
1565 vim_free(node);
1566 return FAIL; /* out of memory */
1567 }
1568
1569 if (channel->ch_part[part].ch_mode == MODE_NL)
1570 {
1571 /* Drop any CR before a NL. */
1572 p = node->rq_buffer;
1573 for (i = 0; i < len; ++i)
1574 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1575 *p++ = buf[i];
1576 *p = NUL;
1577 }
1578 else
1579 {
1580 mch_memmove(node->rq_buffer, buf, len);
1581 node->rq_buffer[len] = NUL;
1582 }
1583
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001584 if (prepend)
1585 {
1586 /* preend node to the head of the queue */
1587 node->rq_next = head->rq_next;
1588 node->rq_prev = NULL;
1589 if (head->rq_next == NULL)
1590 head->rq_prev = node;
1591 else
1592 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001593 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001594 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001595 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001596 {
1597 /* append node to the tail of the queue */
1598 node->rq_next = NULL;
1599 node->rq_prev = head->rq_prev;
1600 if (head->rq_prev == NULL)
1601 head->rq_next = node;
1602 else
1603 head->rq_prev->rq_next = node;
1604 head->rq_prev = node;
1605 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001606
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001607 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001608 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001609 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001610 fprintf(log_fd, "'");
1611 if (fwrite(buf, len, 1, log_fd) != 1)
1612 return FAIL;
1613 fprintf(log_fd, "'\n");
1614 }
1615 return OK;
1616}
1617
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001618 static int
1619channel_fill(js_read_T *reader)
1620{
1621 channel_T *channel = (channel_T *)reader->js_cookie;
1622 int part = reader->js_cookie_arg;
1623 char_u *next = channel_get(channel, part);
1624 int unused;
1625 int len;
1626 char_u *p;
1627
1628 if (next == NULL)
1629 return FALSE;
1630
1631 unused = reader->js_end - reader->js_buf - reader->js_used;
1632 if (unused > 0)
1633 {
1634 /* Prepend unused text. */
1635 len = (int)STRLEN(next);
1636 p = alloc(unused + len + 1);
1637 if (p == NULL)
1638 {
1639 vim_free(next);
1640 return FALSE;
1641 }
1642 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1643 mch_memmove(p + unused, next, len + 1);
1644 vim_free(next);
1645 next = p;
1646 }
1647
1648 vim_free(reader->js_buf);
1649 reader->js_buf = next;
1650 reader->js_used = 0;
1651 return TRUE;
1652}
1653
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001654/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001655 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001656 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001657 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001658 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001659 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001660channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001661{
1662 js_read_T reader;
1663 typval_T listtv;
1664 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001665 chanpart_T *chanpart = &channel->ch_part[part];
1666 jsonq_T *head = &chanpart->ch_json_head;
1667 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001668 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001669
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001670 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001671 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001673 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001674 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001675 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001676 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001677 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001678
1679 /* When a message is incomplete we wait for a short while for more to
1680 * arrive. After the delay drop the input, otherwise a truncated string
1681 * or list will make us hang. */
1682 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001683 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001684 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001685 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001686 /* Only accept the response when it is a list with at least two
1687 * items. */
1688 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001689 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001690 if (listtv.v_type != VAR_LIST)
1691 ch_error(channel, "Did not receive a list, discarding");
1692 else
1693 ch_errorn(channel, "Expected list with two items, got %d",
1694 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001696 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697 else
1698 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001699 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1700 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 else
1703 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001704 item->jq_value = alloc_tv();
1705 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001706 {
1707 vim_free(item);
1708 clear_tv(&listtv);
1709 }
1710 else
1711 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001712 *item->jq_value = listtv;
1713 item->jq_prev = head->jq_prev;
1714 head->jq_prev = item;
1715 item->jq_next = NULL;
1716 if (item->jq_prev == NULL)
1717 head->jq_next = item;
1718 else
1719 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001720 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721 }
1722 }
1723 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001724
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001725 if (status == OK)
1726 chanpart->ch_waiting = FALSE;
1727 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001728 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001729 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001730 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001731 /* First time encountering incomplete message, set a deadline of
1732 * 100 msec. */
1733 ch_log(channel, "Incomplete message - wait for more");
1734 reader.js_used = 0;
1735 chanpart->ch_waiting = TRUE;
1736#ifdef WIN32
1737 chanpart->ch_deadline = GetTickCount() + 100L;
1738#else
1739 gettimeofday(&chanpart->ch_deadline, NULL);
1740 chanpart->ch_deadline.tv_usec += 100 * 1000;
1741 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1742 {
1743 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1744 ++chanpart->ch_deadline.tv_sec;
1745 }
1746#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001747 }
1748 else
1749 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001750 int timeout;
1751#ifdef WIN32
1752 timeout = GetTickCount() > chanpart->ch_deadline;
1753#else
1754 {
1755 struct timeval now_tv;
1756
1757 gettimeofday(&now_tv, NULL);
1758 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1759 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1760 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1761 }
1762#endif
1763 if (timeout)
1764 {
1765 status = FAIL;
1766 chanpart->ch_waiting = FALSE;
1767 }
1768 else
1769 {
1770 reader.js_used = 0;
1771 ch_log(channel, "still waiting on incomplete message");
1772 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001773 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001774 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001775
1776 if (status == FAIL)
1777 {
1778 ch_error(channel, "Decoding failed - discarding input");
1779 ret = FALSE;
1780 chanpart->ch_waiting = FALSE;
1781 }
1782 else if (reader.js_buf[reader.js_used] != NUL)
1783 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001784 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001785 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001786 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1787 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001788 ret = status == MAYBE ? FALSE: TRUE;
1789 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001790 else
1791 ret = FALSE;
1792
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001793 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001794 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001795}
1796
1797/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001798 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001799 */
1800 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001801remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001802{
Bram Moolenaar77073442016-02-13 23:23:53 +01001803 if (node->cq_prev == NULL)
1804 head->cq_next = node->cq_next;
1805 else
1806 node->cq_prev->cq_next = node->cq_next;
1807 if (node->cq_next == NULL)
1808 head->cq_prev = node->cq_prev;
1809 else
1810 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001811}
1812
1813/*
1814 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001815 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001816 */
1817 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001818remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001819{
Bram Moolenaar77073442016-02-13 23:23:53 +01001820 if (node->jq_prev == NULL)
1821 head->jq_next = node->jq_next;
1822 else
1823 node->jq_prev->jq_next = node->jq_next;
1824 if (node->jq_next == NULL)
1825 head->jq_prev = node->jq_prev;
1826 else
1827 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001828 vim_free(node);
1829}
1830
1831/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001832 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001833 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001834 * When "id" is zero or negative jut get the first message. But not the one
1835 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 * Return OK when found and return the value in "rettv".
1837 * Return FAIL otherwise.
1838 */
1839 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001840channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001841{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001842 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001843 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844
Bram Moolenaar77073442016-02-13 23:23:53 +01001845 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001846 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001847 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001848 typval_T *tv = &l->lv_first->li_tv;
1849
1850 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001851 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001852 || tv->vval.v_number == 0
1853 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001854 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001855 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001856 if (tv->v_type == VAR_NUMBER)
1857 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001858 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001859 return OK;
1860 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001861 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 }
1863 return FAIL;
1864}
1865
Bram Moolenaarece61b02016-02-20 21:39:05 +01001866#define CH_JSON_MAX_ARGS 4
1867
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001869 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001870 * "argv[0]" is the command string.
1871 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001872 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001873 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001874channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001875{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001876 char_u *cmd = argv[0].vval.v_string;
1877 char_u *arg;
1878 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001879
Bram Moolenaarece61b02016-02-20 21:39:05 +01001880 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001881 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001882 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001883 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001884 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001885 return;
1886 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001887 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001888 if (arg == NULL)
1889 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001890
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001891 if (STRCMP(cmd, "ex") == 0)
1892 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001893 int save_called_emsg = called_emsg;
1894
1895 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001896 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001897 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001898 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001899 --emsg_silent;
1900 if (called_emsg)
1901 ch_logs(channel, "Ex command error: '%s'",
1902 (char *)get_vim_var_str(VV_ERRMSG));
1903 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001904 }
1905 else if (STRCMP(cmd, "normal") == 0)
1906 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001907 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001908
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001909 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001910 ea.arg = arg;
1911 ea.addr_count = 0;
1912 ea.forceit = TRUE; /* no mapping */
1913 ex_normal(&ea);
1914 }
1915 else if (STRCMP(cmd, "redraw") == 0)
1916 {
1917 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001918
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001919 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001920 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001921 ex_redraw(&ea);
1922 showruler(FALSE);
1923 setcursor();
1924 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001925#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001926 if (gui.in_use)
1927 {
1928 gui_update_cursor(FALSE, FALSE);
1929 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001930 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001931#endif
1932 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001933 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001934 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001935 int is_call = cmd[0] == 'c';
1936 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001937
Bram Moolenaarece61b02016-02-20 21:39:05 +01001938 if (argv[id_idx].v_type != VAR_UNKNOWN
1939 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001940 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001941 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001942 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 EMSG("E904: last argument for expr/call must be a number");
1944 }
1945 else if (is_call && argv[2].v_type != VAR_LIST)
1946 {
1947 ch_error(channel, "third argument for call must be a list");
1948 if (p_verbose > 2)
1949 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001950 }
1951 else
1952 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001953 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001954 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001955 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001956 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001957
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001958 /* Don't pollute the display with errors. */
1959 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001960 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001961 {
1962 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001963 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001964 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001965 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001966 {
1967 ch_logs(channel, "Calling '%s'", (char *)arg);
1968 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1969 tv = &res_tv;
1970 else
1971 tv = NULL;
1972 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001973
1974 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001975 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001976 int id = argv[id_idx].vval.v_number;
1977
Bram Moolenaar55fab432016-02-07 16:53:13 +01001978 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001979 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001980 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001981 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001982 /* If evaluation failed or the result can't be encoded
1983 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001984 vim_free(json);
1985 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001986 err_tv.v_type = VAR_STRING;
1987 err_tv.vval.v_string = (char_u *)"ERROR";
1988 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001989 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001990 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001991 if (json != NULL)
1992 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001993 channel_send(channel,
1994 part == PART_SOCK ? PART_SOCK : PART_IN,
1995 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001996 vim_free(json);
1997 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001998 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001999 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002000 if (tv == &res_tv)
2001 clear_tv(tv);
2002 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002003 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002004 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002005 }
2006 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002007 {
2008 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002009 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002010 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002011}
2012
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002013 static void
2014invoke_one_time_callback(
2015 channel_T *channel,
2016 cbq_T *cbhead,
2017 cbq_T *item,
2018 typval_T *argv)
2019{
2020 ch_logs(channel, "Invoking one-time callback %s",
2021 (char *)item->cq_callback);
2022 /* Remove the item from the list first, if the callback
2023 * invokes ch_close() the list will be cleared. */
2024 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002025 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002026 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002027 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002028 vim_free(item);
2029}
2030
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002031 static void
2032append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2033{
2034 buf_T *save_curbuf = curbuf;
2035 linenr_T lnum = buffer->b_ml.ml_line_count;
2036 int save_write_to = buffer->b_write_to_channel;
2037
2038 /* If the buffer is also used as input insert above the last
2039 * line. Don't write these lines. */
2040 if (save_write_to)
2041 {
2042 --lnum;
2043 buffer->b_write_to_channel = FALSE;
2044 }
2045
2046 /* Append to the buffer */
2047 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2048
2049 curbuf = buffer;
2050 u_sync(TRUE);
2051 /* ignore undo failure, undo is not very useful here */
2052 ignored = u_save(lnum, lnum + 1);
2053
2054 ml_append(lnum, msg, 0, FALSE);
2055 appended_lines_mark(lnum, 1L);
2056 curbuf = save_curbuf;
2057
2058 if (buffer->b_nwindows > 0)
2059 {
2060 win_T *wp;
2061 win_T *save_curwin;
2062
2063 FOR_ALL_WINDOWS(wp)
2064 {
2065 if (wp->w_buffer == buffer
2066 && (save_write_to
2067 ? wp->w_cursor.lnum == lnum + 1
2068 : (wp->w_cursor.lnum == lnum
2069 && wp->w_cursor.col == 0)))
2070 {
2071 ++wp->w_cursor.lnum;
2072 save_curwin = curwin;
2073 curwin = wp;
2074 curbuf = curwin->w_buffer;
2075 scroll_cursor_bot(0, FALSE);
2076 curwin = save_curwin;
2077 curbuf = curwin->w_buffer;
2078 }
2079 }
2080 redraw_buf_later(buffer, VALID);
2081 channel_need_redraw = TRUE;
2082 }
2083
2084 if (save_write_to)
2085 {
2086 channel_T *ch;
2087
2088 /* Find channels reading from this buffer and adjust their
2089 * next-to-read line number. */
2090 buffer->b_write_to_channel = TRUE;
2091 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2092 {
2093 chanpart_T *in_part = &ch->ch_part[PART_IN];
2094
2095 if (in_part->ch_buffer == buffer)
2096 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2097 }
2098 }
2099}
2100
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002101/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002102 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002103 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002104 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002105 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002106may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002107{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002108 char_u *msg = NULL;
2109 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002110 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002111 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002112 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002113 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002114 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002115 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002116 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002117 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002118
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002119 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002120 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002121 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002122
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002123 /* Use a message-specific callback, part callback or channel callback */
2124 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2125 if (cbitem->cq_seq_nr == 0)
2126 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002127 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002128 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002129 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002130 partial = cbitem->cq_partial;
2131 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002132 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002133 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002134 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002135 partial = channel->ch_part[part].ch_partial;
2136 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002137 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002138 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002139 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002140 partial = channel->ch_partial;
2141 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002142
Bram Moolenaar187db502016-02-27 14:44:26 +01002143 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002144 if (buffer != NULL && !buf_valid(buffer))
2145 {
2146 /* buffer was wiped out */
2147 channel->ch_part[part].ch_buffer = NULL;
2148 buffer = NULL;
2149 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002150
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002151 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002152 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002153 listitem_T *item;
2154 int argc = 0;
2155
Bram Moolenaard7ece102016-02-02 23:23:02 +01002156 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002158 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002159 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002160 channel_parse_json(channel, part);
2161 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002162 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002163 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002164
Bram Moolenaarece61b02016-02-20 21:39:05 +01002165 for (item = listtv->vval.v_list->lv_first;
2166 item != NULL && argc < CH_JSON_MAX_ARGS;
2167 item = item->li_next)
2168 argv[argc++] = item->li_tv;
2169 while (argc < CH_JSON_MAX_ARGS)
2170 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002171
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 if (argv[0].v_type == VAR_STRING)
2173 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002174 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002175 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002176 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002177 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002178 }
2179
Bram Moolenaarece61b02016-02-20 21:39:05 +01002180 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002181 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002182 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002183 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002184 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002185 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002186 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002187 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002188 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002189 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002190 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002191 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002192 return FALSE;
2193 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002194 else
2195 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002196 /* If there is no callback or buffer drop the message. */
2197 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002198 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002199 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002200 {
2201 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002202 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01002203 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002204 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002205 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002206
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002207 if (ch_mode == MODE_NL)
2208 {
2209 char_u *nl;
2210 char_u *buf;
2211
2212 /* See if we have a message ending in NL in the first buffer. If
2213 * not try to concatenate the first and the second buffer. */
2214 while (TRUE)
2215 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002216 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002217 nl = vim_strchr(buf, NL);
2218 if (nl != NULL)
2219 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002220 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002221 return FALSE; /* incomplete message */
2222 }
2223 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002224 {
2225 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002226 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002227 *nl = NUL;
2228 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002229 else
2230 {
2231 /* Copy the message into allocated memory and remove it from
2232 * the buffer. */
2233 msg = vim_strnsave(buf, (int)(nl - buf));
2234 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2235 }
2236 }
2237 else
2238 /* For a raw channel we don't know where the message ends, just
2239 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002241
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002242 if (msg == NULL)
2243 return FALSE; /* out of memory (and avoids Coverity warning) */
2244
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002245 argv[1].v_type = VAR_STRING;
2246 argv[1].vval.v_string = msg;
2247 }
2248
Bram Moolenaara07fec92016-02-05 21:04:08 +01002249 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002250 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002251 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002252
2253 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002254 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002255 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002256 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002257 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002258 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002259 break;
2260 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002261 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002262 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002263 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002264 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002265 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002266 if (buffer != NULL)
2267 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002268 if (msg == NULL)
2269 /* JSON or JS mode: re-encode the message. */
2270 msg = json_encode(listtv, ch_mode);
2271 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002272 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002273 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002274
Bram Moolenaar187db502016-02-27 14:44:26 +01002275 if (callback != NULL)
2276 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002277 if (cbitem != NULL)
2278 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2279 else
2280 {
2281 /* invoke the channel callback */
2282 ch_logs(channel, "Invoking channel callback %s",
2283 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002284 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002285 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002286 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002287 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002288 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002289 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002290
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002291 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002292 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002293 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002294
2295 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002296}
2297
2298/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002299 * Return TRUE when channel "channel" is open for writing to.
2300 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002301 */
2302 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002303channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002304{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002305 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002306 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002307}
2308
2309/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002310 * Return TRUE when channel "channel" is open for reading or writing.
2311 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002312 */
2313 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002314channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002315{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002316 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002317 || channel->CH_IN_FD != INVALID_FD
2318 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002319 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002320}
2321
2322/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002323 * Return a string indicating the status of the channel.
2324 */
2325 char *
2326channel_status(channel_T *channel)
2327{
2328 if (channel == NULL)
2329 return "fail";
2330 if (channel_is_open(channel))
2331 return "open";
2332 return "closed";
2333}
2334
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002335 static void
2336channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2337{
2338 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002339 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002340 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002341 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002342
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002343 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002344 STRCAT(namebuf, "_");
2345 tail = STRLEN(namebuf);
2346
2347 STRCPY(namebuf + tail, "status");
2348 dict_add_nr_str(dict, namebuf, 0,
2349 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2350
2351 STRCPY(namebuf + tail, "mode");
2352 switch (chanpart->ch_mode)
2353 {
2354 case MODE_NL: s = "NL"; break;
2355 case MODE_RAW: s = "RAW"; break;
2356 case MODE_JSON: s = "JSON"; break;
2357 case MODE_JS: s = "JS"; break;
2358 }
2359 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2360
2361 STRCPY(namebuf + tail, "io");
2362 if (part == PART_SOCK)
2363 s = "socket";
2364 else switch (chanpart->ch_io)
2365 {
2366 case JIO_NULL: s = "null"; break;
2367 case JIO_PIPE: s = "pipe"; break;
2368 case JIO_FILE: s = "file"; break;
2369 case JIO_BUFFER: s = "buffer"; break;
2370 case JIO_OUT: s = "out"; break;
2371 }
2372 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2373
2374 STRCPY(namebuf + tail, "timeout");
2375 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2376}
2377
2378 void
2379channel_info(channel_T *channel, dict_T *dict)
2380{
2381 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2382 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2383
2384 if (channel->ch_hostname != NULL)
2385 {
2386 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2387 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2388 channel_part_info(channel, dict, "sock", PART_SOCK);
2389 }
2390 else
2391 {
2392 channel_part_info(channel, dict, "out", PART_OUT);
2393 channel_part_info(channel, dict, "err", PART_ERR);
2394 channel_part_info(channel, dict, "in", PART_IN);
2395 }
2396}
2397
Bram Moolenaar77073442016-02-13 23:23:53 +01002398/*
2399 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002400 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002401 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002402 */
2403 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002404channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002405{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002406 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002407
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002408#ifdef FEAT_GUI
2409 channel_gui_unregister(channel);
2410#endif
2411
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002412 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002413 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002414 sock_close(channel->CH_SOCK_FD);
2415 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002416 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002417 may_close_part(&channel->CH_IN_FD);
2418 may_close_part(&channel->CH_OUT_FD);
2419 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002420
Bram Moolenaar8b374212016-02-24 20:43:06 +01002421 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002422 {
2423 typval_T argv[1];
2424 typval_T rettv;
2425 int dummy;
2426
2427 /* invoke the close callback; increment the refcount to avoid it
2428 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002429 ch_logs(channel, "Invoking close callback %s",
2430 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002431 argv[0].v_type = VAR_CHANNEL;
2432 argv[0].vval.v_channel = channel;
2433 ++channel->ch_refcount;
2434 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002435 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2436 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002437 clear_tv(&rettv);
2438 --channel->ch_refcount;
2439
2440 /* the callback is only called once */
2441 vim_free(channel->ch_close_cb);
2442 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002443 partial_unref(channel->ch_close_partial);
2444 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002445 }
2446
2447 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002448}
2449
Bram Moolenaard04a0202016-01-26 23:30:18 +01002450/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002451 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002452 * Returns NULL if there is nothing.
2453 */
2454 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002455channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002456{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002457 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002458
Bram Moolenaar77073442016-02-13 23:23:53 +01002459 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002460 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002461 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002462}
2463
2464/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002465 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002466 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002467 static void
2468channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002469{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002470 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2471 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002472
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002473 while (channel_peek(channel, part) != NULL)
2474 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002475
2476 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002477 {
2478 cbq_T *node = cb_head->cq_next;
2479
2480 remove_cb_node(cb_head, node);
2481 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002482 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002483 vim_free(node);
2484 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002485
2486 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002487 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002488 free_tv(json_head->jq_next->jq_value);
2489 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002490 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002491
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002492 vim_free(channel->ch_part[part].ch_callback);
2493 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002494 partial_unref(channel->ch_part[part].ch_partial);
2495 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002496}
2497
2498/*
2499 * Clear all the read buffers on "channel".
2500 */
2501 void
2502channel_clear(channel_T *channel)
2503{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002504 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002505 vim_free(channel->ch_hostname);
2506 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002507 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002508 channel_clear_one(channel, PART_OUT);
2509 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002510 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002511 vim_free(channel->ch_callback);
2512 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002513 partial_unref(channel->ch_partial);
2514 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002515 vim_free(channel->ch_close_cb);
2516 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002517 partial_unref(channel->ch_close_partial);
2518 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002519}
2520
Bram Moolenaar77073442016-02-13 23:23:53 +01002521#if defined(EXITFREE) || defined(PROTO)
2522 void
2523channel_free_all(void)
2524{
2525 channel_T *channel;
2526
Bram Moolenaard6051b52016-02-28 15:49:03 +01002527 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002528 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2529 channel_clear(channel);
2530}
2531#endif
2532
2533
Bram Moolenaard04a0202016-01-26 23:30:18 +01002534/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002535#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002536
2537/* Buffer size for reading incoming messages. */
2538#define MAXMSGSIZE 4096
2539
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002540#if defined(HAVE_SELECT)
2541/*
2542 * Add write fds where we are waiting for writing to be possible.
2543 */
2544 static int
2545channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2546{
2547 int maxfd = maxfd_arg;
2548 channel_T *ch;
2549
2550 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2551 {
2552 chanpart_T *in_part = &ch->ch_part[PART_IN];
2553
2554 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2555 {
2556 FD_SET((int)in_part->ch_fd, wfds);
2557 if ((int)in_part->ch_fd >= maxfd)
2558 maxfd = (int)in_part->ch_fd + 1;
2559 }
2560 }
2561 return maxfd;
2562}
2563#else
2564/*
2565 * Add write fds where we are waiting for writing to be possible.
2566 */
2567 static int
2568channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2569{
2570 int nfd = nfd_in;
2571 channel_T *ch;
2572
2573 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2574 {
2575 chanpart_T *in_part = &ch->ch_part[PART_IN];
2576
2577 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2578 {
2579 in_part->ch_poll_idx = nfd;
2580 fds[nfd].fd = in_part->ch_fd;
2581 fds[nfd].events = POLLOUT;
2582 ++nfd;
2583 }
2584 else
2585 in_part->ch_poll_idx = -1;
2586 }
2587 return nfd;
2588}
2589#endif
2590
Bram Moolenaard04a0202016-01-26 23:30:18 +01002591/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002592 * Check for reading from "fd" with "timeout" msec.
2593 * Return FAIL when there is nothing to read.
2594 */
2595 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002596channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002597{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002598 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002599 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002600
Bram Moolenaard8070362016-02-15 21:56:54 +01002601# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002602 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002603 {
2604 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002605 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002606 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002607 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002608
2609 /* reading from a pipe, not a socket */
2610 while (TRUE)
2611 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002612 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2613 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002614 return OK;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002615
2616 /* perhaps write some buffer lines */
2617 channel_write_any_lines();
2618
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002619 sleep_time = deadline - GetTickCount();
2620 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002621 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002622 /* Wait for a little while. Very short at first, up to 10 msec
2623 * after looping a few times. */
2624 if (sleep_time > delay)
2625 sleep_time = delay;
2626 Sleep(sleep_time);
2627 delay = delay * 2;
2628 if (delay > 10)
2629 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002630 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002631 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002632 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002633#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002634 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002635#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002636 struct timeval tval;
2637 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002638 fd_set wfds;
2639 int ret;
2640 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002641
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002642 tval.tv_sec = timeout / 1000;
2643 tval.tv_usec = (timeout % 1000) * 1000;
2644 for (;;)
2645 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002646 FD_ZERO(&rfds);
2647 FD_SET((int)fd, &rfds);
2648
2649 /* Write lines to a pipe when a pipe can be written to. Need to
2650 * set this every time, some buffers may be done. */
2651 maxfd = (int)fd + 1;
2652 FD_ZERO(&wfds);
2653 maxfd = channel_fill_wfds(maxfd, &wfds);
2654
2655 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002656# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002657 SOCK_ERRNO;
2658 if (ret == -1 && errno == EINTR)
2659 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002660# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002661 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002662 {
2663 if (FD_ISSET(fd, &rfds))
2664 return OK;
2665 channel_write_any_lines();
2666 continue;
2667 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002668 break;
2669 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002670#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002671 for (;;)
2672 {
2673 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2674 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002675
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002676 fds[0].fd = fd;
2677 fds[0].events = POLLIN;
2678 nfd = channel_fill_poll_write(nfd, fds);
2679 if (poll(fds, nfd, timeout) > 0)
2680 {
2681 if (fds[0].revents & POLLIN)
2682 return OK;
2683 channel_write_any_lines();
2684 continue;
2685 }
2686 break;
2687 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002688#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002689 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002690 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002691}
2692
2693/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002694 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002695 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002696 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002697 */
2698 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002699channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002700{
2701 static char_u *buf = NULL;
2702 int len = 0;
2703 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002704 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002705 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002706
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002707 fd = channel->ch_part[part].ch_fd;
2708 if (fd == INVALID_FD)
2709 {
2710 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002711 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002712 }
2713 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002714
2715 /* Allocate a buffer to read into. */
2716 if (buf == NULL)
2717 {
2718 buf = alloc(MAXMSGSIZE);
2719 if (buf == NULL)
2720 return; /* out of memory! */
2721 }
2722
2723 /* Keep on reading for as long as there is something to read.
2724 * Use select() or poll() to avoid blocking on a message that is exactly
2725 * MAXMSGSIZE long. */
2726 for (;;)
2727 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002728 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002729 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002730 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002731 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002732 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002733 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002734 if (len <= 0)
2735 break; /* error or nothing more to read */
2736
2737 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002738 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002739 readlen += len;
2740 if (len < MAXMSGSIZE)
2741 break; /* did read everything that's available */
2742 }
2743
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002744 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002745 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002746 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002747 /* Do not give an error message, most likely the other end just
2748 * exited. */
2749 ch_errors(channel, "%s(): Cannot read from channel", func);
2750
Bram Moolenaard04a0202016-01-26 23:30:18 +01002751 /* Queue a "DETACH" netbeans message in the command queue in order to
2752 * terminate the netbeans session later. Do not end the session here
2753 * directly as we may be running in the context of a call to
2754 * netbeans_parse_messages():
2755 * netbeans_parse_messages
2756 * -> autocmd triggered while processing the netbeans cmd
2757 * -> ui_breakcheck
2758 * -> gui event loop or select loop
2759 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002760 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002761 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002762 if (channel->ch_part[part].ch_mode == MODE_RAW
2763 || channel->ch_part[part].ch_mode == MODE_NL)
2764 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002765 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002766
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02002767 /* When reading from stdout is not possible, assume the other side has
2768 * died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002769 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002770 if (channel->ch_nb_close_cb != NULL)
2771 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002772 }
2773
2774#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002775 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002776 if (CH_HAS_GUI && gtk_main_level() > 0)
2777 gtk_main_quit();
2778#endif
2779}
2780
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002781/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002782 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002783 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002784 * Returns what was read in allocated memory.
2785 * Returns NULL in case of error or timeout.
2786 */
2787 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002788channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002789{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002790 char_u *buf;
2791 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002792 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002793 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002794 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002795
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002796 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002797 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002798
2799 while (TRUE)
2800 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002801 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002802 if (buf != NULL && (mode == MODE_RAW
2803 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2804 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002805 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002806 continue;
2807
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002808 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002809 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002810 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002811 if (channel_wait(channel, fd, timeout) == FAIL)
2812 {
2813 ch_log(channel, "Timed out");
2814 return NULL;
2815 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002816 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002817 }
2818
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002819 if (mode == MODE_RAW)
2820 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002821 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002822 }
2823 else
2824 {
2825 nl = vim_strchr(buf, NL);
2826 if (nl[1] == NUL)
2827 {
2828 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002829 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002830 *nl = NUL;
2831 }
2832 else
2833 {
2834 /* Copy the message into allocated memory and remove it from the
2835 * buffer. */
2836 msg = vim_strnsave(buf, (int)(nl - buf));
2837 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2838 }
2839 }
2840 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002841 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002842 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002843}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002844
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002845/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002846 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002847 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002848 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002849 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002850 */
2851 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002852channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002853 channel_T *channel,
2854 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002855 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002856 int id,
2857 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002858{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002859 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002860 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002861 int timeout;
2862 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002863
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002864 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002865 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002866 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002867 for (;;)
2868 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002869 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002870
2871 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002872 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002873 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002874 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002875 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002876 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002877
Bram Moolenaard7ece102016-02-02 23:23:02 +01002878 if (!more)
2879 {
2880 /* Handle any other messages in the queue. If done some more
2881 * messages may have arrived. */
2882 if (channel_parse_messages())
2883 continue;
2884
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002885 /* Wait for up to the timeout. If there was an incomplete message
2886 * use the deadline for that. */
2887 timeout = timeout_arg;
2888 if (chanpart->ch_waiting)
2889 {
2890#ifdef WIN32
2891 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2892#else
2893 {
2894 struct timeval now_tv;
2895
2896 gettimeofday(&now_tv, NULL);
2897 timeout = (chanpart->ch_deadline.tv_sec
2898 - now_tv.tv_sec) * 1000
2899 + (chanpart->ch_deadline.tv_usec
2900 - now_tv.tv_usec) / 1000
2901 + 1;
2902 }
2903#endif
2904 if (timeout < 0)
2905 {
2906 /* Something went wrong, channel_parse_json() didn't
2907 * discard message. Cancel waiting. */
2908 chanpart->ch_waiting = FALSE;
2909 timeout = timeout_arg;
2910 }
2911 else if (timeout > timeout_arg)
2912 timeout = timeout_arg;
2913 }
2914 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002915 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002916 {
2917 if (timeout == timeout_arg)
2918 {
2919 if (fd != INVALID_FD)
2920 ch_log(channel, "Timed out");
2921 break;
2922 }
2923 }
2924 else
2925 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002926 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002927 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002928 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002929 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002930}
2931
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002932/*
2933 * Common for ch_read() and ch_readraw().
2934 */
2935 void
2936common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2937{
2938 channel_T *channel;
2939 int part;
2940 jobopt_T opt;
2941 int mode;
2942 int timeout;
2943 int id = -1;
2944 typval_T *listtv = NULL;
2945
2946 /* return an empty string by default */
2947 rettv->v_type = VAR_STRING;
2948 rettv->vval.v_string = NULL;
2949
2950 clear_job_options(&opt);
2951 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2952 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002953 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002954
2955 channel = get_channel_arg(&argvars[0], TRUE);
2956 if (channel != NULL)
2957 {
2958 if (opt.jo_set & JO_PART)
2959 part = opt.jo_part;
2960 else
2961 part = channel_part_read(channel);
2962 mode = channel_get_mode(channel, part);
2963 timeout = channel_get_timeout(channel, part);
2964 if (opt.jo_set & JO_TIMEOUT)
2965 timeout = opt.jo_timeout;
2966
2967 if (raw || mode == MODE_RAW || mode == MODE_NL)
2968 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2969 else
2970 {
2971 if (opt.jo_set & JO_ID)
2972 id = opt.jo_id;
2973 channel_read_json_block(channel, part, timeout, id, &listtv);
2974 if (listtv != NULL)
2975 {
2976 *rettv = *listtv;
2977 vim_free(listtv);
2978 }
2979 else
2980 {
2981 rettv->v_type = VAR_SPECIAL;
2982 rettv->vval.v_number = VVAL_NONE;
2983 }
2984 }
2985 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002986
2987theend:
2988 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002989}
2990
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002991# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2992 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002993/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002994 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002995 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002996 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002997 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002998channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002999{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003000 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003001 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003002
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003003 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003004 for (channel = first_channel; channel != NULL;
3005 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003006 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003007 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003008 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003009 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003010 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003011 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003012 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003013 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003014 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003015}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003016# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003017
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003018# if defined(WIN32) || defined(PROTO)
3019/*
3020 * Check the channels for anything that is ready to be read.
3021 * The data is put in the read queue.
3022 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003023 void
3024channel_handle_events(void)
3025{
3026 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003027 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003028 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003029
3030 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3031 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003032 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003033 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003034 {
3035 fd = channel->ch_part[part].ch_fd;
3036 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
3037 channel_read(channel, part, "channel_handle_events");
3038 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003039 }
3040}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003041# endif
3042
Bram Moolenaard04a0202016-01-26 23:30:18 +01003043/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003044 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003045 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003046 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003047 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003048 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003049channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003050{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003051 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003052 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003053 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003055 fd = channel->ch_part[part].ch_fd;
3056 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003057 {
3058 if (!channel->ch_error && fun != NULL)
3059 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003060 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003061 EMSG2("E630: %s(): write while not connected", fun);
3062 }
3063 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003064 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003065 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003066
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003067 if (log_fd != NULL)
3068 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003069 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003070 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003071 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003072 fprintf(log_fd, "'\n");
3073 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003074 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003075 }
3076
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003077 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003078 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003079 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003080 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003081 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003082 {
3083 if (!channel->ch_error && fun != NULL)
3084 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003085 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003086 EMSG2("E631: %s(): write failed", fun);
3087 }
3088 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003089 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003091
3092 channel->ch_error = FALSE;
3093 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003094}
3095
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003096/*
3097 * Common for "ch_sendexpr()" and "ch_sendraw()".
3098 * Returns the channel if the caller should read the response.
3099 * Sets "part_read" to the the read fd.
3100 * Otherwise returns NULL.
3101 */
3102 channel_T *
3103send_common(
3104 typval_T *argvars,
3105 char_u *text,
3106 int id,
3107 int eval,
3108 jobopt_T *opt,
3109 char *fun,
3110 int *part_read)
3111{
3112 channel_T *channel;
3113 int part_send;
3114
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003115 clear_job_options(opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003116 channel = get_channel_arg(&argvars[0], TRUE);
3117 if (channel == NULL)
3118 return NULL;
3119 part_send = channel_part_send(channel);
3120 *part_read = channel_part_read(channel);
3121
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003122 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3123 return NULL;
3124
3125 /* Set the callback. An empty callback means no callback and not reading
3126 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3127 * allowed. */
3128 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3129 {
3130 if (eval)
3131 {
3132 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3133 return NULL;
3134 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003135 channel_set_req_callback(channel, part_send,
3136 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003137 }
3138
3139 if (channel_send(channel, part_send, text, fun) == OK
3140 && opt->jo_callback == NULL)
3141 return channel;
3142 return NULL;
3143}
3144
3145/*
3146 * common for "ch_evalexpr()" and "ch_sendexpr()"
3147 */
3148 void
3149ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3150{
3151 char_u *text;
3152 typval_T *listtv;
3153 channel_T *channel;
3154 int id;
3155 ch_mode_T ch_mode;
3156 int part_send;
3157 int part_read;
3158 jobopt_T opt;
3159 int timeout;
3160
3161 /* return an empty string by default */
3162 rettv->v_type = VAR_STRING;
3163 rettv->vval.v_string = NULL;
3164
3165 channel = get_channel_arg(&argvars[0], TRUE);
3166 if (channel == NULL)
3167 return;
3168 part_send = channel_part_send(channel);
3169
3170 ch_mode = channel_get_mode(channel, part_send);
3171 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3172 {
3173 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3174 return;
3175 }
3176
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003177 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003178 text = json_encode_nr_expr(id, &argvars[1],
3179 ch_mode == MODE_JS ? JSON_JS : 0);
3180 if (text == NULL)
3181 return;
3182
3183 channel = send_common(argvars, text, id, eval, &opt,
3184 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3185 vim_free(text);
3186 if (channel != NULL && eval)
3187 {
3188 if (opt.jo_set & JO_TIMEOUT)
3189 timeout = opt.jo_timeout;
3190 else
3191 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003192 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3193 == OK)
3194 {
3195 list_T *list = listtv->vval.v_list;
3196
3197 /* Move the item from the list and then change the type to
3198 * avoid the value being freed. */
3199 *rettv = list->lv_last->li_tv;
3200 list->lv_last->li_tv.v_type = VAR_NUMBER;
3201 free_tv(listtv);
3202 }
3203 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003204 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003205}
3206
3207/*
3208 * common for "ch_evalraw()" and "ch_sendraw()"
3209 */
3210 void
3211ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3212{
3213 char_u buf[NUMBUFLEN];
3214 char_u *text;
3215 channel_T *channel;
3216 int part_read;
3217 jobopt_T opt;
3218 int timeout;
3219
3220 /* return an empty string by default */
3221 rettv->v_type = VAR_STRING;
3222 rettv->vval.v_string = NULL;
3223
3224 text = get_tv_string_buf(&argvars[1], buf);
3225 channel = send_common(argvars, text, 0, eval, &opt,
3226 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3227 if (channel != NULL && eval)
3228 {
3229 if (opt.jo_set & JO_TIMEOUT)
3230 timeout = opt.jo_timeout;
3231 else
3232 timeout = channel_get_timeout(channel, part_read);
3233 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3234 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003235 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003236}
3237
Bram Moolenaard04a0202016-01-26 23:30:18 +01003238# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003239/*
3240 * Add open channels to the poll struct.
3241 * Return the adjusted struct index.
3242 * The type of "fds" is hidden to avoid problems with the function proto.
3243 */
3244 int
3245channel_poll_setup(int nfd_in, void *fds_in)
3246{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003247 int nfd = nfd_in;
3248 channel_T *channel;
3249 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003250 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003251
Bram Moolenaar77073442016-02-13 23:23:53 +01003252 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003253 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003254 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003255 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003256 chanpart_T *ch_part = &channel->ch_part[part];
3257
3258 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003259 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003260 ch_part->ch_poll_idx = nfd;
3261 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003262 fds[nfd].events = POLLIN;
3263 nfd++;
3264 }
3265 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003266 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003267 }
3268 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003269
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003270 nfd = channel_fill_poll_write(nfd, fds);
3271
Bram Moolenaare0874f82016-01-24 20:36:41 +01003272 return nfd;
3273}
3274
3275/*
3276 * The type of "fds" is hidden to avoid problems with the function proto.
3277 */
3278 int
3279channel_poll_check(int ret_in, void *fds_in)
3280{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003281 int ret = ret_in;
3282 channel_T *channel;
3283 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003284 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003285 int idx;
3286 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003287
Bram Moolenaar77073442016-02-13 23:23:53 +01003288 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003289 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003290 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003291 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003292 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003293
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003294 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003295 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003296 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003297 --ret;
3298 }
3299 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003300
3301 in_part = &channel->ch_part[PART_IN];
3302 idx = in_part->ch_poll_idx;
3303 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3304 {
3305 if (in_part->ch_buf_append)
3306 {
3307 if (in_part->ch_buffer != NULL)
3308 channel_write_new_lines(in_part->ch_buffer);
3309 }
3310 else
3311 channel_write_in(channel);
3312 --ret;
3313 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003314 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003315
3316 return ret;
3317}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003318# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003319
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003320# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003321/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003322 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003323 */
3324 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003325channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003326{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003327 int maxfd = maxfd_in;
3328 channel_T *channel;
3329 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003330 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003331 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003332
Bram Moolenaar77073442016-02-13 23:23:53 +01003333 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003334 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003335 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003336 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003338
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003339 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003340 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003341 FD_SET((int)fd, rfds);
3342 if (maxfd < (int)fd)
3343 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003344 }
3345 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003346 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003347
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003348 maxfd = channel_fill_wfds(maxfd, wfds);
3349
Bram Moolenaare0874f82016-01-24 20:36:41 +01003350 return maxfd;
3351}
3352
3353/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003354 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003355 */
3356 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003357channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003358{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003359 int ret = ret_in;
3360 channel_T *channel;
3361 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003362 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003363 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003364 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003365
Bram Moolenaar77073442016-02-13 23:23:53 +01003366 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003367 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003368 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003369 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003370 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003371
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003372 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003373 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003374 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003375 --ret;
3376 }
3377 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003378
3379 in_part = &channel->ch_part[PART_IN];
3380 if (ret > 0 && in_part->ch_fd != INVALID_FD
3381 && FD_ISSET(in_part->ch_fd, wfds))
3382 {
3383 if (in_part->ch_buf_append)
3384 {
3385 if (in_part->ch_buffer != NULL)
3386 channel_write_new_lines(in_part->ch_buffer);
3387 }
3388 else
3389 channel_write_in(channel);
3390 --ret;
3391 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003392 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003393
3394 return ret;
3395}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003396# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003397
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003398/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003399 * Return TRUE if "channel" has JSON or other typeahead.
3400 */
3401 static int
3402channel_has_readahead(channel_T *channel, int part)
3403{
3404 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3405
3406 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3407 {
3408 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3409 jsonq_T *item = head->jq_next;
3410
3411 return item != NULL;
3412 }
3413 return channel_peek(channel, part) != NULL;
3414}
3415
3416/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003417 * Execute queued up commands.
3418 * Invoked from the main loop when it's safe to execute received commands.
3419 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003420 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003421 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003422channel_parse_messages(void)
3423{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003424 channel_T *channel = first_channel;
3425 int ret = FALSE;
3426 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003427 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003428
Bram Moolenaard0b65022016-03-06 21:50:33 +01003429 /* Only do this message when another message was given, otherwise we get
3430 * lots of them. */
3431 if (did_log_msg)
3432 {
3433 ch_log(NULL, "looking for messages on channels");
3434 did_log_msg = FALSE;
3435 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003436 while (channel != NULL)
3437 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003438 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003439 {
3440 /* channel is no longer useful, free it */
3441 channel_free(channel);
3442 channel = first_channel;
3443 part = PART_SOCK;
3444 continue;
3445 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003446 if (channel->ch_part[part].ch_fd != INVALID_FD
3447 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003448 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003449 /* Increase the refcount, in case the handler causes the channel
3450 * to be unreferenced or closed. */
3451 ++channel->ch_refcount;
3452 r = may_invoke_callback(channel, part);
3453 if (r == OK)
3454 ret = TRUE;
3455 if (channel_unref(channel) || r == OK)
3456 {
3457 /* channel was freed or something was done, start over */
3458 channel = first_channel;
3459 part = PART_SOCK;
3460 continue;
3461 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003462 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003463 if (part < PART_ERR)
3464 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003465 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003466 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003467 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003468 part = PART_SOCK;
3469 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003470 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003471
3472 if (channel_need_redraw && must_redraw)
3473 {
3474 channel_need_redraw = FALSE;
3475 update_screen(0);
3476 setcursor();
3477 cursor_on();
3478 out_flush();
3479 }
3480
Bram Moolenaard7ece102016-02-02 23:23:02 +01003481 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003482}
3483
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003484/*
3485 * Mark references to lists used in channels.
3486 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003487 int
3488set_ref_in_channel(int copyID)
3489{
Bram Moolenaar77073442016-02-13 23:23:53 +01003490 int abort = FALSE;
3491 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003492 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003493
Bram Moolenaar77073442016-02-13 23:23:53 +01003494 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003495 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003497 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003498 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3499 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003500
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003501 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003502 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003503 list_T *l = item->jq_value->vval.v_list;
3504
3505 if (l->lv_copyID != copyID)
3506 {
3507 l->lv_copyID = copyID;
3508 abort = abort || set_ref_in_list(l, copyID, NULL);
3509 }
3510 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003511 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003512 }
3513 }
3514 return abort;
3515}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003516
3517/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003518 * Return the "part" to write to for "channel".
3519 */
3520 int
3521channel_part_send(channel_T *channel)
3522{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003523 if (channel->CH_SOCK_FD == INVALID_FD)
3524 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003525 return PART_SOCK;
3526}
3527
3528/*
3529 * Return the default "part" to read from for "channel".
3530 */
3531 int
3532channel_part_read(channel_T *channel)
3533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003534 if (channel->CH_SOCK_FD == INVALID_FD)
3535 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003536 return PART_SOCK;
3537}
3538
3539/*
3540 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003541 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003542 */
3543 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003544channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003545{
Bram Moolenaar77073442016-02-13 23:23:53 +01003546 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003547 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003548 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003549}
3550
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003551/*
3552 * Return the timeout of "channel"/"part"
3553 */
3554 int
3555channel_get_timeout(channel_T *channel, int part)
3556{
3557 return channel->ch_part[part].ch_timeout;
3558}
3559
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003560 static int
3561handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3562{
3563 char_u *val = get_tv_string(item);
3564
3565 opt->jo_set |= jo;
3566 if (STRCMP(val, "nl") == 0)
3567 *modep = MODE_NL;
3568 else if (STRCMP(val, "raw") == 0)
3569 *modep = MODE_RAW;
3570 else if (STRCMP(val, "js") == 0)
3571 *modep = MODE_JS;
3572 else if (STRCMP(val, "json") == 0)
3573 *modep = MODE_JSON;
3574 else
3575 {
3576 EMSG2(_(e_invarg2), val);
3577 return FAIL;
3578 }
3579 return OK;
3580}
3581
3582 static int
3583handle_io(typval_T *item, int part, jobopt_T *opt)
3584{
3585 char_u *val = get_tv_string(item);
3586
3587 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3588 if (STRCMP(val, "null") == 0)
3589 opt->jo_io[part] = JIO_NULL;
3590 else if (STRCMP(val, "pipe") == 0)
3591 opt->jo_io[part] = JIO_PIPE;
3592 else if (STRCMP(val, "file") == 0)
3593 opt->jo_io[part] = JIO_FILE;
3594 else if (STRCMP(val, "buffer") == 0)
3595 opt->jo_io[part] = JIO_BUFFER;
3596 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3597 opt->jo_io[part] = JIO_OUT;
3598 else
3599 {
3600 EMSG2(_(e_invarg2), val);
3601 return FAIL;
3602 }
3603 return OK;
3604}
3605
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003606/*
3607 * Clear a jobopt_T before using it.
3608 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003609 void
3610clear_job_options(jobopt_T *opt)
3611{
3612 vim_memset(opt, 0, sizeof(jobopt_T));
3613}
3614
3615/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003616 * Free any members of a jobopt_T.
3617 */
3618 void
3619free_job_options(jobopt_T *opt)
3620{
3621 if (opt->jo_partial != NULL)
3622 partial_unref(opt->jo_partial);
3623 if (opt->jo_out_partial != NULL)
3624 partial_unref(opt->jo_out_partial);
3625 if (opt->jo_err_partial != NULL)
3626 partial_unref(opt->jo_err_partial);
3627 if (opt->jo_close_partial != NULL)
3628 partial_unref(opt->jo_close_partial);
3629}
3630
3631/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003632 * Get the PART_ number from the first character of an option name.
3633 */
3634 static int
3635part_from_char(int c)
3636{
3637 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3638}
3639
3640/*
3641 * Get the option entries from the dict in "tv", parse them and put the result
3642 * in "opt".
3643 * Only accept options in "supported".
3644 * If an option value is invalid return FAIL.
3645 */
3646 int
3647get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3648{
3649 typval_T *item;
3650 char_u *val;
3651 dict_T *dict;
3652 int todo;
3653 hashitem_T *hi;
3654 int part;
3655
3656 opt->jo_set = 0;
3657 if (tv->v_type == VAR_UNKNOWN)
3658 return OK;
3659 if (tv->v_type != VAR_DICT)
3660 {
3661 EMSG(_(e_invarg));
3662 return FAIL;
3663 }
3664 dict = tv->vval.v_dict;
3665 if (dict == NULL)
3666 return OK;
3667
3668 todo = (int)dict->dv_hashtab.ht_used;
3669 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3670 if (!HASHITEM_EMPTY(hi))
3671 {
3672 item = &dict_lookup(hi)->di_tv;
3673
3674 if (STRCMP(hi->hi_key, "mode") == 0)
3675 {
3676 if (!(supported & JO_MODE))
3677 break;
3678 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3679 return FAIL;
3680 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003681 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003682 {
3683 if (!(supported & JO_IN_MODE))
3684 break;
3685 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3686 == FAIL)
3687 return FAIL;
3688 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003689 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003690 {
3691 if (!(supported & JO_OUT_MODE))
3692 break;
3693 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3694 == FAIL)
3695 return FAIL;
3696 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003697 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698 {
3699 if (!(supported & JO_ERR_MODE))
3700 break;
3701 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3702 == FAIL)
3703 return FAIL;
3704 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003705 else if (STRCMP(hi->hi_key, "in_io") == 0
3706 || STRCMP(hi->hi_key, "out_io") == 0
3707 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003708 {
3709 if (!(supported & JO_OUT_IO))
3710 break;
3711 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3712 return FAIL;
3713 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003714 else if (STRCMP(hi->hi_key, "in_name") == 0
3715 || STRCMP(hi->hi_key, "out_name") == 0
3716 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003717 {
3718 part = part_from_char(*hi->hi_key);
3719
3720 if (!(supported & JO_OUT_IO))
3721 break;
3722 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3723 opt->jo_io_name[part] =
3724 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3725 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003726 else if (STRCMP(hi->hi_key, "in_buf") == 0
3727 || STRCMP(hi->hi_key, "out_buf") == 0
3728 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003729 {
3730 part = part_from_char(*hi->hi_key);
3731
3732 if (!(supported & JO_OUT_IO))
3733 break;
3734 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3735 opt->jo_io_buf[part] = get_tv_number(item);
3736 if (opt->jo_io_buf[part] <= 0)
3737 {
3738 EMSG2(_(e_invarg2), get_tv_string(item));
3739 return FAIL;
3740 }
3741 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3742 {
3743 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3744 return FAIL;
3745 }
3746 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003747 else if (STRCMP(hi->hi_key, "in_top") == 0
3748 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003749 {
3750 linenr_T *lp;
3751
3752 if (!(supported & JO_OUT_IO))
3753 break;
3754 if (hi->hi_key[3] == 't')
3755 {
3756 lp = &opt->jo_in_top;
3757 opt->jo_set |= JO_IN_TOP;
3758 }
3759 else
3760 {
3761 lp = &opt->jo_in_bot;
3762 opt->jo_set |= JO_IN_BOT;
3763 }
3764 *lp = get_tv_number(item);
3765 if (*lp < 0)
3766 {
3767 EMSG2(_(e_invarg2), get_tv_string(item));
3768 return FAIL;
3769 }
3770 }
3771 else if (STRCMP(hi->hi_key, "channel") == 0)
3772 {
3773 if (!(supported & JO_OUT_IO))
3774 break;
3775 opt->jo_set |= JO_CHANNEL;
3776 if (item->v_type != VAR_CHANNEL)
3777 {
3778 EMSG2(_(e_invarg2), "channel");
3779 return FAIL;
3780 }
3781 opt->jo_channel = item->vval.v_channel;
3782 }
3783 else if (STRCMP(hi->hi_key, "callback") == 0)
3784 {
3785 if (!(supported & JO_CALLBACK))
3786 break;
3787 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003788 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003789 if (opt->jo_callback == NULL)
3790 {
3791 EMSG2(_(e_invarg2), "callback");
3792 return FAIL;
3793 }
3794 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003795 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003796 {
3797 if (!(supported & JO_OUT_CALLBACK))
3798 break;
3799 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003800 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003801 if (opt->jo_out_cb == NULL)
3802 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003803 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003804 return FAIL;
3805 }
3806 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003807 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003808 {
3809 if (!(supported & JO_ERR_CALLBACK))
3810 break;
3811 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003812 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003813 if (opt->jo_err_cb == NULL)
3814 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003815 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003816 return FAIL;
3817 }
3818 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003819 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003820 {
3821 if (!(supported & JO_CLOSE_CALLBACK))
3822 break;
3823 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003824 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003825 if (opt->jo_close_cb == NULL)
3826 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003827 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003828 return FAIL;
3829 }
3830 }
3831 else if (STRCMP(hi->hi_key, "waittime") == 0)
3832 {
3833 if (!(supported & JO_WAITTIME))
3834 break;
3835 opt->jo_set |= JO_WAITTIME;
3836 opt->jo_waittime = get_tv_number(item);
3837 }
3838 else if (STRCMP(hi->hi_key, "timeout") == 0)
3839 {
3840 if (!(supported & JO_TIMEOUT))
3841 break;
3842 opt->jo_set |= JO_TIMEOUT;
3843 opt->jo_timeout = get_tv_number(item);
3844 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003845 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003846 {
3847 if (!(supported & JO_OUT_TIMEOUT))
3848 break;
3849 opt->jo_set |= JO_OUT_TIMEOUT;
3850 opt->jo_out_timeout = get_tv_number(item);
3851 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003852 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003853 {
3854 if (!(supported & JO_ERR_TIMEOUT))
3855 break;
3856 opt->jo_set |= JO_ERR_TIMEOUT;
3857 opt->jo_err_timeout = get_tv_number(item);
3858 }
3859 else if (STRCMP(hi->hi_key, "part") == 0)
3860 {
3861 if (!(supported & JO_PART))
3862 break;
3863 opt->jo_set |= JO_PART;
3864 val = get_tv_string(item);
3865 if (STRCMP(val, "err") == 0)
3866 opt->jo_part = PART_ERR;
3867 else
3868 {
3869 EMSG2(_(e_invarg2), val);
3870 return FAIL;
3871 }
3872 }
3873 else if (STRCMP(hi->hi_key, "id") == 0)
3874 {
3875 if (!(supported & JO_ID))
3876 break;
3877 opt->jo_set |= JO_ID;
3878 opt->jo_id = get_tv_number(item);
3879 }
3880 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3881 {
3882 if (!(supported & JO_STOPONEXIT))
3883 break;
3884 opt->jo_set |= JO_STOPONEXIT;
3885 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3886 opt->jo_soe_buf);
3887 if (opt->jo_stoponexit == NULL)
3888 {
3889 EMSG2(_(e_invarg2), "stoponexit");
3890 return FAIL;
3891 }
3892 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003893 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003894 {
3895 if (!(supported & JO_EXIT_CB))
3896 break;
3897 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003898 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3899 {
3900 opt->jo_exit_partial = item->vval.v_partial;
3901 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3902 }
3903 else
3904 opt->jo_exit_cb = get_tv_string_buf_chk(
3905 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003906 if (opt->jo_exit_cb == NULL)
3907 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003908 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003909 return FAIL;
3910 }
3911 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003912 else if (STRCMP(hi->hi_key, "block_write") == 0)
3913 {
3914 if (!(supported & JO_BLOCK_WRITE))
3915 break;
3916 opt->jo_set |= JO_BLOCK_WRITE;
3917 opt->jo_block_write = get_tv_number(item);
3918 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003919 else
3920 break;
3921 --todo;
3922 }
3923 if (todo > 0)
3924 {
3925 EMSG2(_(e_invarg2), hi->hi_key);
3926 return FAIL;
3927 }
3928
3929 return OK;
3930}
3931
3932/*
3933 * Get the channel from the argument.
3934 * Returns NULL if the handle is invalid.
3935 */
3936 channel_T *
3937get_channel_arg(typval_T *tv, int check_open)
3938{
3939 channel_T *channel = NULL;
3940
3941 if (tv->v_type == VAR_JOB)
3942 {
3943 if (tv->vval.v_job != NULL)
3944 channel = tv->vval.v_job->jv_channel;
3945 }
3946 else if (tv->v_type == VAR_CHANNEL)
3947 {
3948 channel = tv->vval.v_channel;
3949 }
3950 else
3951 {
3952 EMSG2(_(e_invarg2), get_tv_string(tv));
3953 return NULL;
3954 }
3955
3956 if (check_open && (channel == NULL || !channel_is_open(channel)))
3957 {
3958 EMSG(_("E906: not an open channel"));
3959 return NULL;
3960 }
3961 return channel;
3962}
3963
3964static job_T *first_job = NULL;
3965
3966 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003967job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003968{
3969 ch_log(job->jv_channel, "Freeing job");
3970 if (job->jv_channel != NULL)
3971 {
3972 /* The link from the channel to the job doesn't count as a reference,
3973 * thus don't decrement the refcount of the job. The reference from
3974 * the job to the channel does count the refrence, decrement it and
3975 * NULL the reference. We don't set ch_job_killed, unreferencing the
3976 * job doesn't mean it stops running. */
3977 job->jv_channel->ch_job = NULL;
3978 channel_unref(job->jv_channel);
3979 }
3980 mch_clear_job(job);
3981
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003982 vim_free(job->jv_stoponexit);
3983 vim_free(job->jv_exit_cb);
3984 partial_unref(job->jv_exit_partial);
3985}
3986
3987 static void
3988job_free_job(job_T *job)
3989{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003990 if (job->jv_next != NULL)
3991 job->jv_next->jv_prev = job->jv_prev;
3992 if (job->jv_prev == NULL)
3993 first_job = job->jv_next;
3994 else
3995 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003996 vim_free(job);
3997}
3998
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003999 static void
4000job_free(job_T *job)
4001{
4002 if (!in_free_unref_items)
4003 {
4004 job_free_contents(job);
4005 job_free_job(job);
4006 }
4007}
4008
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009 void
4010job_unref(job_T *job)
4011{
4012 if (job != NULL && --job->jv_refcount <= 0)
4013 {
4014 /* Do not free the job when it has not ended yet and there is a
4015 * "stoponexit" flag or an exit callback. */
4016 if (job->jv_status != JOB_STARTED
4017 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
4018 {
4019 job_free(job);
4020 }
4021 else if (job->jv_channel != NULL)
4022 {
4023 /* Do remove the link to the channel, otherwise it hangs
4024 * around until Vim exits. See job_free() for refcount. */
4025 job->jv_channel->ch_job = NULL;
4026 channel_unref(job->jv_channel);
4027 job->jv_channel = NULL;
4028 }
4029 }
4030}
4031
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004032 int
4033free_unused_jobs_contents(int copyID, int mask)
4034{
4035 int did_free = FALSE;
4036 job_T *job;
4037
4038 for (job = first_job; job != NULL; job = job->jv_next)
4039 if ((job->jv_copyID & mask) != (copyID & mask))
4040 {
4041 /* Free the channel and ordinary items it contains, but don't
4042 * recurse into Lists, Dictionaries etc. */
4043 job_free_contents(job);
4044 did_free = TRUE;
4045 }
4046 return did_free;
4047}
4048
4049 void
4050free_unused_jobs(int copyID, int mask)
4051{
4052 job_T *job;
4053 job_T *job_next;
4054
4055 for (job = first_job; job != NULL; job = job_next)
4056 {
4057 job_next = job->jv_next;
4058 if ((job->jv_copyID & mask) != (copyID & mask))
4059 {
4060 /* Free the channel and ordinary items it contains, but don't
4061 * recurse into Lists, Dictionaries etc. */
4062 job_free_job(job);
4063 }
4064 }
4065}
4066
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004067/*
4068 * Allocate a job. Sets the refcount to one and sets options default.
4069 */
4070 static job_T *
4071job_alloc(void)
4072{
4073 job_T *job;
4074
4075 job = (job_T *)alloc_clear(sizeof(job_T));
4076 if (job != NULL)
4077 {
4078 job->jv_refcount = 1;
4079 job->jv_stoponexit = vim_strsave((char_u *)"term");
4080
4081 if (first_job != NULL)
4082 {
4083 first_job->jv_prev = job;
4084 job->jv_next = first_job;
4085 }
4086 first_job = job;
4087 }
4088 return job;
4089}
4090
4091 void
4092job_set_options(job_T *job, jobopt_T *opt)
4093{
4094 if (opt->jo_set & JO_STOPONEXIT)
4095 {
4096 vim_free(job->jv_stoponexit);
4097 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4098 job->jv_stoponexit = NULL;
4099 else
4100 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4101 }
4102 if (opt->jo_set & JO_EXIT_CB)
4103 {
4104 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004105 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004106 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004107 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004108 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004109 job->jv_exit_partial = NULL;
4110 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004111 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004112 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004114 job->jv_exit_partial = opt->jo_exit_partial;
4115 if (job->jv_exit_partial != NULL)
4116 ++job->jv_exit_partial->pt_refcount;
4117 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 }
4119}
4120
4121/*
4122 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4123 */
4124 void
4125job_stop_on_exit()
4126{
4127 job_T *job;
4128
4129 for (job = first_job; job != NULL; job = job->jv_next)
4130 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4131 mch_stop_job(job, job->jv_stoponexit);
4132}
4133
4134/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004135 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004136 */
4137 void
4138job_check_ended(void)
4139{
4140 static time_t last_check = 0;
4141 time_t now;
4142 job_T *job;
4143 job_T *next;
4144
4145 /* Only do this once in 10 seconds. */
4146 now = time(NULL);
4147 if (last_check + 10 < now)
4148 {
4149 last_check = now;
4150 for (job = first_job; job != NULL; job = next)
4151 {
4152 next = job->jv_next;
4153 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4154 job_status(job); /* may free "job" */
4155 }
4156 }
4157}
4158
4159/*
4160 * "job_start()" function
4161 */
4162 job_T *
4163job_start(typval_T *argvars)
4164{
4165 job_T *job;
4166 char_u *cmd = NULL;
4167#if defined(UNIX)
4168# define USE_ARGV
4169 char **argv = NULL;
4170 int argc = 0;
4171#else
4172 garray_T ga;
4173#endif
4174 jobopt_T opt;
4175 int part;
4176
4177 job = job_alloc();
4178 if (job == NULL)
4179 return NULL;
4180
4181 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004182#ifndef USE_ARGV
4183 ga_init2(&ga, (int)sizeof(char*), 20);
4184#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004185
4186 /* Default mode is NL. */
4187 clear_job_options(&opt);
4188 opt.jo_mode = MODE_NL;
4189 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004190 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4191 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004192 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004193
4194 /* Check that when io is "file" that there is a file name. */
4195 for (part = PART_OUT; part <= PART_IN; ++part)
4196 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4197 && opt.jo_io[part] == JIO_FILE
4198 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4199 || *opt.jo_io_name[part] == NUL))
4200 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004201 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004202 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004203 }
4204
4205 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4206 {
4207 buf_T *buf = NULL;
4208
4209 /* check that we can find the buffer before starting the job */
4210 if (opt.jo_set & JO_IN_BUF)
4211 {
4212 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4213 if (buf == NULL)
4214 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4215 }
4216 else if (!(opt.jo_set & JO_IN_NAME))
4217 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004218 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004219 }
4220 else
4221 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4222 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004223 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004224 if (buf->b_ml.ml_mfp == NULL)
4225 {
4226 char_u numbuf[NUMBUFLEN];
4227 char_u *s;
4228
4229 if (opt.jo_set & JO_IN_BUF)
4230 {
4231 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4232 s = numbuf;
4233 }
4234 else
4235 s = opt.jo_io_name[PART_IN];
4236 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004237 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004238 }
4239 job->jv_in_buf = buf;
4240 }
4241
4242 job_set_options(job, &opt);
4243
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004244 if (argvars[0].v_type == VAR_STRING)
4245 {
4246 /* Command is a string. */
4247 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004248 if (cmd == NULL || *cmd == NUL)
4249 {
4250 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004251 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004252 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004253#ifdef USE_ARGV
4254 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004255 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004256 argv[argc] = NULL;
4257#endif
4258 }
4259 else if (argvars[0].v_type != VAR_LIST
4260 || argvars[0].vval.v_list == NULL
4261 || argvars[0].vval.v_list->lv_len < 1)
4262 {
4263 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004264 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004265 }
4266 else
4267 {
4268 list_T *l = argvars[0].vval.v_list;
4269 listitem_T *li;
4270 char_u *s;
4271
4272#ifdef USE_ARGV
4273 /* Pass argv[] to mch_call_shell(). */
4274 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4275 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004276 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004277#endif
4278 for (li = l->lv_first; li != NULL; li = li->li_next)
4279 {
4280 s = get_tv_string_chk(&li->li_tv);
4281 if (s == NULL)
4282 goto theend;
4283#ifdef USE_ARGV
4284 argv[argc++] = (char *)s;
4285#else
4286 /* Only escape when needed, double quotes are not always allowed. */
4287 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4288 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004289# ifdef WIN32
4290 int old_ssl = p_ssl;
4291
4292 /* This is using CreateProcess, not cmd.exe. Always use
4293 * double quote and backslashes. */
4294 p_ssl = 0;
4295# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004296 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004297# ifdef WIN32
4298 p_ssl = old_ssl;
4299# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004300 if (s == NULL)
4301 goto theend;
4302 ga_concat(&ga, s);
4303 vim_free(s);
4304 }
4305 else
4306 ga_concat(&ga, s);
4307 if (li->li_next != NULL)
4308 ga_append(&ga, ' ');
4309#endif
4310 }
4311#ifdef USE_ARGV
4312 argv[argc] = NULL;
4313#else
4314 cmd = ga.ga_data;
4315#endif
4316 }
4317
4318#ifdef USE_ARGV
4319 if (ch_log_active())
4320 {
4321 garray_T ga;
4322 int i;
4323
4324 ga_init2(&ga, (int)sizeof(char), 200);
4325 for (i = 0; i < argc; ++i)
4326 {
4327 if (i > 0)
4328 ga_concat(&ga, (char_u *)" ");
4329 ga_concat(&ga, (char_u *)argv[i]);
4330 }
4331 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4332 ga_clear(&ga);
4333 }
4334 mch_start_job(argv, job, &opt);
4335#else
4336 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4337 mch_start_job((char *)cmd, job, &opt);
4338#endif
4339
4340 /* If the channel is reading from a buffer, write lines now. */
4341 if (job->jv_channel != NULL)
4342 channel_write_in(job->jv_channel);
4343
4344theend:
4345#ifdef USE_ARGV
4346 vim_free(argv);
4347#else
4348 vim_free(ga.ga_data);
4349#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004350 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004351 return job;
4352}
4353
4354/*
4355 * Get the status of "job" and invoke the exit callback when needed.
4356 * The returned string is not allocated.
4357 */
4358 char *
4359job_status(job_T *job)
4360{
4361 char *result;
4362
4363 if (job->jv_status == JOB_ENDED)
4364 /* No need to check, dead is dead. */
4365 result = "dead";
4366 else if (job->jv_status == JOB_FAILED)
4367 result = "fail";
4368 else
4369 {
4370 result = mch_job_status(job);
4371 if (job->jv_status == JOB_ENDED)
4372 ch_log(job->jv_channel, "Job ended");
4373 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4374 {
4375 typval_T argv[3];
4376 typval_T rettv;
4377 int dummy;
4378
4379 /* invoke the exit callback; make sure the refcount is > 0 */
4380 ++job->jv_refcount;
4381 argv[0].v_type = VAR_JOB;
4382 argv[0].vval.v_job = job;
4383 argv[1].v_type = VAR_NUMBER;
4384 argv[1].vval.v_number = job->jv_exitval;
4385 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004386 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4387 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004388 clear_tv(&rettv);
4389 --job->jv_refcount;
4390 }
4391 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4392 {
4393 /* The job was already unreferenced, now that it ended it can be
4394 * freed. Careful: caller must not use "job" after this! */
4395 job_free(job);
4396 }
4397 }
4398 return result;
4399}
4400
Bram Moolenaar8950a562016-03-12 15:22:55 +01004401/*
4402 * Implementation of job_info().
4403 */
4404 void
4405job_info(job_T *job, dict_T *dict)
4406{
4407 dictitem_T *item;
4408 varnumber_T nr;
4409
4410 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4411
4412 item = dictitem_alloc((char_u *)"channel");
4413 if (item == NULL)
4414 return;
4415 item->di_tv.v_lock = 0;
4416 item->di_tv.v_type = VAR_CHANNEL;
4417 item->di_tv.vval.v_channel = job->jv_channel;
4418 if (job->jv_channel != NULL)
4419 ++job->jv_channel->ch_refcount;
4420 if (dict_add(dict, item) == FAIL)
4421 dictitem_free(item);
4422
4423#ifdef UNIX
4424 nr = job->jv_pid;
4425#else
4426 nr = job->jv_proc_info.dwProcessId;
4427#endif
4428 dict_add_nr_str(dict, "process", nr, NULL);
4429
4430 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004431 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004432 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4433}
4434
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004435 int
4436job_stop(job_T *job, typval_T *argvars)
4437{
4438 char_u *arg;
4439
4440 if (argvars[1].v_type == VAR_UNKNOWN)
4441 arg = (char_u *)"";
4442 else
4443 {
4444 arg = get_tv_string_chk(&argvars[1]);
4445 if (arg == NULL)
4446 {
4447 EMSG(_(e_invarg));
4448 return 0;
4449 }
4450 }
4451 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4452 if (mch_stop_job(job, arg) == FAIL)
4453 return 0;
4454
4455 /* Assume that "hup" does not kill the job. */
4456 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4457 job->jv_channel->ch_job_killed = TRUE;
4458
4459 /* We don't try freeing the job, obviously the caller still has a
4460 * reference to it. */
4461 return 1;
4462}
4463
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004464#endif /* FEAT_JOB_CHANNEL */