blob: b21c4432cc2ed14761d77e72c888f885ff6b582c [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/*
371 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100372 * possible, there is no callback to be invoked or the associated job was
373 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100374 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100375 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100376 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377channel_may_free(channel_T *channel)
378{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100379 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100380 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100382 return TRUE;
383 }
384 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100385}
386
387/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100388 * Decrement the reference count on "channel" and maybe free it when it goes
389 * down to zero. Don't free it if there is a pending action.
390 * Returns TRUE when the channel is no longer referenced.
391 */
392 int
393channel_unref(channel_T *channel)
394{
395 if (channel != NULL && --channel->ch_refcount <= 0)
396 return channel_may_free(channel);
397 return FALSE;
398}
399
400/*
Bram Moolenaar77073442016-02-13 23:23:53 +0100401 * Close a channel and free all its resources.
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100402 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100403 void
404channel_free(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100405{
Bram Moolenaar8b374212016-02-24 20:43:06 +0100406 channel_close(channel, TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +0100407 channel_clear(channel);
Bram Moolenaard6051b52016-02-28 15:49:03 +0100408 ch_log(channel, "Freeing channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100409 if (channel->ch_next != NULL)
410 channel->ch_next->ch_prev = channel->ch_prev;
411 if (channel->ch_prev == NULL)
412 first_channel = channel->ch_next;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100413 else
Bram Moolenaar77073442016-02-13 23:23:53 +0100414 channel->ch_prev->ch_next = channel->ch_next;
415 vim_free(channel);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100416}
417
Bram Moolenaard04a0202016-01-26 23:30:18 +0100418#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100419
420#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
421 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100422channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100423{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100424 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100425 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100426
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100427 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100428 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100429 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100430 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100431 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100432}
433#endif
434
Bram Moolenaare0874f82016-01-24 20:36:41 +0100435/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100436 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100437 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100438#ifdef FEAT_GUI_X11
439 static void
440messageFromNetbeans(XtPointer clientData,
441 int *unused1 UNUSED,
442 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100443{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100444 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100445}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100446#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100447
Bram Moolenaard04a0202016-01-26 23:30:18 +0100448#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100449# if GTK_CHECK_VERSION(3,0,0)
450 static gboolean
451messageFromNetbeans(GIOChannel *unused1 UNUSED,
452 GIOCondition unused2 UNUSED,
453 gpointer clientData)
454{
455 channel_read_fd(GPOINTER_TO_INT(clientData));
456 return TRUE; /* Return FALSE instead in case the event source is to
457 * be removed after this function returns. */
458}
459# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100460 static void
461messageFromNetbeans(gpointer clientData,
462 gint unused1 UNUSED,
463 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100464{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100465 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100466}
Bram Moolenaar98921892016-02-23 17:14:37 +0100467# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100468#endif
469
470 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100471channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100472{
Bram Moolenaarde279892016-03-11 22:19:44 +0100473 if (!CH_HAS_GUI)
474 return;
475
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100476# ifdef FEAT_GUI_X11
477 /* Tell notifier we are interested in being called
478 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100479 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
480 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100481 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100482 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100483 (XtPointer)(XtInputReadMask + XtInputExceptMask),
484 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100485 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100486# else
487# ifdef FEAT_GUI_GTK
488 /* Tell gdk we are interested in being called when there
489 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100490 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100491# if GTK_CHECK_VERSION(3,0,0)
492 {
493 GIOChannel *chnnl = g_io_channel_unix_new(
494 (gint)channel->ch_part[part].ch_fd);
495
496 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
497 chnnl,
498 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
499 messageFromNetbeans,
500 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
501
502 g_io_channel_unref(chnnl);
503 }
504# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100505 channel->ch_part[part].ch_inputHandler = gdk_input_add(
506 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100507 (GdkInputCondition)
508 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100509 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100510 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100511# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100512# endif
513# endif
514}
515
Bram Moolenaarde279892016-03-11 22:19:44 +0100516 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100517channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100519 if (channel->CH_SOCK_FD != INVALID_FD)
520 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100521 if (channel->CH_OUT_FD != INVALID_FD)
522 channel_gui_register_one(channel, PART_OUT);
523 if (channel->CH_ERR_FD != INVALID_FD)
524 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525}
526
527/*
528 * Register any of our file descriptors with the GUI event handling system.
529 * Called when the GUI has started.
530 */
531 void
532channel_gui_register_all(void)
533{
Bram Moolenaar77073442016-02-13 23:23:53 +0100534 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535
Bram Moolenaar77073442016-02-13 23:23:53 +0100536 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100537 channel_gui_register(channel);
538}
539
540 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100541channel_gui_unregister_one(channel_T *channel, int part)
542{
543# ifdef FEAT_GUI_X11
544 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
545 {
546 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
547 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
548 }
549# else
550# ifdef FEAT_GUI_GTK
551 if (channel->ch_part[part].ch_inputHandler != 0)
552 {
553# if GTK_CHECK_VERSION(3,0,0)
554 g_source_remove(channel->ch_part[part].ch_inputHandler);
555# else
556 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
557# endif
558 channel->ch_part[part].ch_inputHandler = 0;
559 }
560# endif
561# endif
562}
563
564 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100565channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100566{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100567 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100568
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100570 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100571}
572
573#endif
574
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100575static char *e_cannot_connect = N_("E902: Cannot connect to port");
576
Bram Moolenaard04a0202016-01-26 23:30:18 +0100577/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100578 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100579 * "waittime" is the time in msec to wait for the connection.
580 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100581 * Returns the channel for success.
582 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100583 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100584 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100585channel_open(
586 char *hostname,
587 int port_in,
588 int waittime,
589 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100590{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100591 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100592 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100593 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100594#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100595 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100596 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100597#else
598 int port = port_in;
599#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100600 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100601 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100602
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100603#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100604 channel_init_winsock();
605#endif
606
Bram Moolenaar77073442016-02-13 23:23:53 +0100607 channel = add_channel();
608 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100609 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100610 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612 }
613
614 /* Get the server internet address and put into addr structure */
615 /* fill in the socket address structure and connect to server */
616 vim_memset((char *)&server, 0, sizeof(server));
617 server.sin_family = AF_INET;
618 server.sin_port = htons(port);
619 if ((host = gethostbyname(hostname)) == NULL)
620 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100621 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100622 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100623 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100624 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100625 }
626 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
627
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100628 /* On Mac and Solaris a zero timeout almost never works. At least wait
629 * one millisecond. Let's do it for all systems, because we don't know why
630 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100631 if (waittime == 0)
632 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100633
634 /*
635 * For Unix we need to call connect() again after connect() failed.
636 * On Win32 one time is sufficient.
637 */
638 while (TRUE)
639 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100640 long elapsed_msec = 0;
641 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100642
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100643 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100644 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100645 sd = socket(AF_INET, SOCK_STREAM, 0);
646 if (sd == -1)
647 {
648 ch_error(channel, "in socket() in channel_open().");
649 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100650 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100651 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100653
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100654 if (waittime >= 0)
655 {
656 /* Make connect() non-blocking. */
657 if (
658#ifdef _WIN32
659 ioctlsocket(sd, FIONBIO, &val) < 0
660#else
661 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
662#endif
663 )
664 {
665 SOCK_ERRNO;
666 ch_errorn(channel,
667 "channel_open: Connect failed with errno %d", errno);
668 sock_close(sd);
669 channel_free(channel);
670 return NULL;
671 }
672 }
673
674 /* Try connecting to the server. */
675 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
676 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
677
Bram Moolenaar045a2842016-03-08 22:33:07 +0100678 if (ret == 0)
679 /* The connection could be established. */
680 break;
681
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100683 if (waittime < 0 || (errno != EWOULDBLOCK
684 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100685#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100686 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100687#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100688 ))
689 {
690 ch_errorn(channel,
691 "channel_open: Connect failed with errno %d", errno);
692 PERROR(_(e_cannot_connect));
693 sock_close(sd);
694 channel_free(channel);
695 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100698 /* Limit the waittime to 50 msec. If it doesn't work within this
699 * time we close the socket and try creating it again. */
700 waitnow = waittime > 50 ? 50 : waittime;
701
Bram Moolenaar045a2842016-03-08 22:33:07 +0100702 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100703 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100704#ifndef WIN32
705 if (errno != ECONNREFUSED)
706#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100707 {
708 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100709 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100710 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100711#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100712 int so_error = 0;
713 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100714 struct timeval start_tv;
715 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100716#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100717 FD_ZERO(&rfds);
718 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100719 FD_ZERO(&wfds);
720 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100721
Bram Moolenaar562ca712016-03-09 21:50:05 +0100722 tv.tv_sec = waitnow / 1000;
723 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100724#ifndef WIN32
725 gettimeofday(&start_tv, NULL);
726#endif
727 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100728 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100729 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (ret < 0)
732 {
733 SOCK_ERRNO;
734 ch_errorn(channel,
735 "channel_open: Connect failed with errno %d", errno);
736 PERROR(_(e_cannot_connect));
737 sock_close(sd);
738 channel_free(channel);
739 return NULL;
740 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100741
Bram Moolenaare081e212016-02-28 22:33:46 +0100742#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100743 /* On Win32: select() is expected to work and wait for up to
744 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100745 if (FD_ISSET(sd, &wfds))
746 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100747 elapsed_msec = waitnow;
748 if (waittime > 1 && elapsed_msec < waittime)
749 {
750 waittime -= elapsed_msec;
751 continue;
752 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100753#else
754 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100755 * After putting the socket in non-blocking mode, connect() will
756 * return EINPROGRESS, select() will not wait (as if writing is
757 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100758 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100759 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100760 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100761 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100762 {
763 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100764 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100765 if (ret < 0 || (so_error != 0
766 && so_error != EWOULDBLOCK
767 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100768# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100769 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100770# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100771 ))
772 {
773 ch_errorn(channel,
774 "channel_open: Connect failed with errno %d",
775 so_error);
776 PERROR(_(e_cannot_connect));
777 sock_close(sd);
778 channel_free(channel);
779 return NULL;
780 }
781 }
782
Bram Moolenaar045a2842016-03-08 22:33:07 +0100783 if (FD_ISSET(sd, &wfds) && so_error == 0)
784 /* Did not detect an error, connection is established. */
785 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100786
Bram Moolenaar045a2842016-03-08 22:33:07 +0100787 gettimeofday(&end_tv, NULL);
788 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
789 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100790#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100791 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100792
793#ifndef WIN32
794 if (waittime > 1 && elapsed_msec < waittime)
795 {
796 /* The port isn't ready but we also didn't get an error.
797 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100798 * yet. Select() may return early, wait until the remaining
799 * "waitnow" and try again. */
800 waitnow -= elapsed_msec;
801 waittime -= elapsed_msec;
802 if (waitnow > 0)
803 {
804 mch_delay((long)waitnow, TRUE);
805 ui_breakcheck();
806 waittime -= waitnow;
807 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 if (!got_int)
809 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100810 if (waittime <= 0)
811 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 waittime = 1;
813 continue;
814 }
815 /* we were interrupted, behave as if timed out */
816 }
817#endif
818
819 /* We timed out. */
820 ch_error(channel, "Connection timed out");
821 sock_close(sd);
822 channel_free(channel);
823 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100824 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100825
Bram Moolenaar045a2842016-03-08 22:33:07 +0100826 ch_log(channel, "Connection made");
827
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100828 if (waittime >= 0)
829 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100830#ifdef _WIN32
831 val = 0;
832 ioctlsocket(sd, FIONBIO, &val);
833#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100834 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100835#endif
836 }
837
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100838 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100839 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100840 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
841 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100842
843#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100844 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100845#endif
846
Bram Moolenaar77073442016-02-13 23:23:53 +0100847 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100848}
849
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100850/*
851 * Implements ch_open().
852 */
853 channel_T *
854channel_open_func(typval_T *argvars)
855{
856 char_u *address;
857 char_u *p;
858 char *rest;
859 int port;
860 jobopt_T opt;
861 channel_T *channel;
862
863 address = get_tv_string(&argvars[0]);
864 if (argvars[1].v_type != VAR_UNKNOWN
865 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
866 {
867 EMSG(_(e_invarg));
868 return NULL;
869 }
870
871 /* parse address */
872 p = vim_strchr(address, ':');
873 if (p == NULL)
874 {
875 EMSG2(_(e_invarg2), address);
876 return NULL;
877 }
878 *p++ = NUL;
879 port = strtol((char *)p, &rest, 10);
880 if (*address == NUL || port <= 0 || *rest != NUL)
881 {
882 p[-1] = ':';
883 EMSG2(_(e_invarg2), address);
884 return NULL;
885 }
886
887 /* parse options */
888 clear_job_options(&opt);
889 opt.jo_mode = MODE_JSON;
890 opt.jo_timeout = 2000;
891 if (get_job_options(&argvars[1], &opt,
892 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
893 return NULL;
894 if (opt.jo_timeout < 0)
895 {
896 EMSG(_(e_invarg));
897 return NULL;
898 }
899
900 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
901 if (channel != NULL)
902 {
903 opt.jo_set = JO_ALL;
904 channel_set_options(channel, &opt);
905 }
906 return channel;
907}
908
Bram Moolenaarde279892016-03-11 22:19:44 +0100909 static void
910may_close_part(sock_T *fd)
911{
912 if (*fd != INVALID_FD)
913 {
914 fd_close(*fd);
915 *fd = INVALID_FD;
916 }
917}
918
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100919 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100920channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100921{
Bram Moolenaarde279892016-03-11 22:19:44 +0100922 if (in != INVALID_FD)
923 {
924 may_close_part(&channel->CH_IN_FD);
925 channel->CH_IN_FD = in;
926 }
927 if (out != INVALID_FD)
928 {
929# if defined(FEAT_GUI)
930 channel_gui_unregister_one(channel, PART_OUT);
931# endif
932 may_close_part(&channel->CH_OUT_FD);
933 channel->CH_OUT_FD = out;
934# if defined(FEAT_GUI)
935 channel_gui_register_one(channel, PART_OUT);
936# endif
937 }
938 if (err != INVALID_FD)
939 {
940# if defined(FEAT_GUI)
941 channel_gui_unregister_one(channel, PART_ERR);
942# endif
943 may_close_part(&channel->CH_ERR_FD);
944 channel->CH_ERR_FD = err;
945# if defined(FEAT_GUI)
946 channel_gui_register_one(channel, PART_ERR);
947# endif
948 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100949}
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100950
Bram Moolenaard6051b52016-02-28 15:49:03 +0100951/*
Bram Moolenaar014069a2016-03-03 22:51:40 +0100952 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +0100953 * This does not keep a refcount, when the job is freed ch_job is cleared.
954 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100955 void
Bram Moolenaar014069a2016-03-03 22:51:40 +0100956channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100957{
Bram Moolenaar77073442016-02-13 23:23:53 +0100958 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +0100959
960 channel_set_options(channel, options);
961
962 if (job->jv_in_buf != NULL)
963 {
964 chanpart_T *in_part = &channel->ch_part[PART_IN];
965
966 in_part->ch_buffer = job->jv_in_buf;
967 ch_logs(channel, "reading from buffer '%s'",
968 (char *)in_part->ch_buffer->b_ffname);
969 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100970 {
971 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
972 {
973 /* Special mode: send last-but-one line when appending a line
974 * to the buffer. */
975 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200976 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100977 in_part->ch_buf_top =
978 in_part->ch_buffer->b_ml.ml_line_count + 1;
979 }
980 else
981 in_part->ch_buf_top = options->jo_in_top;
982 }
Bram Moolenaar014069a2016-03-03 22:51:40 +0100983 else
984 in_part->ch_buf_top = 1;
985 if (options->jo_set & JO_IN_BOT)
986 in_part->ch_buf_bot = options->jo_in_bot;
987 else
988 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
989 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100990}
991
992/*
Bram Moolenaar187db502016-02-27 14:44:26 +0100993 * Find a buffer matching "name" or create a new one.
994 */
995 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100996find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +0100997{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100998 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +0100999 buf_T *save_curbuf = curbuf;
1000
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001001 if (name != NULL && *name != NUL)
1002 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001003 if (buf == NULL)
1004 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001005 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001006 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001007 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001008 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001009#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001010 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1011 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001012#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001013 if (curbuf->b_ml.ml_mfp == NULL)
1014 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001015 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1016 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001017 changed_bytes(1, 0);
1018 curbuf = save_curbuf;
1019 }
1020
1021 return buf;
1022}
1023
1024/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001025 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001026 */
1027 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001028channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001029{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001030 int part;
1031 char_u **cbp;
1032 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001033
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001034 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001035 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001036 channel->ch_part[part].ch_mode = opt->jo_mode;
1037 if (opt->jo_set & JO_IN_MODE)
1038 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1039 if (opt->jo_set & JO_OUT_MODE)
1040 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1041 if (opt->jo_set & JO_ERR_MODE)
1042 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001043
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001044 if (opt->jo_set & JO_TIMEOUT)
1045 for (part = PART_SOCK; part <= PART_IN; ++part)
1046 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1047 if (opt->jo_set & JO_OUT_TIMEOUT)
1048 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1049 if (opt->jo_set & JO_ERR_TIMEOUT)
1050 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001051 if (opt->jo_set & JO_BLOCK_WRITE)
1052 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001053
1054 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001055 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001056 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001057 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001058 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001059 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001060 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1061 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001062 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001063 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001064 *pp = opt->jo_partial;
1065 if (*pp != NULL)
1066 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001067 }
1068 if (opt->jo_set & JO_OUT_CALLBACK)
1069 {
1070 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001071 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001072 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001073 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001074 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1075 *cbp = vim_strsave(opt->jo_out_cb);
1076 else
1077 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001078 *pp = opt->jo_out_partial;
1079 if (*pp != NULL)
1080 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001081 }
1082 if (opt->jo_set & JO_ERR_CALLBACK)
1083 {
1084 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001085 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001086 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001087 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001088 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1089 *cbp = vim_strsave(opt->jo_err_cb);
1090 else
1091 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001092 *pp = opt->jo_err_partial;
1093 if (*pp != NULL)
1094 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001095 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001096 if (opt->jo_set & JO_CLOSE_CALLBACK)
1097 {
1098 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001099 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001100 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001101 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001102 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1103 *cbp = vim_strsave(opt->jo_close_cb);
1104 else
1105 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001106 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001107 if (*pp != NULL)
1108 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001109 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001110
1111 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1112 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001113 /* writing output to a buffer. Default mode is NL. */
1114 if (!(opt->jo_set & JO_OUT_MODE))
1115 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001116 if (opt->jo_set & JO_OUT_BUF)
1117 channel->ch_part[PART_OUT].ch_buffer =
1118 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1119 else
1120 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001121 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1122 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001123 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1124 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001125
1126 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1127 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1128 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1129 {
1130 /* writing err to a buffer. Default mode is NL. */
1131 if (!(opt->jo_set & JO_ERR_MODE))
1132 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1133 if (opt->jo_io[PART_ERR] == JIO_OUT)
1134 channel->ch_part[PART_ERR].ch_buffer =
1135 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001136 else if (opt->jo_set & JO_ERR_BUF)
1137 channel->ch_part[PART_ERR].ch_buffer =
1138 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001139 else
1140 channel->ch_part[PART_ERR].ch_buffer =
1141 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1142 ch_logs(channel, "writing err to buffer '%s'",
1143 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1144 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001145
1146 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1147 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1148 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001149}
1150
1151/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001152 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001153 */
1154 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001155channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001156 channel_T *channel,
1157 int part,
1158 char_u *callback,
1159 partial_T *partial,
1160 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001161{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001162 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001163 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1164
1165 if (item != NULL)
1166 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001167 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001168 item->cq_partial = partial;
1169 if (partial != NULL)
1170 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001171 item->cq_seq_nr = id;
1172 item->cq_prev = head->cq_prev;
1173 head->cq_prev = item;
1174 item->cq_next = NULL;
1175 if (item->cq_prev == NULL)
1176 head->cq_next = item;
1177 else
1178 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001179 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001180}
1181
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001182 static void
1183write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1184{
1185 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001186 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001187 char_u *p;
1188
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001189 if ((p = alloc(len + 2)) == NULL)
1190 return;
1191 STRCPY(p, line);
1192 p[len] = NL;
1193 p[len + 1] = NUL;
1194 channel_send(channel, PART_IN, p, "write_buf_line()");
1195 vim_free(p);
1196}
1197
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001198/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001199 * Return TRUE if "channel" can be written to.
1200 * Returns FALSE if the input is closed or the write would block.
1201 */
1202 static int
1203can_write_buf_line(channel_T *channel)
1204{
1205 chanpart_T *in_part = &channel->ch_part[PART_IN];
1206
1207 if (in_part->ch_fd == INVALID_FD)
1208 return FALSE; /* pipe was closed */
1209
1210 /* for testing: block every other attempt to write */
1211 if (in_part->ch_block_write == 1)
1212 in_part->ch_block_write = -1;
1213 else if (in_part->ch_block_write == -1)
1214 in_part->ch_block_write = 1;
1215
1216 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1217#ifndef WIN32
1218 {
1219# if defined(HAVE_SELECT)
1220 struct timeval tval;
1221 fd_set wfds;
1222 int ret;
1223
1224 FD_ZERO(&wfds);
1225 FD_SET((int)in_part->ch_fd, &wfds);
1226 tval.tv_sec = 0;
1227 tval.tv_usec = 0;
1228 for (;;)
1229 {
1230 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1231# ifdef EINTR
1232 SOCK_ERRNO;
1233 if (ret == -1 && errno == EINTR)
1234 continue;
1235# endif
1236 if (ret <= 0 || in_part->ch_block_write == 1)
1237 {
1238 if (ret > 0)
1239 ch_log(channel, "FAKED Input not ready for writing");
1240 else
1241 ch_log(channel, "Input not ready for writing");
1242 return FALSE;
1243 }
1244 break;
1245 }
1246# else
1247 struct pollfd fds;
1248
1249 fds.fd = in_part->ch_fd;
1250 fds.events = POLLOUT;
1251 if (poll(&fds, 1, 0) <= 0)
1252 {
1253 ch_log(channel, "Input not ready for writing");
1254 return FALSE;
1255 }
1256 if (in_part->ch_block_write == 1)
1257 {
1258 ch_log(channel, "FAKED Input not ready for writing");
1259 return FALSE;
1260 }
1261# endif
1262 }
1263#endif
1264 return TRUE;
1265}
1266
1267/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001268 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001269 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001270 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001271channel_write_in(channel_T *channel)
1272{
1273 chanpart_T *in_part = &channel->ch_part[PART_IN];
1274 linenr_T lnum;
1275 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001276 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001277
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001278 if (buf == NULL || in_part->ch_buf_append)
1279 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001280 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1281 {
1282 /* buffer was wiped out or unloaded */
1283 in_part->ch_buffer = NULL;
1284 return;
1285 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001286
1287 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1288 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1289 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001290 if (!can_write_buf_line(channel))
1291 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001292 write_buf_line(buf, lnum, channel);
1293 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001294 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001295
1296 if (written == 1)
1297 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1298 else if (written > 1)
1299 ch_logn(channel, "written %d lines to channel", written);
1300
Bram Moolenaar014069a2016-03-03 22:51:40 +01001301 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001302 if (lnum > buf->b_ml.ml_line_count)
1303 {
1304 /* Writing is done, no longer need the buffer. */
1305 in_part->ch_buffer = NULL;
1306 ch_log(channel, "Finished writing all lines to channel");
1307 }
1308 else
1309 ch_logn(channel, "Still %d more lines to write",
1310 buf->b_ml.ml_line_count - lnum + 1);
1311}
1312
1313/*
1314 * Write any lines waiting to be written to a channel.
1315 */
1316 void
1317channel_write_any_lines()
1318{
1319 channel_T *channel;
1320
1321 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1322 {
1323 chanpart_T *in_part = &channel->ch_part[PART_IN];
1324
1325 if (in_part->ch_buffer != NULL)
1326 {
1327 if (in_part->ch_buf_append)
1328 channel_write_new_lines(in_part->ch_buffer);
1329 else
1330 channel_write_in(channel);
1331 }
1332 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001333}
1334
1335/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001336 * Write appended lines above the last one in "buf" to the channel.
1337 */
1338 void
1339channel_write_new_lines(buf_T *buf)
1340{
1341 channel_T *channel;
1342 int found_one = FALSE;
1343
1344 /* There could be more than one channel for the buffer, loop over all of
1345 * them. */
1346 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1347 {
1348 chanpart_T *in_part = &channel->ch_part[PART_IN];
1349 linenr_T lnum;
1350 int written = 0;
1351
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001352 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 {
1354 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001355 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001356 found_one = TRUE;
1357 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1358 ++lnum)
1359 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001360 if (!can_write_buf_line(channel))
1361 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001362 write_buf_line(buf, lnum, channel);
1363 ++written;
1364 }
1365
1366 if (written == 1)
1367 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1368 else if (written > 1)
1369 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001370 if (lnum < buf->b_ml.ml_line_count)
1371 ch_logn(channel, "Still %d more lines to write",
1372 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001373
1374 in_part->ch_buf_bot = lnum;
1375 }
1376 }
1377 if (!found_one)
1378 buf->b_write_to_channel = FALSE;
1379}
1380
1381/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001382 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001383 */
1384 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001385invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1386 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001387{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001388 typval_T rettv;
1389 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001390
Bram Moolenaar77073442016-02-13 23:23:53 +01001391 argv[0].v_type = VAR_CHANNEL;
1392 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001393
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001394 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001395 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001396 clear_tv(&rettv);
1397
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001398 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001399}
1400
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001401/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001402 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001403 * The caller must free it.
1404 * Returns NULL if there is nothing.
1405 */
1406 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001407channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001408{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001409 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001410 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001411 char_u *p;
1412
Bram Moolenaar77073442016-02-13 23:23:53 +01001413 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001414 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001415 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001416 p = node->rq_buffer;
1417 head->rq_next = node->rq_next;
1418 if (node->rq_next == NULL)
1419 head->rq_prev = NULL;
1420 else
1421 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001422 vim_free(node);
1423 return p;
1424}
1425
1426/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001427 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001428 */
1429 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001430channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001431{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001432 readq_T *head = &channel->ch_part[part].ch_head;
1433 readq_T *node = head->rq_next;
1434 long_u len = 1;
1435 char_u *res;
1436 char_u *p;
1437
1438 /* If there is only one buffer just get that one. */
1439 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1440 return channel_get(channel, part);
1441
1442 /* Concatenate everything into one buffer. */
1443 for (node = head->rq_next; node != NULL; node = node->rq_next)
1444 len += (long_u)STRLEN(node->rq_buffer);
1445 res = lalloc(len, TRUE);
1446 if (res == NULL)
1447 return NULL;
1448 *res = NUL;
1449 for (node = head->rq_next; node != NULL; node = node->rq_next)
1450 STRCAT(res, node->rq_buffer);
1451
1452 /* Free all buffers */
1453 do
1454 {
1455 p = channel_get(channel, part);
1456 vim_free(p);
1457 } while (p != NULL);
1458
1459 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001460}
1461
1462/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001463 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001464 * Returns FAIL if that is not possible.
1465 */
1466 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001467channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001468{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001469 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001470 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001471 char_u *p;
1472
Bram Moolenaar77073442016-02-13 23:23:53 +01001473 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001474 return FAIL;
1475
Bram Moolenaar77073442016-02-13 23:23:53 +01001476 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1477 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001478 if (p == NULL)
1479 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001480 STRCPY(p, node->rq_buffer);
1481 STRCAT(p, node->rq_next->rq_buffer);
1482 vim_free(node->rq_next->rq_buffer);
1483 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001484
Bram Moolenaar77073442016-02-13 23:23:53 +01001485 /* dispose of the node and its buffer */
1486 head->rq_next = node->rq_next;
1487 head->rq_next->rq_prev = NULL;
1488 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001489 vim_free(node);
1490 return OK;
1491}
1492
1493/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001494 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001495 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001496 * Returns OK or FAIL.
1497 */
1498 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001499channel_save(channel_T *channel, int part, char_u *buf, int len,
1500 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001501{
1502 readq_T *node;
1503 readq_T *head = &channel->ch_part[part].ch_head;
1504 char_u *p;
1505 int i;
1506
1507 node = (readq_T *)alloc(sizeof(readq_T));
1508 if (node == NULL)
1509 return FAIL; /* out of memory */
1510 node->rq_buffer = alloc(len + 1);
1511 if (node->rq_buffer == NULL)
1512 {
1513 vim_free(node);
1514 return FAIL; /* out of memory */
1515 }
1516
1517 if (channel->ch_part[part].ch_mode == MODE_NL)
1518 {
1519 /* Drop any CR before a NL. */
1520 p = node->rq_buffer;
1521 for (i = 0; i < len; ++i)
1522 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1523 *p++ = buf[i];
1524 *p = NUL;
1525 }
1526 else
1527 {
1528 mch_memmove(node->rq_buffer, buf, len);
1529 node->rq_buffer[len] = NUL;
1530 }
1531
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001532 if (prepend)
1533 {
1534 /* preend node to the head of the queue */
1535 node->rq_next = head->rq_next;
1536 node->rq_prev = NULL;
1537 if (head->rq_next == NULL)
1538 head->rq_prev = node;
1539 else
1540 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001541 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001542 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001543 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001544 {
1545 /* append node to the tail of the queue */
1546 node->rq_next = NULL;
1547 node->rq_prev = head->rq_prev;
1548 if (head->rq_prev == NULL)
1549 head->rq_next = node;
1550 else
1551 head->rq_prev->rq_next = node;
1552 head->rq_prev = node;
1553 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001554
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001555 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001557 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001558 fprintf(log_fd, "'");
1559 if (fwrite(buf, len, 1, log_fd) != 1)
1560 return FAIL;
1561 fprintf(log_fd, "'\n");
1562 }
1563 return OK;
1564}
1565
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001566 static int
1567channel_fill(js_read_T *reader)
1568{
1569 channel_T *channel = (channel_T *)reader->js_cookie;
1570 int part = reader->js_cookie_arg;
1571 char_u *next = channel_get(channel, part);
1572 int unused;
1573 int len;
1574 char_u *p;
1575
1576 if (next == NULL)
1577 return FALSE;
1578
1579 unused = reader->js_end - reader->js_buf - reader->js_used;
1580 if (unused > 0)
1581 {
1582 /* Prepend unused text. */
1583 len = (int)STRLEN(next);
1584 p = alloc(unused + len + 1);
1585 if (p == NULL)
1586 {
1587 vim_free(next);
1588 return FALSE;
1589 }
1590 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1591 mch_memmove(p + unused, next, len + 1);
1592 vim_free(next);
1593 next = p;
1594 }
1595
1596 vim_free(reader->js_buf);
1597 reader->js_buf = next;
1598 reader->js_used = 0;
1599 return TRUE;
1600}
1601
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001602/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001603 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001604 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001605 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001606 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001607 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001608channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609{
1610 js_read_T reader;
1611 typval_T listtv;
1612 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001613 chanpart_T *chanpart = &channel->ch_part[part];
1614 jsonq_T *head = &chanpart->ch_json_head;
1615 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001616 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001617
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001619 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001620
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001621 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001623 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001624 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001625 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001626
1627 /* When a message is incomplete we wait for a short while for more to
1628 * arrive. After the delay drop the input, otherwise a truncated string
1629 * or list will make us hang. */
1630 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001631 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001632 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001633 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001634 /* Only accept the response when it is a list with at least two
1635 * items. */
1636 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001637 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001638 if (listtv.v_type != VAR_LIST)
1639 ch_error(channel, "Did not receive a list, discarding");
1640 else
1641 ch_errorn(channel, "Expected list with two items, got %d",
1642 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001643 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001644 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001645 else
1646 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001647 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1648 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001649 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001650 else
1651 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001652 item->jq_value = alloc_tv();
1653 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001654 {
1655 vim_free(item);
1656 clear_tv(&listtv);
1657 }
1658 else
1659 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001660 *item->jq_value = listtv;
1661 item->jq_prev = head->jq_prev;
1662 head->jq_prev = item;
1663 item->jq_next = NULL;
1664 if (item->jq_prev == NULL)
1665 head->jq_next = item;
1666 else
1667 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001668 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001669 }
1670 }
1671 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001672
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001673 if (status == OK)
1674 chanpart->ch_waiting = FALSE;
1675 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001676 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001677 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001678 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001679 /* First time encountering incomplete message, set a deadline of
1680 * 100 msec. */
1681 ch_log(channel, "Incomplete message - wait for more");
1682 reader.js_used = 0;
1683 chanpart->ch_waiting = TRUE;
1684#ifdef WIN32
1685 chanpart->ch_deadline = GetTickCount() + 100L;
1686#else
1687 gettimeofday(&chanpart->ch_deadline, NULL);
1688 chanpart->ch_deadline.tv_usec += 100 * 1000;
1689 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1690 {
1691 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1692 ++chanpart->ch_deadline.tv_sec;
1693 }
1694#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001695 }
1696 else
1697 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001698 int timeout;
1699#ifdef WIN32
1700 timeout = GetTickCount() > chanpart->ch_deadline;
1701#else
1702 {
1703 struct timeval now_tv;
1704
1705 gettimeofday(&now_tv, NULL);
1706 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1707 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1708 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1709 }
1710#endif
1711 if (timeout)
1712 {
1713 status = FAIL;
1714 chanpart->ch_waiting = FALSE;
1715 }
1716 else
1717 {
1718 reader.js_used = 0;
1719 ch_log(channel, "still waiting on incomplete message");
1720 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001721 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001722 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001723
1724 if (status == FAIL)
1725 {
1726 ch_error(channel, "Decoding failed - discarding input");
1727 ret = FALSE;
1728 chanpart->ch_waiting = FALSE;
1729 }
1730 else if (reader.js_buf[reader.js_used] != NUL)
1731 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001732 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001733 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001734 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1735 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001736 ret = status == MAYBE ? FALSE: TRUE;
1737 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001738 else
1739 ret = FALSE;
1740
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001741 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001742 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001743}
1744
1745/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001746 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001747 */
1748 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001749remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001750{
Bram Moolenaar77073442016-02-13 23:23:53 +01001751 if (node->cq_prev == NULL)
1752 head->cq_next = node->cq_next;
1753 else
1754 node->cq_prev->cq_next = node->cq_next;
1755 if (node->cq_next == NULL)
1756 head->cq_prev = node->cq_prev;
1757 else
1758 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001759}
1760
1761/*
1762 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001763 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001764 */
1765 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001766remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001767{
Bram Moolenaar77073442016-02-13 23:23:53 +01001768 if (node->jq_prev == NULL)
1769 head->jq_next = node->jq_next;
1770 else
1771 node->jq_prev->jq_next = node->jq_next;
1772 if (node->jq_next == NULL)
1773 head->jq_prev = node->jq_prev;
1774 else
1775 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001776 vim_free(node);
1777}
1778
1779/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001780 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001781 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001782 * When "id" is zero or negative jut get the first message. But not the one
1783 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001784 * Return OK when found and return the value in "rettv".
1785 * Return FAIL otherwise.
1786 */
1787 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001788channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001789{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001790 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001791 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001792
Bram Moolenaar77073442016-02-13 23:23:53 +01001793 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001795 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 typval_T *tv = &l->lv_first->li_tv;
1797
1798 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001799 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001800 || tv->vval.v_number == 0
1801 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001802 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001803 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001804 if (tv->v_type == VAR_NUMBER)
1805 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001806 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807 return OK;
1808 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001809 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001810 }
1811 return FAIL;
1812}
1813
Bram Moolenaarece61b02016-02-20 21:39:05 +01001814#define CH_JSON_MAX_ARGS 4
1815
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001816/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001817 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001818 * "argv[0]" is the command string.
1819 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001820 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001821 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001822channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001823{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001824 char_u *cmd = argv[0].vval.v_string;
1825 char_u *arg;
1826 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001827
Bram Moolenaarece61b02016-02-20 21:39:05 +01001828 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001829 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001830 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001831 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001832 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001833 return;
1834 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001835 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001836 if (arg == NULL)
1837 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001838
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001839 if (STRCMP(cmd, "ex") == 0)
1840 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001841 int save_called_emsg = called_emsg;
1842
1843 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001844 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001845 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001846 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001847 --emsg_silent;
1848 if (called_emsg)
1849 ch_logs(channel, "Ex command error: '%s'",
1850 (char *)get_vim_var_str(VV_ERRMSG));
1851 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001852 }
1853 else if (STRCMP(cmd, "normal") == 0)
1854 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001855 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001856
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001857 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001858 ea.arg = arg;
1859 ea.addr_count = 0;
1860 ea.forceit = TRUE; /* no mapping */
1861 ex_normal(&ea);
1862 }
1863 else if (STRCMP(cmd, "redraw") == 0)
1864 {
1865 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001866
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001867 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001868 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001869 ex_redraw(&ea);
1870 showruler(FALSE);
1871 setcursor();
1872 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001873#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001874 if (gui.in_use)
1875 {
1876 gui_update_cursor(FALSE, FALSE);
1877 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001878 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001879#endif
1880 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001881 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 int is_call = cmd[0] == 'c';
1884 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001885
Bram Moolenaarece61b02016-02-20 21:39:05 +01001886 if (argv[id_idx].v_type != VAR_UNKNOWN
1887 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001888 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001889 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001890 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001891 EMSG("E904: last argument for expr/call must be a number");
1892 }
1893 else if (is_call && argv[2].v_type != VAR_LIST)
1894 {
1895 ch_error(channel, "third argument for call must be a list");
1896 if (p_verbose > 2)
1897 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001898 }
1899 else
1900 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001901 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001902 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001903 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001904 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001905
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001906 /* Don't pollute the display with errors. */
1907 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001908 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001909 {
1910 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001911 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001912 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001913 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001914 {
1915 ch_logs(channel, "Calling '%s'", (char *)arg);
1916 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1917 tv = &res_tv;
1918 else
1919 tv = NULL;
1920 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001921
1922 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001923 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001924 int id = argv[id_idx].vval.v_number;
1925
Bram Moolenaar55fab432016-02-07 16:53:13 +01001926 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001927 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001928 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001929 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001930 /* If evaluation failed or the result can't be encoded
1931 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001932 vim_free(json);
1933 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001934 err_tv.v_type = VAR_STRING;
1935 err_tv.vval.v_string = (char_u *)"ERROR";
1936 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001937 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001938 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001939 if (json != NULL)
1940 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001941 channel_send(channel,
1942 part == PART_SOCK ? PART_SOCK : PART_IN,
1943 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001944 vim_free(json);
1945 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001946 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001947 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001948 if (tv == &res_tv)
1949 clear_tv(tv);
1950 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001951 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001952 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001953 }
1954 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001955 {
1956 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001957 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001958 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001959}
1960
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001961 static void
1962invoke_one_time_callback(
1963 channel_T *channel,
1964 cbq_T *cbhead,
1965 cbq_T *item,
1966 typval_T *argv)
1967{
1968 ch_logs(channel, "Invoking one-time callback %s",
1969 (char *)item->cq_callback);
1970 /* Remove the item from the list first, if the callback
1971 * invokes ch_close() the list will be cleared. */
1972 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001973 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001974 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001975 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001976 vim_free(item);
1977}
1978
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001979 static void
1980append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
1981{
1982 buf_T *save_curbuf = curbuf;
1983 linenr_T lnum = buffer->b_ml.ml_line_count;
1984 int save_write_to = buffer->b_write_to_channel;
1985
1986 /* If the buffer is also used as input insert above the last
1987 * line. Don't write these lines. */
1988 if (save_write_to)
1989 {
1990 --lnum;
1991 buffer->b_write_to_channel = FALSE;
1992 }
1993
1994 /* Append to the buffer */
1995 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
1996
1997 curbuf = buffer;
1998 u_sync(TRUE);
1999 /* ignore undo failure, undo is not very useful here */
2000 ignored = u_save(lnum, lnum + 1);
2001
2002 ml_append(lnum, msg, 0, FALSE);
2003 appended_lines_mark(lnum, 1L);
2004 curbuf = save_curbuf;
2005
2006 if (buffer->b_nwindows > 0)
2007 {
2008 win_T *wp;
2009 win_T *save_curwin;
2010
2011 FOR_ALL_WINDOWS(wp)
2012 {
2013 if (wp->w_buffer == buffer
2014 && (save_write_to
2015 ? wp->w_cursor.lnum == lnum + 1
2016 : (wp->w_cursor.lnum == lnum
2017 && wp->w_cursor.col == 0)))
2018 {
2019 ++wp->w_cursor.lnum;
2020 save_curwin = curwin;
2021 curwin = wp;
2022 curbuf = curwin->w_buffer;
2023 scroll_cursor_bot(0, FALSE);
2024 curwin = save_curwin;
2025 curbuf = curwin->w_buffer;
2026 }
2027 }
2028 redraw_buf_later(buffer, VALID);
2029 channel_need_redraw = TRUE;
2030 }
2031
2032 if (save_write_to)
2033 {
2034 channel_T *ch;
2035
2036 /* Find channels reading from this buffer and adjust their
2037 * next-to-read line number. */
2038 buffer->b_write_to_channel = TRUE;
2039 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2040 {
2041 chanpart_T *in_part = &ch->ch_part[PART_IN];
2042
2043 if (in_part->ch_buffer == buffer)
2044 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2045 }
2046 }
2047}
2048
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002049/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002050 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002051 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002052 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002053 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002054may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002055{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002056 char_u *msg = NULL;
2057 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002058 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002059 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002060 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002061 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002062 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002063 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002064 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002065 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002066
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002067 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002068 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002069 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002070
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002071 /* Use a message-specific callback, part callback or channel callback */
2072 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2073 if (cbitem->cq_seq_nr == 0)
2074 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002075 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002076 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002077 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002078 partial = cbitem->cq_partial;
2079 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002080 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002081 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002082 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002083 partial = channel->ch_part[part].ch_partial;
2084 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002085 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002086 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002087 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002088 partial = channel->ch_partial;
2089 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002090
Bram Moolenaar187db502016-02-27 14:44:26 +01002091 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002092 if (buffer != NULL && !buf_valid(buffer))
2093 {
2094 /* buffer was wiped out */
2095 channel->ch_part[part].ch_buffer = NULL;
2096 buffer = NULL;
2097 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002098
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002099 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002100 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002101 listitem_T *item;
2102 int argc = 0;
2103
Bram Moolenaard7ece102016-02-02 23:23:02 +01002104 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002105 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002106 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002107 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002108 channel_parse_json(channel, part);
2109 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002110 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002111 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002112
Bram Moolenaarece61b02016-02-20 21:39:05 +01002113 for (item = listtv->vval.v_list->lv_first;
2114 item != NULL && argc < CH_JSON_MAX_ARGS;
2115 item = item->li_next)
2116 argv[argc++] = item->li_tv;
2117 while (argc < CH_JSON_MAX_ARGS)
2118 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002119
Bram Moolenaarece61b02016-02-20 21:39:05 +01002120 if (argv[0].v_type == VAR_STRING)
2121 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002122 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002123 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002124 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002125 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002126 }
2127
Bram Moolenaarece61b02016-02-20 21:39:05 +01002128 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002129 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002130 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002131 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002132 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002133 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002134 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002135 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002136 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002137 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002138 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002139 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002140 return FALSE;
2141 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002142 else
2143 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002144 /* If there is no callback or buffer drop the message. */
2145 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002146 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002147 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002148 {
2149 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002150 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01002151 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002152 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002153 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002154
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002155 if (ch_mode == MODE_NL)
2156 {
2157 char_u *nl;
2158 char_u *buf;
2159
2160 /* See if we have a message ending in NL in the first buffer. If
2161 * not try to concatenate the first and the second buffer. */
2162 while (TRUE)
2163 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002164 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002165 nl = vim_strchr(buf, NL);
2166 if (nl != NULL)
2167 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002168 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002169 return FALSE; /* incomplete message */
2170 }
2171 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002172 {
2173 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002174 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002175 *nl = NUL;
2176 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002177 else
2178 {
2179 /* Copy the message into allocated memory and remove it from
2180 * the buffer. */
2181 msg = vim_strnsave(buf, (int)(nl - buf));
2182 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2183 }
2184 }
2185 else
2186 /* For a raw channel we don't know where the message ends, just
2187 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002188 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002189
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002190 if (msg == NULL)
2191 return FALSE; /* out of memory (and avoids Coverity warning) */
2192
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002193 argv[1].v_type = VAR_STRING;
2194 argv[1].vval.v_string = msg;
2195 }
2196
Bram Moolenaara07fec92016-02-05 21:04:08 +01002197 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002198 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002199 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002200
2201 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002202 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002203 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002204 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002205 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002206 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002207 break;
2208 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002209 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002210 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002211 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002212 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002213 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002214 if (buffer != NULL)
2215 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002216 if (msg == NULL)
2217 /* JSON or JS mode: re-encode the message. */
2218 msg = json_encode(listtv, ch_mode);
2219 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002220 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002221 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002222
Bram Moolenaar187db502016-02-27 14:44:26 +01002223 if (callback != NULL)
2224 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002225 if (cbitem != NULL)
2226 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2227 else
2228 {
2229 /* invoke the channel callback */
2230 ch_logs(channel, "Invoking channel callback %s",
2231 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002232 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002233 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002234 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002235 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002236 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002237 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002238
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002239 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002240 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002241 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002242
2243 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002244}
2245
2246/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002247 * Return TRUE when channel "channel" is open for writing to.
2248 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002249 */
2250 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002251channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002252{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002253 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002254 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002255}
2256
2257/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002258 * Return TRUE when channel "channel" is open for reading or writing.
2259 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002260 */
2261 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002262channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002263{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002264 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002265 || channel->CH_IN_FD != INVALID_FD
2266 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002267 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002268}
2269
2270/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002271 * Return a string indicating the status of the channel.
2272 */
2273 char *
2274channel_status(channel_T *channel)
2275{
2276 if (channel == NULL)
2277 return "fail";
2278 if (channel_is_open(channel))
2279 return "open";
2280 return "closed";
2281}
2282
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002283 static void
2284channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2285{
2286 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002287 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002288 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002289 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002290
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002291 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002292 STRCAT(namebuf, "_");
2293 tail = STRLEN(namebuf);
2294
2295 STRCPY(namebuf + tail, "status");
2296 dict_add_nr_str(dict, namebuf, 0,
2297 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2298
2299 STRCPY(namebuf + tail, "mode");
2300 switch (chanpart->ch_mode)
2301 {
2302 case MODE_NL: s = "NL"; break;
2303 case MODE_RAW: s = "RAW"; break;
2304 case MODE_JSON: s = "JSON"; break;
2305 case MODE_JS: s = "JS"; break;
2306 }
2307 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2308
2309 STRCPY(namebuf + tail, "io");
2310 if (part == PART_SOCK)
2311 s = "socket";
2312 else switch (chanpart->ch_io)
2313 {
2314 case JIO_NULL: s = "null"; break;
2315 case JIO_PIPE: s = "pipe"; break;
2316 case JIO_FILE: s = "file"; break;
2317 case JIO_BUFFER: s = "buffer"; break;
2318 case JIO_OUT: s = "out"; break;
2319 }
2320 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2321
2322 STRCPY(namebuf + tail, "timeout");
2323 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2324}
2325
2326 void
2327channel_info(channel_T *channel, dict_T *dict)
2328{
2329 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2330 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2331
2332 if (channel->ch_hostname != NULL)
2333 {
2334 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2335 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2336 channel_part_info(channel, dict, "sock", PART_SOCK);
2337 }
2338 else
2339 {
2340 channel_part_info(channel, dict, "out", PART_OUT);
2341 channel_part_info(channel, dict, "err", PART_ERR);
2342 channel_part_info(channel, dict, "in", PART_IN);
2343 }
2344}
2345
Bram Moolenaar77073442016-02-13 23:23:53 +01002346/*
2347 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002348 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002349 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002350 */
2351 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002352channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002353{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002354 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002355
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002356#ifdef FEAT_GUI
2357 channel_gui_unregister(channel);
2358#endif
2359
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002360 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002361 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002362 sock_close(channel->CH_SOCK_FD);
2363 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002364 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002365 may_close_part(&channel->CH_IN_FD);
2366 may_close_part(&channel->CH_OUT_FD);
2367 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002368
Bram Moolenaar8b374212016-02-24 20:43:06 +01002369 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002370 {
2371 typval_T argv[1];
2372 typval_T rettv;
2373 int dummy;
2374
2375 /* invoke the close callback; increment the refcount to avoid it
2376 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002377 ch_logs(channel, "Invoking close callback %s",
2378 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002379 argv[0].v_type = VAR_CHANNEL;
2380 argv[0].vval.v_channel = channel;
2381 ++channel->ch_refcount;
2382 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002383 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2384 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002385 clear_tv(&rettv);
2386 --channel->ch_refcount;
2387
2388 /* the callback is only called once */
2389 vim_free(channel->ch_close_cb);
2390 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002391 partial_unref(channel->ch_close_partial);
2392 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002393 }
2394
2395 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002396}
2397
Bram Moolenaard04a0202016-01-26 23:30:18 +01002398/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002399 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002400 * Returns NULL if there is nothing.
2401 */
2402 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002403channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002404{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002405 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002406
Bram Moolenaar77073442016-02-13 23:23:53 +01002407 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002408 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002409 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002410}
2411
2412/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002413 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002414 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002415 static void
2416channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002417{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002418 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2419 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002420
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002421 while (channel_peek(channel, part) != NULL)
2422 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002423
2424 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002425 {
2426 cbq_T *node = cb_head->cq_next;
2427
2428 remove_cb_node(cb_head, node);
2429 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002430 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002431 vim_free(node);
2432 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002433
2434 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002435 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002436 free_tv(json_head->jq_next->jq_value);
2437 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002438 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002439
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002440 vim_free(channel->ch_part[part].ch_callback);
2441 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002442 partial_unref(channel->ch_part[part].ch_partial);
2443 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002444}
2445
2446/*
2447 * Clear all the read buffers on "channel".
2448 */
2449 void
2450channel_clear(channel_T *channel)
2451{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002452 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002453 vim_free(channel->ch_hostname);
2454 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002455 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002456 channel_clear_one(channel, PART_OUT);
2457 channel_clear_one(channel, PART_ERR);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002458 vim_free(channel->ch_callback);
2459 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002460 partial_unref(channel->ch_partial);
2461 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002462 vim_free(channel->ch_close_cb);
2463 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002464 partial_unref(channel->ch_close_partial);
2465 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002466}
2467
Bram Moolenaar77073442016-02-13 23:23:53 +01002468#if defined(EXITFREE) || defined(PROTO)
2469 void
2470channel_free_all(void)
2471{
2472 channel_T *channel;
2473
Bram Moolenaard6051b52016-02-28 15:49:03 +01002474 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002475 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2476 channel_clear(channel);
2477}
2478#endif
2479
2480
Bram Moolenaard04a0202016-01-26 23:30:18 +01002481/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002482#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002483
2484/* Buffer size for reading incoming messages. */
2485#define MAXMSGSIZE 4096
2486
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002487#if defined(HAVE_SELECT)
2488/*
2489 * Add write fds where we are waiting for writing to be possible.
2490 */
2491 static int
2492channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2493{
2494 int maxfd = maxfd_arg;
2495 channel_T *ch;
2496
2497 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2498 {
2499 chanpart_T *in_part = &ch->ch_part[PART_IN];
2500
2501 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2502 {
2503 FD_SET((int)in_part->ch_fd, wfds);
2504 if ((int)in_part->ch_fd >= maxfd)
2505 maxfd = (int)in_part->ch_fd + 1;
2506 }
2507 }
2508 return maxfd;
2509}
2510#else
2511/*
2512 * Add write fds where we are waiting for writing to be possible.
2513 */
2514 static int
2515channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2516{
2517 int nfd = nfd_in;
2518 channel_T *ch;
2519
2520 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2521 {
2522 chanpart_T *in_part = &ch->ch_part[PART_IN];
2523
2524 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2525 {
2526 in_part->ch_poll_idx = nfd;
2527 fds[nfd].fd = in_part->ch_fd;
2528 fds[nfd].events = POLLOUT;
2529 ++nfd;
2530 }
2531 else
2532 in_part->ch_poll_idx = -1;
2533 }
2534 return nfd;
2535}
2536#endif
2537
Bram Moolenaard04a0202016-01-26 23:30:18 +01002538/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002539 * Check for reading from "fd" with "timeout" msec.
2540 * Return FAIL when there is nothing to read.
2541 */
2542 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002543channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002544{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002545 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002546 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002547
Bram Moolenaard8070362016-02-15 21:56:54 +01002548# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002549 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002550 {
2551 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002552 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002553 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002554 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002555
2556 /* reading from a pipe, not a socket */
2557 while (TRUE)
2558 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002559 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2560 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002561 return OK;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002562
2563 /* perhaps write some buffer lines */
2564 channel_write_any_lines();
2565
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002566 sleep_time = deadline - GetTickCount();
2567 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002568 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002569 /* Wait for a little while. Very short at first, up to 10 msec
2570 * after looping a few times. */
2571 if (sleep_time > delay)
2572 sleep_time = delay;
2573 Sleep(sleep_time);
2574 delay = delay * 2;
2575 if (delay > 10)
2576 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002577 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002578 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002579 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002580#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002581 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002582#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002583 struct timeval tval;
2584 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002585 fd_set wfds;
2586 int ret;
2587 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002588
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002589 tval.tv_sec = timeout / 1000;
2590 tval.tv_usec = (timeout % 1000) * 1000;
2591 for (;;)
2592 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002593 FD_ZERO(&rfds);
2594 FD_SET((int)fd, &rfds);
2595
2596 /* Write lines to a pipe when a pipe can be written to. Need to
2597 * set this every time, some buffers may be done. */
2598 maxfd = (int)fd + 1;
2599 FD_ZERO(&wfds);
2600 maxfd = channel_fill_wfds(maxfd, &wfds);
2601
2602 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002603# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002604 SOCK_ERRNO;
2605 if (ret == -1 && errno == EINTR)
2606 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002607# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002608 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002609 {
2610 if (FD_ISSET(fd, &rfds))
2611 return OK;
2612 channel_write_any_lines();
2613 continue;
2614 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002615 break;
2616 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002617#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002618 for (;;)
2619 {
2620 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2621 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002622
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002623 fds[0].fd = fd;
2624 fds[0].events = POLLIN;
2625 nfd = channel_fill_poll_write(nfd, fds);
2626 if (poll(fds, nfd, timeout) > 0)
2627 {
2628 if (fds[0].revents & POLLIN)
2629 return OK;
2630 channel_write_any_lines();
2631 continue;
2632 }
2633 break;
2634 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002635#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002636 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002637 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002638}
2639
2640/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002641 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002642 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002643 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002644 */
2645 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002646channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002647{
2648 static char_u *buf = NULL;
2649 int len = 0;
2650 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002651 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002652 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002653
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002654 fd = channel->ch_part[part].ch_fd;
2655 if (fd == INVALID_FD)
2656 {
2657 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002658 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002659 }
2660 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002661
2662 /* Allocate a buffer to read into. */
2663 if (buf == NULL)
2664 {
2665 buf = alloc(MAXMSGSIZE);
2666 if (buf == NULL)
2667 return; /* out of memory! */
2668 }
2669
2670 /* Keep on reading for as long as there is something to read.
2671 * Use select() or poll() to avoid blocking on a message that is exactly
2672 * MAXMSGSIZE long. */
2673 for (;;)
2674 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002675 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002676 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002677 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002678 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002679 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002680 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002681 if (len <= 0)
2682 break; /* error or nothing more to read */
2683
2684 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002685 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002686 readlen += len;
2687 if (len < MAXMSGSIZE)
2688 break; /* did read everything that's available */
2689 }
2690
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002691 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002692 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002693 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002694 /* Do not give an error message, most likely the other end just
2695 * exited. */
2696 ch_errors(channel, "%s(): Cannot read from channel", func);
2697
Bram Moolenaard04a0202016-01-26 23:30:18 +01002698 /* Queue a "DETACH" netbeans message in the command queue in order to
2699 * terminate the netbeans session later. Do not end the session here
2700 * directly as we may be running in the context of a call to
2701 * netbeans_parse_messages():
2702 * netbeans_parse_messages
2703 * -> autocmd triggered while processing the netbeans cmd
2704 * -> ui_breakcheck
2705 * -> gui event loop or select loop
2706 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002707 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002708 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002709 if (channel->ch_part[part].ch_mode == MODE_RAW
2710 || channel->ch_part[part].ch_mode == MODE_NL)
2711 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002712 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002713
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02002714 /* When reading from stdout is not possible, assume the other side has
2715 * died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002716 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002717 if (channel->ch_nb_close_cb != NULL)
2718 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002719 }
2720
2721#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002722 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002723 if (CH_HAS_GUI && gtk_main_level() > 0)
2724 gtk_main_quit();
2725#endif
2726}
2727
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002728/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002729 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002730 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002731 * Returns what was read in allocated memory.
2732 * Returns NULL in case of error or timeout.
2733 */
2734 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002735channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002736{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002737 char_u *buf;
2738 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002739 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002740 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002741 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002742
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002743 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002744 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002745
2746 while (TRUE)
2747 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002748 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002749 if (buf != NULL && (mode == MODE_RAW
2750 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2751 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002752 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002753 continue;
2754
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002755 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002756 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002757 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002758 if (channel_wait(channel, fd, timeout) == FAIL)
2759 {
2760 ch_log(channel, "Timed out");
2761 return NULL;
2762 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002763 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002764 }
2765
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002766 if (mode == MODE_RAW)
2767 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002768 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002769 }
2770 else
2771 {
2772 nl = vim_strchr(buf, NL);
2773 if (nl[1] == NUL)
2774 {
2775 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002776 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002777 *nl = NUL;
2778 }
2779 else
2780 {
2781 /* Copy the message into allocated memory and remove it from the
2782 * buffer. */
2783 msg = vim_strnsave(buf, (int)(nl - buf));
2784 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2785 }
2786 }
2787 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002788 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002789 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002790}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002791
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002792/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002793 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002794 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002795 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002796 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002797 */
2798 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002799channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002800 channel_T *channel,
2801 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002802 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002803 int id,
2804 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002805{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002806 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002807 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002808 int timeout;
2809 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002810
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002811 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002812 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002813 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002814 for (;;)
2815 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002816 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002817
2818 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002819 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002820 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002821 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002822 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002823 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002824
Bram Moolenaard7ece102016-02-02 23:23:02 +01002825 if (!more)
2826 {
2827 /* Handle any other messages in the queue. If done some more
2828 * messages may have arrived. */
2829 if (channel_parse_messages())
2830 continue;
2831
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002832 /* Wait for up to the timeout. If there was an incomplete message
2833 * use the deadline for that. */
2834 timeout = timeout_arg;
2835 if (chanpart->ch_waiting)
2836 {
2837#ifdef WIN32
2838 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2839#else
2840 {
2841 struct timeval now_tv;
2842
2843 gettimeofday(&now_tv, NULL);
2844 timeout = (chanpart->ch_deadline.tv_sec
2845 - now_tv.tv_sec) * 1000
2846 + (chanpart->ch_deadline.tv_usec
2847 - now_tv.tv_usec) / 1000
2848 + 1;
2849 }
2850#endif
2851 if (timeout < 0)
2852 {
2853 /* Something went wrong, channel_parse_json() didn't
2854 * discard message. Cancel waiting. */
2855 chanpart->ch_waiting = FALSE;
2856 timeout = timeout_arg;
2857 }
2858 else if (timeout > timeout_arg)
2859 timeout = timeout_arg;
2860 }
2861 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002862 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002863 {
2864 if (timeout == timeout_arg)
2865 {
2866 if (fd != INVALID_FD)
2867 ch_log(channel, "Timed out");
2868 break;
2869 }
2870 }
2871 else
2872 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002873 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002874 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002875 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002876 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002877}
2878
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002879/*
2880 * Common for ch_read() and ch_readraw().
2881 */
2882 void
2883common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2884{
2885 channel_T *channel;
2886 int part;
2887 jobopt_T opt;
2888 int mode;
2889 int timeout;
2890 int id = -1;
2891 typval_T *listtv = NULL;
2892
2893 /* return an empty string by default */
2894 rettv->v_type = VAR_STRING;
2895 rettv->vval.v_string = NULL;
2896
2897 clear_job_options(&opt);
2898 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2899 == FAIL)
2900 return;
2901
2902 channel = get_channel_arg(&argvars[0], TRUE);
2903 if (channel != NULL)
2904 {
2905 if (opt.jo_set & JO_PART)
2906 part = opt.jo_part;
2907 else
2908 part = channel_part_read(channel);
2909 mode = channel_get_mode(channel, part);
2910 timeout = channel_get_timeout(channel, part);
2911 if (opt.jo_set & JO_TIMEOUT)
2912 timeout = opt.jo_timeout;
2913
2914 if (raw || mode == MODE_RAW || mode == MODE_NL)
2915 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2916 else
2917 {
2918 if (opt.jo_set & JO_ID)
2919 id = opt.jo_id;
2920 channel_read_json_block(channel, part, timeout, id, &listtv);
2921 if (listtv != NULL)
2922 {
2923 *rettv = *listtv;
2924 vim_free(listtv);
2925 }
2926 else
2927 {
2928 rettv->v_type = VAR_SPECIAL;
2929 rettv->vval.v_number = VVAL_NONE;
2930 }
2931 }
2932 }
2933}
2934
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002935# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2936 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002937/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002938 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002939 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002940 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002941 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002942channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002943{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002944 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002945 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002946
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002947 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01002948 for (channel = first_channel; channel != NULL;
2949 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002950 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002951 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002952 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002953 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002954 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002955 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002956 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002957 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002958 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002959}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002960# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002961
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002962# if defined(WIN32) || defined(PROTO)
2963/*
2964 * Check the channels for anything that is ready to be read.
2965 * The data is put in the read queue.
2966 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002967 void
2968channel_handle_events(void)
2969{
2970 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002971 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002972 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002973
2974 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2975 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002976 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002977 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002978 {
2979 fd = channel->ch_part[part].ch_fd;
2980 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
2981 channel_read(channel, part, "channel_handle_events");
2982 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002983 }
2984}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002985# endif
2986
Bram Moolenaard04a0202016-01-26 23:30:18 +01002987/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002988 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002989 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002990 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002991 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002992 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002993channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002994{
Bram Moolenaard04a0202016-01-26 23:30:18 +01002995 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002996 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002997 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002998
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002999 fd = channel->ch_part[part].ch_fd;
3000 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003001 {
3002 if (!channel->ch_error && fun != NULL)
3003 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003004 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003005 EMSG2("E630: %s(): write while not connected", fun);
3006 }
3007 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003008 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003009 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003010
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003011 if (log_fd != NULL)
3012 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003013 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003014 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003015 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003016 fprintf(log_fd, "'\n");
3017 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003018 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003019 }
3020
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003021 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003022 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003023 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003024 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003025 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003026 {
3027 if (!channel->ch_error && fun != NULL)
3028 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003029 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003030 EMSG2("E631: %s(): write failed", fun);
3031 }
3032 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003033 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003034 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003035
3036 channel->ch_error = FALSE;
3037 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003038}
3039
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003040/*
3041 * Common for "ch_sendexpr()" and "ch_sendraw()".
3042 * Returns the channel if the caller should read the response.
3043 * Sets "part_read" to the the read fd.
3044 * Otherwise returns NULL.
3045 */
3046 channel_T *
3047send_common(
3048 typval_T *argvars,
3049 char_u *text,
3050 int id,
3051 int eval,
3052 jobopt_T *opt,
3053 char *fun,
3054 int *part_read)
3055{
3056 channel_T *channel;
3057 int part_send;
3058
3059 channel = get_channel_arg(&argvars[0], TRUE);
3060 if (channel == NULL)
3061 return NULL;
3062 part_send = channel_part_send(channel);
3063 *part_read = channel_part_read(channel);
3064
3065 clear_job_options(opt);
3066 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3067 return NULL;
3068
3069 /* Set the callback. An empty callback means no callback and not reading
3070 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3071 * allowed. */
3072 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3073 {
3074 if (eval)
3075 {
3076 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3077 return NULL;
3078 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003079 channel_set_req_callback(channel, part_send,
3080 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003081 }
3082
3083 if (channel_send(channel, part_send, text, fun) == OK
3084 && opt->jo_callback == NULL)
3085 return channel;
3086 return NULL;
3087}
3088
3089/*
3090 * common for "ch_evalexpr()" and "ch_sendexpr()"
3091 */
3092 void
3093ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3094{
3095 char_u *text;
3096 typval_T *listtv;
3097 channel_T *channel;
3098 int id;
3099 ch_mode_T ch_mode;
3100 int part_send;
3101 int part_read;
3102 jobopt_T opt;
3103 int timeout;
3104
3105 /* return an empty string by default */
3106 rettv->v_type = VAR_STRING;
3107 rettv->vval.v_string = NULL;
3108
3109 channel = get_channel_arg(&argvars[0], TRUE);
3110 if (channel == NULL)
3111 return;
3112 part_send = channel_part_send(channel);
3113
3114 ch_mode = channel_get_mode(channel, part_send);
3115 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3116 {
3117 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3118 return;
3119 }
3120
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003121 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003122 text = json_encode_nr_expr(id, &argvars[1],
3123 ch_mode == MODE_JS ? JSON_JS : 0);
3124 if (text == NULL)
3125 return;
3126
3127 channel = send_common(argvars, text, id, eval, &opt,
3128 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3129 vim_free(text);
3130 if (channel != NULL && eval)
3131 {
3132 if (opt.jo_set & JO_TIMEOUT)
3133 timeout = opt.jo_timeout;
3134 else
3135 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003136 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3137 == OK)
3138 {
3139 list_T *list = listtv->vval.v_list;
3140
3141 /* Move the item from the list and then change the type to
3142 * avoid the value being freed. */
3143 *rettv = list->lv_last->li_tv;
3144 list->lv_last->li_tv.v_type = VAR_NUMBER;
3145 free_tv(listtv);
3146 }
3147 }
3148}
3149
3150/*
3151 * common for "ch_evalraw()" and "ch_sendraw()"
3152 */
3153 void
3154ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3155{
3156 char_u buf[NUMBUFLEN];
3157 char_u *text;
3158 channel_T *channel;
3159 int part_read;
3160 jobopt_T opt;
3161 int timeout;
3162
3163 /* return an empty string by default */
3164 rettv->v_type = VAR_STRING;
3165 rettv->vval.v_string = NULL;
3166
3167 text = get_tv_string_buf(&argvars[1], buf);
3168 channel = send_common(argvars, text, 0, eval, &opt,
3169 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3170 if (channel != NULL && eval)
3171 {
3172 if (opt.jo_set & JO_TIMEOUT)
3173 timeout = opt.jo_timeout;
3174 else
3175 timeout = channel_get_timeout(channel, part_read);
3176 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3177 }
3178}
3179
Bram Moolenaard04a0202016-01-26 23:30:18 +01003180# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003181/*
3182 * Add open channels to the poll struct.
3183 * Return the adjusted struct index.
3184 * The type of "fds" is hidden to avoid problems with the function proto.
3185 */
3186 int
3187channel_poll_setup(int nfd_in, void *fds_in)
3188{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003189 int nfd = nfd_in;
3190 channel_T *channel;
3191 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003192 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003193
Bram Moolenaar77073442016-02-13 23:23:53 +01003194 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003195 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003196 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003197 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003198 chanpart_T *ch_part = &channel->ch_part[part];
3199
3200 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003201 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003202 ch_part->ch_poll_idx = nfd;
3203 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003204 fds[nfd].events = POLLIN;
3205 nfd++;
3206 }
3207 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003208 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003209 }
3210 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003211
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003212 nfd = channel_fill_poll_write(nfd, fds);
3213
Bram Moolenaare0874f82016-01-24 20:36:41 +01003214 return nfd;
3215}
3216
3217/*
3218 * The type of "fds" is hidden to avoid problems with the function proto.
3219 */
3220 int
3221channel_poll_check(int ret_in, void *fds_in)
3222{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003223 int ret = ret_in;
3224 channel_T *channel;
3225 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003226 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003227 int idx;
3228 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003229
Bram Moolenaar77073442016-02-13 23:23:53 +01003230 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003231 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003232 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003233 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003234 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003235
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003236 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003237 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003238 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003239 --ret;
3240 }
3241 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003242
3243 in_part = &channel->ch_part[PART_IN];
3244 idx = in_part->ch_poll_idx;
3245 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3246 {
3247 if (in_part->ch_buf_append)
3248 {
3249 if (in_part->ch_buffer != NULL)
3250 channel_write_new_lines(in_part->ch_buffer);
3251 }
3252 else
3253 channel_write_in(channel);
3254 --ret;
3255 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003256 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003257
3258 return ret;
3259}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003260# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003261
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003262# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003263/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003264 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003265 */
3266 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003267channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003268{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003269 int maxfd = maxfd_in;
3270 channel_T *channel;
3271 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003272 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003273 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003274
Bram Moolenaar77073442016-02-13 23:23:53 +01003275 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003276 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003277 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003278 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003279 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003280
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003281 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003282 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003283 FD_SET((int)fd, rfds);
3284 if (maxfd < (int)fd)
3285 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003286 }
3287 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003288 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003289
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003290 maxfd = channel_fill_wfds(maxfd, wfds);
3291
Bram Moolenaare0874f82016-01-24 20:36:41 +01003292 return maxfd;
3293}
3294
3295/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003296 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003297 */
3298 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003299channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003300{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003301 int ret = ret_in;
3302 channel_T *channel;
3303 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003304 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003305 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003306 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003307
Bram Moolenaar77073442016-02-13 23:23:53 +01003308 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003309 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003310 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003311 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003312 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003313
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003314 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003315 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003316 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003317 --ret;
3318 }
3319 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003320
3321 in_part = &channel->ch_part[PART_IN];
3322 if (ret > 0 && in_part->ch_fd != INVALID_FD
3323 && FD_ISSET(in_part->ch_fd, wfds))
3324 {
3325 if (in_part->ch_buf_append)
3326 {
3327 if (in_part->ch_buffer != NULL)
3328 channel_write_new_lines(in_part->ch_buffer);
3329 }
3330 else
3331 channel_write_in(channel);
3332 --ret;
3333 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003334 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003335
3336 return ret;
3337}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003338# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003339
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003340/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003341 * Return TRUE if "channel" has JSON or other typeahead.
3342 */
3343 static int
3344channel_has_readahead(channel_T *channel, int part)
3345{
3346 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3347
3348 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3349 {
3350 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3351 jsonq_T *item = head->jq_next;
3352
3353 return item != NULL;
3354 }
3355 return channel_peek(channel, part) != NULL;
3356}
3357
3358/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003359 * Execute queued up commands.
3360 * Invoked from the main loop when it's safe to execute received commands.
3361 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003362 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003363 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003364channel_parse_messages(void)
3365{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003366 channel_T *channel = first_channel;
3367 int ret = FALSE;
3368 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003369 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003370
Bram Moolenaard0b65022016-03-06 21:50:33 +01003371 /* Only do this message when another message was given, otherwise we get
3372 * lots of them. */
3373 if (did_log_msg)
3374 {
3375 ch_log(NULL, "looking for messages on channels");
3376 did_log_msg = FALSE;
3377 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003378 while (channel != NULL)
3379 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003380 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003381 {
3382 /* channel is no longer useful, free it */
3383 channel_free(channel);
3384 channel = first_channel;
3385 part = PART_SOCK;
3386 continue;
3387 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003388 if (channel->ch_part[part].ch_fd != INVALID_FD
3389 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003390 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003391 /* Increase the refcount, in case the handler causes the channel
3392 * to be unreferenced or closed. */
3393 ++channel->ch_refcount;
3394 r = may_invoke_callback(channel, part);
3395 if (r == OK)
3396 ret = TRUE;
3397 if (channel_unref(channel) || r == OK)
3398 {
3399 /* channel was freed or something was done, start over */
3400 channel = first_channel;
3401 part = PART_SOCK;
3402 continue;
3403 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003404 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003405 if (part < PART_ERR)
3406 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003407 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003408 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003409 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003410 part = PART_SOCK;
3411 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003412 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003413
3414 if (channel_need_redraw && must_redraw)
3415 {
3416 channel_need_redraw = FALSE;
3417 update_screen(0);
3418 setcursor();
3419 cursor_on();
3420 out_flush();
3421 }
3422
Bram Moolenaard7ece102016-02-02 23:23:02 +01003423 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003424}
3425
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003426/*
3427 * Mark references to lists used in channels.
3428 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003429 int
3430set_ref_in_channel(int copyID)
3431{
Bram Moolenaar77073442016-02-13 23:23:53 +01003432 int abort = FALSE;
3433 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003434 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003435
Bram Moolenaar77073442016-02-13 23:23:53 +01003436 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003437 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003438 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003439 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003440 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3441 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003442
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003443 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003444 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003445 list_T *l = item->jq_value->vval.v_list;
3446
3447 if (l->lv_copyID != copyID)
3448 {
3449 l->lv_copyID = copyID;
3450 abort = abort || set_ref_in_list(l, copyID, NULL);
3451 }
3452 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003453 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003454 }
3455 }
3456 return abort;
3457}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003458
3459/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003460 * Return the "part" to write to for "channel".
3461 */
3462 int
3463channel_part_send(channel_T *channel)
3464{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003465 if (channel->CH_SOCK_FD == INVALID_FD)
3466 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003467 return PART_SOCK;
3468}
3469
3470/*
3471 * Return the default "part" to read from for "channel".
3472 */
3473 int
3474channel_part_read(channel_T *channel)
3475{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003476 if (channel->CH_SOCK_FD == INVALID_FD)
3477 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003478 return PART_SOCK;
3479}
3480
3481/*
3482 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003483 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003484 */
3485 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003486channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003487{
Bram Moolenaar77073442016-02-13 23:23:53 +01003488 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003489 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003490 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003491}
3492
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003493/*
3494 * Return the timeout of "channel"/"part"
3495 */
3496 int
3497channel_get_timeout(channel_T *channel, int part)
3498{
3499 return channel->ch_part[part].ch_timeout;
3500}
3501
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003502 static int
3503handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3504{
3505 char_u *val = get_tv_string(item);
3506
3507 opt->jo_set |= jo;
3508 if (STRCMP(val, "nl") == 0)
3509 *modep = MODE_NL;
3510 else if (STRCMP(val, "raw") == 0)
3511 *modep = MODE_RAW;
3512 else if (STRCMP(val, "js") == 0)
3513 *modep = MODE_JS;
3514 else if (STRCMP(val, "json") == 0)
3515 *modep = MODE_JSON;
3516 else
3517 {
3518 EMSG2(_(e_invarg2), val);
3519 return FAIL;
3520 }
3521 return OK;
3522}
3523
3524 static int
3525handle_io(typval_T *item, int part, jobopt_T *opt)
3526{
3527 char_u *val = get_tv_string(item);
3528
3529 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3530 if (STRCMP(val, "null") == 0)
3531 opt->jo_io[part] = JIO_NULL;
3532 else if (STRCMP(val, "pipe") == 0)
3533 opt->jo_io[part] = JIO_PIPE;
3534 else if (STRCMP(val, "file") == 0)
3535 opt->jo_io[part] = JIO_FILE;
3536 else if (STRCMP(val, "buffer") == 0)
3537 opt->jo_io[part] = JIO_BUFFER;
3538 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3539 opt->jo_io[part] = JIO_OUT;
3540 else
3541 {
3542 EMSG2(_(e_invarg2), val);
3543 return FAIL;
3544 }
3545 return OK;
3546}
3547
3548 void
3549clear_job_options(jobopt_T *opt)
3550{
3551 vim_memset(opt, 0, sizeof(jobopt_T));
3552}
3553
3554/*
3555 * Get the PART_ number from the first character of an option name.
3556 */
3557 static int
3558part_from_char(int c)
3559{
3560 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3561}
3562
3563/*
3564 * Get the option entries from the dict in "tv", parse them and put the result
3565 * in "opt".
3566 * Only accept options in "supported".
3567 * If an option value is invalid return FAIL.
3568 */
3569 int
3570get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3571{
3572 typval_T *item;
3573 char_u *val;
3574 dict_T *dict;
3575 int todo;
3576 hashitem_T *hi;
3577 int part;
3578
3579 opt->jo_set = 0;
3580 if (tv->v_type == VAR_UNKNOWN)
3581 return OK;
3582 if (tv->v_type != VAR_DICT)
3583 {
3584 EMSG(_(e_invarg));
3585 return FAIL;
3586 }
3587 dict = tv->vval.v_dict;
3588 if (dict == NULL)
3589 return OK;
3590
3591 todo = (int)dict->dv_hashtab.ht_used;
3592 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3593 if (!HASHITEM_EMPTY(hi))
3594 {
3595 item = &dict_lookup(hi)->di_tv;
3596
3597 if (STRCMP(hi->hi_key, "mode") == 0)
3598 {
3599 if (!(supported & JO_MODE))
3600 break;
3601 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3602 return FAIL;
3603 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003604 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003605 {
3606 if (!(supported & JO_IN_MODE))
3607 break;
3608 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3609 == FAIL)
3610 return FAIL;
3611 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003612 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003613 {
3614 if (!(supported & JO_OUT_MODE))
3615 break;
3616 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3617 == FAIL)
3618 return FAIL;
3619 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003620 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003621 {
3622 if (!(supported & JO_ERR_MODE))
3623 break;
3624 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3625 == FAIL)
3626 return FAIL;
3627 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003628 else if (STRCMP(hi->hi_key, "in_io") == 0
3629 || STRCMP(hi->hi_key, "out_io") == 0
3630 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003631 {
3632 if (!(supported & JO_OUT_IO))
3633 break;
3634 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3635 return FAIL;
3636 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003637 else if (STRCMP(hi->hi_key, "in_name") == 0
3638 || STRCMP(hi->hi_key, "out_name") == 0
3639 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003640 {
3641 part = part_from_char(*hi->hi_key);
3642
3643 if (!(supported & JO_OUT_IO))
3644 break;
3645 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3646 opt->jo_io_name[part] =
3647 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3648 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003649 else if (STRCMP(hi->hi_key, "in_buf") == 0
3650 || STRCMP(hi->hi_key, "out_buf") == 0
3651 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003652 {
3653 part = part_from_char(*hi->hi_key);
3654
3655 if (!(supported & JO_OUT_IO))
3656 break;
3657 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3658 opt->jo_io_buf[part] = get_tv_number(item);
3659 if (opt->jo_io_buf[part] <= 0)
3660 {
3661 EMSG2(_(e_invarg2), get_tv_string(item));
3662 return FAIL;
3663 }
3664 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3665 {
3666 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3667 return FAIL;
3668 }
3669 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003670 else if (STRCMP(hi->hi_key, "in_top") == 0
3671 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003672 {
3673 linenr_T *lp;
3674
3675 if (!(supported & JO_OUT_IO))
3676 break;
3677 if (hi->hi_key[3] == 't')
3678 {
3679 lp = &opt->jo_in_top;
3680 opt->jo_set |= JO_IN_TOP;
3681 }
3682 else
3683 {
3684 lp = &opt->jo_in_bot;
3685 opt->jo_set |= JO_IN_BOT;
3686 }
3687 *lp = get_tv_number(item);
3688 if (*lp < 0)
3689 {
3690 EMSG2(_(e_invarg2), get_tv_string(item));
3691 return FAIL;
3692 }
3693 }
3694 else if (STRCMP(hi->hi_key, "channel") == 0)
3695 {
3696 if (!(supported & JO_OUT_IO))
3697 break;
3698 opt->jo_set |= JO_CHANNEL;
3699 if (item->v_type != VAR_CHANNEL)
3700 {
3701 EMSG2(_(e_invarg2), "channel");
3702 return FAIL;
3703 }
3704 opt->jo_channel = item->vval.v_channel;
3705 }
3706 else if (STRCMP(hi->hi_key, "callback") == 0)
3707 {
3708 if (!(supported & JO_CALLBACK))
3709 break;
3710 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003711 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003712 if (opt->jo_callback == NULL)
3713 {
3714 EMSG2(_(e_invarg2), "callback");
3715 return FAIL;
3716 }
3717 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003718 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003719 {
3720 if (!(supported & JO_OUT_CALLBACK))
3721 break;
3722 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003723 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003724 if (opt->jo_out_cb == NULL)
3725 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003726 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003727 return FAIL;
3728 }
3729 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003730 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003731 {
3732 if (!(supported & JO_ERR_CALLBACK))
3733 break;
3734 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003735 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003736 if (opt->jo_err_cb == NULL)
3737 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003738 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003739 return FAIL;
3740 }
3741 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003742 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003743 {
3744 if (!(supported & JO_CLOSE_CALLBACK))
3745 break;
3746 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003747 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003748 if (opt->jo_close_cb == NULL)
3749 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003750 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003751 return FAIL;
3752 }
3753 }
3754 else if (STRCMP(hi->hi_key, "waittime") == 0)
3755 {
3756 if (!(supported & JO_WAITTIME))
3757 break;
3758 opt->jo_set |= JO_WAITTIME;
3759 opt->jo_waittime = get_tv_number(item);
3760 }
3761 else if (STRCMP(hi->hi_key, "timeout") == 0)
3762 {
3763 if (!(supported & JO_TIMEOUT))
3764 break;
3765 opt->jo_set |= JO_TIMEOUT;
3766 opt->jo_timeout = get_tv_number(item);
3767 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003768 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003769 {
3770 if (!(supported & JO_OUT_TIMEOUT))
3771 break;
3772 opt->jo_set |= JO_OUT_TIMEOUT;
3773 opt->jo_out_timeout = get_tv_number(item);
3774 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003775 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003776 {
3777 if (!(supported & JO_ERR_TIMEOUT))
3778 break;
3779 opt->jo_set |= JO_ERR_TIMEOUT;
3780 opt->jo_err_timeout = get_tv_number(item);
3781 }
3782 else if (STRCMP(hi->hi_key, "part") == 0)
3783 {
3784 if (!(supported & JO_PART))
3785 break;
3786 opt->jo_set |= JO_PART;
3787 val = get_tv_string(item);
3788 if (STRCMP(val, "err") == 0)
3789 opt->jo_part = PART_ERR;
3790 else
3791 {
3792 EMSG2(_(e_invarg2), val);
3793 return FAIL;
3794 }
3795 }
3796 else if (STRCMP(hi->hi_key, "id") == 0)
3797 {
3798 if (!(supported & JO_ID))
3799 break;
3800 opt->jo_set |= JO_ID;
3801 opt->jo_id = get_tv_number(item);
3802 }
3803 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3804 {
3805 if (!(supported & JO_STOPONEXIT))
3806 break;
3807 opt->jo_set |= JO_STOPONEXIT;
3808 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3809 opt->jo_soe_buf);
3810 if (opt->jo_stoponexit == NULL)
3811 {
3812 EMSG2(_(e_invarg2), "stoponexit");
3813 return FAIL;
3814 }
3815 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003816 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003817 {
3818 if (!(supported & JO_EXIT_CB))
3819 break;
3820 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003821 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3822 {
3823 opt->jo_exit_partial = item->vval.v_partial;
3824 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3825 }
3826 else
3827 opt->jo_exit_cb = get_tv_string_buf_chk(
3828 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003829 if (opt->jo_exit_cb == NULL)
3830 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003831 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003832 return FAIL;
3833 }
3834 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003835 else if (STRCMP(hi->hi_key, "block_write") == 0)
3836 {
3837 if (!(supported & JO_BLOCK_WRITE))
3838 break;
3839 opt->jo_set |= JO_BLOCK_WRITE;
3840 opt->jo_block_write = get_tv_number(item);
3841 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003842 else
3843 break;
3844 --todo;
3845 }
3846 if (todo > 0)
3847 {
3848 EMSG2(_(e_invarg2), hi->hi_key);
3849 return FAIL;
3850 }
3851
3852 return OK;
3853}
3854
3855/*
3856 * Get the channel from the argument.
3857 * Returns NULL if the handle is invalid.
3858 */
3859 channel_T *
3860get_channel_arg(typval_T *tv, int check_open)
3861{
3862 channel_T *channel = NULL;
3863
3864 if (tv->v_type == VAR_JOB)
3865 {
3866 if (tv->vval.v_job != NULL)
3867 channel = tv->vval.v_job->jv_channel;
3868 }
3869 else if (tv->v_type == VAR_CHANNEL)
3870 {
3871 channel = tv->vval.v_channel;
3872 }
3873 else
3874 {
3875 EMSG2(_(e_invarg2), get_tv_string(tv));
3876 return NULL;
3877 }
3878
3879 if (check_open && (channel == NULL || !channel_is_open(channel)))
3880 {
3881 EMSG(_("E906: not an open channel"));
3882 return NULL;
3883 }
3884 return channel;
3885}
3886
3887static job_T *first_job = NULL;
3888
3889 static void
3890job_free(job_T *job)
3891{
3892 ch_log(job->jv_channel, "Freeing job");
3893 if (job->jv_channel != NULL)
3894 {
3895 /* The link from the channel to the job doesn't count as a reference,
3896 * thus don't decrement the refcount of the job. The reference from
3897 * the job to the channel does count the refrence, decrement it and
3898 * NULL the reference. We don't set ch_job_killed, unreferencing the
3899 * job doesn't mean it stops running. */
3900 job->jv_channel->ch_job = NULL;
3901 channel_unref(job->jv_channel);
3902 }
3903 mch_clear_job(job);
3904
3905 if (job->jv_next != NULL)
3906 job->jv_next->jv_prev = job->jv_prev;
3907 if (job->jv_prev == NULL)
3908 first_job = job->jv_next;
3909 else
3910 job->jv_prev->jv_next = job->jv_next;
3911
3912 vim_free(job->jv_stoponexit);
3913 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003914 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003915 vim_free(job);
3916}
3917
3918 void
3919job_unref(job_T *job)
3920{
3921 if (job != NULL && --job->jv_refcount <= 0)
3922 {
3923 /* Do not free the job when it has not ended yet and there is a
3924 * "stoponexit" flag or an exit callback. */
3925 if (job->jv_status != JOB_STARTED
3926 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
3927 {
3928 job_free(job);
3929 }
3930 else if (job->jv_channel != NULL)
3931 {
3932 /* Do remove the link to the channel, otherwise it hangs
3933 * around until Vim exits. See job_free() for refcount. */
3934 job->jv_channel->ch_job = NULL;
3935 channel_unref(job->jv_channel);
3936 job->jv_channel = NULL;
3937 }
3938 }
3939}
3940
3941/*
3942 * Allocate a job. Sets the refcount to one and sets options default.
3943 */
3944 static job_T *
3945job_alloc(void)
3946{
3947 job_T *job;
3948
3949 job = (job_T *)alloc_clear(sizeof(job_T));
3950 if (job != NULL)
3951 {
3952 job->jv_refcount = 1;
3953 job->jv_stoponexit = vim_strsave((char_u *)"term");
3954
3955 if (first_job != NULL)
3956 {
3957 first_job->jv_prev = job;
3958 job->jv_next = first_job;
3959 }
3960 first_job = job;
3961 }
3962 return job;
3963}
3964
3965 void
3966job_set_options(job_T *job, jobopt_T *opt)
3967{
3968 if (opt->jo_set & JO_STOPONEXIT)
3969 {
3970 vim_free(job->jv_stoponexit);
3971 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
3972 job->jv_stoponexit = NULL;
3973 else
3974 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
3975 }
3976 if (opt->jo_set & JO_EXIT_CB)
3977 {
3978 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003979 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003980 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003981 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003982 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003983 job->jv_exit_partial = NULL;
3984 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003985 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003986 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003987 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003988 job->jv_exit_partial = opt->jo_exit_partial;
3989 if (job->jv_exit_partial != NULL)
3990 ++job->jv_exit_partial->pt_refcount;
3991 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003992 }
3993}
3994
3995/*
3996 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
3997 */
3998 void
3999job_stop_on_exit()
4000{
4001 job_T *job;
4002
4003 for (job = first_job; job != NULL; job = job->jv_next)
4004 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4005 mch_stop_job(job, job->jv_stoponexit);
4006}
4007
4008/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004009 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004010 */
4011 void
4012job_check_ended(void)
4013{
4014 static time_t last_check = 0;
4015 time_t now;
4016 job_T *job;
4017 job_T *next;
4018
4019 /* Only do this once in 10 seconds. */
4020 now = time(NULL);
4021 if (last_check + 10 < now)
4022 {
4023 last_check = now;
4024 for (job = first_job; job != NULL; job = next)
4025 {
4026 next = job->jv_next;
4027 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4028 job_status(job); /* may free "job" */
4029 }
4030 }
4031}
4032
4033/*
4034 * "job_start()" function
4035 */
4036 job_T *
4037job_start(typval_T *argvars)
4038{
4039 job_T *job;
4040 char_u *cmd = NULL;
4041#if defined(UNIX)
4042# define USE_ARGV
4043 char **argv = NULL;
4044 int argc = 0;
4045#else
4046 garray_T ga;
4047#endif
4048 jobopt_T opt;
4049 int part;
4050
4051 job = job_alloc();
4052 if (job == NULL)
4053 return NULL;
4054
4055 job->jv_status = JOB_FAILED;
4056
4057 /* Default mode is NL. */
4058 clear_job_options(&opt);
4059 opt.jo_mode = MODE_NL;
4060 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004061 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4062 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004063 return job;
4064
4065 /* Check that when io is "file" that there is a file name. */
4066 for (part = PART_OUT; part <= PART_IN; ++part)
4067 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4068 && opt.jo_io[part] == JIO_FILE
4069 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4070 || *opt.jo_io_name[part] == NUL))
4071 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004072 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004073 return job;
4074 }
4075
4076 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4077 {
4078 buf_T *buf = NULL;
4079
4080 /* check that we can find the buffer before starting the job */
4081 if (opt.jo_set & JO_IN_BUF)
4082 {
4083 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4084 if (buf == NULL)
4085 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4086 }
4087 else if (!(opt.jo_set & JO_IN_NAME))
4088 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004089 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004090 }
4091 else
4092 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4093 if (buf == NULL)
4094 return job;
4095 if (buf->b_ml.ml_mfp == NULL)
4096 {
4097 char_u numbuf[NUMBUFLEN];
4098 char_u *s;
4099
4100 if (opt.jo_set & JO_IN_BUF)
4101 {
4102 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4103 s = numbuf;
4104 }
4105 else
4106 s = opt.jo_io_name[PART_IN];
4107 EMSG2(_("E918: buffer must be loaded: %s"), s);
4108 return job;
4109 }
4110 job->jv_in_buf = buf;
4111 }
4112
4113 job_set_options(job, &opt);
4114
4115#ifndef USE_ARGV
4116 ga_init2(&ga, (int)sizeof(char*), 20);
4117#endif
4118
4119 if (argvars[0].v_type == VAR_STRING)
4120 {
4121 /* Command is a string. */
4122 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004123 if (cmd == NULL || *cmd == NUL)
4124 {
4125 EMSG(_(e_invarg));
4126 return job;
4127 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128#ifdef USE_ARGV
4129 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
4130 return job;
4131 argv[argc] = NULL;
4132#endif
4133 }
4134 else if (argvars[0].v_type != VAR_LIST
4135 || argvars[0].vval.v_list == NULL
4136 || argvars[0].vval.v_list->lv_len < 1)
4137 {
4138 EMSG(_(e_invarg));
4139 return job;
4140 }
4141 else
4142 {
4143 list_T *l = argvars[0].vval.v_list;
4144 listitem_T *li;
4145 char_u *s;
4146
4147#ifdef USE_ARGV
4148 /* Pass argv[] to mch_call_shell(). */
4149 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4150 if (argv == NULL)
4151 return job;
4152#endif
4153 for (li = l->lv_first; li != NULL; li = li->li_next)
4154 {
4155 s = get_tv_string_chk(&li->li_tv);
4156 if (s == NULL)
4157 goto theend;
4158#ifdef USE_ARGV
4159 argv[argc++] = (char *)s;
4160#else
4161 /* Only escape when needed, double quotes are not always allowed. */
4162 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4163 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004164# ifdef WIN32
4165 int old_ssl = p_ssl;
4166
4167 /* This is using CreateProcess, not cmd.exe. Always use
4168 * double quote and backslashes. */
4169 p_ssl = 0;
4170# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004171 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004172# ifdef WIN32
4173 p_ssl = old_ssl;
4174# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004175 if (s == NULL)
4176 goto theend;
4177 ga_concat(&ga, s);
4178 vim_free(s);
4179 }
4180 else
4181 ga_concat(&ga, s);
4182 if (li->li_next != NULL)
4183 ga_append(&ga, ' ');
4184#endif
4185 }
4186#ifdef USE_ARGV
4187 argv[argc] = NULL;
4188#else
4189 cmd = ga.ga_data;
4190#endif
4191 }
4192
4193#ifdef USE_ARGV
4194 if (ch_log_active())
4195 {
4196 garray_T ga;
4197 int i;
4198
4199 ga_init2(&ga, (int)sizeof(char), 200);
4200 for (i = 0; i < argc; ++i)
4201 {
4202 if (i > 0)
4203 ga_concat(&ga, (char_u *)" ");
4204 ga_concat(&ga, (char_u *)argv[i]);
4205 }
4206 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4207 ga_clear(&ga);
4208 }
4209 mch_start_job(argv, job, &opt);
4210#else
4211 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4212 mch_start_job((char *)cmd, job, &opt);
4213#endif
4214
4215 /* If the channel is reading from a buffer, write lines now. */
4216 if (job->jv_channel != NULL)
4217 channel_write_in(job->jv_channel);
4218
4219theend:
4220#ifdef USE_ARGV
4221 vim_free(argv);
4222#else
4223 vim_free(ga.ga_data);
4224#endif
4225 return job;
4226}
4227
4228/*
4229 * Get the status of "job" and invoke the exit callback when needed.
4230 * The returned string is not allocated.
4231 */
4232 char *
4233job_status(job_T *job)
4234{
4235 char *result;
4236
4237 if (job->jv_status == JOB_ENDED)
4238 /* No need to check, dead is dead. */
4239 result = "dead";
4240 else if (job->jv_status == JOB_FAILED)
4241 result = "fail";
4242 else
4243 {
4244 result = mch_job_status(job);
4245 if (job->jv_status == JOB_ENDED)
4246 ch_log(job->jv_channel, "Job ended");
4247 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4248 {
4249 typval_T argv[3];
4250 typval_T rettv;
4251 int dummy;
4252
4253 /* invoke the exit callback; make sure the refcount is > 0 */
4254 ++job->jv_refcount;
4255 argv[0].v_type = VAR_JOB;
4256 argv[0].vval.v_job = job;
4257 argv[1].v_type = VAR_NUMBER;
4258 argv[1].vval.v_number = job->jv_exitval;
4259 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004260 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4261 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004262 clear_tv(&rettv);
4263 --job->jv_refcount;
4264 }
4265 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4266 {
4267 /* The job was already unreferenced, now that it ended it can be
4268 * freed. Careful: caller must not use "job" after this! */
4269 job_free(job);
4270 }
4271 }
4272 return result;
4273}
4274
Bram Moolenaar8950a562016-03-12 15:22:55 +01004275/*
4276 * Implementation of job_info().
4277 */
4278 void
4279job_info(job_T *job, dict_T *dict)
4280{
4281 dictitem_T *item;
4282 varnumber_T nr;
4283
4284 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4285
4286 item = dictitem_alloc((char_u *)"channel");
4287 if (item == NULL)
4288 return;
4289 item->di_tv.v_lock = 0;
4290 item->di_tv.v_type = VAR_CHANNEL;
4291 item->di_tv.vval.v_channel = job->jv_channel;
4292 if (job->jv_channel != NULL)
4293 ++job->jv_channel->ch_refcount;
4294 if (dict_add(dict, item) == FAIL)
4295 dictitem_free(item);
4296
4297#ifdef UNIX
4298 nr = job->jv_pid;
4299#else
4300 nr = job->jv_proc_info.dwProcessId;
4301#endif
4302 dict_add_nr_str(dict, "process", nr, NULL);
4303
4304 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004305 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004306 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4307}
4308
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004309 int
4310job_stop(job_T *job, typval_T *argvars)
4311{
4312 char_u *arg;
4313
4314 if (argvars[1].v_type == VAR_UNKNOWN)
4315 arg = (char_u *)"";
4316 else
4317 {
4318 arg = get_tv_string_chk(&argvars[1]);
4319 if (arg == NULL)
4320 {
4321 EMSG(_(e_invarg));
4322 return 0;
4323 }
4324 }
4325 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4326 if (mch_stop_job(job, arg) == FAIL)
4327 return 0;
4328
4329 /* Assume that "hup" does not kill the job. */
4330 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4331 job->jv_channel->ch_job_killed = TRUE;
4332
4333 /* We don't try freeing the job, obviously the caller still has a
4334 * reference to it. */
4335 return 1;
4336}
4337
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004338#endif /* FEAT_JOB_CHANNEL */