blob: 0d3f452d5b8b9cf1a9ff61fa842bef30d4e51b10 [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 Moolenaar42d38a22016-02-20 18:18:59 +0100437 * TODO: instead of channel ID use the FD.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100438 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100439#ifdef FEAT_GUI_X11
440 static void
441messageFromNetbeans(XtPointer clientData,
442 int *unused1 UNUSED,
443 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100444{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100445 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100446}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100447#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100448
Bram Moolenaard04a0202016-01-26 23:30:18 +0100449#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100450# if GTK_CHECK_VERSION(3,0,0)
451 static gboolean
452messageFromNetbeans(GIOChannel *unused1 UNUSED,
453 GIOCondition unused2 UNUSED,
454 gpointer clientData)
455{
456 channel_read_fd(GPOINTER_TO_INT(clientData));
457 return TRUE; /* Return FALSE instead in case the event source is to
458 * be removed after this function returns. */
459}
460# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100461 static void
462messageFromNetbeans(gpointer clientData,
463 gint unused1 UNUSED,
464 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100466 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467}
Bram Moolenaar98921892016-02-23 17:14:37 +0100468# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100469#endif
470
471 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100472channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100473{
Bram Moolenaarde279892016-03-11 22:19:44 +0100474 if (!CH_HAS_GUI)
475 return;
476
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100477# ifdef FEAT_GUI_X11
478 /* Tell notifier we are interested in being called
479 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100480 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
481 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100482 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100483 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100484 (XtPointer)(XtInputReadMask + XtInputExceptMask),
485 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100486 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100487# else
488# ifdef FEAT_GUI_GTK
489 /* Tell gdk we are interested in being called when there
490 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100491 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100492# if GTK_CHECK_VERSION(3,0,0)
493 {
494 GIOChannel *chnnl = g_io_channel_unix_new(
495 (gint)channel->ch_part[part].ch_fd);
496
497 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
498 chnnl,
499 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
500 messageFromNetbeans,
501 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
502
503 g_io_channel_unref(chnnl);
504 }
505# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100506 channel->ch_part[part].ch_inputHandler = gdk_input_add(
507 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100508 (GdkInputCondition)
509 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100510 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100512# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100513# endif
514# endif
515}
516
Bram Moolenaarde279892016-03-11 22:19:44 +0100517 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100518channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100520 if (channel->CH_SOCK_FD != INVALID_FD)
521 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100522 if (channel->CH_OUT_FD != INVALID_FD)
523 channel_gui_register_one(channel, PART_OUT);
524 if (channel->CH_ERR_FD != INVALID_FD)
525 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100526}
527
528/*
529 * Register any of our file descriptors with the GUI event handling system.
530 * Called when the GUI has started.
531 */
532 void
533channel_gui_register_all(void)
534{
Bram Moolenaar77073442016-02-13 23:23:53 +0100535 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100536
Bram Moolenaar77073442016-02-13 23:23:53 +0100537 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100538 channel_gui_register(channel);
539}
540
541 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100542channel_gui_unregister_one(channel_T *channel, int part)
543{
544# ifdef FEAT_GUI_X11
545 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
546 {
547 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
548 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
549 }
550# else
551# ifdef FEAT_GUI_GTK
552 if (channel->ch_part[part].ch_inputHandler != 0)
553 {
554# if GTK_CHECK_VERSION(3,0,0)
555 g_source_remove(channel->ch_part[part].ch_inputHandler);
556# else
557 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
558# endif
559 channel->ch_part[part].ch_inputHandler = 0;
560 }
561# endif
562# endif
563}
564
565 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100566channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100567{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100569
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100571 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100572}
573
574#endif
575
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100576static char *e_cannot_connect = N_("E902: Cannot connect to port");
577
Bram Moolenaard04a0202016-01-26 23:30:18 +0100578/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100579 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100580 * "waittime" is the time in msec to wait for the connection.
581 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100582 * Returns the channel for success.
583 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100584 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100585 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100586channel_open(
587 char *hostname,
588 int port_in,
589 int waittime,
590 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100591{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100592 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100593 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100594 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100595#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100596 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100597 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100598#else
599 int port = port_in;
600#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100601 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100602 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100603
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100604#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100605 channel_init_winsock();
606#endif
607
Bram Moolenaar77073442016-02-13 23:23:53 +0100608 channel = add_channel();
609 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100610 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100611 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100612 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100613 }
614
615 /* Get the server internet address and put into addr structure */
616 /* fill in the socket address structure and connect to server */
617 vim_memset((char *)&server, 0, sizeof(server));
618 server.sin_family = AF_INET;
619 server.sin_port = htons(port);
620 if ((host = gethostbyname(hostname)) == NULL)
621 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100622 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100623 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100624 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100625 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100626 }
627 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
628
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100629 /* On Mac and Solaris a zero timeout almost never works. At least wait
630 * one millisecond. Let's do it for all systems, because we don't know why
631 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100632 if (waittime == 0)
633 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100634
635 /*
636 * For Unix we need to call connect() again after connect() failed.
637 * On Win32 one time is sufficient.
638 */
639 while (TRUE)
640 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100641 long elapsed_msec = 0;
642 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100643
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100645 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100646 sd = socket(AF_INET, SOCK_STREAM, 0);
647 if (sd == -1)
648 {
649 ch_error(channel, "in socket() in channel_open().");
650 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100651 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100652 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100654
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100655 if (waittime >= 0)
656 {
657 /* Make connect() non-blocking. */
658 if (
659#ifdef _WIN32
660 ioctlsocket(sd, FIONBIO, &val) < 0
661#else
662 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
663#endif
664 )
665 {
666 SOCK_ERRNO;
667 ch_errorn(channel,
668 "channel_open: Connect failed with errno %d", errno);
669 sock_close(sd);
670 channel_free(channel);
671 return NULL;
672 }
673 }
674
675 /* Try connecting to the server. */
676 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
677 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
678
Bram Moolenaar045a2842016-03-08 22:33:07 +0100679 if (ret == 0)
680 /* The connection could be established. */
681 break;
682
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100683 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100684 if (waittime < 0 || (errno != EWOULDBLOCK
685 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100686#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100687 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100688#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100689 ))
690 {
691 ch_errorn(channel,
692 "channel_open: Connect failed with errno %d", errno);
693 PERROR(_(e_cannot_connect));
694 sock_close(sd);
695 channel_free(channel);
696 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100699 /* Limit the waittime to 50 msec. If it doesn't work within this
700 * time we close the socket and try creating it again. */
701 waitnow = waittime > 50 ? 50 : waittime;
702
Bram Moolenaar045a2842016-03-08 22:33:07 +0100703 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100704 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100705#ifndef WIN32
706 if (errno != ECONNREFUSED)
707#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 {
709 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100710 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100711 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100712#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100713 int so_error = 0;
714 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100715 struct timeval start_tv;
716 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100717#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100718 FD_ZERO(&rfds);
719 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 FD_ZERO(&wfds);
721 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100722
Bram Moolenaar562ca712016-03-09 21:50:05 +0100723 tv.tv_sec = waitnow / 1000;
724 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725#ifndef WIN32
726 gettimeofday(&start_tv, NULL);
727#endif
728 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100729 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100730 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100731
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100732 if (ret < 0)
733 {
734 SOCK_ERRNO;
735 ch_errorn(channel,
736 "channel_open: Connect failed with errno %d", errno);
737 PERROR(_(e_cannot_connect));
738 sock_close(sd);
739 channel_free(channel);
740 return NULL;
741 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100742
Bram Moolenaare081e212016-02-28 22:33:46 +0100743#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100744 /* On Win32: select() is expected to work and wait for up to
745 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100746 if (FD_ISSET(sd, &wfds))
747 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100748 elapsed_msec = waitnow;
749 if (waittime > 1 && elapsed_msec < waittime)
750 {
751 waittime -= elapsed_msec;
752 continue;
753 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100754#else
755 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 * After putting the socket in non-blocking mode, connect() will
757 * return EINPROGRESS, select() will not wait (as if writing is
758 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100759 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100760 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100761 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100762 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100763 {
764 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100766 if (ret < 0 || (so_error != 0
767 && so_error != EWOULDBLOCK
768 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100769# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100770 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100771# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100772 ))
773 {
774 ch_errorn(channel,
775 "channel_open: Connect failed with errno %d",
776 so_error);
777 PERROR(_(e_cannot_connect));
778 sock_close(sd);
779 channel_free(channel);
780 return NULL;
781 }
782 }
783
Bram Moolenaar045a2842016-03-08 22:33:07 +0100784 if (FD_ISSET(sd, &wfds) && so_error == 0)
785 /* Did not detect an error, connection is established. */
786 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787
Bram Moolenaar045a2842016-03-08 22:33:07 +0100788 gettimeofday(&end_tv, NULL);
789 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
790 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100791#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100792 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100793
794#ifndef WIN32
795 if (waittime > 1 && elapsed_msec < waittime)
796 {
797 /* The port isn't ready but we also didn't get an error.
798 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 * yet. Select() may return early, wait until the remaining
800 * "waitnow" and try again. */
801 waitnow -= elapsed_msec;
802 waittime -= elapsed_msec;
803 if (waitnow > 0)
804 {
805 mch_delay((long)waitnow, TRUE);
806 ui_breakcheck();
807 waittime -= waitnow;
808 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100809 if (!got_int)
810 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100811 if (waittime <= 0)
812 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100813 waittime = 1;
814 continue;
815 }
816 /* we were interrupted, behave as if timed out */
817 }
818#endif
819
820 /* We timed out. */
821 ch_error(channel, "Connection timed out");
822 sock_close(sd);
823 channel_free(channel);
824 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100825 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100826
Bram Moolenaar045a2842016-03-08 22:33:07 +0100827 ch_log(channel, "Connection made");
828
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100829 if (waittime >= 0)
830 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100831#ifdef _WIN32
832 val = 0;
833 ioctlsocket(sd, FIONBIO, &val);
834#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100835 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100836#endif
837 }
838
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100839 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100840 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100841
842#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100843 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100844#endif
845
Bram Moolenaar77073442016-02-13 23:23:53 +0100846 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100847}
848
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100849/*
850 * Implements ch_open().
851 */
852 channel_T *
853channel_open_func(typval_T *argvars)
854{
855 char_u *address;
856 char_u *p;
857 char *rest;
858 int port;
859 jobopt_T opt;
860 channel_T *channel;
861
862 address = get_tv_string(&argvars[0]);
863 if (argvars[1].v_type != VAR_UNKNOWN
864 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
865 {
866 EMSG(_(e_invarg));
867 return NULL;
868 }
869
870 /* parse address */
871 p = vim_strchr(address, ':');
872 if (p == NULL)
873 {
874 EMSG2(_(e_invarg2), address);
875 return NULL;
876 }
877 *p++ = NUL;
878 port = strtol((char *)p, &rest, 10);
879 if (*address == NUL || port <= 0 || *rest != NUL)
880 {
881 p[-1] = ':';
882 EMSG2(_(e_invarg2), address);
883 return NULL;
884 }
885
886 /* parse options */
887 clear_job_options(&opt);
888 opt.jo_mode = MODE_JSON;
889 opt.jo_timeout = 2000;
890 if (get_job_options(&argvars[1], &opt,
891 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
892 return NULL;
893 if (opt.jo_timeout < 0)
894 {
895 EMSG(_(e_invarg));
896 return NULL;
897 }
898
899 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
900 if (channel != NULL)
901 {
902 opt.jo_set = JO_ALL;
903 channel_set_options(channel, &opt);
904 }
905 return channel;
906}
907
Bram Moolenaarde279892016-03-11 22:19:44 +0100908 static void
909may_close_part(sock_T *fd)
910{
911 if (*fd != INVALID_FD)
912 {
913 fd_close(*fd);
914 *fd = INVALID_FD;
915 }
916}
917
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100918 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100919channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100920{
Bram Moolenaarde279892016-03-11 22:19:44 +0100921 if (in != INVALID_FD)
922 {
923 may_close_part(&channel->CH_IN_FD);
924 channel->CH_IN_FD = in;
925 }
926 if (out != INVALID_FD)
927 {
928# if defined(FEAT_GUI)
929 channel_gui_unregister_one(channel, PART_OUT);
930# endif
931 may_close_part(&channel->CH_OUT_FD);
932 channel->CH_OUT_FD = out;
933# if defined(FEAT_GUI)
934 channel_gui_register_one(channel, PART_OUT);
935# endif
936 }
937 if (err != INVALID_FD)
938 {
939# if defined(FEAT_GUI)
940 channel_gui_unregister_one(channel, PART_ERR);
941# endif
942 may_close_part(&channel->CH_ERR_FD);
943 channel->CH_ERR_FD = err;
944# if defined(FEAT_GUI)
945 channel_gui_register_one(channel, PART_ERR);
946# endif
947 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100948}
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100949
Bram Moolenaard6051b52016-02-28 15:49:03 +0100950/*
Bram Moolenaar014069a2016-03-03 22:51:40 +0100951 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +0100952 * This does not keep a refcount, when the job is freed ch_job is cleared.
953 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100954 void
Bram Moolenaar014069a2016-03-03 22:51:40 +0100955channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100956{
Bram Moolenaar77073442016-02-13 23:23:53 +0100957 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +0100958
959 channel_set_options(channel, options);
960
961 if (job->jv_in_buf != NULL)
962 {
963 chanpart_T *in_part = &channel->ch_part[PART_IN];
964
965 in_part->ch_buffer = job->jv_in_buf;
966 ch_logs(channel, "reading from buffer '%s'",
967 (char *)in_part->ch_buffer->b_ffname);
968 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100969 {
970 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
971 {
972 /* Special mode: send last-but-one line when appending a line
973 * to the buffer. */
974 in_part->ch_buffer->b_write_to_channel = TRUE;
975 in_part->ch_buf_top =
976 in_part->ch_buffer->b_ml.ml_line_count + 1;
977 }
978 else
979 in_part->ch_buf_top = options->jo_in_top;
980 }
Bram Moolenaar014069a2016-03-03 22:51:40 +0100981 else
982 in_part->ch_buf_top = 1;
983 if (options->jo_set & JO_IN_BOT)
984 in_part->ch_buf_bot = options->jo_in_bot;
985 else
986 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
987 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100988}
989
990/*
Bram Moolenaar187db502016-02-27 14:44:26 +0100991 * Find a buffer matching "name" or create a new one.
992 */
993 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100994find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +0100995{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100996 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +0100997 buf_T *save_curbuf = curbuf;
998
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100999 if (name != NULL && *name != NUL)
1000 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001001 if (buf == NULL)
1002 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001003 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001004 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001005 buf_copy_options(buf, BCO_ENTER);
1006#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001007 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1008 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001009#endif
1010 curbuf = buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001011 if (curbuf->b_ml.ml_mfp == NULL)
1012 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001013 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1014 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001015 changed_bytes(1, 0);
1016 curbuf = save_curbuf;
1017 }
1018
1019 return buf;
1020}
1021
1022/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001023 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001024 */
1025 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001026channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001027{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001028 int part;
1029 char_u **cbp;
1030 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001031
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001032 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001033 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001034 channel->ch_part[part].ch_mode = opt->jo_mode;
1035 if (opt->jo_set & JO_IN_MODE)
1036 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1037 if (opt->jo_set & JO_OUT_MODE)
1038 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1039 if (opt->jo_set & JO_ERR_MODE)
1040 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001041
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001042 if (opt->jo_set & JO_TIMEOUT)
1043 for (part = PART_SOCK; part <= PART_IN; ++part)
1044 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1045 if (opt->jo_set & JO_OUT_TIMEOUT)
1046 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1047 if (opt->jo_set & JO_ERR_TIMEOUT)
1048 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1049
1050 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001051 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001052 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001053 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001054 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001055 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001056 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1057 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001058 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001059 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001060 *pp = opt->jo_partial;
1061 if (*pp != NULL)
1062 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001063 }
1064 if (opt->jo_set & JO_OUT_CALLBACK)
1065 {
1066 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001067 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001068 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001069 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001070 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1071 *cbp = vim_strsave(opt->jo_out_cb);
1072 else
1073 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001074 *pp = opt->jo_out_partial;
1075 if (*pp != NULL)
1076 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001077 }
1078 if (opt->jo_set & JO_ERR_CALLBACK)
1079 {
1080 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001081 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001082 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001083 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001084 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1085 *cbp = vim_strsave(opt->jo_err_cb);
1086 else
1087 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001088 *pp = opt->jo_err_partial;
1089 if (*pp != NULL)
1090 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001091 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001092 if (opt->jo_set & JO_CLOSE_CALLBACK)
1093 {
1094 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001095 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001096 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001097 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001098 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1099 *cbp = vim_strsave(opt->jo_close_cb);
1100 else
1101 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001102 *pp = opt->jo_err_partial;
1103 if (*pp != NULL)
1104 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001105 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001106
1107 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1108 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001109 /* writing output to a buffer. Default mode is NL. */
1110 if (!(opt->jo_set & JO_OUT_MODE))
1111 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001112 if (opt->jo_set & JO_OUT_BUF)
1113 channel->ch_part[PART_OUT].ch_buffer =
1114 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1115 else
1116 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001117 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1118 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001119 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1120 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001121
1122 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1123 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1124 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1125 {
1126 /* writing err to a buffer. Default mode is NL. */
1127 if (!(opt->jo_set & JO_ERR_MODE))
1128 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1129 if (opt->jo_io[PART_ERR] == JIO_OUT)
1130 channel->ch_part[PART_ERR].ch_buffer =
1131 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001132 else if (opt->jo_set & JO_ERR_BUF)
1133 channel->ch_part[PART_ERR].ch_buffer =
1134 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001135 else
1136 channel->ch_part[PART_ERR].ch_buffer =
1137 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1138 ch_logs(channel, "writing err to buffer '%s'",
1139 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1140 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001141}
1142
1143/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001144 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001145 */
1146 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001147channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001148 channel_T *channel,
1149 int part,
1150 char_u *callback,
1151 partial_T *partial,
1152 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001153{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001154 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001155 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1156
1157 if (item != NULL)
1158 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001159 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 item->cq_partial = partial;
1161 if (partial != NULL)
1162 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001163 item->cq_seq_nr = id;
1164 item->cq_prev = head->cq_prev;
1165 head->cq_prev = item;
1166 item->cq_next = NULL;
1167 if (item->cq_prev == NULL)
1168 head->cq_next = item;
1169 else
1170 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001171 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001172}
1173
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001174 static void
1175write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1176{
1177 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001178 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001179 char_u *p;
1180
1181 /* TODO: check if channel can be written to, do not block on write */
1182 if ((p = alloc(len + 2)) == NULL)
1183 return;
1184 STRCPY(p, line);
1185 p[len] = NL;
1186 p[len + 1] = NUL;
1187 channel_send(channel, PART_IN, p, "write_buf_line()");
1188 vim_free(p);
1189}
1190
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001191/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001192 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001193 */
1194 void
1195channel_write_in(channel_T *channel)
1196{
1197 chanpart_T *in_part = &channel->ch_part[PART_IN];
1198 linenr_T lnum;
1199 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001200 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001201
1202 if (buf == NULL)
1203 return;
1204 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1205 {
1206 /* buffer was wiped out or unloaded */
1207 in_part->ch_buffer = NULL;
1208 return;
1209 }
1210 if (in_part->ch_fd == INVALID_FD)
1211 /* pipe was closed */
1212 return;
1213
1214 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1215 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1216 {
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001217 write_buf_line(buf, lnum, channel);
1218 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001219 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001220
1221 if (written == 1)
1222 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1223 else if (written > 1)
1224 ch_logn(channel, "written %d lines to channel", written);
1225
Bram Moolenaar014069a2016-03-03 22:51:40 +01001226 in_part->ch_buf_top = lnum;
1227}
1228
1229/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001230 * Write appended lines above the last one in "buf" to the channel.
1231 */
1232 void
1233channel_write_new_lines(buf_T *buf)
1234{
1235 channel_T *channel;
1236 int found_one = FALSE;
1237
1238 /* There could be more than one channel for the buffer, loop over all of
1239 * them. */
1240 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1241 {
1242 chanpart_T *in_part = &channel->ch_part[PART_IN];
1243 linenr_T lnum;
1244 int written = 0;
1245
1246 if (in_part->ch_buffer == buf)
1247 {
1248 if (in_part->ch_fd == INVALID_FD)
1249 /* pipe was closed */
1250 continue;
1251 found_one = TRUE;
1252 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1253 ++lnum)
1254 {
1255 write_buf_line(buf, lnum, channel);
1256 ++written;
1257 }
1258
1259 if (written == 1)
1260 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1261 else if (written > 1)
1262 ch_logn(channel, "written %d lines to channel", written);
1263
1264 in_part->ch_buf_bot = lnum;
1265 }
1266 }
1267 if (!found_one)
1268 buf->b_write_to_channel = FALSE;
1269}
1270
1271/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001272 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001273 */
1274 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001275invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1276 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001278 typval_T rettv;
1279 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001280
Bram Moolenaar77073442016-02-13 23:23:53 +01001281 argv[0].v_type = VAR_CHANNEL;
1282 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001283
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001284 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001285 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001286 clear_tv(&rettv);
1287
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001288 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001289}
1290
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001291/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001292 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001293 * The caller must free it.
1294 * Returns NULL if there is nothing.
1295 */
1296 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001297channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001298{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001300 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001301 char_u *p;
1302
Bram Moolenaar77073442016-02-13 23:23:53 +01001303 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001304 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001305 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001306 p = node->rq_buffer;
1307 head->rq_next = node->rq_next;
1308 if (node->rq_next == NULL)
1309 head->rq_prev = NULL;
1310 else
1311 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001312 vim_free(node);
1313 return p;
1314}
1315
1316/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001317 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001318 */
1319 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001320channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001321{
1322 /* Concatenate everything into one buffer.
1323 * TODO: avoid multiple allocations. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001324 while (channel_collapse(channel, part) == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001325 ;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001326 return channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001327}
1328
1329/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001330 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001331 * Returns FAIL if that is not possible.
1332 */
1333 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001334channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001335{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001336 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001337 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001338 char_u *p;
1339
Bram Moolenaar77073442016-02-13 23:23:53 +01001340 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001341 return FAIL;
1342
Bram Moolenaar77073442016-02-13 23:23:53 +01001343 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1344 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001345 if (p == NULL)
1346 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001347 STRCPY(p, node->rq_buffer);
1348 STRCAT(p, node->rq_next->rq_buffer);
1349 vim_free(node->rq_next->rq_buffer);
1350 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001351
Bram Moolenaar77073442016-02-13 23:23:53 +01001352 /* dispose of the node and its buffer */
1353 head->rq_next = node->rq_next;
1354 head->rq_next->rq_prev = NULL;
1355 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001356 vim_free(node);
1357 return OK;
1358}
1359
1360/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001361 * Store "buf[len]" on "channel"/"part".
1362 * Returns OK or FAIL.
1363 */
1364 static int
1365channel_save(channel_T *channel, int part, char_u *buf, int len)
1366{
1367 readq_T *node;
1368 readq_T *head = &channel->ch_part[part].ch_head;
1369 char_u *p;
1370 int i;
1371
1372 node = (readq_T *)alloc(sizeof(readq_T));
1373 if (node == NULL)
1374 return FAIL; /* out of memory */
1375 node->rq_buffer = alloc(len + 1);
1376 if (node->rq_buffer == NULL)
1377 {
1378 vim_free(node);
1379 return FAIL; /* out of memory */
1380 }
1381
1382 if (channel->ch_part[part].ch_mode == MODE_NL)
1383 {
1384 /* Drop any CR before a NL. */
1385 p = node->rq_buffer;
1386 for (i = 0; i < len; ++i)
1387 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1388 *p++ = buf[i];
1389 *p = NUL;
1390 }
1391 else
1392 {
1393 mch_memmove(node->rq_buffer, buf, len);
1394 node->rq_buffer[len] = NUL;
1395 }
1396
1397 /* append node to the tail of the queue */
1398 node->rq_next = NULL;
1399 node->rq_prev = head->rq_prev;
1400 if (head->rq_prev == NULL)
1401 head->rq_next = node;
1402 else
1403 head->rq_prev->rq_next = node;
1404 head->rq_prev = node;
1405
1406 if (log_fd != NULL)
1407 {
1408 ch_log_lead("RECV ", channel);
1409 fprintf(log_fd, "'");
1410 if (fwrite(buf, len, 1, log_fd) != 1)
1411 return FAIL;
1412 fprintf(log_fd, "'\n");
1413 }
1414 return OK;
1415}
1416
1417/*
1418 * Use the read buffer of "channel"/"part" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001419 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001420 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001421 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001422 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001423channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001424{
1425 js_read_T reader;
1426 typval_T listtv;
1427 jsonq_T *item;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001428 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001429 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001430
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001431 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001432 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001433
1434 /* TODO: make reader work properly */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001435 /* reader.js_buf = channel_peek(channel, part); */
1436 reader.js_buf = channel_get_all(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001437 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001438 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001439 /* reader.js_fill = channel_fill; */
Bram Moolenaar77073442016-02-13 23:23:53 +01001440 reader.js_cookie = channel;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001441 ret = json_decode(&reader, &listtv,
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001442 channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001443 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001444 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001445 /* Only accept the response when it is a list with at least two
1446 * items. */
1447 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001448 {
1449 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001450 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001451 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001452 else
1453 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001454 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1455 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001456 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001457 else
1458 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001459 item->jq_value = alloc_tv();
1460 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001461 {
1462 vim_free(item);
1463 clear_tv(&listtv);
1464 }
1465 else
1466 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001467 *item->jq_value = listtv;
1468 item->jq_prev = head->jq_prev;
1469 head->jq_prev = item;
1470 item->jq_next = NULL;
1471 if (item->jq_prev == NULL)
1472 head->jq_next = item;
1473 else
1474 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001475 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001476 }
1477 }
1478 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001479
1480 /* Put the unread part back into the channel.
1481 * TODO: insert in front */
1482 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001483 {
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001484 if (ret == FAIL)
1485 {
1486 ch_error(channel, "Decoding failed - discarding input");
1487 ret = FALSE;
1488 }
1489 else
1490 {
1491 channel_save(channel, part, reader.js_buf + reader.js_used,
1492 (int)(reader.js_end - reader.js_buf) - reader.js_used);
1493 ret = TRUE;
1494 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001495 }
1496 else
1497 ret = FALSE;
1498
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001499 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001500 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001501}
1502
1503/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001504 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001505 */
1506 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001507remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001508{
Bram Moolenaar77073442016-02-13 23:23:53 +01001509 if (node->cq_prev == NULL)
1510 head->cq_next = node->cq_next;
1511 else
1512 node->cq_prev->cq_next = node->cq_next;
1513 if (node->cq_next == NULL)
1514 head->cq_prev = node->cq_prev;
1515 else
1516 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001517}
1518
1519/*
1520 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001521 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001522 */
1523 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001524remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001525{
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 if (node->jq_prev == NULL)
1527 head->jq_next = node->jq_next;
1528 else
1529 node->jq_prev->jq_next = node->jq_next;
1530 if (node->jq_next == NULL)
1531 head->jq_prev = node->jq_prev;
1532 else
1533 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001534 vim_free(node);
1535}
1536
1537/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001538 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001540 * When "id" is zero or negative jut get the first message. But not the one
1541 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001542 * Return OK when found and return the value in "rettv".
1543 * Return FAIL otherwise.
1544 */
1545 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001546channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001547{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001548 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001549 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001550
Bram Moolenaar77073442016-02-13 23:23:53 +01001551 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001552 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001553 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001554 typval_T *tv = &l->lv_first->li_tv;
1555
1556 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001557 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001558 || tv->vval.v_number == 0
1559 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001560 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001561 *rettv = item->jq_value;
1562 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001563 return OK;
1564 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001565 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001566 }
1567 return FAIL;
1568}
1569
Bram Moolenaarece61b02016-02-20 21:39:05 +01001570#define CH_JSON_MAX_ARGS 4
1571
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001572/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001573 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001574 * "argv[0]" is the command string.
1575 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001576 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001577 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001578channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001579{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001580 char_u *cmd = argv[0].vval.v_string;
1581 char_u *arg;
1582 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001583
Bram Moolenaarece61b02016-02-20 21:39:05 +01001584 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001585 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001586 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001587 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001588 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001589 return;
1590 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001591 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001592 if (arg == NULL)
1593 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001594
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001595 if (STRCMP(cmd, "ex") == 0)
1596 {
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001597 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001598 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001599 }
1600 else if (STRCMP(cmd, "normal") == 0)
1601 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001602 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001603
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001604 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001605 ea.arg = arg;
1606 ea.addr_count = 0;
1607 ea.forceit = TRUE; /* no mapping */
1608 ex_normal(&ea);
1609 }
1610 else if (STRCMP(cmd, "redraw") == 0)
1611 {
1612 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001613
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001614 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001615 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001616 ex_redraw(&ea);
1617 showruler(FALSE);
1618 setcursor();
1619 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001620#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001621 if (gui.in_use)
1622 {
1623 gui_update_cursor(FALSE, FALSE);
1624 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001625 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001626#endif
1627 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001628 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001629 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001630 int is_call = cmd[0] == 'c';
1631 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001632
Bram Moolenaarece61b02016-02-20 21:39:05 +01001633 if (argv[id_idx].v_type != VAR_UNKNOWN
1634 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001635 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001636 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001637 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001638 EMSG("E904: last argument for expr/call must be a number");
1639 }
1640 else if (is_call && argv[2].v_type != VAR_LIST)
1641 {
1642 ch_error(channel, "third argument for call must be a list");
1643 if (p_verbose > 2)
1644 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001645 }
1646 else
1647 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001648 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001649 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001650 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001651 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001652
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001653 /* Don't pollute the display with errors. */
1654 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001655 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001656 {
1657 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001658 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001659 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001660 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001661 {
1662 ch_logs(channel, "Calling '%s'", (char *)arg);
1663 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1664 tv = &res_tv;
1665 else
1666 tv = NULL;
1667 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001668
1669 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001670 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001671 int id = argv[id_idx].vval.v_number;
1672
Bram Moolenaar55fab432016-02-07 16:53:13 +01001673 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001674 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001675 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001676 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001677 /* If evaluation failed or the result can't be encoded
1678 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001679 vim_free(json);
1680 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001681 err_tv.v_type = VAR_STRING;
1682 err_tv.vval.v_string = (char_u *)"ERROR";
1683 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001684 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001685 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001686 if (json != NULL)
1687 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001688 channel_send(channel,
1689 part == PART_SOCK ? PART_SOCK : PART_IN,
1690 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001691 vim_free(json);
1692 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001693 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001694 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001695 if (tv == &res_tv)
1696 clear_tv(tv);
1697 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001698 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001699 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001700 }
1701 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001702 {
1703 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001704 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001705 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001706}
1707
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001708 static void
1709invoke_one_time_callback(
1710 channel_T *channel,
1711 cbq_T *cbhead,
1712 cbq_T *item,
1713 typval_T *argv)
1714{
1715 ch_logs(channel, "Invoking one-time callback %s",
1716 (char *)item->cq_callback);
1717 /* Remove the item from the list first, if the callback
1718 * invokes ch_close() the list will be cleared. */
1719 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001720 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001721 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001722 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001723 vim_free(item);
1724}
1725
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001726 static void
1727append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
1728{
1729 buf_T *save_curbuf = curbuf;
1730 linenr_T lnum = buffer->b_ml.ml_line_count;
1731 int save_write_to = buffer->b_write_to_channel;
1732
1733 /* If the buffer is also used as input insert above the last
1734 * line. Don't write these lines. */
1735 if (save_write_to)
1736 {
1737 --lnum;
1738 buffer->b_write_to_channel = FALSE;
1739 }
1740
1741 /* Append to the buffer */
1742 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
1743
1744 curbuf = buffer;
1745 u_sync(TRUE);
1746 /* ignore undo failure, undo is not very useful here */
1747 ignored = u_save(lnum, lnum + 1);
1748
1749 ml_append(lnum, msg, 0, FALSE);
1750 appended_lines_mark(lnum, 1L);
1751 curbuf = save_curbuf;
1752
1753 if (buffer->b_nwindows > 0)
1754 {
1755 win_T *wp;
1756 win_T *save_curwin;
1757
1758 FOR_ALL_WINDOWS(wp)
1759 {
1760 if (wp->w_buffer == buffer
1761 && (save_write_to
1762 ? wp->w_cursor.lnum == lnum + 1
1763 : (wp->w_cursor.lnum == lnum
1764 && wp->w_cursor.col == 0)))
1765 {
1766 ++wp->w_cursor.lnum;
1767 save_curwin = curwin;
1768 curwin = wp;
1769 curbuf = curwin->w_buffer;
1770 scroll_cursor_bot(0, FALSE);
1771 curwin = save_curwin;
1772 curbuf = curwin->w_buffer;
1773 }
1774 }
1775 redraw_buf_later(buffer, VALID);
1776 channel_need_redraw = TRUE;
1777 }
1778
1779 if (save_write_to)
1780 {
1781 channel_T *ch;
1782
1783 /* Find channels reading from this buffer and adjust their
1784 * next-to-read line number. */
1785 buffer->b_write_to_channel = TRUE;
1786 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
1787 {
1788 chanpart_T *in_part = &ch->ch_part[PART_IN];
1789
1790 if (in_part->ch_buffer == buffer)
1791 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
1792 }
1793 }
1794}
1795
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001796/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001797 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001798 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001799 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001800 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001801may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001802{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001803 char_u *msg = NULL;
1804 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001805 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001806 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001807 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001808 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001809 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001810 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001811 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001812 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001813
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001814 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001815 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001816 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001817
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001818 /* Use a message-specific callback, part callback or channel callback */
1819 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
1820 if (cbitem->cq_seq_nr == 0)
1821 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001822 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001823 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001824 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001825 partial = cbitem->cq_partial;
1826 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001827 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001828 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001829 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001830 partial = channel->ch_part[part].ch_partial;
1831 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001832 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001833 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001834 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001835 partial = channel->ch_partial;
1836 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001837
Bram Moolenaar187db502016-02-27 14:44:26 +01001838 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001839 if (buffer != NULL && !buf_valid(buffer))
1840 {
1841 /* buffer was wiped out */
1842 channel->ch_part[part].ch_buffer = NULL;
1843 buffer = NULL;
1844 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001845
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001846 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001847 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001848 listitem_T *item;
1849 int argc = 0;
1850
Bram Moolenaard7ece102016-02-02 23:23:02 +01001851 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001852 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001853 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001854 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001855 channel_parse_json(channel, part);
1856 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001857 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001858 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001859
Bram Moolenaarece61b02016-02-20 21:39:05 +01001860 for (item = listtv->vval.v_list->lv_first;
1861 item != NULL && argc < CH_JSON_MAX_ARGS;
1862 item = item->li_next)
1863 argv[argc++] = item->li_tv;
1864 while (argc < CH_JSON_MAX_ARGS)
1865 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001866
Bram Moolenaarece61b02016-02-20 21:39:05 +01001867 if (argv[0].v_type == VAR_STRING)
1868 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001869 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01001870 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01001871 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001872 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001873 }
1874
Bram Moolenaarece61b02016-02-20 21:39:05 +01001875 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001877 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001878 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01001879 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001880 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001881 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001882 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001883 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001884 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001885 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001886 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001887 return FALSE;
1888 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001889 else
1890 {
Bram Moolenaar187db502016-02-27 14:44:26 +01001891 /* If there is no callback or buffer drop the message. */
1892 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001893 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001894 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01001895 {
1896 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001897 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01001898 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001899 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001900 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001901
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001902 if (ch_mode == MODE_NL)
1903 {
1904 char_u *nl;
1905 char_u *buf;
1906
1907 /* See if we have a message ending in NL in the first buffer. If
1908 * not try to concatenate the first and the second buffer. */
1909 while (TRUE)
1910 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001911 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001912 nl = vim_strchr(buf, NL);
1913 if (nl != NULL)
1914 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001916 return FALSE; /* incomplete message */
1917 }
1918 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01001919 {
1920 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001921 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01001922 *nl = NUL;
1923 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001924 else
1925 {
1926 /* Copy the message into allocated memory and remove it from
1927 * the buffer. */
1928 msg = vim_strnsave(buf, (int)(nl - buf));
1929 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
1930 }
1931 }
1932 else
1933 /* For a raw channel we don't know where the message ends, just
1934 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001935 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001936
Bram Moolenaarbf73b912016-03-02 21:16:59 +01001937 if (msg == NULL)
1938 return FALSE; /* out of memory (and avoids Coverity warning) */
1939
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001940 argv[1].v_type = VAR_STRING;
1941 argv[1].vval.v_string = msg;
1942 }
1943
Bram Moolenaara07fec92016-02-05 21:04:08 +01001944 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001945 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001946 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001947
1948 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001949 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001950 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001951 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001952 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001953 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001954 break;
1955 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001956 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01001957 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001958 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001959 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001960 {
Bram Moolenaar187db502016-02-27 14:44:26 +01001961 if (buffer != NULL)
1962 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001963 if (msg == NULL)
1964 /* JSON or JS mode: re-encode the message. */
1965 msg = json_encode(listtv, ch_mode);
1966 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001967 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01001968 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001969
Bram Moolenaar187db502016-02-27 14:44:26 +01001970 if (callback != NULL)
1971 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001972 if (cbitem != NULL)
1973 invoke_one_time_callback(channel, cbhead, cbitem, argv);
1974 else
1975 {
1976 /* invoke the channel callback */
1977 ch_logs(channel, "Invoking channel callback %s",
1978 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001979 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001980 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001981 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001982 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001983 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001984 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001985
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001986 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01001987 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001988 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001989
1990 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001991}
1992
1993/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001994 * Return TRUE when channel "channel" is open for writing to.
1995 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01001996 */
1997 int
Bram Moolenaar77073442016-02-13 23:23:53 +01001998channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001999{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002000 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002001 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002002}
2003
2004/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002005 * Return TRUE when channel "channel" is open for reading or writing.
2006 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002007 */
2008 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002009channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002010{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002011 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002012 || channel->CH_IN_FD != INVALID_FD
2013 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002014 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002015}
2016
2017/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002018 * Return a string indicating the status of the channel.
2019 */
2020 char *
2021channel_status(channel_T *channel)
2022{
2023 if (channel == NULL)
2024 return "fail";
2025 if (channel_is_open(channel))
2026 return "open";
2027 return "closed";
2028}
2029
2030/*
2031 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002032 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002033 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002034 */
2035 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002036channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002037{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002038 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002039
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002040#ifdef FEAT_GUI
2041 channel_gui_unregister(channel);
2042#endif
2043
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002044 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002045 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002046 sock_close(channel->CH_SOCK_FD);
2047 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002048 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002049 may_close_part(&channel->CH_IN_FD);
2050 may_close_part(&channel->CH_OUT_FD);
2051 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002052
Bram Moolenaar8b374212016-02-24 20:43:06 +01002053 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002054 {
2055 typval_T argv[1];
2056 typval_T rettv;
2057 int dummy;
2058
2059 /* invoke the close callback; increment the refcount to avoid it
2060 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002061 ch_logs(channel, "Invoking close callback %s",
2062 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002063 argv[0].v_type = VAR_CHANNEL;
2064 argv[0].vval.v_channel = channel;
2065 ++channel->ch_refcount;
2066 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002067 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2068 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002069 clear_tv(&rettv);
2070 --channel->ch_refcount;
2071
2072 /* the callback is only called once */
2073 vim_free(channel->ch_close_cb);
2074 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002075 partial_unref(channel->ch_close_partial);
2076 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002077 }
2078
2079 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002080}
2081
Bram Moolenaard04a0202016-01-26 23:30:18 +01002082/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002083 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002084 * Returns NULL if there is nothing.
2085 */
2086 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002087channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002088{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002089 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002090
Bram Moolenaar77073442016-02-13 23:23:53 +01002091 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002092 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002093 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002094}
2095
2096/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002097 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002098 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002099 static void
2100channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002101{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002102 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2103 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002104
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002105 while (channel_peek(channel, part) != NULL)
2106 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002107
2108 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002109 {
2110 cbq_T *node = cb_head->cq_next;
2111
2112 remove_cb_node(cb_head, node);
2113 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002114 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002115 vim_free(node);
2116 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002117
2118 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002119 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002120 free_tv(json_head->jq_next->jq_value);
2121 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002122 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002123
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002124 vim_free(channel->ch_part[part].ch_callback);
2125 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002126 partial_unref(channel->ch_part[part].ch_partial);
2127 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002128}
2129
2130/*
2131 * Clear all the read buffers on "channel".
2132 */
2133 void
2134channel_clear(channel_T *channel)
2135{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002136 ch_log(channel, "Clearing channel");
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002137 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002138 channel_clear_one(channel, PART_OUT);
2139 channel_clear_one(channel, PART_ERR);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002140 vim_free(channel->ch_callback);
2141 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002142 partial_unref(channel->ch_partial);
2143 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002144 vim_free(channel->ch_close_cb);
2145 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002146 partial_unref(channel->ch_close_partial);
2147 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002148}
2149
Bram Moolenaar77073442016-02-13 23:23:53 +01002150#if defined(EXITFREE) || defined(PROTO)
2151 void
2152channel_free_all(void)
2153{
2154 channel_T *channel;
2155
Bram Moolenaard6051b52016-02-28 15:49:03 +01002156 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002157 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2158 channel_clear(channel);
2159}
2160#endif
2161
2162
Bram Moolenaard04a0202016-01-26 23:30:18 +01002163/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002164#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002165
2166/* Buffer size for reading incoming messages. */
2167#define MAXMSGSIZE 4096
2168
2169/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002170 * Check for reading from "fd" with "timeout" msec.
2171 * Return FAIL when there is nothing to read.
2172 */
2173 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002174channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002175{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002176 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002177 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002178
Bram Moolenaard8070362016-02-15 21:56:54 +01002179# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002180 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002181 {
2182 DWORD nread;
2183 int diff;
2184 DWORD deadline = GetTickCount() + timeout;
2185
2186 /* reading from a pipe, not a socket */
2187 while (TRUE)
2188 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002189 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2190 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002191 return OK;
2192 diff = deadline - GetTickCount();
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002193 if (diff <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002194 break;
2195 /* Wait for 5 msec.
2196 * TODO: increase the sleep time when looping more often */
2197 Sleep(5);
2198 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002199 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002200 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002201#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002202 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002203#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002204 struct timeval tval;
2205 fd_set rfds;
2206 int ret;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002207
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002208 FD_ZERO(&rfds);
2209 FD_SET((int)fd, &rfds);
2210 tval.tv_sec = timeout / 1000;
2211 tval.tv_usec = (timeout % 1000) * 1000;
2212 for (;;)
2213 {
2214 ret = select((int)fd + 1, &rfds, NULL, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002215# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002216 SOCK_ERRNO;
2217 if (ret == -1 && errno == EINTR)
2218 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002219# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002220 if (ret > 0)
2221 return OK;
2222 break;
2223 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002224#else
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002225 struct pollfd fds;
2226
2227 fds.fd = fd;
2228 fds.events = POLLIN;
2229 if (poll(&fds, 1, timeout) > 0)
2230 return OK;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002231#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002232 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002233 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002234}
2235
2236/*
2237 * Return a unique ID to be used in a message.
2238 */
2239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002241{
2242 static int next_id = 1;
2243
2244 return next_id++;
2245}
2246
2247/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002248 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002249 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002250 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002251 */
2252 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002253channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002254{
2255 static char_u *buf = NULL;
2256 int len = 0;
2257 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002258 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002259 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002260
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002261 fd = channel->ch_part[part].ch_fd;
2262 if (fd == INVALID_FD)
2263 {
2264 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002265 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002266 }
2267 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002268
2269 /* Allocate a buffer to read into. */
2270 if (buf == NULL)
2271 {
2272 buf = alloc(MAXMSGSIZE);
2273 if (buf == NULL)
2274 return; /* out of memory! */
2275 }
2276
2277 /* Keep on reading for as long as there is something to read.
2278 * Use select() or poll() to avoid blocking on a message that is exactly
2279 * MAXMSGSIZE long. */
2280 for (;;)
2281 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002282 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002283 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002284 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002285 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002286 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002287 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002288 if (len <= 0)
2289 break; /* error or nothing more to read */
2290
2291 /* Store the read message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002292 channel_save(channel, part, buf, len);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002293 readlen += len;
2294 if (len < MAXMSGSIZE)
2295 break; /* did read everything that's available */
2296 }
2297
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002298 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002299 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002300 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002301 /* Do not give an error message, most likely the other end just
2302 * exited. */
2303 ch_errors(channel, "%s(): Cannot read from channel", func);
2304
Bram Moolenaard04a0202016-01-26 23:30:18 +01002305 /* Queue a "DETACH" netbeans message in the command queue in order to
2306 * terminate the netbeans session later. Do not end the session here
2307 * directly as we may be running in the context of a call to
2308 * netbeans_parse_messages():
2309 * netbeans_parse_messages
2310 * -> autocmd triggered while processing the netbeans cmd
2311 * -> ui_breakcheck
2312 * -> gui event loop or select loop
2313 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002314 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002315 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002316 if (channel->ch_part[part].ch_mode == MODE_RAW
2317 || channel->ch_part[part].ch_mode == MODE_NL)
2318 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
2319 (int)STRLEN(DETACH_MSG_RAW));
Bram Moolenaard04a0202016-01-26 23:30:18 +01002320
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002321 /* TODO: When reading from stdout is not possible, should we try to
2322 * keep stdin and stderr open? Probably not, assume the other side
2323 * has died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002324 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002325 if (channel->ch_nb_close_cb != NULL)
2326 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002327 }
2328
2329#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002330 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002331 if (CH_HAS_GUI && gtk_main_level() > 0)
2332 gtk_main_quit();
2333#endif
2334}
2335
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002336/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002337 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002338 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002339 * Returns what was read in allocated memory.
2340 * Returns NULL in case of error or timeout.
2341 */
2342 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002343channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002344{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002345 char_u *buf;
2346 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002347 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002348 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002349 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002350
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002351 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002352 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002353
2354 while (TRUE)
2355 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002356 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002357 if (buf != NULL && (mode == MODE_RAW
2358 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2359 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002360 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002361 continue;
2362
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002363 /* Wait for up to the channel timeout. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002364 if (fd == INVALID_FD
2365 || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002366 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002367 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002368 }
2369
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002370 if (mode == MODE_RAW)
2371 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002372 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002373 }
2374 else
2375 {
2376 nl = vim_strchr(buf, NL);
2377 if (nl[1] == NUL)
2378 {
2379 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002380 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002381 *nl = NUL;
2382 }
2383 else
2384 {
2385 /* Copy the message into allocated memory and remove it from the
2386 * buffer. */
2387 msg = vim_strnsave(buf, (int)(nl - buf));
2388 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2389 }
2390 }
2391 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002392 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002393 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002394}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002395
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002396/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002397 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002398 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002399 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002400 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002401 */
2402 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002403channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002404 channel_T *channel,
2405 int part,
2406 int timeout,
2407 int id,
2408 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002409{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002410 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002411 sock_T fd;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002412
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002413 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002414 if (id != -1)
2415 channel->ch_part[part].ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002416 for (;;)
2417 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002418 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002419
2420 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002421 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002422 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002423 channel->ch_part[part].ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002424 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002425 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002426
Bram Moolenaard7ece102016-02-02 23:23:02 +01002427 if (!more)
2428 {
2429 /* Handle any other messages in the queue. If done some more
2430 * messages may have arrived. */
2431 if (channel_parse_messages())
2432 continue;
2433
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002434 /* Wait for up to the timeout. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002435 fd = channel->ch_part[part].ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002436 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002437 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002438 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002439 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002440 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002441 channel->ch_part[part].ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002442 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002443}
2444
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002445/*
2446 * Common for ch_read() and ch_readraw().
2447 */
2448 void
2449common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2450{
2451 channel_T *channel;
2452 int part;
2453 jobopt_T opt;
2454 int mode;
2455 int timeout;
2456 int id = -1;
2457 typval_T *listtv = NULL;
2458
2459 /* return an empty string by default */
2460 rettv->v_type = VAR_STRING;
2461 rettv->vval.v_string = NULL;
2462
2463 clear_job_options(&opt);
2464 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2465 == FAIL)
2466 return;
2467
2468 channel = get_channel_arg(&argvars[0], TRUE);
2469 if (channel != NULL)
2470 {
2471 if (opt.jo_set & JO_PART)
2472 part = opt.jo_part;
2473 else
2474 part = channel_part_read(channel);
2475 mode = channel_get_mode(channel, part);
2476 timeout = channel_get_timeout(channel, part);
2477 if (opt.jo_set & JO_TIMEOUT)
2478 timeout = opt.jo_timeout;
2479
2480 if (raw || mode == MODE_RAW || mode == MODE_NL)
2481 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2482 else
2483 {
2484 if (opt.jo_set & JO_ID)
2485 id = opt.jo_id;
2486 channel_read_json_block(channel, part, timeout, id, &listtv);
2487 if (listtv != NULL)
2488 {
2489 *rettv = *listtv;
2490 vim_free(listtv);
2491 }
2492 else
2493 {
2494 rettv->v_type = VAR_SPECIAL;
2495 rettv->vval.v_number = VVAL_NONE;
2496 }
2497 }
2498 }
2499}
2500
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002501# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2502 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002503/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002504 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002505 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002506 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002507 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002508channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002509{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002510 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002511 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002512
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002513 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01002514 for (channel = first_channel; channel != NULL;
2515 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002516 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002517 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002518 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002519 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002520 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002521 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002522 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002523 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002524 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002525}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002526# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002527
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002528# if defined(WIN32) || defined(PROTO)
2529/*
2530 * Check the channels for anything that is ready to be read.
2531 * The data is put in the read queue.
2532 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002533 void
2534channel_handle_events(void)
2535{
2536 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002537 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002538 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002539
2540 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2541 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002542 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002543 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002544 {
2545 fd = channel->ch_part[part].ch_fd;
2546 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
2547 channel_read(channel, part, "channel_handle_events");
2548 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002549 }
2550}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002551# endif
2552
Bram Moolenaard04a0202016-01-26 23:30:18 +01002553/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002554 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002555 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002556 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002557 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002558 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002559channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002560{
Bram Moolenaard04a0202016-01-26 23:30:18 +01002561 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002562 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002563 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002564
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002565 fd = channel->ch_part[part].ch_fd;
2566 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002567 {
2568 if (!channel->ch_error && fun != NULL)
2569 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002570 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002571 EMSG2("E630: %s(): write while not connected", fun);
2572 }
2573 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002574 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002575 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002576
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002577 if (log_fd != NULL)
2578 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002579 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002580 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002581 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002582 fprintf(log_fd, "'\n");
2583 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01002584 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002585 }
2586
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002587 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002588 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002589 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002590 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002591 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002592 {
2593 if (!channel->ch_error && fun != NULL)
2594 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002595 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002596 EMSG2("E631: %s(): write failed", fun);
2597 }
2598 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002599 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002600 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002601
2602 channel->ch_error = FALSE;
2603 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002604}
2605
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002606/*
2607 * Common for "ch_sendexpr()" and "ch_sendraw()".
2608 * Returns the channel if the caller should read the response.
2609 * Sets "part_read" to the the read fd.
2610 * Otherwise returns NULL.
2611 */
2612 channel_T *
2613send_common(
2614 typval_T *argvars,
2615 char_u *text,
2616 int id,
2617 int eval,
2618 jobopt_T *opt,
2619 char *fun,
2620 int *part_read)
2621{
2622 channel_T *channel;
2623 int part_send;
2624
2625 channel = get_channel_arg(&argvars[0], TRUE);
2626 if (channel == NULL)
2627 return NULL;
2628 part_send = channel_part_send(channel);
2629 *part_read = channel_part_read(channel);
2630
2631 clear_job_options(opt);
2632 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
2633 return NULL;
2634
2635 /* Set the callback. An empty callback means no callback and not reading
2636 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
2637 * allowed. */
2638 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
2639 {
2640 if (eval)
2641 {
2642 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
2643 return NULL;
2644 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002645 channel_set_req_callback(channel, part_send,
2646 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002647 }
2648
2649 if (channel_send(channel, part_send, text, fun) == OK
2650 && opt->jo_callback == NULL)
2651 return channel;
2652 return NULL;
2653}
2654
2655/*
2656 * common for "ch_evalexpr()" and "ch_sendexpr()"
2657 */
2658 void
2659ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
2660{
2661 char_u *text;
2662 typval_T *listtv;
2663 channel_T *channel;
2664 int id;
2665 ch_mode_T ch_mode;
2666 int part_send;
2667 int part_read;
2668 jobopt_T opt;
2669 int timeout;
2670
2671 /* return an empty string by default */
2672 rettv->v_type = VAR_STRING;
2673 rettv->vval.v_string = NULL;
2674
2675 channel = get_channel_arg(&argvars[0], TRUE);
2676 if (channel == NULL)
2677 return;
2678 part_send = channel_part_send(channel);
2679
2680 ch_mode = channel_get_mode(channel, part_send);
2681 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
2682 {
2683 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
2684 return;
2685 }
2686
2687 id = channel_get_id();
2688 text = json_encode_nr_expr(id, &argvars[1],
2689 ch_mode == MODE_JS ? JSON_JS : 0);
2690 if (text == NULL)
2691 return;
2692
2693 channel = send_common(argvars, text, id, eval, &opt,
2694 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
2695 vim_free(text);
2696 if (channel != NULL && eval)
2697 {
2698 if (opt.jo_set & JO_TIMEOUT)
2699 timeout = opt.jo_timeout;
2700 else
2701 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002702 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
2703 == OK)
2704 {
2705 list_T *list = listtv->vval.v_list;
2706
2707 /* Move the item from the list and then change the type to
2708 * avoid the value being freed. */
2709 *rettv = list->lv_last->li_tv;
2710 list->lv_last->li_tv.v_type = VAR_NUMBER;
2711 free_tv(listtv);
2712 }
2713 }
2714}
2715
2716/*
2717 * common for "ch_evalraw()" and "ch_sendraw()"
2718 */
2719 void
2720ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
2721{
2722 char_u buf[NUMBUFLEN];
2723 char_u *text;
2724 channel_T *channel;
2725 int part_read;
2726 jobopt_T opt;
2727 int timeout;
2728
2729 /* return an empty string by default */
2730 rettv->v_type = VAR_STRING;
2731 rettv->vval.v_string = NULL;
2732
2733 text = get_tv_string_buf(&argvars[1], buf);
2734 channel = send_common(argvars, text, 0, eval, &opt,
2735 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
2736 if (channel != NULL && eval)
2737 {
2738 if (opt.jo_set & JO_TIMEOUT)
2739 timeout = opt.jo_timeout;
2740 else
2741 timeout = channel_get_timeout(channel, part_read);
2742 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
2743 }
2744}
2745
Bram Moolenaard04a0202016-01-26 23:30:18 +01002746# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002747/*
2748 * Add open channels to the poll struct.
2749 * Return the adjusted struct index.
2750 * The type of "fds" is hidden to avoid problems with the function proto.
2751 */
2752 int
2753channel_poll_setup(int nfd_in, void *fds_in)
2754{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002755 int nfd = nfd_in;
2756 channel_T *channel;
2757 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002758 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002759
Bram Moolenaar77073442016-02-13 23:23:53 +01002760 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002761 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002762 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002763 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002764 if (channel->ch_part[part].ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002765 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002766 channel->ch_part[part].ch_poll_idx = nfd;
2767 fds[nfd].fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002768 fds[nfd].events = POLLIN;
2769 nfd++;
2770 }
2771 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002772 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002773 }
2774 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002775
2776 return nfd;
2777}
2778
2779/*
2780 * The type of "fds" is hidden to avoid problems with the function proto.
2781 */
2782 int
2783channel_poll_check(int ret_in, void *fds_in)
2784{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002785 int ret = ret_in;
2786 channel_T *channel;
2787 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002788 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002789
Bram Moolenaar77073442016-02-13 23:23:53 +01002790 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002791 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002792 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002793 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002794 int idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002795
2796 if (ret > 0 && idx != -1 && fds[idx].revents & POLLIN)
2797 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002798 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002799 --ret;
2800 }
2801 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002802 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002803
2804 return ret;
2805}
Bram Moolenaard04a0202016-01-26 23:30:18 +01002806# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002807
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002808# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002809/*
2810 * The type of "rfds" is hidden to avoid problems with the function proto.
2811 */
2812 int
2813channel_select_setup(int maxfd_in, void *rfds_in)
2814{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002815 int maxfd = maxfd_in;
2816 channel_T *channel;
2817 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002818 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002819
Bram Moolenaar77073442016-02-13 23:23:53 +01002820 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002821 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002822 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002823 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002824 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002825
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002826 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002827 {
Bram Moolenaard8070362016-02-15 21:56:54 +01002828 FD_SET((int)fd, rfds);
2829 if (maxfd < (int)fd)
2830 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002831 }
2832 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002833 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002834
2835 return maxfd;
2836}
2837
2838/*
2839 * The type of "rfds" is hidden to avoid problems with the function proto.
2840 */
2841 int
2842channel_select_check(int ret_in, void *rfds_in)
2843{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002844 int ret = ret_in;
2845 channel_T *channel;
2846 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002847 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002848
Bram Moolenaar77073442016-02-13 23:23:53 +01002849 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002850 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002851 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002852 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002853 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002854
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002855 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002856 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002857 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002858 --ret;
2859 }
2860 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002861 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002862
2863 return ret;
2864}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002865# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002866
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002867/*
Bram Moolenaar187db502016-02-27 14:44:26 +01002868 * Return TRUE if "channel" has JSON or other typeahead.
2869 */
2870 static int
2871channel_has_readahead(channel_T *channel, int part)
2872{
2873 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2874
2875 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2876 {
2877 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2878 jsonq_T *item = head->jq_next;
2879
2880 return item != NULL;
2881 }
2882 return channel_peek(channel, part) != NULL;
2883}
2884
2885/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01002886 * Execute queued up commands.
2887 * Invoked from the main loop when it's safe to execute received commands.
2888 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002889 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002890 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002891channel_parse_messages(void)
2892{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002893 channel_T *channel = first_channel;
2894 int ret = FALSE;
2895 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002896 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002897
Bram Moolenaard0b65022016-03-06 21:50:33 +01002898 /* Only do this message when another message was given, otherwise we get
2899 * lots of them. */
2900 if (did_log_msg)
2901 {
2902 ch_log(NULL, "looking for messages on channels");
2903 did_log_msg = FALSE;
2904 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002905 while (channel != NULL)
2906 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01002907 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002908 {
2909 /* channel is no longer useful, free it */
2910 channel_free(channel);
2911 channel = first_channel;
2912 part = PART_SOCK;
2913 continue;
2914 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002915 if (channel->ch_part[part].ch_fd != INVALID_FD
2916 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01002917 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002918 /* Increase the refcount, in case the handler causes the channel
2919 * to be unreferenced or closed. */
2920 ++channel->ch_refcount;
2921 r = may_invoke_callback(channel, part);
2922 if (r == OK)
2923 ret = TRUE;
2924 if (channel_unref(channel) || r == OK)
2925 {
2926 /* channel was freed or something was done, start over */
2927 channel = first_channel;
2928 part = PART_SOCK;
2929 continue;
2930 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002931 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002932 if (part < PART_ERR)
2933 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002934 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002935 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002936 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002937 part = PART_SOCK;
2938 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002939 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002940
2941 if (channel_need_redraw && must_redraw)
2942 {
2943 channel_need_redraw = FALSE;
2944 update_screen(0);
2945 setcursor();
2946 cursor_on();
2947 out_flush();
2948 }
2949
Bram Moolenaard7ece102016-02-02 23:23:02 +01002950 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002951}
2952
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002953/*
2954 * Mark references to lists used in channels.
2955 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002956 int
2957set_ref_in_channel(int copyID)
2958{
Bram Moolenaar77073442016-02-13 23:23:53 +01002959 int abort = FALSE;
2960 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002961 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002962
Bram Moolenaar77073442016-02-13 23:23:53 +01002963 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002964 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002965 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002966 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002967 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2968 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002969
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002970 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002971 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002972 list_T *l = item->jq_value->vval.v_list;
2973
2974 if (l->lv_copyID != copyID)
2975 {
2976 l->lv_copyID = copyID;
2977 abort = abort || set_ref_in_list(l, copyID, NULL);
2978 }
2979 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002980 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002981 }
2982 }
2983 return abort;
2984}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01002985
2986/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002987 * Return the "part" to write to for "channel".
2988 */
2989 int
2990channel_part_send(channel_T *channel)
2991{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002992 if (channel->CH_SOCK_FD == INVALID_FD)
2993 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002994 return PART_SOCK;
2995}
2996
2997/*
2998 * Return the default "part" to read from for "channel".
2999 */
3000 int
3001channel_part_read(channel_T *channel)
3002{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003003 if (channel->CH_SOCK_FD == INVALID_FD)
3004 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003005 return PART_SOCK;
3006}
3007
3008/*
3009 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003010 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003011 */
3012 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003013channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003014{
Bram Moolenaar77073442016-02-13 23:23:53 +01003015 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003016 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003017 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003018}
3019
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003020/*
3021 * Return the timeout of "channel"/"part"
3022 */
3023 int
3024channel_get_timeout(channel_T *channel, int part)
3025{
3026 return channel->ch_part[part].ch_timeout;
3027}
3028
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003029 static int
3030handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3031{
3032 char_u *val = get_tv_string(item);
3033
3034 opt->jo_set |= jo;
3035 if (STRCMP(val, "nl") == 0)
3036 *modep = MODE_NL;
3037 else if (STRCMP(val, "raw") == 0)
3038 *modep = MODE_RAW;
3039 else if (STRCMP(val, "js") == 0)
3040 *modep = MODE_JS;
3041 else if (STRCMP(val, "json") == 0)
3042 *modep = MODE_JSON;
3043 else
3044 {
3045 EMSG2(_(e_invarg2), val);
3046 return FAIL;
3047 }
3048 return OK;
3049}
3050
3051 static int
3052handle_io(typval_T *item, int part, jobopt_T *opt)
3053{
3054 char_u *val = get_tv_string(item);
3055
3056 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3057 if (STRCMP(val, "null") == 0)
3058 opt->jo_io[part] = JIO_NULL;
3059 else if (STRCMP(val, "pipe") == 0)
3060 opt->jo_io[part] = JIO_PIPE;
3061 else if (STRCMP(val, "file") == 0)
3062 opt->jo_io[part] = JIO_FILE;
3063 else if (STRCMP(val, "buffer") == 0)
3064 opt->jo_io[part] = JIO_BUFFER;
3065 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3066 opt->jo_io[part] = JIO_OUT;
3067 else
3068 {
3069 EMSG2(_(e_invarg2), val);
3070 return FAIL;
3071 }
3072 return OK;
3073}
3074
3075 void
3076clear_job_options(jobopt_T *opt)
3077{
3078 vim_memset(opt, 0, sizeof(jobopt_T));
3079}
3080
3081/*
3082 * Get the PART_ number from the first character of an option name.
3083 */
3084 static int
3085part_from_char(int c)
3086{
3087 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3088}
3089
3090/*
3091 * Get the option entries from the dict in "tv", parse them and put the result
3092 * in "opt".
3093 * Only accept options in "supported".
3094 * If an option value is invalid return FAIL.
3095 */
3096 int
3097get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3098{
3099 typval_T *item;
3100 char_u *val;
3101 dict_T *dict;
3102 int todo;
3103 hashitem_T *hi;
3104 int part;
3105
3106 opt->jo_set = 0;
3107 if (tv->v_type == VAR_UNKNOWN)
3108 return OK;
3109 if (tv->v_type != VAR_DICT)
3110 {
3111 EMSG(_(e_invarg));
3112 return FAIL;
3113 }
3114 dict = tv->vval.v_dict;
3115 if (dict == NULL)
3116 return OK;
3117
3118 todo = (int)dict->dv_hashtab.ht_used;
3119 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3120 if (!HASHITEM_EMPTY(hi))
3121 {
3122 item = &dict_lookup(hi)->di_tv;
3123
3124 if (STRCMP(hi->hi_key, "mode") == 0)
3125 {
3126 if (!(supported & JO_MODE))
3127 break;
3128 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3129 return FAIL;
3130 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003131 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003132 {
3133 if (!(supported & JO_IN_MODE))
3134 break;
3135 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3136 == FAIL)
3137 return FAIL;
3138 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003139 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003140 {
3141 if (!(supported & JO_OUT_MODE))
3142 break;
3143 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3144 == FAIL)
3145 return FAIL;
3146 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003147 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003148 {
3149 if (!(supported & JO_ERR_MODE))
3150 break;
3151 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3152 == FAIL)
3153 return FAIL;
3154 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003155 else if (STRCMP(hi->hi_key, "in_io") == 0
3156 || STRCMP(hi->hi_key, "out_io") == 0
3157 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003158 {
3159 if (!(supported & JO_OUT_IO))
3160 break;
3161 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3162 return FAIL;
3163 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003164 else if (STRCMP(hi->hi_key, "in_name") == 0
3165 || STRCMP(hi->hi_key, "out_name") == 0
3166 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003167 {
3168 part = part_from_char(*hi->hi_key);
3169
3170 if (!(supported & JO_OUT_IO))
3171 break;
3172 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3173 opt->jo_io_name[part] =
3174 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3175 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003176 else if (STRCMP(hi->hi_key, "in_buf") == 0
3177 || STRCMP(hi->hi_key, "out_buf") == 0
3178 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003179 {
3180 part = part_from_char(*hi->hi_key);
3181
3182 if (!(supported & JO_OUT_IO))
3183 break;
3184 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3185 opt->jo_io_buf[part] = get_tv_number(item);
3186 if (opt->jo_io_buf[part] <= 0)
3187 {
3188 EMSG2(_(e_invarg2), get_tv_string(item));
3189 return FAIL;
3190 }
3191 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3192 {
3193 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3194 return FAIL;
3195 }
3196 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003197 else if (STRCMP(hi->hi_key, "in_top") == 0
3198 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003199 {
3200 linenr_T *lp;
3201
3202 if (!(supported & JO_OUT_IO))
3203 break;
3204 if (hi->hi_key[3] == 't')
3205 {
3206 lp = &opt->jo_in_top;
3207 opt->jo_set |= JO_IN_TOP;
3208 }
3209 else
3210 {
3211 lp = &opt->jo_in_bot;
3212 opt->jo_set |= JO_IN_BOT;
3213 }
3214 *lp = get_tv_number(item);
3215 if (*lp < 0)
3216 {
3217 EMSG2(_(e_invarg2), get_tv_string(item));
3218 return FAIL;
3219 }
3220 }
3221 else if (STRCMP(hi->hi_key, "channel") == 0)
3222 {
3223 if (!(supported & JO_OUT_IO))
3224 break;
3225 opt->jo_set |= JO_CHANNEL;
3226 if (item->v_type != VAR_CHANNEL)
3227 {
3228 EMSG2(_(e_invarg2), "channel");
3229 return FAIL;
3230 }
3231 opt->jo_channel = item->vval.v_channel;
3232 }
3233 else if (STRCMP(hi->hi_key, "callback") == 0)
3234 {
3235 if (!(supported & JO_CALLBACK))
3236 break;
3237 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003238 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003239 if (opt->jo_callback == NULL)
3240 {
3241 EMSG2(_(e_invarg2), "callback");
3242 return FAIL;
3243 }
3244 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003245 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003246 {
3247 if (!(supported & JO_OUT_CALLBACK))
3248 break;
3249 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003250 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003251 if (opt->jo_out_cb == NULL)
3252 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003253 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003254 return FAIL;
3255 }
3256 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003257 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003258 {
3259 if (!(supported & JO_ERR_CALLBACK))
3260 break;
3261 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003262 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003263 if (opt->jo_err_cb == NULL)
3264 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003265 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003266 return FAIL;
3267 }
3268 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003269 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003270 {
3271 if (!(supported & JO_CLOSE_CALLBACK))
3272 break;
3273 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003274 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003275 if (opt->jo_close_cb == NULL)
3276 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003277 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003278 return FAIL;
3279 }
3280 }
3281 else if (STRCMP(hi->hi_key, "waittime") == 0)
3282 {
3283 if (!(supported & JO_WAITTIME))
3284 break;
3285 opt->jo_set |= JO_WAITTIME;
3286 opt->jo_waittime = get_tv_number(item);
3287 }
3288 else if (STRCMP(hi->hi_key, "timeout") == 0)
3289 {
3290 if (!(supported & JO_TIMEOUT))
3291 break;
3292 opt->jo_set |= JO_TIMEOUT;
3293 opt->jo_timeout = get_tv_number(item);
3294 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003295 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003296 {
3297 if (!(supported & JO_OUT_TIMEOUT))
3298 break;
3299 opt->jo_set |= JO_OUT_TIMEOUT;
3300 opt->jo_out_timeout = get_tv_number(item);
3301 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003302 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003303 {
3304 if (!(supported & JO_ERR_TIMEOUT))
3305 break;
3306 opt->jo_set |= JO_ERR_TIMEOUT;
3307 opt->jo_err_timeout = get_tv_number(item);
3308 }
3309 else if (STRCMP(hi->hi_key, "part") == 0)
3310 {
3311 if (!(supported & JO_PART))
3312 break;
3313 opt->jo_set |= JO_PART;
3314 val = get_tv_string(item);
3315 if (STRCMP(val, "err") == 0)
3316 opt->jo_part = PART_ERR;
3317 else
3318 {
3319 EMSG2(_(e_invarg2), val);
3320 return FAIL;
3321 }
3322 }
3323 else if (STRCMP(hi->hi_key, "id") == 0)
3324 {
3325 if (!(supported & JO_ID))
3326 break;
3327 opt->jo_set |= JO_ID;
3328 opt->jo_id = get_tv_number(item);
3329 }
3330 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3331 {
3332 if (!(supported & JO_STOPONEXIT))
3333 break;
3334 opt->jo_set |= JO_STOPONEXIT;
3335 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3336 opt->jo_soe_buf);
3337 if (opt->jo_stoponexit == NULL)
3338 {
3339 EMSG2(_(e_invarg2), "stoponexit");
3340 return FAIL;
3341 }
3342 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003343 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003344 {
3345 if (!(supported & JO_EXIT_CB))
3346 break;
3347 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003348 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3349 {
3350 opt->jo_exit_partial = item->vval.v_partial;
3351 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3352 }
3353 else
3354 opt->jo_exit_cb = get_tv_string_buf_chk(
3355 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003356 if (opt->jo_exit_cb == NULL)
3357 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003358 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003359 return FAIL;
3360 }
3361 }
3362 else
3363 break;
3364 --todo;
3365 }
3366 if (todo > 0)
3367 {
3368 EMSG2(_(e_invarg2), hi->hi_key);
3369 return FAIL;
3370 }
3371
3372 return OK;
3373}
3374
3375/*
3376 * Get the channel from the argument.
3377 * Returns NULL if the handle is invalid.
3378 */
3379 channel_T *
3380get_channel_arg(typval_T *tv, int check_open)
3381{
3382 channel_T *channel = NULL;
3383
3384 if (tv->v_type == VAR_JOB)
3385 {
3386 if (tv->vval.v_job != NULL)
3387 channel = tv->vval.v_job->jv_channel;
3388 }
3389 else if (tv->v_type == VAR_CHANNEL)
3390 {
3391 channel = tv->vval.v_channel;
3392 }
3393 else
3394 {
3395 EMSG2(_(e_invarg2), get_tv_string(tv));
3396 return NULL;
3397 }
3398
3399 if (check_open && (channel == NULL || !channel_is_open(channel)))
3400 {
3401 EMSG(_("E906: not an open channel"));
3402 return NULL;
3403 }
3404 return channel;
3405}
3406
3407static job_T *first_job = NULL;
3408
3409 static void
3410job_free(job_T *job)
3411{
3412 ch_log(job->jv_channel, "Freeing job");
3413 if (job->jv_channel != NULL)
3414 {
3415 /* The link from the channel to the job doesn't count as a reference,
3416 * thus don't decrement the refcount of the job. The reference from
3417 * the job to the channel does count the refrence, decrement it and
3418 * NULL the reference. We don't set ch_job_killed, unreferencing the
3419 * job doesn't mean it stops running. */
3420 job->jv_channel->ch_job = NULL;
3421 channel_unref(job->jv_channel);
3422 }
3423 mch_clear_job(job);
3424
3425 if (job->jv_next != NULL)
3426 job->jv_next->jv_prev = job->jv_prev;
3427 if (job->jv_prev == NULL)
3428 first_job = job->jv_next;
3429 else
3430 job->jv_prev->jv_next = job->jv_next;
3431
3432 vim_free(job->jv_stoponexit);
3433 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003434 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003435 vim_free(job);
3436}
3437
3438 void
3439job_unref(job_T *job)
3440{
3441 if (job != NULL && --job->jv_refcount <= 0)
3442 {
3443 /* Do not free the job when it has not ended yet and there is a
3444 * "stoponexit" flag or an exit callback. */
3445 if (job->jv_status != JOB_STARTED
3446 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
3447 {
3448 job_free(job);
3449 }
3450 else if (job->jv_channel != NULL)
3451 {
3452 /* Do remove the link to the channel, otherwise it hangs
3453 * around until Vim exits. See job_free() for refcount. */
3454 job->jv_channel->ch_job = NULL;
3455 channel_unref(job->jv_channel);
3456 job->jv_channel = NULL;
3457 }
3458 }
3459}
3460
3461/*
3462 * Allocate a job. Sets the refcount to one and sets options default.
3463 */
3464 static job_T *
3465job_alloc(void)
3466{
3467 job_T *job;
3468
3469 job = (job_T *)alloc_clear(sizeof(job_T));
3470 if (job != NULL)
3471 {
3472 job->jv_refcount = 1;
3473 job->jv_stoponexit = vim_strsave((char_u *)"term");
3474
3475 if (first_job != NULL)
3476 {
3477 first_job->jv_prev = job;
3478 job->jv_next = first_job;
3479 }
3480 first_job = job;
3481 }
3482 return job;
3483}
3484
3485 void
3486job_set_options(job_T *job, jobopt_T *opt)
3487{
3488 if (opt->jo_set & JO_STOPONEXIT)
3489 {
3490 vim_free(job->jv_stoponexit);
3491 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
3492 job->jv_stoponexit = NULL;
3493 else
3494 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
3495 }
3496 if (opt->jo_set & JO_EXIT_CB)
3497 {
3498 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003499 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003500 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003501 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003502 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003503 job->jv_exit_partial = NULL;
3504 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003505 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003506 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003507 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003508 job->jv_exit_partial = opt->jo_exit_partial;
3509 if (job->jv_exit_partial != NULL)
3510 ++job->jv_exit_partial->pt_refcount;
3511 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003512 }
3513}
3514
3515/*
3516 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
3517 */
3518 void
3519job_stop_on_exit()
3520{
3521 job_T *job;
3522
3523 for (job = first_job; job != NULL; job = job->jv_next)
3524 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
3525 mch_stop_job(job, job->jv_stoponexit);
3526}
3527
3528/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003529 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003530 */
3531 void
3532job_check_ended(void)
3533{
3534 static time_t last_check = 0;
3535 time_t now;
3536 job_T *job;
3537 job_T *next;
3538
3539 /* Only do this once in 10 seconds. */
3540 now = time(NULL);
3541 if (last_check + 10 < now)
3542 {
3543 last_check = now;
3544 for (job = first_job; job != NULL; job = next)
3545 {
3546 next = job->jv_next;
3547 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
3548 job_status(job); /* may free "job" */
3549 }
3550 }
3551}
3552
3553/*
3554 * "job_start()" function
3555 */
3556 job_T *
3557job_start(typval_T *argvars)
3558{
3559 job_T *job;
3560 char_u *cmd = NULL;
3561#if defined(UNIX)
3562# define USE_ARGV
3563 char **argv = NULL;
3564 int argc = 0;
3565#else
3566 garray_T ga;
3567#endif
3568 jobopt_T opt;
3569 int part;
3570
3571 job = job_alloc();
3572 if (job == NULL)
3573 return NULL;
3574
3575 job->jv_status = JOB_FAILED;
3576
3577 /* Default mode is NL. */
3578 clear_job_options(&opt);
3579 opt.jo_mode = MODE_NL;
3580 if (get_job_options(&argvars[1], &opt,
3581 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
3582 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
3583 return job;
3584
3585 /* Check that when io is "file" that there is a file name. */
3586 for (part = PART_OUT; part <= PART_IN; ++part)
3587 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
3588 && opt.jo_io[part] == JIO_FILE
3589 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
3590 || *opt.jo_io_name[part] == NUL))
3591 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003592 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003593 return job;
3594 }
3595
3596 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
3597 {
3598 buf_T *buf = NULL;
3599
3600 /* check that we can find the buffer before starting the job */
3601 if (opt.jo_set & JO_IN_BUF)
3602 {
3603 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
3604 if (buf == NULL)
3605 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
3606 }
3607 else if (!(opt.jo_set & JO_IN_NAME))
3608 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003609 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003610 }
3611 else
3612 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
3613 if (buf == NULL)
3614 return job;
3615 if (buf->b_ml.ml_mfp == NULL)
3616 {
3617 char_u numbuf[NUMBUFLEN];
3618 char_u *s;
3619
3620 if (opt.jo_set & JO_IN_BUF)
3621 {
3622 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
3623 s = numbuf;
3624 }
3625 else
3626 s = opt.jo_io_name[PART_IN];
3627 EMSG2(_("E918: buffer must be loaded: %s"), s);
3628 return job;
3629 }
3630 job->jv_in_buf = buf;
3631 }
3632
3633 job_set_options(job, &opt);
3634
3635#ifndef USE_ARGV
3636 ga_init2(&ga, (int)sizeof(char*), 20);
3637#endif
3638
3639 if (argvars[0].v_type == VAR_STRING)
3640 {
3641 /* Command is a string. */
3642 cmd = argvars[0].vval.v_string;
3643#ifdef USE_ARGV
3644 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
3645 return job;
3646 argv[argc] = NULL;
3647#endif
3648 }
3649 else if (argvars[0].v_type != VAR_LIST
3650 || argvars[0].vval.v_list == NULL
3651 || argvars[0].vval.v_list->lv_len < 1)
3652 {
3653 EMSG(_(e_invarg));
3654 return job;
3655 }
3656 else
3657 {
3658 list_T *l = argvars[0].vval.v_list;
3659 listitem_T *li;
3660 char_u *s;
3661
3662#ifdef USE_ARGV
3663 /* Pass argv[] to mch_call_shell(). */
3664 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
3665 if (argv == NULL)
3666 return job;
3667#endif
3668 for (li = l->lv_first; li != NULL; li = li->li_next)
3669 {
3670 s = get_tv_string_chk(&li->li_tv);
3671 if (s == NULL)
3672 goto theend;
3673#ifdef USE_ARGV
3674 argv[argc++] = (char *)s;
3675#else
3676 /* Only escape when needed, double quotes are not always allowed. */
3677 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
3678 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01003679# ifdef WIN32
3680 int old_ssl = p_ssl;
3681
3682 /* This is using CreateProcess, not cmd.exe. Always use
3683 * double quote and backslashes. */
3684 p_ssl = 0;
3685# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003686 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01003687# ifdef WIN32
3688 p_ssl = old_ssl;
3689# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003690 if (s == NULL)
3691 goto theend;
3692 ga_concat(&ga, s);
3693 vim_free(s);
3694 }
3695 else
3696 ga_concat(&ga, s);
3697 if (li->li_next != NULL)
3698 ga_append(&ga, ' ');
3699#endif
3700 }
3701#ifdef USE_ARGV
3702 argv[argc] = NULL;
3703#else
3704 cmd = ga.ga_data;
3705#endif
3706 }
3707
3708#ifdef USE_ARGV
3709 if (ch_log_active())
3710 {
3711 garray_T ga;
3712 int i;
3713
3714 ga_init2(&ga, (int)sizeof(char), 200);
3715 for (i = 0; i < argc; ++i)
3716 {
3717 if (i > 0)
3718 ga_concat(&ga, (char_u *)" ");
3719 ga_concat(&ga, (char_u *)argv[i]);
3720 }
3721 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
3722 ga_clear(&ga);
3723 }
3724 mch_start_job(argv, job, &opt);
3725#else
3726 ch_logs(NULL, "Starting job: %s", (char *)cmd);
3727 mch_start_job((char *)cmd, job, &opt);
3728#endif
3729
3730 /* If the channel is reading from a buffer, write lines now. */
3731 if (job->jv_channel != NULL)
3732 channel_write_in(job->jv_channel);
3733
3734theend:
3735#ifdef USE_ARGV
3736 vim_free(argv);
3737#else
3738 vim_free(ga.ga_data);
3739#endif
3740 return job;
3741}
3742
3743/*
3744 * Get the status of "job" and invoke the exit callback when needed.
3745 * The returned string is not allocated.
3746 */
3747 char *
3748job_status(job_T *job)
3749{
3750 char *result;
3751
3752 if (job->jv_status == JOB_ENDED)
3753 /* No need to check, dead is dead. */
3754 result = "dead";
3755 else if (job->jv_status == JOB_FAILED)
3756 result = "fail";
3757 else
3758 {
3759 result = mch_job_status(job);
3760 if (job->jv_status == JOB_ENDED)
3761 ch_log(job->jv_channel, "Job ended");
3762 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
3763 {
3764 typval_T argv[3];
3765 typval_T rettv;
3766 int dummy;
3767
3768 /* invoke the exit callback; make sure the refcount is > 0 */
3769 ++job->jv_refcount;
3770 argv[0].v_type = VAR_JOB;
3771 argv[0].vval.v_job = job;
3772 argv[1].v_type = VAR_NUMBER;
3773 argv[1].vval.v_number = job->jv_exitval;
3774 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003775 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
3776 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003777 clear_tv(&rettv);
3778 --job->jv_refcount;
3779 }
3780 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
3781 {
3782 /* The job was already unreferenced, now that it ended it can be
3783 * freed. Careful: caller must not use "job" after this! */
3784 job_free(job);
3785 }
3786 }
3787 return result;
3788}
3789
Bram Moolenaar8950a562016-03-12 15:22:55 +01003790/*
3791 * Implementation of job_info().
3792 */
3793 void
3794job_info(job_T *job, dict_T *dict)
3795{
3796 dictitem_T *item;
3797 varnumber_T nr;
3798
3799 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
3800
3801 item = dictitem_alloc((char_u *)"channel");
3802 if (item == NULL)
3803 return;
3804 item->di_tv.v_lock = 0;
3805 item->di_tv.v_type = VAR_CHANNEL;
3806 item->di_tv.vval.v_channel = job->jv_channel;
3807 if (job->jv_channel != NULL)
3808 ++job->jv_channel->ch_refcount;
3809 if (dict_add(dict, item) == FAIL)
3810 dictitem_free(item);
3811
3812#ifdef UNIX
3813 nr = job->jv_pid;
3814#else
3815 nr = job->jv_proc_info.dwProcessId;
3816#endif
3817 dict_add_nr_str(dict, "process", nr, NULL);
3818
3819 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003820 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01003821 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
3822}
3823
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003824 int
3825job_stop(job_T *job, typval_T *argvars)
3826{
3827 char_u *arg;
3828
3829 if (argvars[1].v_type == VAR_UNKNOWN)
3830 arg = (char_u *)"";
3831 else
3832 {
3833 arg = get_tv_string_chk(&argvars[1]);
3834 if (arg == NULL)
3835 {
3836 EMSG(_(e_invarg));
3837 return 0;
3838 }
3839 }
3840 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
3841 if (mch_stop_job(job, arg) == FAIL)
3842 return 0;
3843
3844 /* Assume that "hup" does not kill the job. */
3845 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
3846 job->jv_channel->ch_job_killed = TRUE;
3847
3848 /* We don't try freeing the job, obviously the caller still has a
3849 * reference to it. */
3850 return 1;
3851}
3852
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003853#endif /* FEAT_JOB_CHANNEL */