blob: ccab2cb447e61c9d6afd6f27ffe41a475da58975 [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 Moolenaar03602ec2016-03-20 20:57:45 +0100841 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
842 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100843
844#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100845 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100846#endif
847
Bram Moolenaar77073442016-02-13 23:23:53 +0100848 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100849}
850
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100851/*
852 * Implements ch_open().
853 */
854 channel_T *
855channel_open_func(typval_T *argvars)
856{
857 char_u *address;
858 char_u *p;
859 char *rest;
860 int port;
861 jobopt_T opt;
862 channel_T *channel;
863
864 address = get_tv_string(&argvars[0]);
865 if (argvars[1].v_type != VAR_UNKNOWN
866 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
867 {
868 EMSG(_(e_invarg));
869 return NULL;
870 }
871
872 /* parse address */
873 p = vim_strchr(address, ':');
874 if (p == NULL)
875 {
876 EMSG2(_(e_invarg2), address);
877 return NULL;
878 }
879 *p++ = NUL;
880 port = strtol((char *)p, &rest, 10);
881 if (*address == NUL || port <= 0 || *rest != NUL)
882 {
883 p[-1] = ':';
884 EMSG2(_(e_invarg2), address);
885 return NULL;
886 }
887
888 /* parse options */
889 clear_job_options(&opt);
890 opt.jo_mode = MODE_JSON;
891 opt.jo_timeout = 2000;
892 if (get_job_options(&argvars[1], &opt,
893 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
894 return NULL;
895 if (opt.jo_timeout < 0)
896 {
897 EMSG(_(e_invarg));
898 return NULL;
899 }
900
901 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
902 if (channel != NULL)
903 {
904 opt.jo_set = JO_ALL;
905 channel_set_options(channel, &opt);
906 }
907 return channel;
908}
909
Bram Moolenaarde279892016-03-11 22:19:44 +0100910 static void
911may_close_part(sock_T *fd)
912{
913 if (*fd != INVALID_FD)
914 {
915 fd_close(*fd);
916 *fd = INVALID_FD;
917 }
918}
919
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100920 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100921channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100922{
Bram Moolenaarde279892016-03-11 22:19:44 +0100923 if (in != INVALID_FD)
924 {
925 may_close_part(&channel->CH_IN_FD);
926 channel->CH_IN_FD = in;
927 }
928 if (out != INVALID_FD)
929 {
930# if defined(FEAT_GUI)
931 channel_gui_unregister_one(channel, PART_OUT);
932# endif
933 may_close_part(&channel->CH_OUT_FD);
934 channel->CH_OUT_FD = out;
935# if defined(FEAT_GUI)
936 channel_gui_register_one(channel, PART_OUT);
937# endif
938 }
939 if (err != INVALID_FD)
940 {
941# if defined(FEAT_GUI)
942 channel_gui_unregister_one(channel, PART_ERR);
943# endif
944 may_close_part(&channel->CH_ERR_FD);
945 channel->CH_ERR_FD = err;
946# if defined(FEAT_GUI)
947 channel_gui_register_one(channel, PART_ERR);
948# endif
949 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100950}
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100951
Bram Moolenaard6051b52016-02-28 15:49:03 +0100952/*
Bram Moolenaar014069a2016-03-03 22:51:40 +0100953 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +0100954 * This does not keep a refcount, when the job is freed ch_job is cleared.
955 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100956 void
Bram Moolenaar014069a2016-03-03 22:51:40 +0100957channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100958{
Bram Moolenaar77073442016-02-13 23:23:53 +0100959 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +0100960
961 channel_set_options(channel, options);
962
963 if (job->jv_in_buf != NULL)
964 {
965 chanpart_T *in_part = &channel->ch_part[PART_IN];
966
967 in_part->ch_buffer = job->jv_in_buf;
968 ch_logs(channel, "reading from buffer '%s'",
969 (char *)in_part->ch_buffer->b_ffname);
970 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100971 {
972 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
973 {
974 /* Special mode: send last-but-one line when appending a line
975 * to the buffer. */
976 in_part->ch_buffer->b_write_to_channel = TRUE;
977 in_part->ch_buf_top =
978 in_part->ch_buffer->b_ml.ml_line_count + 1;
979 }
980 else
981 in_part->ch_buf_top = options->jo_in_top;
982 }
Bram Moolenaar014069a2016-03-03 22:51:40 +0100983 else
984 in_part->ch_buf_top = 1;
985 if (options->jo_set & JO_IN_BOT)
986 in_part->ch_buf_bot = options->jo_in_bot;
987 else
988 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
989 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100990}
991
992/*
Bram Moolenaar187db502016-02-27 14:44:26 +0100993 * Find a buffer matching "name" or create a new one.
994 */
995 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100996find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +0100997{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100998 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +0100999 buf_T *save_curbuf = curbuf;
1000
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001001 if (name != NULL && *name != NUL)
1002 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001003 if (buf == NULL)
1004 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001005 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001006 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001007 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001008 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001009#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001010 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1011 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001012#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001013 if (curbuf->b_ml.ml_mfp == NULL)
1014 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001015 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1016 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001017 changed_bytes(1, 0);
1018 curbuf = save_curbuf;
1019 }
1020
1021 return buf;
1022}
1023
1024/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001025 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001026 */
1027 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001028channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001029{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001030 int part;
1031 char_u **cbp;
1032 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001033
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001034 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001035 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001036 channel->ch_part[part].ch_mode = opt->jo_mode;
1037 if (opt->jo_set & JO_IN_MODE)
1038 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1039 if (opt->jo_set & JO_OUT_MODE)
1040 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1041 if (opt->jo_set & JO_ERR_MODE)
1042 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001043
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001044 if (opt->jo_set & JO_TIMEOUT)
1045 for (part = PART_SOCK; part <= PART_IN; ++part)
1046 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1047 if (opt->jo_set & JO_OUT_TIMEOUT)
1048 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1049 if (opt->jo_set & JO_ERR_TIMEOUT)
1050 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1051
1052 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001053 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001054 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001055 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001056 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001057 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001058 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1059 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001060 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001061 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001062 *pp = opt->jo_partial;
1063 if (*pp != NULL)
1064 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001065 }
1066 if (opt->jo_set & JO_OUT_CALLBACK)
1067 {
1068 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001069 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001070 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001071 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001072 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1073 *cbp = vim_strsave(opt->jo_out_cb);
1074 else
1075 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001076 *pp = opt->jo_out_partial;
1077 if (*pp != NULL)
1078 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001079 }
1080 if (opt->jo_set & JO_ERR_CALLBACK)
1081 {
1082 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001083 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001084 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001085 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001086 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1087 *cbp = vim_strsave(opt->jo_err_cb);
1088 else
1089 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001090 *pp = opt->jo_err_partial;
1091 if (*pp != NULL)
1092 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001093 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001094 if (opt->jo_set & JO_CLOSE_CALLBACK)
1095 {
1096 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001097 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001098 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001099 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001100 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1101 *cbp = vim_strsave(opt->jo_close_cb);
1102 else
1103 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001104 *pp = opt->jo_err_partial;
1105 if (*pp != NULL)
1106 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001107 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001108
1109 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1110 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001111 /* writing output to a buffer. Default mode is NL. */
1112 if (!(opt->jo_set & JO_OUT_MODE))
1113 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001114 if (opt->jo_set & JO_OUT_BUF)
1115 channel->ch_part[PART_OUT].ch_buffer =
1116 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1117 else
1118 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001119 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1120 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001121 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1122 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001123
1124 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1125 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1126 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1127 {
1128 /* writing err to a buffer. Default mode is NL. */
1129 if (!(opt->jo_set & JO_ERR_MODE))
1130 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1131 if (opt->jo_io[PART_ERR] == JIO_OUT)
1132 channel->ch_part[PART_ERR].ch_buffer =
1133 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001134 else if (opt->jo_set & JO_ERR_BUF)
1135 channel->ch_part[PART_ERR].ch_buffer =
1136 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001137 else
1138 channel->ch_part[PART_ERR].ch_buffer =
1139 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1140 ch_logs(channel, "writing err to buffer '%s'",
1141 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1142 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001143
1144 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1145 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1146 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001147}
1148
1149/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001150 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001151 */
1152 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001153channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 channel_T *channel,
1155 int part,
1156 char_u *callback,
1157 partial_T *partial,
1158 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001159{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001160 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001161 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1162
1163 if (item != NULL)
1164 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001165 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001166 item->cq_partial = partial;
1167 if (partial != NULL)
1168 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001169 item->cq_seq_nr = id;
1170 item->cq_prev = head->cq_prev;
1171 head->cq_prev = item;
1172 item->cq_next = NULL;
1173 if (item->cq_prev == NULL)
1174 head->cq_next = item;
1175 else
1176 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001177 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001178}
1179
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001180 static void
1181write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1182{
1183 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001184 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001185 char_u *p;
1186
1187 /* TODO: check if channel can be written to, do not block on write */
1188 if ((p = alloc(len + 2)) == NULL)
1189 return;
1190 STRCPY(p, line);
1191 p[len] = NL;
1192 p[len + 1] = NUL;
1193 channel_send(channel, PART_IN, p, "write_buf_line()");
1194 vim_free(p);
1195}
1196
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001197/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001198 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001199 */
1200 void
1201channel_write_in(channel_T *channel)
1202{
1203 chanpart_T *in_part = &channel->ch_part[PART_IN];
1204 linenr_T lnum;
1205 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001206 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001207
1208 if (buf == NULL)
1209 return;
1210 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1211 {
1212 /* buffer was wiped out or unloaded */
1213 in_part->ch_buffer = NULL;
1214 return;
1215 }
1216 if (in_part->ch_fd == INVALID_FD)
1217 /* pipe was closed */
1218 return;
1219
1220 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1221 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1222 {
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001223 write_buf_line(buf, lnum, channel);
1224 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001225 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001226
1227 if (written == 1)
1228 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1229 else if (written > 1)
1230 ch_logn(channel, "written %d lines to channel", written);
1231
Bram Moolenaar014069a2016-03-03 22:51:40 +01001232 in_part->ch_buf_top = lnum;
1233}
1234
1235/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001236 * Write appended lines above the last one in "buf" to the channel.
1237 */
1238 void
1239channel_write_new_lines(buf_T *buf)
1240{
1241 channel_T *channel;
1242 int found_one = FALSE;
1243
1244 /* There could be more than one channel for the buffer, loop over all of
1245 * them. */
1246 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1247 {
1248 chanpart_T *in_part = &channel->ch_part[PART_IN];
1249 linenr_T lnum;
1250 int written = 0;
1251
1252 if (in_part->ch_buffer == buf)
1253 {
1254 if (in_part->ch_fd == INVALID_FD)
1255 /* pipe was closed */
1256 continue;
1257 found_one = TRUE;
1258 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1259 ++lnum)
1260 {
1261 write_buf_line(buf, lnum, channel);
1262 ++written;
1263 }
1264
1265 if (written == 1)
1266 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1267 else if (written > 1)
1268 ch_logn(channel, "written %d lines to channel", written);
1269
1270 in_part->ch_buf_bot = lnum;
1271 }
1272 }
1273 if (!found_one)
1274 buf->b_write_to_channel = FALSE;
1275}
1276
1277/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001278 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001279 */
1280 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001281invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1282 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001283{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001284 typval_T rettv;
1285 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001286
Bram Moolenaar77073442016-02-13 23:23:53 +01001287 argv[0].v_type = VAR_CHANNEL;
1288 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001289
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001290 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001291 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001292 clear_tv(&rettv);
1293
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001294 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001295}
1296
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001297/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001298 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001299 * The caller must free it.
1300 * Returns NULL if there is nothing.
1301 */
1302 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001303channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001304{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001305 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001306 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001307 char_u *p;
1308
Bram Moolenaar77073442016-02-13 23:23:53 +01001309 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001310 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001311 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001312 p = node->rq_buffer;
1313 head->rq_next = node->rq_next;
1314 if (node->rq_next == NULL)
1315 head->rq_prev = NULL;
1316 else
1317 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001318 vim_free(node);
1319 return p;
1320}
1321
1322/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001323 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001324 */
1325 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001326channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001327{
1328 /* Concatenate everything into one buffer.
1329 * TODO: avoid multiple allocations. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001330 while (channel_collapse(channel, part) == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001331 ;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001332 return channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001333}
1334
1335/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001336 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001337 * Returns FAIL if that is not possible.
1338 */
1339 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001340channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001341{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001342 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001343 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001344 char_u *p;
1345
Bram Moolenaar77073442016-02-13 23:23:53 +01001346 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001347 return FAIL;
1348
Bram Moolenaar77073442016-02-13 23:23:53 +01001349 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1350 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001351 if (p == NULL)
1352 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001353 STRCPY(p, node->rq_buffer);
1354 STRCAT(p, node->rq_next->rq_buffer);
1355 vim_free(node->rq_next->rq_buffer);
1356 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001357
Bram Moolenaar77073442016-02-13 23:23:53 +01001358 /* dispose of the node and its buffer */
1359 head->rq_next = node->rq_next;
1360 head->rq_next->rq_prev = NULL;
1361 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001362 vim_free(node);
1363 return OK;
1364}
1365
1366/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001367 * Store "buf[len]" on "channel"/"part".
1368 * Returns OK or FAIL.
1369 */
1370 static int
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001371channel_save(channel_T *channel, int part, char_u *buf, int len, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001372{
1373 readq_T *node;
1374 readq_T *head = &channel->ch_part[part].ch_head;
1375 char_u *p;
1376 int i;
1377
1378 node = (readq_T *)alloc(sizeof(readq_T));
1379 if (node == NULL)
1380 return FAIL; /* out of memory */
1381 node->rq_buffer = alloc(len + 1);
1382 if (node->rq_buffer == NULL)
1383 {
1384 vim_free(node);
1385 return FAIL; /* out of memory */
1386 }
1387
1388 if (channel->ch_part[part].ch_mode == MODE_NL)
1389 {
1390 /* Drop any CR before a NL. */
1391 p = node->rq_buffer;
1392 for (i = 0; i < len; ++i)
1393 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1394 *p++ = buf[i];
1395 *p = NUL;
1396 }
1397 else
1398 {
1399 mch_memmove(node->rq_buffer, buf, len);
1400 node->rq_buffer[len] = NUL;
1401 }
1402
1403 /* append node to the tail of the queue */
1404 node->rq_next = NULL;
1405 node->rq_prev = head->rq_prev;
1406 if (head->rq_prev == NULL)
1407 head->rq_next = node;
1408 else
1409 head->rq_prev->rq_next = node;
1410 head->rq_prev = node;
1411
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001412 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001413 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001414 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001415 fprintf(log_fd, "'");
1416 if (fwrite(buf, len, 1, log_fd) != 1)
1417 return FAIL;
1418 fprintf(log_fd, "'\n");
1419 }
1420 return OK;
1421}
1422
1423/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001424 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001425 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001426 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001427 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001428 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001429channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001430{
1431 js_read_T reader;
1432 typval_T listtv;
1433 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001434 chanpart_T *chanpart = &channel->ch_part[part];
1435 jsonq_T *head = &chanpart->ch_json_head;
1436 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001437 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001438
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001439 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001440 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001441
1442 /* TODO: make reader work properly */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001443 /* reader.js_buf = channel_peek(channel, part); */
1444 reader.js_buf = channel_get_all(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001445 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001446 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001447 /* reader.js_fill = channel_fill; */
Bram Moolenaar77073442016-02-13 23:23:53 +01001448 reader.js_cookie = channel;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001449
1450 /* When a message is incomplete we wait for a short while for more to
1451 * arrive. After the delay drop the input, otherwise a truncated string
1452 * or list will make us hang. */
1453 status = json_decode(&reader, &listtv,
1454 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
1455 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001456 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001457 /* Only accept the response when it is a list with at least two
1458 * items. */
1459 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001460 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001461 if (listtv.v_type != VAR_LIST)
1462 ch_error(channel, "Did not receive a list, discarding");
1463 else
1464 ch_errorn(channel, "Expected list with two items, got %d",
1465 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001467 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001468 else
1469 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001470 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1471 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001472 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001473 else
1474 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001475 item->jq_value = alloc_tv();
1476 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001477 {
1478 vim_free(item);
1479 clear_tv(&listtv);
1480 }
1481 else
1482 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001483 *item->jq_value = listtv;
1484 item->jq_prev = head->jq_prev;
1485 head->jq_prev = item;
1486 item->jq_next = NULL;
1487 if (item->jq_prev == NULL)
1488 head->jq_next = item;
1489 else
1490 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001491 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001492 }
1493 }
1494 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001495
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001496 if (status == OK)
1497 chanpart->ch_waiting = FALSE;
1498 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001499 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001500 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001501 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001502 /* First time encountering incomplete message, set a deadline of
1503 * 100 msec. */
1504 ch_log(channel, "Incomplete message - wait for more");
1505 reader.js_used = 0;
1506 chanpart->ch_waiting = TRUE;
1507#ifdef WIN32
1508 chanpart->ch_deadline = GetTickCount() + 100L;
1509#else
1510 gettimeofday(&chanpart->ch_deadline, NULL);
1511 chanpart->ch_deadline.tv_usec += 100 * 1000;
1512 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1513 {
1514 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1515 ++chanpart->ch_deadline.tv_sec;
1516 }
1517#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001518 }
1519 else
1520 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001521 int timeout;
1522#ifdef WIN32
1523 timeout = GetTickCount() > chanpart->ch_deadline;
1524#else
1525 {
1526 struct timeval now_tv;
1527
1528 gettimeofday(&now_tv, NULL);
1529 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1530 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1531 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1532 }
1533#endif
1534 if (timeout)
1535 {
1536 status = FAIL;
1537 chanpart->ch_waiting = FALSE;
1538 }
1539 else
1540 {
1541 reader.js_used = 0;
1542 ch_log(channel, "still waiting on incomplete message");
1543 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001544 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001545 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001546
1547 if (status == FAIL)
1548 {
1549 ch_error(channel, "Decoding failed - discarding input");
1550 ret = FALSE;
1551 chanpart->ch_waiting = FALSE;
1552 }
1553 else if (reader.js_buf[reader.js_used] != NUL)
1554 {
1555 /* Put the unread part back into the channel.
1556 * TODO: insert in front */
1557 channel_save(channel, part, reader.js_buf + reader.js_used,
1558 (int)(reader.js_end - reader.js_buf) - reader.js_used, NULL);
1559 ret = status == MAYBE ? FALSE: TRUE;
1560 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001561 else
1562 ret = FALSE;
1563
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001564 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001565 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001566}
1567
1568/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001569 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001570 */
1571 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001572remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001573{
Bram Moolenaar77073442016-02-13 23:23:53 +01001574 if (node->cq_prev == NULL)
1575 head->cq_next = node->cq_next;
1576 else
1577 node->cq_prev->cq_next = node->cq_next;
1578 if (node->cq_next == NULL)
1579 head->cq_prev = node->cq_prev;
1580 else
1581 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001582}
1583
1584/*
1585 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001586 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001587 */
1588 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001589remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001590{
Bram Moolenaar77073442016-02-13 23:23:53 +01001591 if (node->jq_prev == NULL)
1592 head->jq_next = node->jq_next;
1593 else
1594 node->jq_prev->jq_next = node->jq_next;
1595 if (node->jq_next == NULL)
1596 head->jq_prev = node->jq_prev;
1597 else
1598 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599 vim_free(node);
1600}
1601
1602/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001603 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001604 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001605 * When "id" is zero or negative jut get the first message. But not the one
1606 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001607 * Return OK when found and return the value in "rettv".
1608 * Return FAIL otherwise.
1609 */
1610 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001613 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001614 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615
Bram Moolenaar77073442016-02-13 23:23:53 +01001616 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001617 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001618 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 typval_T *tv = &l->lv_first->li_tv;
1620
1621 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001622 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001623 || tv->vval.v_number == 0
1624 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001625 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001627 if (tv->v_type == VAR_NUMBER)
1628 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001629 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001630 return OK;
1631 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001632 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001633 }
1634 return FAIL;
1635}
1636
Bram Moolenaarece61b02016-02-20 21:39:05 +01001637#define CH_JSON_MAX_ARGS 4
1638
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001639/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001640 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001641 * "argv[0]" is the command string.
1642 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001643 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001644 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001645channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001646{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001647 char_u *cmd = argv[0].vval.v_string;
1648 char_u *arg;
1649 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001650
Bram Moolenaarece61b02016-02-20 21:39:05 +01001651 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001652 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001653 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001654 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001655 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001656 return;
1657 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001658 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001659 if (arg == NULL)
1660 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001661
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001662 if (STRCMP(cmd, "ex") == 0)
1663 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001664 int save_called_emsg = called_emsg;
1665
1666 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001667 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001668 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001669 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001670 --emsg_silent;
1671 if (called_emsg)
1672 ch_logs(channel, "Ex command error: '%s'",
1673 (char *)get_vim_var_str(VV_ERRMSG));
1674 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001675 }
1676 else if (STRCMP(cmd, "normal") == 0)
1677 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001678 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001679
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001680 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001681 ea.arg = arg;
1682 ea.addr_count = 0;
1683 ea.forceit = TRUE; /* no mapping */
1684 ex_normal(&ea);
1685 }
1686 else if (STRCMP(cmd, "redraw") == 0)
1687 {
1688 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001689
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001690 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001691 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001692 ex_redraw(&ea);
1693 showruler(FALSE);
1694 setcursor();
1695 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001696#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001697 if (gui.in_use)
1698 {
1699 gui_update_cursor(FALSE, FALSE);
1700 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001701 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001702#endif
1703 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001704 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001705 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001706 int is_call = cmd[0] == 'c';
1707 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001708
Bram Moolenaarece61b02016-02-20 21:39:05 +01001709 if (argv[id_idx].v_type != VAR_UNKNOWN
1710 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001711 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001712 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001713 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001714 EMSG("E904: last argument for expr/call must be a number");
1715 }
1716 else if (is_call && argv[2].v_type != VAR_LIST)
1717 {
1718 ch_error(channel, "third argument for call must be a list");
1719 if (p_verbose > 2)
1720 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001721 }
1722 else
1723 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001724 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001725 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001726 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001727 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001728
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001729 /* Don't pollute the display with errors. */
1730 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001731 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001732 {
1733 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001734 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001735 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001736 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001737 {
1738 ch_logs(channel, "Calling '%s'", (char *)arg);
1739 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1740 tv = &res_tv;
1741 else
1742 tv = NULL;
1743 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001744
1745 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001746 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001747 int id = argv[id_idx].vval.v_number;
1748
Bram Moolenaar55fab432016-02-07 16:53:13 +01001749 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001750 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001751 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001752 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001753 /* If evaluation failed or the result can't be encoded
1754 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001755 vim_free(json);
1756 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001757 err_tv.v_type = VAR_STRING;
1758 err_tv.vval.v_string = (char_u *)"ERROR";
1759 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001760 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001761 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001762 if (json != NULL)
1763 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001764 channel_send(channel,
1765 part == PART_SOCK ? PART_SOCK : PART_IN,
1766 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001767 vim_free(json);
1768 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001769 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001770 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001771 if (tv == &res_tv)
1772 clear_tv(tv);
1773 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001774 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001775 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001776 }
1777 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001778 {
1779 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001780 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001781 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001782}
1783
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001784 static void
1785invoke_one_time_callback(
1786 channel_T *channel,
1787 cbq_T *cbhead,
1788 cbq_T *item,
1789 typval_T *argv)
1790{
1791 ch_logs(channel, "Invoking one-time callback %s",
1792 (char *)item->cq_callback);
1793 /* Remove the item from the list first, if the callback
1794 * invokes ch_close() the list will be cleared. */
1795 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001796 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001797 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001798 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001799 vim_free(item);
1800}
1801
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001802 static void
1803append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
1804{
1805 buf_T *save_curbuf = curbuf;
1806 linenr_T lnum = buffer->b_ml.ml_line_count;
1807 int save_write_to = buffer->b_write_to_channel;
1808
1809 /* If the buffer is also used as input insert above the last
1810 * line. Don't write these lines. */
1811 if (save_write_to)
1812 {
1813 --lnum;
1814 buffer->b_write_to_channel = FALSE;
1815 }
1816
1817 /* Append to the buffer */
1818 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
1819
1820 curbuf = buffer;
1821 u_sync(TRUE);
1822 /* ignore undo failure, undo is not very useful here */
1823 ignored = u_save(lnum, lnum + 1);
1824
1825 ml_append(lnum, msg, 0, FALSE);
1826 appended_lines_mark(lnum, 1L);
1827 curbuf = save_curbuf;
1828
1829 if (buffer->b_nwindows > 0)
1830 {
1831 win_T *wp;
1832 win_T *save_curwin;
1833
1834 FOR_ALL_WINDOWS(wp)
1835 {
1836 if (wp->w_buffer == buffer
1837 && (save_write_to
1838 ? wp->w_cursor.lnum == lnum + 1
1839 : (wp->w_cursor.lnum == lnum
1840 && wp->w_cursor.col == 0)))
1841 {
1842 ++wp->w_cursor.lnum;
1843 save_curwin = curwin;
1844 curwin = wp;
1845 curbuf = curwin->w_buffer;
1846 scroll_cursor_bot(0, FALSE);
1847 curwin = save_curwin;
1848 curbuf = curwin->w_buffer;
1849 }
1850 }
1851 redraw_buf_later(buffer, VALID);
1852 channel_need_redraw = TRUE;
1853 }
1854
1855 if (save_write_to)
1856 {
1857 channel_T *ch;
1858
1859 /* Find channels reading from this buffer and adjust their
1860 * next-to-read line number. */
1861 buffer->b_write_to_channel = TRUE;
1862 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
1863 {
1864 chanpart_T *in_part = &ch->ch_part[PART_IN];
1865
1866 if (in_part->ch_buffer == buffer)
1867 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
1868 }
1869 }
1870}
1871
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001872/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001873 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001874 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001875 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001876 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001877may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001878{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001879 char_u *msg = NULL;
1880 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001881 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001882 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001883 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001884 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001885 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001886 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001887 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001888 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001889
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001890 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001891 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001892 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001893
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001894 /* Use a message-specific callback, part callback or channel callback */
1895 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
1896 if (cbitem->cq_seq_nr == 0)
1897 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001898 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001899 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001900 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001901 partial = cbitem->cq_partial;
1902 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001903 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001904 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001905 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001906 partial = channel->ch_part[part].ch_partial;
1907 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001908 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001909 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001910 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001911 partial = channel->ch_partial;
1912 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001913
Bram Moolenaar187db502016-02-27 14:44:26 +01001914 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001915 if (buffer != NULL && !buf_valid(buffer))
1916 {
1917 /* buffer was wiped out */
1918 channel->ch_part[part].ch_buffer = NULL;
1919 buffer = NULL;
1920 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001921
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001922 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001923 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001924 listitem_T *item;
1925 int argc = 0;
1926
Bram Moolenaard7ece102016-02-02 23:23:02 +01001927 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001928 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001929 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001930 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001931 channel_parse_json(channel, part);
1932 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001933 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001934 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 for (item = listtv->vval.v_list->lv_first;
1937 item != NULL && argc < CH_JSON_MAX_ARGS;
1938 item = item->li_next)
1939 argv[argc++] = item->li_tv;
1940 while (argc < CH_JSON_MAX_ARGS)
1941 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001942
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 if (argv[0].v_type == VAR_STRING)
1944 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001945 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01001946 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01001947 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001948 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 }
1950
Bram Moolenaarece61b02016-02-20 21:39:05 +01001951 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001952 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001953 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001954 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01001955 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001956 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001957 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001958 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001959 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001960 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001961 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001962 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001963 return FALSE;
1964 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001965 else
1966 {
Bram Moolenaar187db502016-02-27 14:44:26 +01001967 /* If there is no callback or buffer drop the message. */
1968 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001969 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001970 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01001971 {
1972 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001973 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01001974 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001975 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001976 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001977
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001978 if (ch_mode == MODE_NL)
1979 {
1980 char_u *nl;
1981 char_u *buf;
1982
1983 /* See if we have a message ending in NL in the first buffer. If
1984 * not try to concatenate the first and the second buffer. */
1985 while (TRUE)
1986 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001987 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001988 nl = vim_strchr(buf, NL);
1989 if (nl != NULL)
1990 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001991 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001992 return FALSE; /* incomplete message */
1993 }
1994 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01001995 {
1996 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001997 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01001998 *nl = NUL;
1999 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002000 else
2001 {
2002 /* Copy the message into allocated memory and remove it from
2003 * the buffer. */
2004 msg = vim_strnsave(buf, (int)(nl - buf));
2005 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2006 }
2007 }
2008 else
2009 /* For a raw channel we don't know where the message ends, just
2010 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002011 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002012
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002013 if (msg == NULL)
2014 return FALSE; /* out of memory (and avoids Coverity warning) */
2015
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002016 argv[1].v_type = VAR_STRING;
2017 argv[1].vval.v_string = msg;
2018 }
2019
Bram Moolenaara07fec92016-02-05 21:04:08 +01002020 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002021 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002022 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002023
2024 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002025 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002026 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002027 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002028 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002029 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002030 break;
2031 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002032 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002033 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002034 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002035 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002036 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002037 if (buffer != NULL)
2038 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002039 if (msg == NULL)
2040 /* JSON or JS mode: re-encode the message. */
2041 msg = json_encode(listtv, ch_mode);
2042 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002043 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002044 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002045
Bram Moolenaar187db502016-02-27 14:44:26 +01002046 if (callback != NULL)
2047 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002048 if (cbitem != NULL)
2049 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2050 else
2051 {
2052 /* invoke the channel callback */
2053 ch_logs(channel, "Invoking channel callback %s",
2054 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002055 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002056 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002057 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002058 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002059 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002060 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002061
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002062 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002063 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002064 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002065
2066 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002067}
2068
2069/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002070 * Return TRUE when channel "channel" is open for writing to.
2071 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002072 */
2073 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002074channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002075{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002076 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002077 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002078}
2079
2080/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002081 * Return TRUE when channel "channel" is open for reading or writing.
2082 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002083 */
2084 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002085channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002086{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002087 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002088 || channel->CH_IN_FD != INVALID_FD
2089 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002090 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002091}
2092
2093/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002094 * Return a string indicating the status of the channel.
2095 */
2096 char *
2097channel_status(channel_T *channel)
2098{
2099 if (channel == NULL)
2100 return "fail";
2101 if (channel_is_open(channel))
2102 return "open";
2103 return "closed";
2104}
2105
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002106 static void
2107channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2108{
2109 chanpart_T *chanpart = &channel->ch_part[part];
2110 char namebuf[20];
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002111 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002112 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002113
2114 STRCPY(namebuf, name);
2115 STRCAT(namebuf, "_");
2116 tail = STRLEN(namebuf);
2117
2118 STRCPY(namebuf + tail, "status");
2119 dict_add_nr_str(dict, namebuf, 0,
2120 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2121
2122 STRCPY(namebuf + tail, "mode");
2123 switch (chanpart->ch_mode)
2124 {
2125 case MODE_NL: s = "NL"; break;
2126 case MODE_RAW: s = "RAW"; break;
2127 case MODE_JSON: s = "JSON"; break;
2128 case MODE_JS: s = "JS"; break;
2129 }
2130 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2131
2132 STRCPY(namebuf + tail, "io");
2133 if (part == PART_SOCK)
2134 s = "socket";
2135 else switch (chanpart->ch_io)
2136 {
2137 case JIO_NULL: s = "null"; break;
2138 case JIO_PIPE: s = "pipe"; break;
2139 case JIO_FILE: s = "file"; break;
2140 case JIO_BUFFER: s = "buffer"; break;
2141 case JIO_OUT: s = "out"; break;
2142 }
2143 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2144
2145 STRCPY(namebuf + tail, "timeout");
2146 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2147}
2148
2149 void
2150channel_info(channel_T *channel, dict_T *dict)
2151{
2152 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2153 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2154
2155 if (channel->ch_hostname != NULL)
2156 {
2157 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2158 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2159 channel_part_info(channel, dict, "sock", PART_SOCK);
2160 }
2161 else
2162 {
2163 channel_part_info(channel, dict, "out", PART_OUT);
2164 channel_part_info(channel, dict, "err", PART_ERR);
2165 channel_part_info(channel, dict, "in", PART_IN);
2166 }
2167}
2168
Bram Moolenaar77073442016-02-13 23:23:53 +01002169/*
2170 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002171 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002172 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002173 */
2174 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002175channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002176{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002177 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002178
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002179#ifdef FEAT_GUI
2180 channel_gui_unregister(channel);
2181#endif
2182
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002183 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002184 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002185 sock_close(channel->CH_SOCK_FD);
2186 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002187 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002188 may_close_part(&channel->CH_IN_FD);
2189 may_close_part(&channel->CH_OUT_FD);
2190 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002191
Bram Moolenaar8b374212016-02-24 20:43:06 +01002192 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002193 {
2194 typval_T argv[1];
2195 typval_T rettv;
2196 int dummy;
2197
2198 /* invoke the close callback; increment the refcount to avoid it
2199 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002200 ch_logs(channel, "Invoking close callback %s",
2201 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002202 argv[0].v_type = VAR_CHANNEL;
2203 argv[0].vval.v_channel = channel;
2204 ++channel->ch_refcount;
2205 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002206 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2207 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002208 clear_tv(&rettv);
2209 --channel->ch_refcount;
2210
2211 /* the callback is only called once */
2212 vim_free(channel->ch_close_cb);
2213 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002214 partial_unref(channel->ch_close_partial);
2215 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002216 }
2217
2218 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002219}
2220
Bram Moolenaard04a0202016-01-26 23:30:18 +01002221/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002222 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002223 * Returns NULL if there is nothing.
2224 */
2225 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002226channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002227{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002228 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002229
Bram Moolenaar77073442016-02-13 23:23:53 +01002230 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002231 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002232 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002233}
2234
2235/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002236 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002237 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002238 static void
2239channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002240{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002241 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2242 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002243
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002244 while (channel_peek(channel, part) != NULL)
2245 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002246
2247 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002248 {
2249 cbq_T *node = cb_head->cq_next;
2250
2251 remove_cb_node(cb_head, node);
2252 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002253 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002254 vim_free(node);
2255 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002256
2257 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002258 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002259 free_tv(json_head->jq_next->jq_value);
2260 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002261 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002262
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002263 vim_free(channel->ch_part[part].ch_callback);
2264 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002265 partial_unref(channel->ch_part[part].ch_partial);
2266 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002267}
2268
2269/*
2270 * Clear all the read buffers on "channel".
2271 */
2272 void
2273channel_clear(channel_T *channel)
2274{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002275 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002276 vim_free(channel->ch_hostname);
2277 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002278 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002279 channel_clear_one(channel, PART_OUT);
2280 channel_clear_one(channel, PART_ERR);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002281 vim_free(channel->ch_callback);
2282 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002283 partial_unref(channel->ch_partial);
2284 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002285 vim_free(channel->ch_close_cb);
2286 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002287 partial_unref(channel->ch_close_partial);
2288 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002289}
2290
Bram Moolenaar77073442016-02-13 23:23:53 +01002291#if defined(EXITFREE) || defined(PROTO)
2292 void
2293channel_free_all(void)
2294{
2295 channel_T *channel;
2296
Bram Moolenaard6051b52016-02-28 15:49:03 +01002297 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002298 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2299 channel_clear(channel);
2300}
2301#endif
2302
2303
Bram Moolenaard04a0202016-01-26 23:30:18 +01002304/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002305#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002306
2307/* Buffer size for reading incoming messages. */
2308#define MAXMSGSIZE 4096
2309
2310/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002311 * Check for reading from "fd" with "timeout" msec.
2312 * Return FAIL when there is nothing to read.
2313 */
2314 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002315channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002316{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002317 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002318 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002319
Bram Moolenaard8070362016-02-15 21:56:54 +01002320# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002321 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002322 {
2323 DWORD nread;
2324 int diff;
2325 DWORD deadline = GetTickCount() + timeout;
2326
2327 /* reading from a pipe, not a socket */
2328 while (TRUE)
2329 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002330 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2331 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002332 return OK;
2333 diff = deadline - GetTickCount();
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002334 if (diff <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002335 break;
2336 /* Wait for 5 msec.
2337 * TODO: increase the sleep time when looping more often */
2338 Sleep(5);
2339 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002340 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002341 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002342#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002343 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002344#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002345 struct timeval tval;
2346 fd_set rfds;
2347 int ret;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002348
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002349 FD_ZERO(&rfds);
2350 FD_SET((int)fd, &rfds);
2351 tval.tv_sec = timeout / 1000;
2352 tval.tv_usec = (timeout % 1000) * 1000;
2353 for (;;)
2354 {
2355 ret = select((int)fd + 1, &rfds, NULL, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002356# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002357 SOCK_ERRNO;
2358 if (ret == -1 && errno == EINTR)
2359 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002360# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002361 if (ret > 0)
2362 return OK;
2363 break;
2364 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002365#else
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002366 struct pollfd fds;
2367
2368 fds.fd = fd;
2369 fds.events = POLLIN;
2370 if (poll(&fds, 1, timeout) > 0)
2371 return OK;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002372#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002373 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002374 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002375}
2376
2377/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002378 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002379 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002380 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002381 */
2382 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002383channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002384{
2385 static char_u *buf = NULL;
2386 int len = 0;
2387 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002388 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002389 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002390
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002391 fd = channel->ch_part[part].ch_fd;
2392 if (fd == INVALID_FD)
2393 {
2394 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002395 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002396 }
2397 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002398
2399 /* Allocate a buffer to read into. */
2400 if (buf == NULL)
2401 {
2402 buf = alloc(MAXMSGSIZE);
2403 if (buf == NULL)
2404 return; /* out of memory! */
2405 }
2406
2407 /* Keep on reading for as long as there is something to read.
2408 * Use select() or poll() to avoid blocking on a message that is exactly
2409 * MAXMSGSIZE long. */
2410 for (;;)
2411 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002412 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002413 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002414 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002415 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002416 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002417 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002418 if (len <= 0)
2419 break; /* error or nothing more to read */
2420
2421 /* Store the read message in the queue. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002422 channel_save(channel, part, buf, len, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002423 readlen += len;
2424 if (len < MAXMSGSIZE)
2425 break; /* did read everything that's available */
2426 }
2427
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002428 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002429 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002430 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002431 /* Do not give an error message, most likely the other end just
2432 * exited. */
2433 ch_errors(channel, "%s(): Cannot read from channel", func);
2434
Bram Moolenaard04a0202016-01-26 23:30:18 +01002435 /* Queue a "DETACH" netbeans message in the command queue in order to
2436 * terminate the netbeans session later. Do not end the session here
2437 * directly as we may be running in the context of a call to
2438 * netbeans_parse_messages():
2439 * netbeans_parse_messages
2440 * -> autocmd triggered while processing the netbeans cmd
2441 * -> ui_breakcheck
2442 * -> gui event loop or select loop
2443 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002444 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002445 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002446 if (channel->ch_part[part].ch_mode == MODE_RAW
2447 || channel->ch_part[part].ch_mode == MODE_NL)
2448 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002449 (int)STRLEN(DETACH_MSG_RAW), "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002450
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002451 /* TODO: When reading from stdout is not possible, should we try to
2452 * keep stdin and stderr open? Probably not, assume the other side
2453 * has died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002454 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002455 if (channel->ch_nb_close_cb != NULL)
2456 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002457 }
2458
2459#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002460 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002461 if (CH_HAS_GUI && gtk_main_level() > 0)
2462 gtk_main_quit();
2463#endif
2464}
2465
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002466/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002467 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002468 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002469 * Returns what was read in allocated memory.
2470 * Returns NULL in case of error or timeout.
2471 */
2472 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002473channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002474{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002475 char_u *buf;
2476 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002477 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002478 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002479 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002480
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002481 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002482 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002483
2484 while (TRUE)
2485 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002486 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002487 if (buf != NULL && (mode == MODE_RAW
2488 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2489 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002490 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002491 continue;
2492
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002493 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002494 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002495 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002496 if (channel_wait(channel, fd, timeout) == FAIL)
2497 {
2498 ch_log(channel, "Timed out");
2499 return NULL;
2500 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002501 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002502 }
2503
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002504 if (mode == MODE_RAW)
2505 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002506 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002507 }
2508 else
2509 {
2510 nl = vim_strchr(buf, NL);
2511 if (nl[1] == NUL)
2512 {
2513 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002514 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002515 *nl = NUL;
2516 }
2517 else
2518 {
2519 /* Copy the message into allocated memory and remove it from the
2520 * buffer. */
2521 msg = vim_strnsave(buf, (int)(nl - buf));
2522 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2523 }
2524 }
2525 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002526 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002527 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002528}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002529
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002530/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002532 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002533 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002534 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002535 */
2536 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002537channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002538 channel_T *channel,
2539 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002540 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002541 int id,
2542 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002543{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002544 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002545 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002546 int timeout;
2547 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002548
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002549 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002550 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002551 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002552 for (;;)
2553 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002554 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002555
2556 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002557 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002558 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002559 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002560 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002561 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002562
Bram Moolenaard7ece102016-02-02 23:23:02 +01002563 if (!more)
2564 {
2565 /* Handle any other messages in the queue. If done some more
2566 * messages may have arrived. */
2567 if (channel_parse_messages())
2568 continue;
2569
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002570 /* Wait for up to the timeout. If there was an incomplete message
2571 * use the deadline for that. */
2572 timeout = timeout_arg;
2573 if (chanpart->ch_waiting)
2574 {
2575#ifdef WIN32
2576 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2577#else
2578 {
2579 struct timeval now_tv;
2580
2581 gettimeofday(&now_tv, NULL);
2582 timeout = (chanpart->ch_deadline.tv_sec
2583 - now_tv.tv_sec) * 1000
2584 + (chanpart->ch_deadline.tv_usec
2585 - now_tv.tv_usec) / 1000
2586 + 1;
2587 }
2588#endif
2589 if (timeout < 0)
2590 {
2591 /* Something went wrong, channel_parse_json() didn't
2592 * discard message. Cancel waiting. */
2593 chanpart->ch_waiting = FALSE;
2594 timeout = timeout_arg;
2595 }
2596 else if (timeout > timeout_arg)
2597 timeout = timeout_arg;
2598 }
2599 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002600 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002601 {
2602 if (timeout == timeout_arg)
2603 {
2604 if (fd != INVALID_FD)
2605 ch_log(channel, "Timed out");
2606 break;
2607 }
2608 }
2609 else
2610 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002611 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002612 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002613 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002614 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002615}
2616
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002617/*
2618 * Common for ch_read() and ch_readraw().
2619 */
2620 void
2621common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2622{
2623 channel_T *channel;
2624 int part;
2625 jobopt_T opt;
2626 int mode;
2627 int timeout;
2628 int id = -1;
2629 typval_T *listtv = NULL;
2630
2631 /* return an empty string by default */
2632 rettv->v_type = VAR_STRING;
2633 rettv->vval.v_string = NULL;
2634
2635 clear_job_options(&opt);
2636 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2637 == FAIL)
2638 return;
2639
2640 channel = get_channel_arg(&argvars[0], TRUE);
2641 if (channel != NULL)
2642 {
2643 if (opt.jo_set & JO_PART)
2644 part = opt.jo_part;
2645 else
2646 part = channel_part_read(channel);
2647 mode = channel_get_mode(channel, part);
2648 timeout = channel_get_timeout(channel, part);
2649 if (opt.jo_set & JO_TIMEOUT)
2650 timeout = opt.jo_timeout;
2651
2652 if (raw || mode == MODE_RAW || mode == MODE_NL)
2653 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2654 else
2655 {
2656 if (opt.jo_set & JO_ID)
2657 id = opt.jo_id;
2658 channel_read_json_block(channel, part, timeout, id, &listtv);
2659 if (listtv != NULL)
2660 {
2661 *rettv = *listtv;
2662 vim_free(listtv);
2663 }
2664 else
2665 {
2666 rettv->v_type = VAR_SPECIAL;
2667 rettv->vval.v_number = VVAL_NONE;
2668 }
2669 }
2670 }
2671}
2672
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002673# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2674 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002675/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002676 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002677 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002678 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002679 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002680channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002681{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002682 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002683 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002684
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002685 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01002686 for (channel = first_channel; channel != NULL;
2687 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002688 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002689 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002690 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002691 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002692 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002693 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002694 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002695 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002696 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002697}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002698# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002699
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002700# if defined(WIN32) || defined(PROTO)
2701/*
2702 * Check the channels for anything that is ready to be read.
2703 * The data is put in the read queue.
2704 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002705 void
2706channel_handle_events(void)
2707{
2708 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002709 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002710 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002711
2712 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2713 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002714 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002715 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002716 {
2717 fd = channel->ch_part[part].ch_fd;
2718 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
2719 channel_read(channel, part, "channel_handle_events");
2720 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002721 }
2722}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002723# endif
2724
Bram Moolenaard04a0202016-01-26 23:30:18 +01002725/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002726 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002727 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002728 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002729 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002730 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002731channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002732{
Bram Moolenaard04a0202016-01-26 23:30:18 +01002733 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002734 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002735 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002736
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002737 fd = channel->ch_part[part].ch_fd;
2738 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002739 {
2740 if (!channel->ch_error && fun != NULL)
2741 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002742 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002743 EMSG2("E630: %s(): write while not connected", fun);
2744 }
2745 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002746 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002747 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002748
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002749 if (log_fd != NULL)
2750 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002751 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002752 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002753 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002754 fprintf(log_fd, "'\n");
2755 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01002756 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002757 }
2758
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002759 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002760 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002761 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002762 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002763 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002764 {
2765 if (!channel->ch_error && fun != NULL)
2766 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002767 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002768 EMSG2("E631: %s(): write failed", fun);
2769 }
2770 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002771 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002772 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002773
2774 channel->ch_error = FALSE;
2775 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002776}
2777
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002778/*
2779 * Common for "ch_sendexpr()" and "ch_sendraw()".
2780 * Returns the channel if the caller should read the response.
2781 * Sets "part_read" to the the read fd.
2782 * Otherwise returns NULL.
2783 */
2784 channel_T *
2785send_common(
2786 typval_T *argvars,
2787 char_u *text,
2788 int id,
2789 int eval,
2790 jobopt_T *opt,
2791 char *fun,
2792 int *part_read)
2793{
2794 channel_T *channel;
2795 int part_send;
2796
2797 channel = get_channel_arg(&argvars[0], TRUE);
2798 if (channel == NULL)
2799 return NULL;
2800 part_send = channel_part_send(channel);
2801 *part_read = channel_part_read(channel);
2802
2803 clear_job_options(opt);
2804 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
2805 return NULL;
2806
2807 /* Set the callback. An empty callback means no callback and not reading
2808 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
2809 * allowed. */
2810 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
2811 {
2812 if (eval)
2813 {
2814 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
2815 return NULL;
2816 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002817 channel_set_req_callback(channel, part_send,
2818 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002819 }
2820
2821 if (channel_send(channel, part_send, text, fun) == OK
2822 && opt->jo_callback == NULL)
2823 return channel;
2824 return NULL;
2825}
2826
2827/*
2828 * common for "ch_evalexpr()" and "ch_sendexpr()"
2829 */
2830 void
2831ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
2832{
2833 char_u *text;
2834 typval_T *listtv;
2835 channel_T *channel;
2836 int id;
2837 ch_mode_T ch_mode;
2838 int part_send;
2839 int part_read;
2840 jobopt_T opt;
2841 int timeout;
2842
2843 /* return an empty string by default */
2844 rettv->v_type = VAR_STRING;
2845 rettv->vval.v_string = NULL;
2846
2847 channel = get_channel_arg(&argvars[0], TRUE);
2848 if (channel == NULL)
2849 return;
2850 part_send = channel_part_send(channel);
2851
2852 ch_mode = channel_get_mode(channel, part_send);
2853 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
2854 {
2855 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
2856 return;
2857 }
2858
Bram Moolenaare9d6a292016-03-20 19:31:33 +01002859 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002860 text = json_encode_nr_expr(id, &argvars[1],
2861 ch_mode == MODE_JS ? JSON_JS : 0);
2862 if (text == NULL)
2863 return;
2864
2865 channel = send_common(argvars, text, id, eval, &opt,
2866 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
2867 vim_free(text);
2868 if (channel != NULL && eval)
2869 {
2870 if (opt.jo_set & JO_TIMEOUT)
2871 timeout = opt.jo_timeout;
2872 else
2873 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002874 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
2875 == OK)
2876 {
2877 list_T *list = listtv->vval.v_list;
2878
2879 /* Move the item from the list and then change the type to
2880 * avoid the value being freed. */
2881 *rettv = list->lv_last->li_tv;
2882 list->lv_last->li_tv.v_type = VAR_NUMBER;
2883 free_tv(listtv);
2884 }
2885 }
2886}
2887
2888/*
2889 * common for "ch_evalraw()" and "ch_sendraw()"
2890 */
2891 void
2892ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
2893{
2894 char_u buf[NUMBUFLEN];
2895 char_u *text;
2896 channel_T *channel;
2897 int part_read;
2898 jobopt_T opt;
2899 int timeout;
2900
2901 /* return an empty string by default */
2902 rettv->v_type = VAR_STRING;
2903 rettv->vval.v_string = NULL;
2904
2905 text = get_tv_string_buf(&argvars[1], buf);
2906 channel = send_common(argvars, text, 0, eval, &opt,
2907 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
2908 if (channel != NULL && eval)
2909 {
2910 if (opt.jo_set & JO_TIMEOUT)
2911 timeout = opt.jo_timeout;
2912 else
2913 timeout = channel_get_timeout(channel, part_read);
2914 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
2915 }
2916}
2917
Bram Moolenaard04a0202016-01-26 23:30:18 +01002918# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002919/*
2920 * Add open channels to the poll struct.
2921 * Return the adjusted struct index.
2922 * The type of "fds" is hidden to avoid problems with the function proto.
2923 */
2924 int
2925channel_poll_setup(int nfd_in, void *fds_in)
2926{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002927 int nfd = nfd_in;
2928 channel_T *channel;
2929 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002930 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002931
Bram Moolenaar77073442016-02-13 23:23:53 +01002932 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002933 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002934 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002935 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002936 if (channel->ch_part[part].ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002937 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002938 channel->ch_part[part].ch_poll_idx = nfd;
2939 fds[nfd].fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002940 fds[nfd].events = POLLIN;
2941 nfd++;
2942 }
2943 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002944 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002945 }
2946 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002947
2948 return nfd;
2949}
2950
2951/*
2952 * The type of "fds" is hidden to avoid problems with the function proto.
2953 */
2954 int
2955channel_poll_check(int ret_in, void *fds_in)
2956{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002957 int ret = ret_in;
2958 channel_T *channel;
2959 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002960 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002961
Bram Moolenaar77073442016-02-13 23:23:53 +01002962 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002963 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002964 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002965 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002966 int idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002967
2968 if (ret > 0 && idx != -1 && fds[idx].revents & POLLIN)
2969 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002970 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002971 --ret;
2972 }
2973 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002974 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002975
2976 return ret;
2977}
Bram Moolenaard04a0202016-01-26 23:30:18 +01002978# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002979
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002980# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002981/*
2982 * The type of "rfds" is hidden to avoid problems with the function proto.
2983 */
2984 int
2985channel_select_setup(int maxfd_in, void *rfds_in)
2986{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002987 int maxfd = maxfd_in;
2988 channel_T *channel;
2989 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002990 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002991
Bram Moolenaar77073442016-02-13 23:23:53 +01002992 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002993 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002994 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002995 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002996 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002997
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002998 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002999 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003000 FD_SET((int)fd, rfds);
3001 if (maxfd < (int)fd)
3002 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003003 }
3004 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003005 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003006
3007 return maxfd;
3008}
3009
3010/*
3011 * The type of "rfds" is hidden to avoid problems with the function proto.
3012 */
3013 int
3014channel_select_check(int ret_in, void *rfds_in)
3015{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003016 int ret = ret_in;
3017 channel_T *channel;
3018 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003019 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003020
Bram Moolenaar77073442016-02-13 23:23:53 +01003021 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003022 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003023 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003024 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003025 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003026
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003027 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003028 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003029 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003030 --ret;
3031 }
3032 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003033 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003034
3035 return ret;
3036}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003037# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003038
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003039/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003040 * Return TRUE if "channel" has JSON or other typeahead.
3041 */
3042 static int
3043channel_has_readahead(channel_T *channel, int part)
3044{
3045 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3046
3047 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3048 {
3049 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3050 jsonq_T *item = head->jq_next;
3051
3052 return item != NULL;
3053 }
3054 return channel_peek(channel, part) != NULL;
3055}
3056
3057/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003058 * Execute queued up commands.
3059 * Invoked from the main loop when it's safe to execute received commands.
3060 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003061 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003062 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003063channel_parse_messages(void)
3064{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003065 channel_T *channel = first_channel;
3066 int ret = FALSE;
3067 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003068 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003069
Bram Moolenaard0b65022016-03-06 21:50:33 +01003070 /* Only do this message when another message was given, otherwise we get
3071 * lots of them. */
3072 if (did_log_msg)
3073 {
3074 ch_log(NULL, "looking for messages on channels");
3075 did_log_msg = FALSE;
3076 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003077 while (channel != NULL)
3078 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003079 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003080 {
3081 /* channel is no longer useful, free it */
3082 channel_free(channel);
3083 channel = first_channel;
3084 part = PART_SOCK;
3085 continue;
3086 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003087 if (channel->ch_part[part].ch_fd != INVALID_FD
3088 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003089 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003090 /* Increase the refcount, in case the handler causes the channel
3091 * to be unreferenced or closed. */
3092 ++channel->ch_refcount;
3093 r = may_invoke_callback(channel, part);
3094 if (r == OK)
3095 ret = TRUE;
3096 if (channel_unref(channel) || r == OK)
3097 {
3098 /* channel was freed or something was done, start over */
3099 channel = first_channel;
3100 part = PART_SOCK;
3101 continue;
3102 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003103 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003104 if (part < PART_ERR)
3105 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003106 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003107 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003108 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003109 part = PART_SOCK;
3110 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003111 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003112
3113 if (channel_need_redraw && must_redraw)
3114 {
3115 channel_need_redraw = FALSE;
3116 update_screen(0);
3117 setcursor();
3118 cursor_on();
3119 out_flush();
3120 }
3121
Bram Moolenaard7ece102016-02-02 23:23:02 +01003122 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003123}
3124
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003125/*
3126 * Mark references to lists used in channels.
3127 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003128 int
3129set_ref_in_channel(int copyID)
3130{
Bram Moolenaar77073442016-02-13 23:23:53 +01003131 int abort = FALSE;
3132 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003133 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003134
Bram Moolenaar77073442016-02-13 23:23:53 +01003135 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003136 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003137 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003138 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003139 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3140 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003141
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003142 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003143 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003144 list_T *l = item->jq_value->vval.v_list;
3145
3146 if (l->lv_copyID != copyID)
3147 {
3148 l->lv_copyID = copyID;
3149 abort = abort || set_ref_in_list(l, copyID, NULL);
3150 }
3151 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003152 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003153 }
3154 }
3155 return abort;
3156}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003157
3158/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003159 * Return the "part" to write to for "channel".
3160 */
3161 int
3162channel_part_send(channel_T *channel)
3163{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003164 if (channel->CH_SOCK_FD == INVALID_FD)
3165 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003166 return PART_SOCK;
3167}
3168
3169/*
3170 * Return the default "part" to read from for "channel".
3171 */
3172 int
3173channel_part_read(channel_T *channel)
3174{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003175 if (channel->CH_SOCK_FD == INVALID_FD)
3176 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003177 return PART_SOCK;
3178}
3179
3180/*
3181 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003182 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003183 */
3184 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003185channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003186{
Bram Moolenaar77073442016-02-13 23:23:53 +01003187 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003188 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003189 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003190}
3191
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003192/*
3193 * Return the timeout of "channel"/"part"
3194 */
3195 int
3196channel_get_timeout(channel_T *channel, int part)
3197{
3198 return channel->ch_part[part].ch_timeout;
3199}
3200
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003201 static int
3202handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3203{
3204 char_u *val = get_tv_string(item);
3205
3206 opt->jo_set |= jo;
3207 if (STRCMP(val, "nl") == 0)
3208 *modep = MODE_NL;
3209 else if (STRCMP(val, "raw") == 0)
3210 *modep = MODE_RAW;
3211 else if (STRCMP(val, "js") == 0)
3212 *modep = MODE_JS;
3213 else if (STRCMP(val, "json") == 0)
3214 *modep = MODE_JSON;
3215 else
3216 {
3217 EMSG2(_(e_invarg2), val);
3218 return FAIL;
3219 }
3220 return OK;
3221}
3222
3223 static int
3224handle_io(typval_T *item, int part, jobopt_T *opt)
3225{
3226 char_u *val = get_tv_string(item);
3227
3228 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3229 if (STRCMP(val, "null") == 0)
3230 opt->jo_io[part] = JIO_NULL;
3231 else if (STRCMP(val, "pipe") == 0)
3232 opt->jo_io[part] = JIO_PIPE;
3233 else if (STRCMP(val, "file") == 0)
3234 opt->jo_io[part] = JIO_FILE;
3235 else if (STRCMP(val, "buffer") == 0)
3236 opt->jo_io[part] = JIO_BUFFER;
3237 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3238 opt->jo_io[part] = JIO_OUT;
3239 else
3240 {
3241 EMSG2(_(e_invarg2), val);
3242 return FAIL;
3243 }
3244 return OK;
3245}
3246
3247 void
3248clear_job_options(jobopt_T *opt)
3249{
3250 vim_memset(opt, 0, sizeof(jobopt_T));
3251}
3252
3253/*
3254 * Get the PART_ number from the first character of an option name.
3255 */
3256 static int
3257part_from_char(int c)
3258{
3259 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3260}
3261
3262/*
3263 * Get the option entries from the dict in "tv", parse them and put the result
3264 * in "opt".
3265 * Only accept options in "supported".
3266 * If an option value is invalid return FAIL.
3267 */
3268 int
3269get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3270{
3271 typval_T *item;
3272 char_u *val;
3273 dict_T *dict;
3274 int todo;
3275 hashitem_T *hi;
3276 int part;
3277
3278 opt->jo_set = 0;
3279 if (tv->v_type == VAR_UNKNOWN)
3280 return OK;
3281 if (tv->v_type != VAR_DICT)
3282 {
3283 EMSG(_(e_invarg));
3284 return FAIL;
3285 }
3286 dict = tv->vval.v_dict;
3287 if (dict == NULL)
3288 return OK;
3289
3290 todo = (int)dict->dv_hashtab.ht_used;
3291 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3292 if (!HASHITEM_EMPTY(hi))
3293 {
3294 item = &dict_lookup(hi)->di_tv;
3295
3296 if (STRCMP(hi->hi_key, "mode") == 0)
3297 {
3298 if (!(supported & JO_MODE))
3299 break;
3300 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3301 return FAIL;
3302 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003303 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003304 {
3305 if (!(supported & JO_IN_MODE))
3306 break;
3307 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3308 == FAIL)
3309 return FAIL;
3310 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003311 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003312 {
3313 if (!(supported & JO_OUT_MODE))
3314 break;
3315 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3316 == FAIL)
3317 return FAIL;
3318 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003319 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003320 {
3321 if (!(supported & JO_ERR_MODE))
3322 break;
3323 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3324 == FAIL)
3325 return FAIL;
3326 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003327 else if (STRCMP(hi->hi_key, "in_io") == 0
3328 || STRCMP(hi->hi_key, "out_io") == 0
3329 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003330 {
3331 if (!(supported & JO_OUT_IO))
3332 break;
3333 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3334 return FAIL;
3335 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003336 else if (STRCMP(hi->hi_key, "in_name") == 0
3337 || STRCMP(hi->hi_key, "out_name") == 0
3338 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003339 {
3340 part = part_from_char(*hi->hi_key);
3341
3342 if (!(supported & JO_OUT_IO))
3343 break;
3344 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3345 opt->jo_io_name[part] =
3346 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3347 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003348 else if (STRCMP(hi->hi_key, "in_buf") == 0
3349 || STRCMP(hi->hi_key, "out_buf") == 0
3350 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003351 {
3352 part = part_from_char(*hi->hi_key);
3353
3354 if (!(supported & JO_OUT_IO))
3355 break;
3356 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3357 opt->jo_io_buf[part] = get_tv_number(item);
3358 if (opt->jo_io_buf[part] <= 0)
3359 {
3360 EMSG2(_(e_invarg2), get_tv_string(item));
3361 return FAIL;
3362 }
3363 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3364 {
3365 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3366 return FAIL;
3367 }
3368 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003369 else if (STRCMP(hi->hi_key, "in_top") == 0
3370 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003371 {
3372 linenr_T *lp;
3373
3374 if (!(supported & JO_OUT_IO))
3375 break;
3376 if (hi->hi_key[3] == 't')
3377 {
3378 lp = &opt->jo_in_top;
3379 opt->jo_set |= JO_IN_TOP;
3380 }
3381 else
3382 {
3383 lp = &opt->jo_in_bot;
3384 opt->jo_set |= JO_IN_BOT;
3385 }
3386 *lp = get_tv_number(item);
3387 if (*lp < 0)
3388 {
3389 EMSG2(_(e_invarg2), get_tv_string(item));
3390 return FAIL;
3391 }
3392 }
3393 else if (STRCMP(hi->hi_key, "channel") == 0)
3394 {
3395 if (!(supported & JO_OUT_IO))
3396 break;
3397 opt->jo_set |= JO_CHANNEL;
3398 if (item->v_type != VAR_CHANNEL)
3399 {
3400 EMSG2(_(e_invarg2), "channel");
3401 return FAIL;
3402 }
3403 opt->jo_channel = item->vval.v_channel;
3404 }
3405 else if (STRCMP(hi->hi_key, "callback") == 0)
3406 {
3407 if (!(supported & JO_CALLBACK))
3408 break;
3409 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003410 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003411 if (opt->jo_callback == NULL)
3412 {
3413 EMSG2(_(e_invarg2), "callback");
3414 return FAIL;
3415 }
3416 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003417 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003418 {
3419 if (!(supported & JO_OUT_CALLBACK))
3420 break;
3421 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003422 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003423 if (opt->jo_out_cb == NULL)
3424 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003425 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003426 return FAIL;
3427 }
3428 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003429 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003430 {
3431 if (!(supported & JO_ERR_CALLBACK))
3432 break;
3433 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003434 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003435 if (opt->jo_err_cb == NULL)
3436 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003437 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003438 return FAIL;
3439 }
3440 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003441 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003442 {
3443 if (!(supported & JO_CLOSE_CALLBACK))
3444 break;
3445 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003446 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003447 if (opt->jo_close_cb == NULL)
3448 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003449 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003450 return FAIL;
3451 }
3452 }
3453 else if (STRCMP(hi->hi_key, "waittime") == 0)
3454 {
3455 if (!(supported & JO_WAITTIME))
3456 break;
3457 opt->jo_set |= JO_WAITTIME;
3458 opt->jo_waittime = get_tv_number(item);
3459 }
3460 else if (STRCMP(hi->hi_key, "timeout") == 0)
3461 {
3462 if (!(supported & JO_TIMEOUT))
3463 break;
3464 opt->jo_set |= JO_TIMEOUT;
3465 opt->jo_timeout = get_tv_number(item);
3466 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003467 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003468 {
3469 if (!(supported & JO_OUT_TIMEOUT))
3470 break;
3471 opt->jo_set |= JO_OUT_TIMEOUT;
3472 opt->jo_out_timeout = get_tv_number(item);
3473 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003474 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003475 {
3476 if (!(supported & JO_ERR_TIMEOUT))
3477 break;
3478 opt->jo_set |= JO_ERR_TIMEOUT;
3479 opt->jo_err_timeout = get_tv_number(item);
3480 }
3481 else if (STRCMP(hi->hi_key, "part") == 0)
3482 {
3483 if (!(supported & JO_PART))
3484 break;
3485 opt->jo_set |= JO_PART;
3486 val = get_tv_string(item);
3487 if (STRCMP(val, "err") == 0)
3488 opt->jo_part = PART_ERR;
3489 else
3490 {
3491 EMSG2(_(e_invarg2), val);
3492 return FAIL;
3493 }
3494 }
3495 else if (STRCMP(hi->hi_key, "id") == 0)
3496 {
3497 if (!(supported & JO_ID))
3498 break;
3499 opt->jo_set |= JO_ID;
3500 opt->jo_id = get_tv_number(item);
3501 }
3502 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3503 {
3504 if (!(supported & JO_STOPONEXIT))
3505 break;
3506 opt->jo_set |= JO_STOPONEXIT;
3507 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3508 opt->jo_soe_buf);
3509 if (opt->jo_stoponexit == NULL)
3510 {
3511 EMSG2(_(e_invarg2), "stoponexit");
3512 return FAIL;
3513 }
3514 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003515 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003516 {
3517 if (!(supported & JO_EXIT_CB))
3518 break;
3519 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003520 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3521 {
3522 opt->jo_exit_partial = item->vval.v_partial;
3523 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3524 }
3525 else
3526 opt->jo_exit_cb = get_tv_string_buf_chk(
3527 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003528 if (opt->jo_exit_cb == NULL)
3529 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003530 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003531 return FAIL;
3532 }
3533 }
3534 else
3535 break;
3536 --todo;
3537 }
3538 if (todo > 0)
3539 {
3540 EMSG2(_(e_invarg2), hi->hi_key);
3541 return FAIL;
3542 }
3543
3544 return OK;
3545}
3546
3547/*
3548 * Get the channel from the argument.
3549 * Returns NULL if the handle is invalid.
3550 */
3551 channel_T *
3552get_channel_arg(typval_T *tv, int check_open)
3553{
3554 channel_T *channel = NULL;
3555
3556 if (tv->v_type == VAR_JOB)
3557 {
3558 if (tv->vval.v_job != NULL)
3559 channel = tv->vval.v_job->jv_channel;
3560 }
3561 else if (tv->v_type == VAR_CHANNEL)
3562 {
3563 channel = tv->vval.v_channel;
3564 }
3565 else
3566 {
3567 EMSG2(_(e_invarg2), get_tv_string(tv));
3568 return NULL;
3569 }
3570
3571 if (check_open && (channel == NULL || !channel_is_open(channel)))
3572 {
3573 EMSG(_("E906: not an open channel"));
3574 return NULL;
3575 }
3576 return channel;
3577}
3578
3579static job_T *first_job = NULL;
3580
3581 static void
3582job_free(job_T *job)
3583{
3584 ch_log(job->jv_channel, "Freeing job");
3585 if (job->jv_channel != NULL)
3586 {
3587 /* The link from the channel to the job doesn't count as a reference,
3588 * thus don't decrement the refcount of the job. The reference from
3589 * the job to the channel does count the refrence, decrement it and
3590 * NULL the reference. We don't set ch_job_killed, unreferencing the
3591 * job doesn't mean it stops running. */
3592 job->jv_channel->ch_job = NULL;
3593 channel_unref(job->jv_channel);
3594 }
3595 mch_clear_job(job);
3596
3597 if (job->jv_next != NULL)
3598 job->jv_next->jv_prev = job->jv_prev;
3599 if (job->jv_prev == NULL)
3600 first_job = job->jv_next;
3601 else
3602 job->jv_prev->jv_next = job->jv_next;
3603
3604 vim_free(job->jv_stoponexit);
3605 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003606 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003607 vim_free(job);
3608}
3609
3610 void
3611job_unref(job_T *job)
3612{
3613 if (job != NULL && --job->jv_refcount <= 0)
3614 {
3615 /* Do not free the job when it has not ended yet and there is a
3616 * "stoponexit" flag or an exit callback. */
3617 if (job->jv_status != JOB_STARTED
3618 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
3619 {
3620 job_free(job);
3621 }
3622 else if (job->jv_channel != NULL)
3623 {
3624 /* Do remove the link to the channel, otherwise it hangs
3625 * around until Vim exits. See job_free() for refcount. */
3626 job->jv_channel->ch_job = NULL;
3627 channel_unref(job->jv_channel);
3628 job->jv_channel = NULL;
3629 }
3630 }
3631}
3632
3633/*
3634 * Allocate a job. Sets the refcount to one and sets options default.
3635 */
3636 static job_T *
3637job_alloc(void)
3638{
3639 job_T *job;
3640
3641 job = (job_T *)alloc_clear(sizeof(job_T));
3642 if (job != NULL)
3643 {
3644 job->jv_refcount = 1;
3645 job->jv_stoponexit = vim_strsave((char_u *)"term");
3646
3647 if (first_job != NULL)
3648 {
3649 first_job->jv_prev = job;
3650 job->jv_next = first_job;
3651 }
3652 first_job = job;
3653 }
3654 return job;
3655}
3656
3657 void
3658job_set_options(job_T *job, jobopt_T *opt)
3659{
3660 if (opt->jo_set & JO_STOPONEXIT)
3661 {
3662 vim_free(job->jv_stoponexit);
3663 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
3664 job->jv_stoponexit = NULL;
3665 else
3666 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
3667 }
3668 if (opt->jo_set & JO_EXIT_CB)
3669 {
3670 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003671 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003672 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003673 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003674 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003675 job->jv_exit_partial = NULL;
3676 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003677 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003678 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003679 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003680 job->jv_exit_partial = opt->jo_exit_partial;
3681 if (job->jv_exit_partial != NULL)
3682 ++job->jv_exit_partial->pt_refcount;
3683 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003684 }
3685}
3686
3687/*
3688 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
3689 */
3690 void
3691job_stop_on_exit()
3692{
3693 job_T *job;
3694
3695 for (job = first_job; job != NULL; job = job->jv_next)
3696 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
3697 mch_stop_job(job, job->jv_stoponexit);
3698}
3699
3700/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003701 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003702 */
3703 void
3704job_check_ended(void)
3705{
3706 static time_t last_check = 0;
3707 time_t now;
3708 job_T *job;
3709 job_T *next;
3710
3711 /* Only do this once in 10 seconds. */
3712 now = time(NULL);
3713 if (last_check + 10 < now)
3714 {
3715 last_check = now;
3716 for (job = first_job; job != NULL; job = next)
3717 {
3718 next = job->jv_next;
3719 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
3720 job_status(job); /* may free "job" */
3721 }
3722 }
3723}
3724
3725/*
3726 * "job_start()" function
3727 */
3728 job_T *
3729job_start(typval_T *argvars)
3730{
3731 job_T *job;
3732 char_u *cmd = NULL;
3733#if defined(UNIX)
3734# define USE_ARGV
3735 char **argv = NULL;
3736 int argc = 0;
3737#else
3738 garray_T ga;
3739#endif
3740 jobopt_T opt;
3741 int part;
3742
3743 job = job_alloc();
3744 if (job == NULL)
3745 return NULL;
3746
3747 job->jv_status = JOB_FAILED;
3748
3749 /* Default mode is NL. */
3750 clear_job_options(&opt);
3751 opt.jo_mode = MODE_NL;
3752 if (get_job_options(&argvars[1], &opt,
3753 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
3754 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
3755 return job;
3756
3757 /* Check that when io is "file" that there is a file name. */
3758 for (part = PART_OUT; part <= PART_IN; ++part)
3759 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
3760 && opt.jo_io[part] == JIO_FILE
3761 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
3762 || *opt.jo_io_name[part] == NUL))
3763 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003764 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003765 return job;
3766 }
3767
3768 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
3769 {
3770 buf_T *buf = NULL;
3771
3772 /* check that we can find the buffer before starting the job */
3773 if (opt.jo_set & JO_IN_BUF)
3774 {
3775 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
3776 if (buf == NULL)
3777 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
3778 }
3779 else if (!(opt.jo_set & JO_IN_NAME))
3780 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003781 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003782 }
3783 else
3784 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
3785 if (buf == NULL)
3786 return job;
3787 if (buf->b_ml.ml_mfp == NULL)
3788 {
3789 char_u numbuf[NUMBUFLEN];
3790 char_u *s;
3791
3792 if (opt.jo_set & JO_IN_BUF)
3793 {
3794 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
3795 s = numbuf;
3796 }
3797 else
3798 s = opt.jo_io_name[PART_IN];
3799 EMSG2(_("E918: buffer must be loaded: %s"), s);
3800 return job;
3801 }
3802 job->jv_in_buf = buf;
3803 }
3804
3805 job_set_options(job, &opt);
3806
3807#ifndef USE_ARGV
3808 ga_init2(&ga, (int)sizeof(char*), 20);
3809#endif
3810
3811 if (argvars[0].v_type == VAR_STRING)
3812 {
3813 /* Command is a string. */
3814 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02003815 if (cmd == NULL || *cmd == NUL)
3816 {
3817 EMSG(_(e_invarg));
3818 return job;
3819 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003820#ifdef USE_ARGV
3821 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
3822 return job;
3823 argv[argc] = NULL;
3824#endif
3825 }
3826 else if (argvars[0].v_type != VAR_LIST
3827 || argvars[0].vval.v_list == NULL
3828 || argvars[0].vval.v_list->lv_len < 1)
3829 {
3830 EMSG(_(e_invarg));
3831 return job;
3832 }
3833 else
3834 {
3835 list_T *l = argvars[0].vval.v_list;
3836 listitem_T *li;
3837 char_u *s;
3838
3839#ifdef USE_ARGV
3840 /* Pass argv[] to mch_call_shell(). */
3841 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
3842 if (argv == NULL)
3843 return job;
3844#endif
3845 for (li = l->lv_first; li != NULL; li = li->li_next)
3846 {
3847 s = get_tv_string_chk(&li->li_tv);
3848 if (s == NULL)
3849 goto theend;
3850#ifdef USE_ARGV
3851 argv[argc++] = (char *)s;
3852#else
3853 /* Only escape when needed, double quotes are not always allowed. */
3854 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
3855 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01003856# ifdef WIN32
3857 int old_ssl = p_ssl;
3858
3859 /* This is using CreateProcess, not cmd.exe. Always use
3860 * double quote and backslashes. */
3861 p_ssl = 0;
3862# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003863 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01003864# ifdef WIN32
3865 p_ssl = old_ssl;
3866# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003867 if (s == NULL)
3868 goto theend;
3869 ga_concat(&ga, s);
3870 vim_free(s);
3871 }
3872 else
3873 ga_concat(&ga, s);
3874 if (li->li_next != NULL)
3875 ga_append(&ga, ' ');
3876#endif
3877 }
3878#ifdef USE_ARGV
3879 argv[argc] = NULL;
3880#else
3881 cmd = ga.ga_data;
3882#endif
3883 }
3884
3885#ifdef USE_ARGV
3886 if (ch_log_active())
3887 {
3888 garray_T ga;
3889 int i;
3890
3891 ga_init2(&ga, (int)sizeof(char), 200);
3892 for (i = 0; i < argc; ++i)
3893 {
3894 if (i > 0)
3895 ga_concat(&ga, (char_u *)" ");
3896 ga_concat(&ga, (char_u *)argv[i]);
3897 }
3898 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
3899 ga_clear(&ga);
3900 }
3901 mch_start_job(argv, job, &opt);
3902#else
3903 ch_logs(NULL, "Starting job: %s", (char *)cmd);
3904 mch_start_job((char *)cmd, job, &opt);
3905#endif
3906
3907 /* If the channel is reading from a buffer, write lines now. */
3908 if (job->jv_channel != NULL)
3909 channel_write_in(job->jv_channel);
3910
3911theend:
3912#ifdef USE_ARGV
3913 vim_free(argv);
3914#else
3915 vim_free(ga.ga_data);
3916#endif
3917 return job;
3918}
3919
3920/*
3921 * Get the status of "job" and invoke the exit callback when needed.
3922 * The returned string is not allocated.
3923 */
3924 char *
3925job_status(job_T *job)
3926{
3927 char *result;
3928
3929 if (job->jv_status == JOB_ENDED)
3930 /* No need to check, dead is dead. */
3931 result = "dead";
3932 else if (job->jv_status == JOB_FAILED)
3933 result = "fail";
3934 else
3935 {
3936 result = mch_job_status(job);
3937 if (job->jv_status == JOB_ENDED)
3938 ch_log(job->jv_channel, "Job ended");
3939 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
3940 {
3941 typval_T argv[3];
3942 typval_T rettv;
3943 int dummy;
3944
3945 /* invoke the exit callback; make sure the refcount is > 0 */
3946 ++job->jv_refcount;
3947 argv[0].v_type = VAR_JOB;
3948 argv[0].vval.v_job = job;
3949 argv[1].v_type = VAR_NUMBER;
3950 argv[1].vval.v_number = job->jv_exitval;
3951 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003952 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
3953 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003954 clear_tv(&rettv);
3955 --job->jv_refcount;
3956 }
3957 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
3958 {
3959 /* The job was already unreferenced, now that it ended it can be
3960 * freed. Careful: caller must not use "job" after this! */
3961 job_free(job);
3962 }
3963 }
3964 return result;
3965}
3966
Bram Moolenaar8950a562016-03-12 15:22:55 +01003967/*
3968 * Implementation of job_info().
3969 */
3970 void
3971job_info(job_T *job, dict_T *dict)
3972{
3973 dictitem_T *item;
3974 varnumber_T nr;
3975
3976 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
3977
3978 item = dictitem_alloc((char_u *)"channel");
3979 if (item == NULL)
3980 return;
3981 item->di_tv.v_lock = 0;
3982 item->di_tv.v_type = VAR_CHANNEL;
3983 item->di_tv.vval.v_channel = job->jv_channel;
3984 if (job->jv_channel != NULL)
3985 ++job->jv_channel->ch_refcount;
3986 if (dict_add(dict, item) == FAIL)
3987 dictitem_free(item);
3988
3989#ifdef UNIX
3990 nr = job->jv_pid;
3991#else
3992 nr = job->jv_proc_info.dwProcessId;
3993#endif
3994 dict_add_nr_str(dict, "process", nr, NULL);
3995
3996 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003997 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01003998 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
3999}
4000
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004001 int
4002job_stop(job_T *job, typval_T *argvars)
4003{
4004 char_u *arg;
4005
4006 if (argvars[1].v_type == VAR_UNKNOWN)
4007 arg = (char_u *)"";
4008 else
4009 {
4010 arg = get_tv_string_chk(&argvars[1]);
4011 if (arg == NULL)
4012 {
4013 EMSG(_(e_invarg));
4014 return 0;
4015 }
4016 }
4017 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4018 if (mch_stop_job(job, arg) == FAIL)
4019 return 0;
4020
4021 /* Assume that "hup" does not kill the job. */
4022 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4023 job->jv_channel->ch_job_killed = TRUE;
4024
4025 /* We don't try freeing the job, obviously the caller still has a
4026 * reference to it. */
4027 return 1;
4028}
4029
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004030#endif /* FEAT_JOB_CHANNEL */