blob: 7b811dbce2b0491ef830e8ec918902e2d1954f13 [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;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200861 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100862
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)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200893 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100894 if (opt.jo_timeout < 0)
895 {
896 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200897 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100898 }
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 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200906theend:
907 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100908 return channel;
909}
910
Bram Moolenaarde279892016-03-11 22:19:44 +0100911 static void
912may_close_part(sock_T *fd)
913{
914 if (*fd != INVALID_FD)
915 {
916 fd_close(*fd);
917 *fd = INVALID_FD;
918 }
919}
920
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100921 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100922channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100923{
Bram Moolenaarde279892016-03-11 22:19:44 +0100924 if (in != INVALID_FD)
925 {
926 may_close_part(&channel->CH_IN_FD);
927 channel->CH_IN_FD = in;
928 }
929 if (out != INVALID_FD)
930 {
931# if defined(FEAT_GUI)
932 channel_gui_unregister_one(channel, PART_OUT);
933# endif
934 may_close_part(&channel->CH_OUT_FD);
935 channel->CH_OUT_FD = out;
936# if defined(FEAT_GUI)
937 channel_gui_register_one(channel, PART_OUT);
938# endif
939 }
940 if (err != INVALID_FD)
941 {
942# if defined(FEAT_GUI)
943 channel_gui_unregister_one(channel, PART_ERR);
944# endif
945 may_close_part(&channel->CH_ERR_FD);
946 channel->CH_ERR_FD = err;
947# if defined(FEAT_GUI)
948 channel_gui_register_one(channel, PART_ERR);
949# endif
950 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100951}
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100952
Bram Moolenaard6051b52016-02-28 15:49:03 +0100953/*
Bram Moolenaar014069a2016-03-03 22:51:40 +0100954 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +0100955 * This does not keep a refcount, when the job is freed ch_job is cleared.
956 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100957 void
Bram Moolenaar014069a2016-03-03 22:51:40 +0100958channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100959{
Bram Moolenaar77073442016-02-13 23:23:53 +0100960 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +0100961
962 channel_set_options(channel, options);
963
964 if (job->jv_in_buf != NULL)
965 {
966 chanpart_T *in_part = &channel->ch_part[PART_IN];
967
968 in_part->ch_buffer = job->jv_in_buf;
969 ch_logs(channel, "reading from buffer '%s'",
970 (char *)in_part->ch_buffer->b_ffname);
971 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100972 {
973 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
974 {
975 /* Special mode: send last-but-one line when appending a line
976 * to the buffer. */
977 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +0200978 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100979 in_part->ch_buf_top =
980 in_part->ch_buffer->b_ml.ml_line_count + 1;
981 }
982 else
983 in_part->ch_buf_top = options->jo_in_top;
984 }
Bram Moolenaar014069a2016-03-03 22:51:40 +0100985 else
986 in_part->ch_buf_top = 1;
987 if (options->jo_set & JO_IN_BOT)
988 in_part->ch_buf_bot = options->jo_in_bot;
989 else
990 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
991 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100992}
993
994/*
Bram Moolenaar187db502016-02-27 14:44:26 +0100995 * Find a buffer matching "name" or create a new one.
996 */
997 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100998find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +0100999{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001000 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001001 buf_T *save_curbuf = curbuf;
1002
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001003 if (name != NULL && *name != NUL)
1004 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001005 if (buf == NULL)
1006 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001007 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001008 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001009 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001010 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001011#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001012 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1013 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001014#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001015 if (curbuf->b_ml.ml_mfp == NULL)
1016 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001017 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1018 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001019 changed_bytes(1, 0);
1020 curbuf = save_curbuf;
1021 }
1022
1023 return buf;
1024}
1025
1026/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001027 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001028 */
1029 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001030channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001031{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001032 int part;
1033 char_u **cbp;
1034 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001035
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001036 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001037 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001038 channel->ch_part[part].ch_mode = opt->jo_mode;
1039 if (opt->jo_set & JO_IN_MODE)
1040 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1041 if (opt->jo_set & JO_OUT_MODE)
1042 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1043 if (opt->jo_set & JO_ERR_MODE)
1044 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001045
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001046 if (opt->jo_set & JO_TIMEOUT)
1047 for (part = PART_SOCK; part <= PART_IN; ++part)
1048 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1049 if (opt->jo_set & JO_OUT_TIMEOUT)
1050 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1051 if (opt->jo_set & JO_ERR_TIMEOUT)
1052 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001053 if (opt->jo_set & JO_BLOCK_WRITE)
1054 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001055
1056 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001057 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001058 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001059 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001060 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001061 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001062 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1063 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001064 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001065 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001066 *pp = opt->jo_partial;
1067 if (*pp != NULL)
1068 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001069 }
1070 if (opt->jo_set & JO_OUT_CALLBACK)
1071 {
1072 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001073 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001074 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001075 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001076 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1077 *cbp = vim_strsave(opt->jo_out_cb);
1078 else
1079 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001080 *pp = opt->jo_out_partial;
1081 if (*pp != NULL)
1082 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001083 }
1084 if (opt->jo_set & JO_ERR_CALLBACK)
1085 {
1086 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001087 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001088 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001089 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001090 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1091 *cbp = vim_strsave(opt->jo_err_cb);
1092 else
1093 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001094 *pp = opt->jo_err_partial;
1095 if (*pp != NULL)
1096 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001097 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001098 if (opt->jo_set & JO_CLOSE_CALLBACK)
1099 {
1100 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001101 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001102 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001103 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001104 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1105 *cbp = vim_strsave(opt->jo_close_cb);
1106 else
1107 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001108 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001109 if (*pp != NULL)
1110 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001111 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001112
1113 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1114 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001115 /* writing output to a buffer. Default mode is NL. */
1116 if (!(opt->jo_set & JO_OUT_MODE))
1117 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001118 if (opt->jo_set & JO_OUT_BUF)
1119 channel->ch_part[PART_OUT].ch_buffer =
1120 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1121 else
1122 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001123 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1124 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001125 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1126 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001127
1128 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1129 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1130 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1131 {
1132 /* writing err to a buffer. Default mode is NL. */
1133 if (!(opt->jo_set & JO_ERR_MODE))
1134 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1135 if (opt->jo_io[PART_ERR] == JIO_OUT)
1136 channel->ch_part[PART_ERR].ch_buffer =
1137 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001138 else if (opt->jo_set & JO_ERR_BUF)
1139 channel->ch_part[PART_ERR].ch_buffer =
1140 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001141 else
1142 channel->ch_part[PART_ERR].ch_buffer =
1143 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1144 ch_logs(channel, "writing err to buffer '%s'",
1145 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1146 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001147
1148 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1149 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1150 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001151}
1152
1153/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001154 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001155 */
1156 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001157channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001158 channel_T *channel,
1159 int part,
1160 char_u *callback,
1161 partial_T *partial,
1162 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001163{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001164 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001165 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1166
1167 if (item != NULL)
1168 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001169 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001170 item->cq_partial = partial;
1171 if (partial != NULL)
1172 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001173 item->cq_seq_nr = id;
1174 item->cq_prev = head->cq_prev;
1175 head->cq_prev = item;
1176 item->cq_next = NULL;
1177 if (item->cq_prev == NULL)
1178 head->cq_next = item;
1179 else
1180 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001181 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001182}
1183
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001184 static void
1185write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1186{
1187 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001188 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001189 char_u *p;
1190
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001191 if ((p = alloc(len + 2)) == NULL)
1192 return;
1193 STRCPY(p, line);
1194 p[len] = NL;
1195 p[len + 1] = NUL;
1196 channel_send(channel, PART_IN, p, "write_buf_line()");
1197 vim_free(p);
1198}
1199
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001200/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001201 * Return TRUE if "channel" can be written to.
1202 * Returns FALSE if the input is closed or the write would block.
1203 */
1204 static int
1205can_write_buf_line(channel_T *channel)
1206{
1207 chanpart_T *in_part = &channel->ch_part[PART_IN];
1208
1209 if (in_part->ch_fd == INVALID_FD)
1210 return FALSE; /* pipe was closed */
1211
1212 /* for testing: block every other attempt to write */
1213 if (in_part->ch_block_write == 1)
1214 in_part->ch_block_write = -1;
1215 else if (in_part->ch_block_write == -1)
1216 in_part->ch_block_write = 1;
1217
1218 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1219#ifndef WIN32
1220 {
1221# if defined(HAVE_SELECT)
1222 struct timeval tval;
1223 fd_set wfds;
1224 int ret;
1225
1226 FD_ZERO(&wfds);
1227 FD_SET((int)in_part->ch_fd, &wfds);
1228 tval.tv_sec = 0;
1229 tval.tv_usec = 0;
1230 for (;;)
1231 {
1232 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1233# ifdef EINTR
1234 SOCK_ERRNO;
1235 if (ret == -1 && errno == EINTR)
1236 continue;
1237# endif
1238 if (ret <= 0 || in_part->ch_block_write == 1)
1239 {
1240 if (ret > 0)
1241 ch_log(channel, "FAKED Input not ready for writing");
1242 else
1243 ch_log(channel, "Input not ready for writing");
1244 return FALSE;
1245 }
1246 break;
1247 }
1248# else
1249 struct pollfd fds;
1250
1251 fds.fd = in_part->ch_fd;
1252 fds.events = POLLOUT;
1253 if (poll(&fds, 1, 0) <= 0)
1254 {
1255 ch_log(channel, "Input not ready for writing");
1256 return FALSE;
1257 }
1258 if (in_part->ch_block_write == 1)
1259 {
1260 ch_log(channel, "FAKED Input not ready for writing");
1261 return FALSE;
1262 }
1263# endif
1264 }
1265#endif
1266 return TRUE;
1267}
1268
1269/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001270 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001271 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001272 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001273channel_write_in(channel_T *channel)
1274{
1275 chanpart_T *in_part = &channel->ch_part[PART_IN];
1276 linenr_T lnum;
1277 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001278 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001279
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001280 if (buf == NULL || in_part->ch_buf_append)
1281 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001282 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1283 {
1284 /* buffer was wiped out or unloaded */
1285 in_part->ch_buffer = NULL;
1286 return;
1287 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001288
1289 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1290 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1291 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001292 if (!can_write_buf_line(channel))
1293 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001294 write_buf_line(buf, lnum, channel);
1295 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001296 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001297
1298 if (written == 1)
1299 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1300 else if (written > 1)
1301 ch_logn(channel, "written %d lines to channel", written);
1302
Bram Moolenaar014069a2016-03-03 22:51:40 +01001303 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001304 if (lnum > buf->b_ml.ml_line_count)
1305 {
1306 /* Writing is done, no longer need the buffer. */
1307 in_part->ch_buffer = NULL;
1308 ch_log(channel, "Finished writing all lines to channel");
1309 }
1310 else
1311 ch_logn(channel, "Still %d more lines to write",
1312 buf->b_ml.ml_line_count - lnum + 1);
1313}
1314
1315/*
1316 * Write any lines waiting to be written to a channel.
1317 */
1318 void
1319channel_write_any_lines()
1320{
1321 channel_T *channel;
1322
1323 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1324 {
1325 chanpart_T *in_part = &channel->ch_part[PART_IN];
1326
1327 if (in_part->ch_buffer != NULL)
1328 {
1329 if (in_part->ch_buf_append)
1330 channel_write_new_lines(in_part->ch_buffer);
1331 else
1332 channel_write_in(channel);
1333 }
1334 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001335}
1336
1337/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001338 * Write appended lines above the last one in "buf" to the channel.
1339 */
1340 void
1341channel_write_new_lines(buf_T *buf)
1342{
1343 channel_T *channel;
1344 int found_one = FALSE;
1345
1346 /* There could be more than one channel for the buffer, loop over all of
1347 * them. */
1348 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1349 {
1350 chanpart_T *in_part = &channel->ch_part[PART_IN];
1351 linenr_T lnum;
1352 int written = 0;
1353
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001354 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001355 {
1356 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001357 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001358 found_one = TRUE;
1359 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1360 ++lnum)
1361 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001362 if (!can_write_buf_line(channel))
1363 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001364 write_buf_line(buf, lnum, channel);
1365 ++written;
1366 }
1367
1368 if (written == 1)
1369 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1370 else if (written > 1)
1371 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001372 if (lnum < buf->b_ml.ml_line_count)
1373 ch_logn(channel, "Still %d more lines to write",
1374 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375
1376 in_part->ch_buf_bot = lnum;
1377 }
1378 }
1379 if (!found_one)
1380 buf->b_write_to_channel = FALSE;
1381}
1382
1383/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001384 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001385 */
1386 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001387invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1388 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001389{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001390 typval_T rettv;
1391 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001392
Bram Moolenaar77073442016-02-13 23:23:53 +01001393 argv[0].v_type = VAR_CHANNEL;
1394 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001395
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001396 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001397 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001398 clear_tv(&rettv);
1399
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001400 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001401}
1402
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001403/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001404 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001405 * The caller must free it.
1406 * Returns NULL if there is nothing.
1407 */
1408 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001409channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001410{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001411 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001412 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001413 char_u *p;
1414
Bram Moolenaar77073442016-02-13 23:23:53 +01001415 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001416 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001417 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001418 p = node->rq_buffer;
1419 head->rq_next = node->rq_next;
1420 if (node->rq_next == NULL)
1421 head->rq_prev = NULL;
1422 else
1423 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001424 vim_free(node);
1425 return p;
1426}
1427
1428/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001429 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001430 */
1431 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001432channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001433{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001434 readq_T *head = &channel->ch_part[part].ch_head;
1435 readq_T *node = head->rq_next;
1436 long_u len = 1;
1437 char_u *res;
1438 char_u *p;
1439
1440 /* If there is only one buffer just get that one. */
1441 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1442 return channel_get(channel, part);
1443
1444 /* Concatenate everything into one buffer. */
1445 for (node = head->rq_next; node != NULL; node = node->rq_next)
1446 len += (long_u)STRLEN(node->rq_buffer);
1447 res = lalloc(len, TRUE);
1448 if (res == NULL)
1449 return NULL;
1450 *res = NUL;
1451 for (node = head->rq_next; node != NULL; node = node->rq_next)
1452 STRCAT(res, node->rq_buffer);
1453
1454 /* Free all buffers */
1455 do
1456 {
1457 p = channel_get(channel, part);
1458 vim_free(p);
1459 } while (p != NULL);
1460
1461 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001462}
1463
1464/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001465 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 * Returns FAIL if that is not possible.
1467 */
1468 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001469channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001470{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001471 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001472 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001473 char_u *p;
1474
Bram Moolenaar77073442016-02-13 23:23:53 +01001475 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001476 return FAIL;
1477
Bram Moolenaar77073442016-02-13 23:23:53 +01001478 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1479 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001480 if (p == NULL)
1481 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001482 STRCPY(p, node->rq_buffer);
1483 STRCAT(p, node->rq_next->rq_buffer);
1484 vim_free(node->rq_next->rq_buffer);
1485 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001486
Bram Moolenaar77073442016-02-13 23:23:53 +01001487 /* dispose of the node and its buffer */
1488 head->rq_next = node->rq_next;
1489 head->rq_next->rq_prev = NULL;
1490 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001491 vim_free(node);
1492 return OK;
1493}
1494
1495/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001496 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001497 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001498 * Returns OK or FAIL.
1499 */
1500 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001501channel_save(channel_T *channel, int part, char_u *buf, int len,
1502 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001503{
1504 readq_T *node;
1505 readq_T *head = &channel->ch_part[part].ch_head;
1506 char_u *p;
1507 int i;
1508
1509 node = (readq_T *)alloc(sizeof(readq_T));
1510 if (node == NULL)
1511 return FAIL; /* out of memory */
1512 node->rq_buffer = alloc(len + 1);
1513 if (node->rq_buffer == NULL)
1514 {
1515 vim_free(node);
1516 return FAIL; /* out of memory */
1517 }
1518
1519 if (channel->ch_part[part].ch_mode == MODE_NL)
1520 {
1521 /* Drop any CR before a NL. */
1522 p = node->rq_buffer;
1523 for (i = 0; i < len; ++i)
1524 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1525 *p++ = buf[i];
1526 *p = NUL;
1527 }
1528 else
1529 {
1530 mch_memmove(node->rq_buffer, buf, len);
1531 node->rq_buffer[len] = NUL;
1532 }
1533
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001534 if (prepend)
1535 {
1536 /* preend node to the head of the queue */
1537 node->rq_next = head->rq_next;
1538 node->rq_prev = NULL;
1539 if (head->rq_next == NULL)
1540 head->rq_prev = node;
1541 else
1542 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001543 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001544 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001545 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001546 {
1547 /* append node to the tail of the queue */
1548 node->rq_next = NULL;
1549 node->rq_prev = head->rq_prev;
1550 if (head->rq_prev == NULL)
1551 head->rq_next = node;
1552 else
1553 head->rq_prev->rq_next = node;
1554 head->rq_prev = node;
1555 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001557 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001558 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001559 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001560 fprintf(log_fd, "'");
1561 if (fwrite(buf, len, 1, log_fd) != 1)
1562 return FAIL;
1563 fprintf(log_fd, "'\n");
1564 }
1565 return OK;
1566}
1567
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001568 static int
1569channel_fill(js_read_T *reader)
1570{
1571 channel_T *channel = (channel_T *)reader->js_cookie;
1572 int part = reader->js_cookie_arg;
1573 char_u *next = channel_get(channel, part);
1574 int unused;
1575 int len;
1576 char_u *p;
1577
1578 if (next == NULL)
1579 return FALSE;
1580
1581 unused = reader->js_end - reader->js_buf - reader->js_used;
1582 if (unused > 0)
1583 {
1584 /* Prepend unused text. */
1585 len = (int)STRLEN(next);
1586 p = alloc(unused + len + 1);
1587 if (p == NULL)
1588 {
1589 vim_free(next);
1590 return FALSE;
1591 }
1592 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1593 mch_memmove(p + unused, next, len + 1);
1594 vim_free(next);
1595 next = p;
1596 }
1597
1598 vim_free(reader->js_buf);
1599 reader->js_buf = next;
1600 reader->js_used = 0;
1601 return TRUE;
1602}
1603
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001604/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001605 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001606 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001607 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001609 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001610channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611{
1612 js_read_T reader;
1613 typval_T listtv;
1614 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001615 chanpart_T *chanpart = &channel->ch_part[part];
1616 jsonq_T *head = &chanpart->ch_json_head;
1617 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001618 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001620 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001621 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001623 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001625 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001627 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001628
1629 /* When a message is incomplete we wait for a short while for more to
1630 * arrive. After the delay drop the input, otherwise a truncated string
1631 * or list will make us hang. */
1632 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001633 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001634 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001635 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001636 /* Only accept the response when it is a list with at least two
1637 * items. */
1638 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001639 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001640 if (listtv.v_type != VAR_LIST)
1641 ch_error(channel, "Did not receive a list, discarding");
1642 else
1643 ch_errorn(channel, "Expected list with two items, got %d",
1644 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001645 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001646 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001647 else
1648 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001649 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1650 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001651 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001652 else
1653 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001654 item->jq_value = alloc_tv();
1655 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001656 {
1657 vim_free(item);
1658 clear_tv(&listtv);
1659 }
1660 else
1661 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001662 *item->jq_value = listtv;
1663 item->jq_prev = head->jq_prev;
1664 head->jq_prev = item;
1665 item->jq_next = NULL;
1666 if (item->jq_prev == NULL)
1667 head->jq_next = item;
1668 else
1669 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001670 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001671 }
1672 }
1673 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001674
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001675 if (status == OK)
1676 chanpart->ch_waiting = FALSE;
1677 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001678 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001679 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001680 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001681 /* First time encountering incomplete message, set a deadline of
1682 * 100 msec. */
1683 ch_log(channel, "Incomplete message - wait for more");
1684 reader.js_used = 0;
1685 chanpart->ch_waiting = TRUE;
1686#ifdef WIN32
1687 chanpart->ch_deadline = GetTickCount() + 100L;
1688#else
1689 gettimeofday(&chanpart->ch_deadline, NULL);
1690 chanpart->ch_deadline.tv_usec += 100 * 1000;
1691 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1692 {
1693 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1694 ++chanpart->ch_deadline.tv_sec;
1695 }
1696#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001697 }
1698 else
1699 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001700 int timeout;
1701#ifdef WIN32
1702 timeout = GetTickCount() > chanpart->ch_deadline;
1703#else
1704 {
1705 struct timeval now_tv;
1706
1707 gettimeofday(&now_tv, NULL);
1708 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1709 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1710 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1711 }
1712#endif
1713 if (timeout)
1714 {
1715 status = FAIL;
1716 chanpart->ch_waiting = FALSE;
1717 }
1718 else
1719 {
1720 reader.js_used = 0;
1721 ch_log(channel, "still waiting on incomplete message");
1722 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001723 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001724 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001725
1726 if (status == FAIL)
1727 {
1728 ch_error(channel, "Decoding failed - discarding input");
1729 ret = FALSE;
1730 chanpart->ch_waiting = FALSE;
1731 }
1732 else if (reader.js_buf[reader.js_used] != NUL)
1733 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001734 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001735 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001736 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1737 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001738 ret = status == MAYBE ? FALSE: TRUE;
1739 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001740 else
1741 ret = FALSE;
1742
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001743 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001744 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001745}
1746
1747/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001748 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001749 */
1750 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001751remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001752{
Bram Moolenaar77073442016-02-13 23:23:53 +01001753 if (node->cq_prev == NULL)
1754 head->cq_next = node->cq_next;
1755 else
1756 node->cq_prev->cq_next = node->cq_next;
1757 if (node->cq_next == NULL)
1758 head->cq_prev = node->cq_prev;
1759 else
1760 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001761}
1762
1763/*
1764 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001765 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001766 */
1767 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001768remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001769{
Bram Moolenaar77073442016-02-13 23:23:53 +01001770 if (node->jq_prev == NULL)
1771 head->jq_next = node->jq_next;
1772 else
1773 node->jq_prev->jq_next = node->jq_next;
1774 if (node->jq_next == NULL)
1775 head->jq_prev = node->jq_prev;
1776 else
1777 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001778 vim_free(node);
1779}
1780
1781/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001782 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001783 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001784 * When "id" is zero or negative jut get the first message. But not the one
1785 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001786 * Return OK when found and return the value in "rettv".
1787 * Return FAIL otherwise.
1788 */
1789 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001790channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001791{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001792 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001793 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794
Bram Moolenaar77073442016-02-13 23:23:53 +01001795 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001797 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798 typval_T *tv = &l->lv_first->li_tv;
1799
1800 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001801 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 || tv->vval.v_number == 0
1803 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001804 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001805 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001806 if (tv->v_type == VAR_NUMBER)
1807 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001808 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001809 return OK;
1810 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001811 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001812 }
1813 return FAIL;
1814}
1815
Bram Moolenaarece61b02016-02-20 21:39:05 +01001816#define CH_JSON_MAX_ARGS 4
1817
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001818/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001820 * "argv[0]" is the command string.
1821 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001822 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001823 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001824channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001825{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001826 char_u *cmd = argv[0].vval.v_string;
1827 char_u *arg;
1828 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001829
Bram Moolenaarece61b02016-02-20 21:39:05 +01001830 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001831 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001832 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001833 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001834 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001835 return;
1836 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001837 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001838 if (arg == NULL)
1839 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001840
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001841 if (STRCMP(cmd, "ex") == 0)
1842 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001843 int save_called_emsg = called_emsg;
1844
1845 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001846 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001847 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001848 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001849 --emsg_silent;
1850 if (called_emsg)
1851 ch_logs(channel, "Ex command error: '%s'",
1852 (char *)get_vim_var_str(VV_ERRMSG));
1853 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001854 }
1855 else if (STRCMP(cmd, "normal") == 0)
1856 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001857 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001858
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001859 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001860 ea.arg = arg;
1861 ea.addr_count = 0;
1862 ea.forceit = TRUE; /* no mapping */
1863 ex_normal(&ea);
1864 }
1865 else if (STRCMP(cmd, "redraw") == 0)
1866 {
1867 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001868
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001869 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001870 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001871 ex_redraw(&ea);
1872 showruler(FALSE);
1873 setcursor();
1874 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001875#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001876 if (gui.in_use)
1877 {
1878 gui_update_cursor(FALSE, FALSE);
1879 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001880 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001881#endif
1882 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 int is_call = cmd[0] == 'c';
1886 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001887
Bram Moolenaarece61b02016-02-20 21:39:05 +01001888 if (argv[id_idx].v_type != VAR_UNKNOWN
1889 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001890 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001891 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001892 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001893 EMSG("E904: last argument for expr/call must be a number");
1894 }
1895 else if (is_call && argv[2].v_type != VAR_LIST)
1896 {
1897 ch_error(channel, "third argument for call must be a list");
1898 if (p_verbose > 2)
1899 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001900 }
1901 else
1902 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001903 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001904 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001905 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001906 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001907
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001908 /* Don't pollute the display with errors. */
1909 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001910 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001911 {
1912 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001913 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001914 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001915 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001916 {
1917 ch_logs(channel, "Calling '%s'", (char *)arg);
1918 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1919 tv = &res_tv;
1920 else
1921 tv = NULL;
1922 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001923
1924 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001925 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001926 int id = argv[id_idx].vval.v_number;
1927
Bram Moolenaar55fab432016-02-07 16:53:13 +01001928 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001929 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001930 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001931 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001932 /* If evaluation failed or the result can't be encoded
1933 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001934 vim_free(json);
1935 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001936 err_tv.v_type = VAR_STRING;
1937 err_tv.vval.v_string = (char_u *)"ERROR";
1938 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001939 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001940 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001941 if (json != NULL)
1942 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 channel_send(channel,
1944 part == PART_SOCK ? PART_SOCK : PART_IN,
1945 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001946 vim_free(json);
1947 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001948 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001949 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001950 if (tv == &res_tv)
1951 clear_tv(tv);
1952 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001953 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001955 }
1956 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 {
1958 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001959 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001960 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001961}
1962
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001963 static void
1964invoke_one_time_callback(
1965 channel_T *channel,
1966 cbq_T *cbhead,
1967 cbq_T *item,
1968 typval_T *argv)
1969{
1970 ch_logs(channel, "Invoking one-time callback %s",
1971 (char *)item->cq_callback);
1972 /* Remove the item from the list first, if the callback
1973 * invokes ch_close() the list will be cleared. */
1974 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001975 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001976 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001977 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001978 vim_free(item);
1979}
1980
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001981 static void
1982append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
1983{
1984 buf_T *save_curbuf = curbuf;
1985 linenr_T lnum = buffer->b_ml.ml_line_count;
1986 int save_write_to = buffer->b_write_to_channel;
1987
1988 /* If the buffer is also used as input insert above the last
1989 * line. Don't write these lines. */
1990 if (save_write_to)
1991 {
1992 --lnum;
1993 buffer->b_write_to_channel = FALSE;
1994 }
1995
1996 /* Append to the buffer */
1997 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
1998
1999 curbuf = buffer;
2000 u_sync(TRUE);
2001 /* ignore undo failure, undo is not very useful here */
2002 ignored = u_save(lnum, lnum + 1);
2003
2004 ml_append(lnum, msg, 0, FALSE);
2005 appended_lines_mark(lnum, 1L);
2006 curbuf = save_curbuf;
2007
2008 if (buffer->b_nwindows > 0)
2009 {
2010 win_T *wp;
2011 win_T *save_curwin;
2012
2013 FOR_ALL_WINDOWS(wp)
2014 {
2015 if (wp->w_buffer == buffer
2016 && (save_write_to
2017 ? wp->w_cursor.lnum == lnum + 1
2018 : (wp->w_cursor.lnum == lnum
2019 && wp->w_cursor.col == 0)))
2020 {
2021 ++wp->w_cursor.lnum;
2022 save_curwin = curwin;
2023 curwin = wp;
2024 curbuf = curwin->w_buffer;
2025 scroll_cursor_bot(0, FALSE);
2026 curwin = save_curwin;
2027 curbuf = curwin->w_buffer;
2028 }
2029 }
2030 redraw_buf_later(buffer, VALID);
2031 channel_need_redraw = TRUE;
2032 }
2033
2034 if (save_write_to)
2035 {
2036 channel_T *ch;
2037
2038 /* Find channels reading from this buffer and adjust their
2039 * next-to-read line number. */
2040 buffer->b_write_to_channel = TRUE;
2041 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2042 {
2043 chanpart_T *in_part = &ch->ch_part[PART_IN];
2044
2045 if (in_part->ch_buffer == buffer)
2046 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2047 }
2048 }
2049}
2050
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002051/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002052 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002053 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002054 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002055 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002056may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002057{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002058 char_u *msg = NULL;
2059 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002060 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002061 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002062 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002063 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002064 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002065 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002066 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002067 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002068
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002069 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002070 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002071 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002072
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002073 /* Use a message-specific callback, part callback or channel callback */
2074 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2075 if (cbitem->cq_seq_nr == 0)
2076 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002077 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002078 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002079 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002080 partial = cbitem->cq_partial;
2081 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002082 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002083 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002084 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002085 partial = channel->ch_part[part].ch_partial;
2086 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002087 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002088 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002089 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002090 partial = channel->ch_partial;
2091 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002092
Bram Moolenaar187db502016-02-27 14:44:26 +01002093 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002094 if (buffer != NULL && !buf_valid(buffer))
2095 {
2096 /* buffer was wiped out */
2097 channel->ch_part[part].ch_buffer = NULL;
2098 buffer = NULL;
2099 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002100
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002101 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002102 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002103 listitem_T *item;
2104 int argc = 0;
2105
Bram Moolenaard7ece102016-02-02 23:23:02 +01002106 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002107 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002108 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002109 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002110 channel_parse_json(channel, part);
2111 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002112 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002113 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002114
Bram Moolenaarece61b02016-02-20 21:39:05 +01002115 for (item = listtv->vval.v_list->lv_first;
2116 item != NULL && argc < CH_JSON_MAX_ARGS;
2117 item = item->li_next)
2118 argv[argc++] = item->li_tv;
2119 while (argc < CH_JSON_MAX_ARGS)
2120 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002121
Bram Moolenaarece61b02016-02-20 21:39:05 +01002122 if (argv[0].v_type == VAR_STRING)
2123 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002124 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002125 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002126 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002127 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 }
2129
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002131 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002132 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002133 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002134 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002135 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002136 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002137 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002139 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002140 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002141 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002142 return FALSE;
2143 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002144 else
2145 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002146 /* If there is no callback or buffer drop the message. */
2147 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002148 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002149 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002150 {
2151 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002152 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01002153 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002154 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002155 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002156
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002157 if (ch_mode == MODE_NL)
2158 {
2159 char_u *nl;
2160 char_u *buf;
2161
2162 /* See if we have a message ending in NL in the first buffer. If
2163 * not try to concatenate the first and the second buffer. */
2164 while (TRUE)
2165 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002166 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002167 nl = vim_strchr(buf, NL);
2168 if (nl != NULL)
2169 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002170 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002171 return FALSE; /* incomplete message */
2172 }
2173 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002174 {
2175 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002176 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002177 *nl = NUL;
2178 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002179 else
2180 {
2181 /* Copy the message into allocated memory and remove it from
2182 * the buffer. */
2183 msg = vim_strnsave(buf, (int)(nl - buf));
2184 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2185 }
2186 }
2187 else
2188 /* For a raw channel we don't know where the message ends, just
2189 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002190 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002191
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002192 if (msg == NULL)
2193 return FALSE; /* out of memory (and avoids Coverity warning) */
2194
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002195 argv[1].v_type = VAR_STRING;
2196 argv[1].vval.v_string = msg;
2197 }
2198
Bram Moolenaara07fec92016-02-05 21:04:08 +01002199 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002200 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002201 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002202
2203 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002204 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002205 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002206 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002207 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002208 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002209 break;
2210 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002211 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002212 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002213 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002214 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002215 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002216 if (buffer != NULL)
2217 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002218 if (msg == NULL)
2219 /* JSON or JS mode: re-encode the message. */
2220 msg = json_encode(listtv, ch_mode);
2221 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002222 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002223 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002224
Bram Moolenaar187db502016-02-27 14:44:26 +01002225 if (callback != NULL)
2226 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002227 if (cbitem != NULL)
2228 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2229 else
2230 {
2231 /* invoke the channel callback */
2232 ch_logs(channel, "Invoking channel callback %s",
2233 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002234 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002235 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002236 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002237 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002238 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002239 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002240
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002241 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002242 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002243 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002244
2245 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002246}
2247
2248/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002249 * Return TRUE when channel "channel" is open for writing to.
2250 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002251 */
2252 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002253channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002254{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002255 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002256 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002257}
2258
2259/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002260 * Return TRUE when channel "channel" is open for reading or writing.
2261 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002262 */
2263 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002264channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002265{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002266 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002267 || channel->CH_IN_FD != INVALID_FD
2268 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002269 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002270}
2271
2272/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002273 * Return a string indicating the status of the channel.
2274 */
2275 char *
2276channel_status(channel_T *channel)
2277{
2278 if (channel == NULL)
2279 return "fail";
2280 if (channel_is_open(channel))
2281 return "open";
2282 return "closed";
2283}
2284
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002285 static void
2286channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2287{
2288 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002289 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002290 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002291 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002292
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002293 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002294 STRCAT(namebuf, "_");
2295 tail = STRLEN(namebuf);
2296
2297 STRCPY(namebuf + tail, "status");
2298 dict_add_nr_str(dict, namebuf, 0,
2299 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2300
2301 STRCPY(namebuf + tail, "mode");
2302 switch (chanpart->ch_mode)
2303 {
2304 case MODE_NL: s = "NL"; break;
2305 case MODE_RAW: s = "RAW"; break;
2306 case MODE_JSON: s = "JSON"; break;
2307 case MODE_JS: s = "JS"; break;
2308 }
2309 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2310
2311 STRCPY(namebuf + tail, "io");
2312 if (part == PART_SOCK)
2313 s = "socket";
2314 else switch (chanpart->ch_io)
2315 {
2316 case JIO_NULL: s = "null"; break;
2317 case JIO_PIPE: s = "pipe"; break;
2318 case JIO_FILE: s = "file"; break;
2319 case JIO_BUFFER: s = "buffer"; break;
2320 case JIO_OUT: s = "out"; break;
2321 }
2322 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2323
2324 STRCPY(namebuf + tail, "timeout");
2325 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2326}
2327
2328 void
2329channel_info(channel_T *channel, dict_T *dict)
2330{
2331 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2332 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2333
2334 if (channel->ch_hostname != NULL)
2335 {
2336 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2337 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2338 channel_part_info(channel, dict, "sock", PART_SOCK);
2339 }
2340 else
2341 {
2342 channel_part_info(channel, dict, "out", PART_OUT);
2343 channel_part_info(channel, dict, "err", PART_ERR);
2344 channel_part_info(channel, dict, "in", PART_IN);
2345 }
2346}
2347
Bram Moolenaar77073442016-02-13 23:23:53 +01002348/*
2349 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002350 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002351 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002352 */
2353 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002354channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002355{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002356 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002357
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002358#ifdef FEAT_GUI
2359 channel_gui_unregister(channel);
2360#endif
2361
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002362 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002363 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002364 sock_close(channel->CH_SOCK_FD);
2365 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002366 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002367 may_close_part(&channel->CH_IN_FD);
2368 may_close_part(&channel->CH_OUT_FD);
2369 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002370
Bram Moolenaar8b374212016-02-24 20:43:06 +01002371 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002372 {
2373 typval_T argv[1];
2374 typval_T rettv;
2375 int dummy;
2376
2377 /* invoke the close callback; increment the refcount to avoid it
2378 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002379 ch_logs(channel, "Invoking close callback %s",
2380 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002381 argv[0].v_type = VAR_CHANNEL;
2382 argv[0].vval.v_channel = channel;
2383 ++channel->ch_refcount;
2384 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002385 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2386 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002387 clear_tv(&rettv);
2388 --channel->ch_refcount;
2389
2390 /* the callback is only called once */
2391 vim_free(channel->ch_close_cb);
2392 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002393 partial_unref(channel->ch_close_partial);
2394 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002395 }
2396
2397 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002398}
2399
Bram Moolenaard04a0202016-01-26 23:30:18 +01002400/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002401 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002402 * Returns NULL if there is nothing.
2403 */
2404 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002405channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002406{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002407 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002408
Bram Moolenaar77073442016-02-13 23:23:53 +01002409 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002410 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002411 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002412}
2413
2414/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002415 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002416 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002417 static void
2418channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002419{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002420 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2421 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002422
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002423 while (channel_peek(channel, part) != NULL)
2424 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002425
2426 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002427 {
2428 cbq_T *node = cb_head->cq_next;
2429
2430 remove_cb_node(cb_head, node);
2431 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002432 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002433 vim_free(node);
2434 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002435
2436 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002437 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002438 free_tv(json_head->jq_next->jq_value);
2439 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002440 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002441
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002442 vim_free(channel->ch_part[part].ch_callback);
2443 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002444 partial_unref(channel->ch_part[part].ch_partial);
2445 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002446}
2447
2448/*
2449 * Clear all the read buffers on "channel".
2450 */
2451 void
2452channel_clear(channel_T *channel)
2453{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002454 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002455 vim_free(channel->ch_hostname);
2456 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002457 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002458 channel_clear_one(channel, PART_OUT);
2459 channel_clear_one(channel, PART_ERR);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002460 vim_free(channel->ch_callback);
2461 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002462 partial_unref(channel->ch_partial);
2463 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002464 vim_free(channel->ch_close_cb);
2465 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002466 partial_unref(channel->ch_close_partial);
2467 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002468}
2469
Bram Moolenaar77073442016-02-13 23:23:53 +01002470#if defined(EXITFREE) || defined(PROTO)
2471 void
2472channel_free_all(void)
2473{
2474 channel_T *channel;
2475
Bram Moolenaard6051b52016-02-28 15:49:03 +01002476 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002477 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2478 channel_clear(channel);
2479}
2480#endif
2481
2482
Bram Moolenaard04a0202016-01-26 23:30:18 +01002483/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002484#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002485
2486/* Buffer size for reading incoming messages. */
2487#define MAXMSGSIZE 4096
2488
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002489#if defined(HAVE_SELECT)
2490/*
2491 * Add write fds where we are waiting for writing to be possible.
2492 */
2493 static int
2494channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2495{
2496 int maxfd = maxfd_arg;
2497 channel_T *ch;
2498
2499 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2500 {
2501 chanpart_T *in_part = &ch->ch_part[PART_IN];
2502
2503 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2504 {
2505 FD_SET((int)in_part->ch_fd, wfds);
2506 if ((int)in_part->ch_fd >= maxfd)
2507 maxfd = (int)in_part->ch_fd + 1;
2508 }
2509 }
2510 return maxfd;
2511}
2512#else
2513/*
2514 * Add write fds where we are waiting for writing to be possible.
2515 */
2516 static int
2517channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2518{
2519 int nfd = nfd_in;
2520 channel_T *ch;
2521
2522 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2523 {
2524 chanpart_T *in_part = &ch->ch_part[PART_IN];
2525
2526 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2527 {
2528 in_part->ch_poll_idx = nfd;
2529 fds[nfd].fd = in_part->ch_fd;
2530 fds[nfd].events = POLLOUT;
2531 ++nfd;
2532 }
2533 else
2534 in_part->ch_poll_idx = -1;
2535 }
2536 return nfd;
2537}
2538#endif
2539
Bram Moolenaard04a0202016-01-26 23:30:18 +01002540/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002541 * Check for reading from "fd" with "timeout" msec.
2542 * Return FAIL when there is nothing to read.
2543 */
2544 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002545channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002546{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002547 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002548 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002549
Bram Moolenaard8070362016-02-15 21:56:54 +01002550# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002551 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002552 {
2553 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002554 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002555 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002556 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002557
2558 /* reading from a pipe, not a socket */
2559 while (TRUE)
2560 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002561 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2562 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002563 return OK;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002564
2565 /* perhaps write some buffer lines */
2566 channel_write_any_lines();
2567
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002568 sleep_time = deadline - GetTickCount();
2569 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002570 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002571 /* Wait for a little while. Very short at first, up to 10 msec
2572 * after looping a few times. */
2573 if (sleep_time > delay)
2574 sleep_time = delay;
2575 Sleep(sleep_time);
2576 delay = delay * 2;
2577 if (delay > 10)
2578 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002579 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002580 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002581 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002582#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002583 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002584#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002585 struct timeval tval;
2586 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002587 fd_set wfds;
2588 int ret;
2589 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002590
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002591 tval.tv_sec = timeout / 1000;
2592 tval.tv_usec = (timeout % 1000) * 1000;
2593 for (;;)
2594 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002595 FD_ZERO(&rfds);
2596 FD_SET((int)fd, &rfds);
2597
2598 /* Write lines to a pipe when a pipe can be written to. Need to
2599 * set this every time, some buffers may be done. */
2600 maxfd = (int)fd + 1;
2601 FD_ZERO(&wfds);
2602 maxfd = channel_fill_wfds(maxfd, &wfds);
2603
2604 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002605# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002606 SOCK_ERRNO;
2607 if (ret == -1 && errno == EINTR)
2608 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002609# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002610 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002611 {
2612 if (FD_ISSET(fd, &rfds))
2613 return OK;
2614 channel_write_any_lines();
2615 continue;
2616 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002617 break;
2618 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002619#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002620 for (;;)
2621 {
2622 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2623 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002624
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002625 fds[0].fd = fd;
2626 fds[0].events = POLLIN;
2627 nfd = channel_fill_poll_write(nfd, fds);
2628 if (poll(fds, nfd, timeout) > 0)
2629 {
2630 if (fds[0].revents & POLLIN)
2631 return OK;
2632 channel_write_any_lines();
2633 continue;
2634 }
2635 break;
2636 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002637#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002638 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002639 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002640}
2641
2642/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002643 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002644 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002645 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002646 */
2647 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002648channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002649{
2650 static char_u *buf = NULL;
2651 int len = 0;
2652 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002653 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002654 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002655
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002656 fd = channel->ch_part[part].ch_fd;
2657 if (fd == INVALID_FD)
2658 {
2659 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002661 }
2662 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002663
2664 /* Allocate a buffer to read into. */
2665 if (buf == NULL)
2666 {
2667 buf = alloc(MAXMSGSIZE);
2668 if (buf == NULL)
2669 return; /* out of memory! */
2670 }
2671
2672 /* Keep on reading for as long as there is something to read.
2673 * Use select() or poll() to avoid blocking on a message that is exactly
2674 * MAXMSGSIZE long. */
2675 for (;;)
2676 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002677 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002678 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002679 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002680 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002681 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002682 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002683 if (len <= 0)
2684 break; /* error or nothing more to read */
2685
2686 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002687 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002688 readlen += len;
2689 if (len < MAXMSGSIZE)
2690 break; /* did read everything that's available */
2691 }
2692
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002693 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002694 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002695 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002696 /* Do not give an error message, most likely the other end just
2697 * exited. */
2698 ch_errors(channel, "%s(): Cannot read from channel", func);
2699
Bram Moolenaard04a0202016-01-26 23:30:18 +01002700 /* Queue a "DETACH" netbeans message in the command queue in order to
2701 * terminate the netbeans session later. Do not end the session here
2702 * directly as we may be running in the context of a call to
2703 * netbeans_parse_messages():
2704 * netbeans_parse_messages
2705 * -> autocmd triggered while processing the netbeans cmd
2706 * -> ui_breakcheck
2707 * -> gui event loop or select loop
2708 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002709 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002711 if (channel->ch_part[part].ch_mode == MODE_RAW
2712 || channel->ch_part[part].ch_mode == MODE_NL)
2713 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002714 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002715
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02002716 /* When reading from stdout is not possible, assume the other side has
2717 * died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002718 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002719 if (channel->ch_nb_close_cb != NULL)
2720 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002721 }
2722
2723#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002724 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002725 if (CH_HAS_GUI && gtk_main_level() > 0)
2726 gtk_main_quit();
2727#endif
2728}
2729
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002730/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002731 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002732 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002733 * Returns what was read in allocated memory.
2734 * Returns NULL in case of error or timeout.
2735 */
2736 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002737channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002738{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002739 char_u *buf;
2740 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002741 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002742 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002743 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002744
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002745 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002746 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002747
2748 while (TRUE)
2749 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002750 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002751 if (buf != NULL && (mode == MODE_RAW
2752 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2753 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002754 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002755 continue;
2756
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002757 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002758 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002759 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002760 if (channel_wait(channel, fd, timeout) == FAIL)
2761 {
2762 ch_log(channel, "Timed out");
2763 return NULL;
2764 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002765 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002766 }
2767
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002768 if (mode == MODE_RAW)
2769 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002770 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002771 }
2772 else
2773 {
2774 nl = vim_strchr(buf, NL);
2775 if (nl[1] == NUL)
2776 {
2777 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002778 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002779 *nl = NUL;
2780 }
2781 else
2782 {
2783 /* Copy the message into allocated memory and remove it from the
2784 * buffer. */
2785 msg = vim_strnsave(buf, (int)(nl - buf));
2786 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2787 }
2788 }
2789 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002790 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002791 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002792}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002793
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002794/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002795 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002796 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002797 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002798 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002799 */
2800 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002801channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002802 channel_T *channel,
2803 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002804 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002805 int id,
2806 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002807{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002808 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002809 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002810 int timeout;
2811 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002812
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002813 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002814 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002815 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002816 for (;;)
2817 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002818 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002819
2820 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002821 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002822 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002823 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002824 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002825 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002826
Bram Moolenaard7ece102016-02-02 23:23:02 +01002827 if (!more)
2828 {
2829 /* Handle any other messages in the queue. If done some more
2830 * messages may have arrived. */
2831 if (channel_parse_messages())
2832 continue;
2833
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002834 /* Wait for up to the timeout. If there was an incomplete message
2835 * use the deadline for that. */
2836 timeout = timeout_arg;
2837 if (chanpart->ch_waiting)
2838 {
2839#ifdef WIN32
2840 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2841#else
2842 {
2843 struct timeval now_tv;
2844
2845 gettimeofday(&now_tv, NULL);
2846 timeout = (chanpart->ch_deadline.tv_sec
2847 - now_tv.tv_sec) * 1000
2848 + (chanpart->ch_deadline.tv_usec
2849 - now_tv.tv_usec) / 1000
2850 + 1;
2851 }
2852#endif
2853 if (timeout < 0)
2854 {
2855 /* Something went wrong, channel_parse_json() didn't
2856 * discard message. Cancel waiting. */
2857 chanpart->ch_waiting = FALSE;
2858 timeout = timeout_arg;
2859 }
2860 else if (timeout > timeout_arg)
2861 timeout = timeout_arg;
2862 }
2863 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002864 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002865 {
2866 if (timeout == timeout_arg)
2867 {
2868 if (fd != INVALID_FD)
2869 ch_log(channel, "Timed out");
2870 break;
2871 }
2872 }
2873 else
2874 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002875 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002876 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002877 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002878 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002879}
2880
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002881/*
2882 * Common for ch_read() and ch_readraw().
2883 */
2884 void
2885common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2886{
2887 channel_T *channel;
2888 int part;
2889 jobopt_T opt;
2890 int mode;
2891 int timeout;
2892 int id = -1;
2893 typval_T *listtv = NULL;
2894
2895 /* return an empty string by default */
2896 rettv->v_type = VAR_STRING;
2897 rettv->vval.v_string = NULL;
2898
2899 clear_job_options(&opt);
2900 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2901 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002902 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002903
2904 channel = get_channel_arg(&argvars[0], TRUE);
2905 if (channel != NULL)
2906 {
2907 if (opt.jo_set & JO_PART)
2908 part = opt.jo_part;
2909 else
2910 part = channel_part_read(channel);
2911 mode = channel_get_mode(channel, part);
2912 timeout = channel_get_timeout(channel, part);
2913 if (opt.jo_set & JO_TIMEOUT)
2914 timeout = opt.jo_timeout;
2915
2916 if (raw || mode == MODE_RAW || mode == MODE_NL)
2917 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2918 else
2919 {
2920 if (opt.jo_set & JO_ID)
2921 id = opt.jo_id;
2922 channel_read_json_block(channel, part, timeout, id, &listtv);
2923 if (listtv != NULL)
2924 {
2925 *rettv = *listtv;
2926 vim_free(listtv);
2927 }
2928 else
2929 {
2930 rettv->v_type = VAR_SPECIAL;
2931 rettv->vval.v_number = VVAL_NONE;
2932 }
2933 }
2934 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002935
2936theend:
2937 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002938}
2939
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002940# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2941 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002942/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002943 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002944 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002945 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002946 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002947channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002948{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002949 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002950 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002951
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002952 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01002953 for (channel = first_channel; channel != NULL;
2954 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002955 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002957 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002958 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002959 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002960 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002961 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002962 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002963 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002964}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002965# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002966
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002967# if defined(WIN32) || defined(PROTO)
2968/*
2969 * Check the channels for anything that is ready to be read.
2970 * The data is put in the read queue.
2971 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002972 void
2973channel_handle_events(void)
2974{
2975 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002976 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002977 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002978
2979 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2980 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002981 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002982 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002983 {
2984 fd = channel->ch_part[part].ch_fd;
2985 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
2986 channel_read(channel, part, "channel_handle_events");
2987 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002988 }
2989}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002990# endif
2991
Bram Moolenaard04a0202016-01-26 23:30:18 +01002992/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002993 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002994 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002995 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002996 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002997 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002998channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002999{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003000 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003001 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003002 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003003
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003004 fd = channel->ch_part[part].ch_fd;
3005 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003006 {
3007 if (!channel->ch_error && fun != NULL)
3008 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003009 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003010 EMSG2("E630: %s(): write while not connected", fun);
3011 }
3012 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003013 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003014 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003015
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003016 if (log_fd != NULL)
3017 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003018 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003019 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003020 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003021 fprintf(log_fd, "'\n");
3022 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003023 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003024 }
3025
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003026 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003027 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003028 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003029 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003030 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003031 {
3032 if (!channel->ch_error && fun != NULL)
3033 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003034 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003035 EMSG2("E631: %s(): write failed", fun);
3036 }
3037 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003038 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003039 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003040
3041 channel->ch_error = FALSE;
3042 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003043}
3044
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003045/*
3046 * Common for "ch_sendexpr()" and "ch_sendraw()".
3047 * Returns the channel if the caller should read the response.
3048 * Sets "part_read" to the the read fd.
3049 * Otherwise returns NULL.
3050 */
3051 channel_T *
3052send_common(
3053 typval_T *argvars,
3054 char_u *text,
3055 int id,
3056 int eval,
3057 jobopt_T *opt,
3058 char *fun,
3059 int *part_read)
3060{
3061 channel_T *channel;
3062 int part_send;
3063
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003064 clear_job_options(opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003065 channel = get_channel_arg(&argvars[0], TRUE);
3066 if (channel == NULL)
3067 return NULL;
3068 part_send = channel_part_send(channel);
3069 *part_read = channel_part_read(channel);
3070
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003071 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3072 return NULL;
3073
3074 /* Set the callback. An empty callback means no callback and not reading
3075 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3076 * allowed. */
3077 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3078 {
3079 if (eval)
3080 {
3081 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3082 return NULL;
3083 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003084 channel_set_req_callback(channel, part_send,
3085 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003086 }
3087
3088 if (channel_send(channel, part_send, text, fun) == OK
3089 && opt->jo_callback == NULL)
3090 return channel;
3091 return NULL;
3092}
3093
3094/*
3095 * common for "ch_evalexpr()" and "ch_sendexpr()"
3096 */
3097 void
3098ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3099{
3100 char_u *text;
3101 typval_T *listtv;
3102 channel_T *channel;
3103 int id;
3104 ch_mode_T ch_mode;
3105 int part_send;
3106 int part_read;
3107 jobopt_T opt;
3108 int timeout;
3109
3110 /* return an empty string by default */
3111 rettv->v_type = VAR_STRING;
3112 rettv->vval.v_string = NULL;
3113
3114 channel = get_channel_arg(&argvars[0], TRUE);
3115 if (channel == NULL)
3116 return;
3117 part_send = channel_part_send(channel);
3118
3119 ch_mode = channel_get_mode(channel, part_send);
3120 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3121 {
3122 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3123 return;
3124 }
3125
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003126 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003127 text = json_encode_nr_expr(id, &argvars[1],
3128 ch_mode == MODE_JS ? JSON_JS : 0);
3129 if (text == NULL)
3130 return;
3131
3132 channel = send_common(argvars, text, id, eval, &opt,
3133 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3134 vim_free(text);
3135 if (channel != NULL && eval)
3136 {
3137 if (opt.jo_set & JO_TIMEOUT)
3138 timeout = opt.jo_timeout;
3139 else
3140 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003141 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3142 == OK)
3143 {
3144 list_T *list = listtv->vval.v_list;
3145
3146 /* Move the item from the list and then change the type to
3147 * avoid the value being freed. */
3148 *rettv = list->lv_last->li_tv;
3149 list->lv_last->li_tv.v_type = VAR_NUMBER;
3150 free_tv(listtv);
3151 }
3152 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003153 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003154}
3155
3156/*
3157 * common for "ch_evalraw()" and "ch_sendraw()"
3158 */
3159 void
3160ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3161{
3162 char_u buf[NUMBUFLEN];
3163 char_u *text;
3164 channel_T *channel;
3165 int part_read;
3166 jobopt_T opt;
3167 int timeout;
3168
3169 /* return an empty string by default */
3170 rettv->v_type = VAR_STRING;
3171 rettv->vval.v_string = NULL;
3172
3173 text = get_tv_string_buf(&argvars[1], buf);
3174 channel = send_common(argvars, text, 0, eval, &opt,
3175 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3176 if (channel != NULL && eval)
3177 {
3178 if (opt.jo_set & JO_TIMEOUT)
3179 timeout = opt.jo_timeout;
3180 else
3181 timeout = channel_get_timeout(channel, part_read);
3182 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3183 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003184 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003185}
3186
Bram Moolenaard04a0202016-01-26 23:30:18 +01003187# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003188/*
3189 * Add open channels to the poll struct.
3190 * Return the adjusted struct index.
3191 * The type of "fds" is hidden to avoid problems with the function proto.
3192 */
3193 int
3194channel_poll_setup(int nfd_in, void *fds_in)
3195{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003196 int nfd = nfd_in;
3197 channel_T *channel;
3198 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003199 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003200
Bram Moolenaar77073442016-02-13 23:23:53 +01003201 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003202 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003203 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003204 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003205 chanpart_T *ch_part = &channel->ch_part[part];
3206
3207 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003208 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003209 ch_part->ch_poll_idx = nfd;
3210 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003211 fds[nfd].events = POLLIN;
3212 nfd++;
3213 }
3214 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003215 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003216 }
3217 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003218
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003219 nfd = channel_fill_poll_write(nfd, fds);
3220
Bram Moolenaare0874f82016-01-24 20:36:41 +01003221 return nfd;
3222}
3223
3224/*
3225 * The type of "fds" is hidden to avoid problems with the function proto.
3226 */
3227 int
3228channel_poll_check(int ret_in, void *fds_in)
3229{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003230 int ret = ret_in;
3231 channel_T *channel;
3232 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003233 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003234 int idx;
3235 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003236
Bram Moolenaar77073442016-02-13 23:23:53 +01003237 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003238 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003239 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003240 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003241 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003242
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003243 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003244 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003245 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003246 --ret;
3247 }
3248 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003249
3250 in_part = &channel->ch_part[PART_IN];
3251 idx = in_part->ch_poll_idx;
3252 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3253 {
3254 if (in_part->ch_buf_append)
3255 {
3256 if (in_part->ch_buffer != NULL)
3257 channel_write_new_lines(in_part->ch_buffer);
3258 }
3259 else
3260 channel_write_in(channel);
3261 --ret;
3262 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003263 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003264
3265 return ret;
3266}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003267# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003268
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003269# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003270/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003271 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003272 */
3273 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003274channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003275{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003276 int maxfd = maxfd_in;
3277 channel_T *channel;
3278 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003279 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003280 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003281
Bram Moolenaar77073442016-02-13 23:23:53 +01003282 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003283 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003284 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003285 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003286 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003287
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003288 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003289 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003290 FD_SET((int)fd, rfds);
3291 if (maxfd < (int)fd)
3292 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003293 }
3294 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003295 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003296
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003297 maxfd = channel_fill_wfds(maxfd, wfds);
3298
Bram Moolenaare0874f82016-01-24 20:36:41 +01003299 return maxfd;
3300}
3301
3302/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003303 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003304 */
3305 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003306channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003307{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003308 int ret = ret_in;
3309 channel_T *channel;
3310 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003311 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003312 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003313 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003314
Bram Moolenaar77073442016-02-13 23:23:53 +01003315 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003316 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003317 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003318 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003319 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003320
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003321 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003322 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003323 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003324 --ret;
3325 }
3326 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003327
3328 in_part = &channel->ch_part[PART_IN];
3329 if (ret > 0 && in_part->ch_fd != INVALID_FD
3330 && FD_ISSET(in_part->ch_fd, wfds))
3331 {
3332 if (in_part->ch_buf_append)
3333 {
3334 if (in_part->ch_buffer != NULL)
3335 channel_write_new_lines(in_part->ch_buffer);
3336 }
3337 else
3338 channel_write_in(channel);
3339 --ret;
3340 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003341 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003342
3343 return ret;
3344}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003345# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003346
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003347/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003348 * Return TRUE if "channel" has JSON or other typeahead.
3349 */
3350 static int
3351channel_has_readahead(channel_T *channel, int part)
3352{
3353 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3354
3355 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3356 {
3357 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3358 jsonq_T *item = head->jq_next;
3359
3360 return item != NULL;
3361 }
3362 return channel_peek(channel, part) != NULL;
3363}
3364
3365/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003366 * Execute queued up commands.
3367 * Invoked from the main loop when it's safe to execute received commands.
3368 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003369 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003370 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003371channel_parse_messages(void)
3372{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003373 channel_T *channel = first_channel;
3374 int ret = FALSE;
3375 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003376 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003377
Bram Moolenaard0b65022016-03-06 21:50:33 +01003378 /* Only do this message when another message was given, otherwise we get
3379 * lots of them. */
3380 if (did_log_msg)
3381 {
3382 ch_log(NULL, "looking for messages on channels");
3383 did_log_msg = FALSE;
3384 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003385 while (channel != NULL)
3386 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003387 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003388 {
3389 /* channel is no longer useful, free it */
3390 channel_free(channel);
3391 channel = first_channel;
3392 part = PART_SOCK;
3393 continue;
3394 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003395 if (channel->ch_part[part].ch_fd != INVALID_FD
3396 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003397 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003398 /* Increase the refcount, in case the handler causes the channel
3399 * to be unreferenced or closed. */
3400 ++channel->ch_refcount;
3401 r = may_invoke_callback(channel, part);
3402 if (r == OK)
3403 ret = TRUE;
3404 if (channel_unref(channel) || r == OK)
3405 {
3406 /* channel was freed or something was done, start over */
3407 channel = first_channel;
3408 part = PART_SOCK;
3409 continue;
3410 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003411 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003412 if (part < PART_ERR)
3413 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003414 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003415 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003416 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003417 part = PART_SOCK;
3418 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003419 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003420
3421 if (channel_need_redraw && must_redraw)
3422 {
3423 channel_need_redraw = FALSE;
3424 update_screen(0);
3425 setcursor();
3426 cursor_on();
3427 out_flush();
3428 }
3429
Bram Moolenaard7ece102016-02-02 23:23:02 +01003430 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003431}
3432
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003433/*
3434 * Mark references to lists used in channels.
3435 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003436 int
3437set_ref_in_channel(int copyID)
3438{
Bram Moolenaar77073442016-02-13 23:23:53 +01003439 int abort = FALSE;
3440 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003441 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003442
Bram Moolenaar77073442016-02-13 23:23:53 +01003443 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003444 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003445 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003446 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003447 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3448 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003449
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003450 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003451 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003452 list_T *l = item->jq_value->vval.v_list;
3453
3454 if (l->lv_copyID != copyID)
3455 {
3456 l->lv_copyID = copyID;
3457 abort = abort || set_ref_in_list(l, copyID, NULL);
3458 }
3459 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003460 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003461 }
3462 }
3463 return abort;
3464}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003465
3466/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003467 * Return the "part" to write to for "channel".
3468 */
3469 int
3470channel_part_send(channel_T *channel)
3471{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003472 if (channel->CH_SOCK_FD == INVALID_FD)
3473 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 return PART_SOCK;
3475}
3476
3477/*
3478 * Return the default "part" to read from for "channel".
3479 */
3480 int
3481channel_part_read(channel_T *channel)
3482{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003483 if (channel->CH_SOCK_FD == INVALID_FD)
3484 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003485 return PART_SOCK;
3486}
3487
3488/*
3489 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003490 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003491 */
3492 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003493channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003494{
Bram Moolenaar77073442016-02-13 23:23:53 +01003495 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003496 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003497 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003498}
3499
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003500/*
3501 * Return the timeout of "channel"/"part"
3502 */
3503 int
3504channel_get_timeout(channel_T *channel, int part)
3505{
3506 return channel->ch_part[part].ch_timeout;
3507}
3508
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003509 static int
3510handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3511{
3512 char_u *val = get_tv_string(item);
3513
3514 opt->jo_set |= jo;
3515 if (STRCMP(val, "nl") == 0)
3516 *modep = MODE_NL;
3517 else if (STRCMP(val, "raw") == 0)
3518 *modep = MODE_RAW;
3519 else if (STRCMP(val, "js") == 0)
3520 *modep = MODE_JS;
3521 else if (STRCMP(val, "json") == 0)
3522 *modep = MODE_JSON;
3523 else
3524 {
3525 EMSG2(_(e_invarg2), val);
3526 return FAIL;
3527 }
3528 return OK;
3529}
3530
3531 static int
3532handle_io(typval_T *item, int part, jobopt_T *opt)
3533{
3534 char_u *val = get_tv_string(item);
3535
3536 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3537 if (STRCMP(val, "null") == 0)
3538 opt->jo_io[part] = JIO_NULL;
3539 else if (STRCMP(val, "pipe") == 0)
3540 opt->jo_io[part] = JIO_PIPE;
3541 else if (STRCMP(val, "file") == 0)
3542 opt->jo_io[part] = JIO_FILE;
3543 else if (STRCMP(val, "buffer") == 0)
3544 opt->jo_io[part] = JIO_BUFFER;
3545 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3546 opt->jo_io[part] = JIO_OUT;
3547 else
3548 {
3549 EMSG2(_(e_invarg2), val);
3550 return FAIL;
3551 }
3552 return OK;
3553}
3554
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003555/*
3556 * Clear a jobopt_T before using it.
3557 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003558 void
3559clear_job_options(jobopt_T *opt)
3560{
3561 vim_memset(opt, 0, sizeof(jobopt_T));
3562}
3563
3564/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003565 * Free any members of a jobopt_T.
3566 */
3567 void
3568free_job_options(jobopt_T *opt)
3569{
3570 if (opt->jo_partial != NULL)
3571 partial_unref(opt->jo_partial);
3572 if (opt->jo_out_partial != NULL)
3573 partial_unref(opt->jo_out_partial);
3574 if (opt->jo_err_partial != NULL)
3575 partial_unref(opt->jo_err_partial);
3576 if (opt->jo_close_partial != NULL)
3577 partial_unref(opt->jo_close_partial);
3578}
3579
3580/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003581 * Get the PART_ number from the first character of an option name.
3582 */
3583 static int
3584part_from_char(int c)
3585{
3586 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3587}
3588
3589/*
3590 * Get the option entries from the dict in "tv", parse them and put the result
3591 * in "opt".
3592 * Only accept options in "supported".
3593 * If an option value is invalid return FAIL.
3594 */
3595 int
3596get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3597{
3598 typval_T *item;
3599 char_u *val;
3600 dict_T *dict;
3601 int todo;
3602 hashitem_T *hi;
3603 int part;
3604
3605 opt->jo_set = 0;
3606 if (tv->v_type == VAR_UNKNOWN)
3607 return OK;
3608 if (tv->v_type != VAR_DICT)
3609 {
3610 EMSG(_(e_invarg));
3611 return FAIL;
3612 }
3613 dict = tv->vval.v_dict;
3614 if (dict == NULL)
3615 return OK;
3616
3617 todo = (int)dict->dv_hashtab.ht_used;
3618 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3619 if (!HASHITEM_EMPTY(hi))
3620 {
3621 item = &dict_lookup(hi)->di_tv;
3622
3623 if (STRCMP(hi->hi_key, "mode") == 0)
3624 {
3625 if (!(supported & JO_MODE))
3626 break;
3627 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3628 return FAIL;
3629 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003630 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003631 {
3632 if (!(supported & JO_IN_MODE))
3633 break;
3634 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3635 == FAIL)
3636 return FAIL;
3637 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003638 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003639 {
3640 if (!(supported & JO_OUT_MODE))
3641 break;
3642 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3643 == FAIL)
3644 return FAIL;
3645 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003646 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003647 {
3648 if (!(supported & JO_ERR_MODE))
3649 break;
3650 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3651 == FAIL)
3652 return FAIL;
3653 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003654 else if (STRCMP(hi->hi_key, "in_io") == 0
3655 || STRCMP(hi->hi_key, "out_io") == 0
3656 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003657 {
3658 if (!(supported & JO_OUT_IO))
3659 break;
3660 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3661 return FAIL;
3662 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003663 else if (STRCMP(hi->hi_key, "in_name") == 0
3664 || STRCMP(hi->hi_key, "out_name") == 0
3665 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003666 {
3667 part = part_from_char(*hi->hi_key);
3668
3669 if (!(supported & JO_OUT_IO))
3670 break;
3671 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3672 opt->jo_io_name[part] =
3673 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3674 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003675 else if (STRCMP(hi->hi_key, "in_buf") == 0
3676 || STRCMP(hi->hi_key, "out_buf") == 0
3677 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003678 {
3679 part = part_from_char(*hi->hi_key);
3680
3681 if (!(supported & JO_OUT_IO))
3682 break;
3683 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3684 opt->jo_io_buf[part] = get_tv_number(item);
3685 if (opt->jo_io_buf[part] <= 0)
3686 {
3687 EMSG2(_(e_invarg2), get_tv_string(item));
3688 return FAIL;
3689 }
3690 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3691 {
3692 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3693 return FAIL;
3694 }
3695 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003696 else if (STRCMP(hi->hi_key, "in_top") == 0
3697 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698 {
3699 linenr_T *lp;
3700
3701 if (!(supported & JO_OUT_IO))
3702 break;
3703 if (hi->hi_key[3] == 't')
3704 {
3705 lp = &opt->jo_in_top;
3706 opt->jo_set |= JO_IN_TOP;
3707 }
3708 else
3709 {
3710 lp = &opt->jo_in_bot;
3711 opt->jo_set |= JO_IN_BOT;
3712 }
3713 *lp = get_tv_number(item);
3714 if (*lp < 0)
3715 {
3716 EMSG2(_(e_invarg2), get_tv_string(item));
3717 return FAIL;
3718 }
3719 }
3720 else if (STRCMP(hi->hi_key, "channel") == 0)
3721 {
3722 if (!(supported & JO_OUT_IO))
3723 break;
3724 opt->jo_set |= JO_CHANNEL;
3725 if (item->v_type != VAR_CHANNEL)
3726 {
3727 EMSG2(_(e_invarg2), "channel");
3728 return FAIL;
3729 }
3730 opt->jo_channel = item->vval.v_channel;
3731 }
3732 else if (STRCMP(hi->hi_key, "callback") == 0)
3733 {
3734 if (!(supported & JO_CALLBACK))
3735 break;
3736 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003737 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003738 if (opt->jo_callback == NULL)
3739 {
3740 EMSG2(_(e_invarg2), "callback");
3741 return FAIL;
3742 }
3743 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003744 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003745 {
3746 if (!(supported & JO_OUT_CALLBACK))
3747 break;
3748 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003749 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003750 if (opt->jo_out_cb == NULL)
3751 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003752 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003753 return FAIL;
3754 }
3755 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003756 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003757 {
3758 if (!(supported & JO_ERR_CALLBACK))
3759 break;
3760 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003761 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003762 if (opt->jo_err_cb == NULL)
3763 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003764 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003765 return FAIL;
3766 }
3767 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003768 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003769 {
3770 if (!(supported & JO_CLOSE_CALLBACK))
3771 break;
3772 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003773 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003774 if (opt->jo_close_cb == NULL)
3775 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003776 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003777 return FAIL;
3778 }
3779 }
3780 else if (STRCMP(hi->hi_key, "waittime") == 0)
3781 {
3782 if (!(supported & JO_WAITTIME))
3783 break;
3784 opt->jo_set |= JO_WAITTIME;
3785 opt->jo_waittime = get_tv_number(item);
3786 }
3787 else if (STRCMP(hi->hi_key, "timeout") == 0)
3788 {
3789 if (!(supported & JO_TIMEOUT))
3790 break;
3791 opt->jo_set |= JO_TIMEOUT;
3792 opt->jo_timeout = get_tv_number(item);
3793 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003794 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003795 {
3796 if (!(supported & JO_OUT_TIMEOUT))
3797 break;
3798 opt->jo_set |= JO_OUT_TIMEOUT;
3799 opt->jo_out_timeout = get_tv_number(item);
3800 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003801 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003802 {
3803 if (!(supported & JO_ERR_TIMEOUT))
3804 break;
3805 opt->jo_set |= JO_ERR_TIMEOUT;
3806 opt->jo_err_timeout = get_tv_number(item);
3807 }
3808 else if (STRCMP(hi->hi_key, "part") == 0)
3809 {
3810 if (!(supported & JO_PART))
3811 break;
3812 opt->jo_set |= JO_PART;
3813 val = get_tv_string(item);
3814 if (STRCMP(val, "err") == 0)
3815 opt->jo_part = PART_ERR;
3816 else
3817 {
3818 EMSG2(_(e_invarg2), val);
3819 return FAIL;
3820 }
3821 }
3822 else if (STRCMP(hi->hi_key, "id") == 0)
3823 {
3824 if (!(supported & JO_ID))
3825 break;
3826 opt->jo_set |= JO_ID;
3827 opt->jo_id = get_tv_number(item);
3828 }
3829 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3830 {
3831 if (!(supported & JO_STOPONEXIT))
3832 break;
3833 opt->jo_set |= JO_STOPONEXIT;
3834 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3835 opt->jo_soe_buf);
3836 if (opt->jo_stoponexit == NULL)
3837 {
3838 EMSG2(_(e_invarg2), "stoponexit");
3839 return FAIL;
3840 }
3841 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003842 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003843 {
3844 if (!(supported & JO_EXIT_CB))
3845 break;
3846 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003847 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3848 {
3849 opt->jo_exit_partial = item->vval.v_partial;
3850 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3851 }
3852 else
3853 opt->jo_exit_cb = get_tv_string_buf_chk(
3854 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003855 if (opt->jo_exit_cb == NULL)
3856 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003857 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003858 return FAIL;
3859 }
3860 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003861 else if (STRCMP(hi->hi_key, "block_write") == 0)
3862 {
3863 if (!(supported & JO_BLOCK_WRITE))
3864 break;
3865 opt->jo_set |= JO_BLOCK_WRITE;
3866 opt->jo_block_write = get_tv_number(item);
3867 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003868 else
3869 break;
3870 --todo;
3871 }
3872 if (todo > 0)
3873 {
3874 EMSG2(_(e_invarg2), hi->hi_key);
3875 return FAIL;
3876 }
3877
3878 return OK;
3879}
3880
3881/*
3882 * Get the channel from the argument.
3883 * Returns NULL if the handle is invalid.
3884 */
3885 channel_T *
3886get_channel_arg(typval_T *tv, int check_open)
3887{
3888 channel_T *channel = NULL;
3889
3890 if (tv->v_type == VAR_JOB)
3891 {
3892 if (tv->vval.v_job != NULL)
3893 channel = tv->vval.v_job->jv_channel;
3894 }
3895 else if (tv->v_type == VAR_CHANNEL)
3896 {
3897 channel = tv->vval.v_channel;
3898 }
3899 else
3900 {
3901 EMSG2(_(e_invarg2), get_tv_string(tv));
3902 return NULL;
3903 }
3904
3905 if (check_open && (channel == NULL || !channel_is_open(channel)))
3906 {
3907 EMSG(_("E906: not an open channel"));
3908 return NULL;
3909 }
3910 return channel;
3911}
3912
3913static job_T *first_job = NULL;
3914
3915 static void
3916job_free(job_T *job)
3917{
3918 ch_log(job->jv_channel, "Freeing job");
3919 if (job->jv_channel != NULL)
3920 {
3921 /* The link from the channel to the job doesn't count as a reference,
3922 * thus don't decrement the refcount of the job. The reference from
3923 * the job to the channel does count the refrence, decrement it and
3924 * NULL the reference. We don't set ch_job_killed, unreferencing the
3925 * job doesn't mean it stops running. */
3926 job->jv_channel->ch_job = NULL;
3927 channel_unref(job->jv_channel);
3928 }
3929 mch_clear_job(job);
3930
3931 if (job->jv_next != NULL)
3932 job->jv_next->jv_prev = job->jv_prev;
3933 if (job->jv_prev == NULL)
3934 first_job = job->jv_next;
3935 else
3936 job->jv_prev->jv_next = job->jv_next;
3937
3938 vim_free(job->jv_stoponexit);
3939 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003940 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003941 vim_free(job);
3942}
3943
3944 void
3945job_unref(job_T *job)
3946{
3947 if (job != NULL && --job->jv_refcount <= 0)
3948 {
3949 /* Do not free the job when it has not ended yet and there is a
3950 * "stoponexit" flag or an exit callback. */
3951 if (job->jv_status != JOB_STARTED
3952 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
3953 {
3954 job_free(job);
3955 }
3956 else if (job->jv_channel != NULL)
3957 {
3958 /* Do remove the link to the channel, otherwise it hangs
3959 * around until Vim exits. See job_free() for refcount. */
3960 job->jv_channel->ch_job = NULL;
3961 channel_unref(job->jv_channel);
3962 job->jv_channel = NULL;
3963 }
3964 }
3965}
3966
3967/*
3968 * Allocate a job. Sets the refcount to one and sets options default.
3969 */
3970 static job_T *
3971job_alloc(void)
3972{
3973 job_T *job;
3974
3975 job = (job_T *)alloc_clear(sizeof(job_T));
3976 if (job != NULL)
3977 {
3978 job->jv_refcount = 1;
3979 job->jv_stoponexit = vim_strsave((char_u *)"term");
3980
3981 if (first_job != NULL)
3982 {
3983 first_job->jv_prev = job;
3984 job->jv_next = first_job;
3985 }
3986 first_job = job;
3987 }
3988 return job;
3989}
3990
3991 void
3992job_set_options(job_T *job, jobopt_T *opt)
3993{
3994 if (opt->jo_set & JO_STOPONEXIT)
3995 {
3996 vim_free(job->jv_stoponexit);
3997 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
3998 job->jv_stoponexit = NULL;
3999 else
4000 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4001 }
4002 if (opt->jo_set & JO_EXIT_CB)
4003 {
4004 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004005 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004006 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004007 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004008 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004009 job->jv_exit_partial = NULL;
4010 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004011 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004012 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004013 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004014 job->jv_exit_partial = opt->jo_exit_partial;
4015 if (job->jv_exit_partial != NULL)
4016 ++job->jv_exit_partial->pt_refcount;
4017 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004018 }
4019}
4020
4021/*
4022 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4023 */
4024 void
4025job_stop_on_exit()
4026{
4027 job_T *job;
4028
4029 for (job = first_job; job != NULL; job = job->jv_next)
4030 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4031 mch_stop_job(job, job->jv_stoponexit);
4032}
4033
4034/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004035 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004036 */
4037 void
4038job_check_ended(void)
4039{
4040 static time_t last_check = 0;
4041 time_t now;
4042 job_T *job;
4043 job_T *next;
4044
4045 /* Only do this once in 10 seconds. */
4046 now = time(NULL);
4047 if (last_check + 10 < now)
4048 {
4049 last_check = now;
4050 for (job = first_job; job != NULL; job = next)
4051 {
4052 next = job->jv_next;
4053 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4054 job_status(job); /* may free "job" */
4055 }
4056 }
4057}
4058
4059/*
4060 * "job_start()" function
4061 */
4062 job_T *
4063job_start(typval_T *argvars)
4064{
4065 job_T *job;
4066 char_u *cmd = NULL;
4067#if defined(UNIX)
4068# define USE_ARGV
4069 char **argv = NULL;
4070 int argc = 0;
4071#else
4072 garray_T ga;
4073#endif
4074 jobopt_T opt;
4075 int part;
4076
4077 job = job_alloc();
4078 if (job == NULL)
4079 return NULL;
4080
4081 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004082#ifndef USE_ARGV
4083 ga_init2(&ga, (int)sizeof(char*), 20);
4084#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085
4086 /* Default mode is NL. */
4087 clear_job_options(&opt);
4088 opt.jo_mode = MODE_NL;
4089 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004090 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4091 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004092 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004093
4094 /* Check that when io is "file" that there is a file name. */
4095 for (part = PART_OUT; part <= PART_IN; ++part)
4096 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4097 && opt.jo_io[part] == JIO_FILE
4098 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4099 || *opt.jo_io_name[part] == NUL))
4100 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004101 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004102 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103 }
4104
4105 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4106 {
4107 buf_T *buf = NULL;
4108
4109 /* check that we can find the buffer before starting the job */
4110 if (opt.jo_set & JO_IN_BUF)
4111 {
4112 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4113 if (buf == NULL)
4114 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4115 }
4116 else if (!(opt.jo_set & JO_IN_NAME))
4117 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004118 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119 }
4120 else
4121 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4122 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004123 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004124 if (buf->b_ml.ml_mfp == NULL)
4125 {
4126 char_u numbuf[NUMBUFLEN];
4127 char_u *s;
4128
4129 if (opt.jo_set & JO_IN_BUF)
4130 {
4131 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4132 s = numbuf;
4133 }
4134 else
4135 s = opt.jo_io_name[PART_IN];
4136 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004137 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004138 }
4139 job->jv_in_buf = buf;
4140 }
4141
4142 job_set_options(job, &opt);
4143
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 if (argvars[0].v_type == VAR_STRING)
4145 {
4146 /* Command is a string. */
4147 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004148 if (cmd == NULL || *cmd == NUL)
4149 {
4150 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004151 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004152 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004153#ifdef USE_ARGV
4154 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004155 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 argv[argc] = NULL;
4157#endif
4158 }
4159 else if (argvars[0].v_type != VAR_LIST
4160 || argvars[0].vval.v_list == NULL
4161 || argvars[0].vval.v_list->lv_len < 1)
4162 {
4163 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004164 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004165 }
4166 else
4167 {
4168 list_T *l = argvars[0].vval.v_list;
4169 listitem_T *li;
4170 char_u *s;
4171
4172#ifdef USE_ARGV
4173 /* Pass argv[] to mch_call_shell(). */
4174 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4175 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004176 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004177#endif
4178 for (li = l->lv_first; li != NULL; li = li->li_next)
4179 {
4180 s = get_tv_string_chk(&li->li_tv);
4181 if (s == NULL)
4182 goto theend;
4183#ifdef USE_ARGV
4184 argv[argc++] = (char *)s;
4185#else
4186 /* Only escape when needed, double quotes are not always allowed. */
4187 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4188 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004189# ifdef WIN32
4190 int old_ssl = p_ssl;
4191
4192 /* This is using CreateProcess, not cmd.exe. Always use
4193 * double quote and backslashes. */
4194 p_ssl = 0;
4195# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004196 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004197# ifdef WIN32
4198 p_ssl = old_ssl;
4199# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 if (s == NULL)
4201 goto theend;
4202 ga_concat(&ga, s);
4203 vim_free(s);
4204 }
4205 else
4206 ga_concat(&ga, s);
4207 if (li->li_next != NULL)
4208 ga_append(&ga, ' ');
4209#endif
4210 }
4211#ifdef USE_ARGV
4212 argv[argc] = NULL;
4213#else
4214 cmd = ga.ga_data;
4215#endif
4216 }
4217
4218#ifdef USE_ARGV
4219 if (ch_log_active())
4220 {
4221 garray_T ga;
4222 int i;
4223
4224 ga_init2(&ga, (int)sizeof(char), 200);
4225 for (i = 0; i < argc; ++i)
4226 {
4227 if (i > 0)
4228 ga_concat(&ga, (char_u *)" ");
4229 ga_concat(&ga, (char_u *)argv[i]);
4230 }
4231 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4232 ga_clear(&ga);
4233 }
4234 mch_start_job(argv, job, &opt);
4235#else
4236 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4237 mch_start_job((char *)cmd, job, &opt);
4238#endif
4239
4240 /* If the channel is reading from a buffer, write lines now. */
4241 if (job->jv_channel != NULL)
4242 channel_write_in(job->jv_channel);
4243
4244theend:
4245#ifdef USE_ARGV
4246 vim_free(argv);
4247#else
4248 vim_free(ga.ga_data);
4249#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004250 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004251 return job;
4252}
4253
4254/*
4255 * Get the status of "job" and invoke the exit callback when needed.
4256 * The returned string is not allocated.
4257 */
4258 char *
4259job_status(job_T *job)
4260{
4261 char *result;
4262
4263 if (job->jv_status == JOB_ENDED)
4264 /* No need to check, dead is dead. */
4265 result = "dead";
4266 else if (job->jv_status == JOB_FAILED)
4267 result = "fail";
4268 else
4269 {
4270 result = mch_job_status(job);
4271 if (job->jv_status == JOB_ENDED)
4272 ch_log(job->jv_channel, "Job ended");
4273 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4274 {
4275 typval_T argv[3];
4276 typval_T rettv;
4277 int dummy;
4278
4279 /* invoke the exit callback; make sure the refcount is > 0 */
4280 ++job->jv_refcount;
4281 argv[0].v_type = VAR_JOB;
4282 argv[0].vval.v_job = job;
4283 argv[1].v_type = VAR_NUMBER;
4284 argv[1].vval.v_number = job->jv_exitval;
4285 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004286 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4287 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004288 clear_tv(&rettv);
4289 --job->jv_refcount;
4290 }
4291 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4292 {
4293 /* The job was already unreferenced, now that it ended it can be
4294 * freed. Careful: caller must not use "job" after this! */
4295 job_free(job);
4296 }
4297 }
4298 return result;
4299}
4300
Bram Moolenaar8950a562016-03-12 15:22:55 +01004301/*
4302 * Implementation of job_info().
4303 */
4304 void
4305job_info(job_T *job, dict_T *dict)
4306{
4307 dictitem_T *item;
4308 varnumber_T nr;
4309
4310 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4311
4312 item = dictitem_alloc((char_u *)"channel");
4313 if (item == NULL)
4314 return;
4315 item->di_tv.v_lock = 0;
4316 item->di_tv.v_type = VAR_CHANNEL;
4317 item->di_tv.vval.v_channel = job->jv_channel;
4318 if (job->jv_channel != NULL)
4319 ++job->jv_channel->ch_refcount;
4320 if (dict_add(dict, item) == FAIL)
4321 dictitem_free(item);
4322
4323#ifdef UNIX
4324 nr = job->jv_pid;
4325#else
4326 nr = job->jv_proc_info.dwProcessId;
4327#endif
4328 dict_add_nr_str(dict, "process", nr, NULL);
4329
4330 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004331 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004332 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4333}
4334
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004335 int
4336job_stop(job_T *job, typval_T *argvars)
4337{
4338 char_u *arg;
4339
4340 if (argvars[1].v_type == VAR_UNKNOWN)
4341 arg = (char_u *)"";
4342 else
4343 {
4344 arg = get_tv_string_chk(&argvars[1]);
4345 if (arg == NULL)
4346 {
4347 EMSG(_(e_invarg));
4348 return 0;
4349 }
4350 }
4351 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4352 if (mch_stop_job(job, arg) == FAIL)
4353 return 0;
4354
4355 /* Assume that "hup" does not kill the job. */
4356 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4357 job->jv_channel->ch_job_killed = TRUE;
4358
4359 /* We don't try freeing the job, obviously the caller still has a
4360 * reference to it. */
4361 return 1;
4362}
4363
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004364#endif /* FEAT_JOB_CHANNEL */