blob: 1ebb675d503557c7e8a88505f7474eebd465526a [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 Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
62
Bram Moolenaard8070362016-02-15 21:56:54 +010063#ifdef WIN32
64 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010065fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010066{
67 HANDLE h = (HANDLE)fd;
68 DWORD nread;
69
70 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
71 return -1;
72 return (int)nread;
73}
74
75 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010076fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010077{
78 HANDLE h = (HANDLE)fd;
79 DWORD nwrite;
80
81 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
82 return -1;
83 return (int)nwrite;
84}
85
86 static void
87fd_close(sock_T fd)
88{
89 HANDLE h = (HANDLE)fd;
90
91 CloseHandle(h);
92}
93#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010094
Bram Moolenaar6463ca22016-02-13 17:04:46 +010095/* Log file opened with ch_logfile(). */
96static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010097#ifdef FEAT_RELTIME
98static proftime_T log_start;
99#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100
101 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100103{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100104 FILE *file = NULL;
105
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100106 if (log_fd != NULL)
107 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100108
109 if (*fname != NUL)
110 {
111 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
112 if (file == NULL)
113 {
114 EMSG2(_(e_notopen), fname);
115 return;
116 }
117 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100119
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100122 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100123#ifdef FEAT_RELTIME
124 profile_start(&log_start);
125#endif
126 }
127}
128
129 int
130ch_log_active()
131{
132 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100133}
134
135 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100136ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137{
138 if (log_fd != NULL)
139 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100140#ifdef FEAT_RELTIME
141 proftime_T log_now;
142
143 profile_start(&log_now);
144 profile_sub(&log_now, &log_start);
145 fprintf(log_fd, "%s ", profile_msg(&log_now));
146#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100147 if (ch != NULL)
148 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100149 else
150 fprintf(log_fd, "%s: ", what);
151 }
152}
153
Bram Moolenaard0b65022016-03-06 21:50:33 +0100154static int did_log_msg = TRUE;
155
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100156 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100157ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100158{
159 if (log_fd != NULL)
160 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100165 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100166 }
167}
168
169 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100170ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171{
172 if (log_fd != NULL)
173 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100174 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100176 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100178 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100179 }
180}
181
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100182 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100183ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184{
185 if (log_fd != NULL)
186 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100187 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
194
195 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100196ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100200 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100202 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100204 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100205 }
206}
207
208 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100209ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210{
211 if (log_fd != NULL)
212 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100213 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100215 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100217 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100218 }
219}
220
221 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100222ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223{
224 if (log_fd != NULL)
225 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100226 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100228 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100230 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100231 }
232}
233
234 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100235ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236{
237 if (log_fd != NULL)
238 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100239 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100241 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100243 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100244 }
245}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100246
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100247#ifdef _WIN32
248# undef PERROR
249# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
250 (char_u *)msg, (char_u *)strerror_win32(errno))
251
252 static char *
253strerror_win32(int eno)
254{
255 static LPVOID msgbuf = NULL;
256 char_u *ptr;
257
258 if (msgbuf)
259 LocalFree(msgbuf);
260 FormatMessage(
261 FORMAT_MESSAGE_ALLOCATE_BUFFER |
262 FORMAT_MESSAGE_FROM_SYSTEM |
263 FORMAT_MESSAGE_IGNORE_INSERTS,
264 NULL,
265 eno,
266 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
267 (LPTSTR) &msgbuf,
268 0,
269 NULL);
270 /* chomp \r or \n */
271 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
272 switch (*ptr)
273 {
274 case '\r':
275 STRMOVE(ptr, ptr + 1);
276 ptr--;
277 break;
278 case '\n':
279 if (*(ptr + 1) == '\0')
280 *ptr = '\0';
281 else
282 *ptr = ' ';
283 break;
284 }
285 return msgbuf;
286}
287#endif
288
Bram Moolenaar77073442016-02-13 23:23:53 +0100289/*
290 * The list of all allocated channels.
291 */
292static channel_T *first_channel = NULL;
293static int next_ch_id = 0;
294
295/*
296 * Allocate a new channel. The refcount is set to 1.
297 * The channel isn't actually used until it is opened.
298 * Returns NULL if out of memory.
299 */
300 channel_T *
301add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100302{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100303 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100304 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100305
Bram Moolenaar77073442016-02-13 23:23:53 +0100306 if (channel == NULL)
307 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308
Bram Moolenaar77073442016-02-13 23:23:53 +0100309 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100310 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100311
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100316 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317#endif
318#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100323
Bram Moolenaar77073442016-02-13 23:23:53 +0100324 if (first_channel != NULL)
325 {
326 first_channel->ch_prev = channel;
327 channel->ch_next = first_channel;
328 }
329 first_channel = channel;
330
331 channel->ch_refcount = 1;
332 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100334
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100335/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100336 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100337 * Return TRUE if "channel" has a callback and the associated job wasn't
338 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100339 */
340 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100341channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100342{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344 int has_out_msg;
345 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100346
347 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100348 if (channel->ch_job_killed && channel->ch_job == NULL)
349 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100350
351 /* If there is a close callback it may still need to be invoked. */
352 if (channel->ch_close_cb != NULL)
353 return TRUE;
354
355 /* If there is no callback then nobody can get readahead. If the fd is
356 * closed and there is no readahead then the callback won't be called. */
357 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
358 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
359 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100360 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
361 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
362 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
363 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
364 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
365 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100369 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100370}
371
372/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 * Close a channel and free all its resources.
374 */
375 static void
376channel_free_contents(channel_T *channel)
377{
378 channel_close(channel, TRUE);
379 channel_clear(channel);
380 ch_log(channel, "Freeing channel");
381}
382
383 static void
384channel_free_channel(channel_T *channel)
385{
386 if (channel->ch_next != NULL)
387 channel->ch_next->ch_prev = channel->ch_prev;
388 if (channel->ch_prev == NULL)
389 first_channel = channel->ch_next;
390 else
391 channel->ch_prev->ch_next = channel->ch_next;
392 vim_free(channel);
393}
394
395 static void
396channel_free(channel_T *channel)
397{
398 if (!in_free_unref_items)
399 {
400 channel_free_contents(channel);
401 channel_free_channel(channel);
402 }
403}
404
405/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100406 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100407 * possible, there is no callback to be invoked or the associated job was
408 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100409 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100411 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412channel_may_free(channel_T *channel)
413{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100414 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100417 return TRUE;
418 }
419 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100420}
421
422/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100423 * Decrement the reference count on "channel" and maybe free it when it goes
424 * down to zero. Don't free it if there is a pending action.
425 * Returns TRUE when the channel is no longer referenced.
426 */
427 int
428channel_unref(channel_T *channel)
429{
430 if (channel != NULL && --channel->ch_refcount <= 0)
431 return channel_may_free(channel);
432 return FALSE;
433}
434
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200435 int
436free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100437{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200438 int did_free = FALSE;
439 channel_T *ch;
440
441 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
442 if ((ch->ch_copyID & mask) != (copyID & mask))
443 {
444 /* Free the channel and ordinary items it contains, but don't
445 * recurse into Lists, Dictionaries etc. */
446 channel_free_contents(ch);
447 did_free = TRUE;
448 }
449 return did_free;
450}
451
452 void
453free_unused_channels(int copyID, int mask)
454{
455 channel_T *ch;
456 channel_T *ch_next;
457
458 for (ch = first_channel; ch != NULL; ch = ch_next)
459 {
460 ch_next = ch->ch_next;
461 if ((ch->ch_copyID & mask) != (copyID & mask))
462 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200463 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 channel_free_channel(ch);
465 }
466 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467}
468
Bram Moolenaard04a0202016-01-26 23:30:18 +0100469#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100470
471#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
472 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100473channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100474{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100476 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100477
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100478 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100479 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100480 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100481 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100482 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100483}
484#endif
485
Bram Moolenaare0874f82016-01-24 20:36:41 +0100486/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100487 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100488 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100489#ifdef FEAT_GUI_X11
490 static void
491messageFromNetbeans(XtPointer clientData,
492 int *unused1 UNUSED,
493 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100495 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100497#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100500# if GTK_CHECK_VERSION(3,0,0)
501 static gboolean
502messageFromNetbeans(GIOChannel *unused1 UNUSED,
503 GIOCondition unused2 UNUSED,
504 gpointer clientData)
505{
506 channel_read_fd(GPOINTER_TO_INT(clientData));
507 return TRUE; /* Return FALSE instead in case the event source is to
508 * be removed after this function returns. */
509}
510# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511 static void
512messageFromNetbeans(gpointer clientData,
513 gint unused1 UNUSED,
514 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517}
Bram Moolenaar98921892016-02-23 17:14:37 +0100518# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519#endif
520
521 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100522channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100523{
Bram Moolenaarde279892016-03-11 22:19:44 +0100524 if (!CH_HAS_GUI)
525 return;
526
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100527# ifdef FEAT_GUI_X11
528 /* Tell notifier we are interested in being called
529 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100530 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
531 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100532 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100533 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100534 (XtPointer)(XtInputReadMask + XtInputExceptMask),
535 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100536 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100537# else
538# ifdef FEAT_GUI_GTK
539 /* Tell gdk we are interested in being called when there
540 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100541 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100542# if GTK_CHECK_VERSION(3,0,0)
543 {
544 GIOChannel *chnnl = g_io_channel_unix_new(
545 (gint)channel->ch_part[part].ch_fd);
546
547 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
548 chnnl,
549 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
550 messageFromNetbeans,
551 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
552
553 g_io_channel_unref(chnnl);
554 }
555# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100556 channel->ch_part[part].ch_inputHandler = gdk_input_add(
557 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100558 (GdkInputCondition)
559 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100561 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100562# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# endif
564# endif
565}
566
Bram Moolenaarde279892016-03-11 22:19:44 +0100567 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100568channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100569{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 if (channel->CH_SOCK_FD != INVALID_FD)
571 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100572 if (channel->CH_OUT_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_OUT);
574 if (channel->CH_ERR_FD != INVALID_FD)
575 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100576}
577
578/*
579 * Register any of our file descriptors with the GUI event handling system.
580 * Called when the GUI has started.
581 */
582 void
583channel_gui_register_all(void)
584{
Bram Moolenaar77073442016-02-13 23:23:53 +0100585 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100586
Bram Moolenaar77073442016-02-13 23:23:53 +0100587 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100588 channel_gui_register(channel);
589}
590
591 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100592channel_gui_unregister_one(channel_T *channel, int part)
593{
594# ifdef FEAT_GUI_X11
595 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
596 {
597 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
598 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
599 }
600# else
601# ifdef FEAT_GUI_GTK
602 if (channel->ch_part[part].ch_inputHandler != 0)
603 {
604# if GTK_CHECK_VERSION(3,0,0)
605 g_source_remove(channel->ch_part[part].ch_inputHandler);
606# else
607 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
608# endif
609 channel->ch_part[part].ch_inputHandler = 0;
610 }
611# endif
612# endif
613}
614
615 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100616channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100617{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100619
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100620 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100621 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622}
623
624#endif
625
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100626static char *e_cannot_connect = N_("E902: Cannot connect to port");
627
Bram Moolenaard04a0202016-01-26 23:30:18 +0100628/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100629 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100630 * "waittime" is the time in msec to wait for the connection.
631 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100632 * Returns the channel for success.
633 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100634 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100635 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100636channel_open(
637 char *hostname,
638 int port_in,
639 int waittime,
640 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100642 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100643 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100645#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100647 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648#else
649 int port = port_in;
650#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100651 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100652 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100654#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655 channel_init_winsock();
656#endif
657
Bram Moolenaar77073442016-02-13 23:23:53 +0100658 channel = add_channel();
659 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100661 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100662 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100663 }
664
665 /* Get the server internet address and put into addr structure */
666 /* fill in the socket address structure and connect to server */
667 vim_memset((char *)&server, 0, sizeof(server));
668 server.sin_family = AF_INET;
669 server.sin_port = htons(port);
670 if ((host = gethostbyname(hostname)) == NULL)
671 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100672 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100673 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100674 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100675 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100676 }
677 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
678
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100679 /* On Mac and Solaris a zero timeout almost never works. At least wait
680 * one millisecond. Let's do it for all systems, because we don't know why
681 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 if (waittime == 0)
683 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684
685 /*
686 * For Unix we need to call connect() again after connect() failed.
687 * On Win32 one time is sufficient.
688 */
689 while (TRUE)
690 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100691 long elapsed_msec = 0;
692 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100693
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100694 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 sd = socket(AF_INET, SOCK_STREAM, 0);
697 if (sd == -1)
698 {
699 ch_error(channel, "in socket() in channel_open().");
700 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100701 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100702 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100703 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100704
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100705 if (waittime >= 0)
706 {
707 /* Make connect() non-blocking. */
708 if (
709#ifdef _WIN32
710 ioctlsocket(sd, FIONBIO, &val) < 0
711#else
712 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
713#endif
714 )
715 {
716 SOCK_ERRNO;
717 ch_errorn(channel,
718 "channel_open: Connect failed with errno %d", errno);
719 sock_close(sd);
720 channel_free(channel);
721 return NULL;
722 }
723 }
724
725 /* Try connecting to the server. */
726 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
727 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
728
Bram Moolenaar045a2842016-03-08 22:33:07 +0100729 if (ret == 0)
730 /* The connection could be established. */
731 break;
732
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100733 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100734 if (waittime < 0 || (errno != EWOULDBLOCK
735 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100736#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100737 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100738#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 ))
740 {
741 ch_errorn(channel,
742 "channel_open: Connect failed with errno %d", errno);
743 PERROR(_(e_cannot_connect));
744 sock_close(sd);
745 channel_free(channel);
746 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100747 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100748
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100749 /* Limit the waittime to 50 msec. If it doesn't work within this
750 * time we close the socket and try creating it again. */
751 waitnow = waittime > 50 ? 50 : waittime;
752
Bram Moolenaar045a2842016-03-08 22:33:07 +0100753 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100754 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755#ifndef WIN32
756 if (errno != ECONNREFUSED)
757#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100758 {
759 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100760 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100762#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100763 int so_error = 0;
764 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 struct timeval start_tv;
766 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100767#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768 FD_ZERO(&rfds);
769 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 FD_ZERO(&wfds);
771 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100772
Bram Moolenaar562ca712016-03-09 21:50:05 +0100773 tv.tv_sec = waitnow / 1000;
774 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100775#ifndef WIN32
776 gettimeofday(&start_tv, NULL);
777#endif
778 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100779 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100780 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100781
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100782 if (ret < 0)
783 {
784 SOCK_ERRNO;
785 ch_errorn(channel,
786 "channel_open: Connect failed with errno %d", errno);
787 PERROR(_(e_cannot_connect));
788 sock_close(sd);
789 channel_free(channel);
790 return NULL;
791 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100792
Bram Moolenaare081e212016-02-28 22:33:46 +0100793#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100794 /* On Win32: select() is expected to work and wait for up to
795 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 if (FD_ISSET(sd, &wfds))
797 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100798 elapsed_msec = waitnow;
799 if (waittime > 1 && elapsed_msec < waittime)
800 {
801 waittime -= elapsed_msec;
802 continue;
803 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100804#else
805 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 * After putting the socket in non-blocking mode, connect() will
807 * return EINPROGRESS, select() will not wait (as if writing is
808 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100809 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100810 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100811 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100812 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 {
814 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100815 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100816 if (ret < 0 || (so_error != 0
817 && so_error != EWOULDBLOCK
818 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100819# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100820 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100821# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822 ))
823 {
824 ch_errorn(channel,
825 "channel_open: Connect failed with errno %d",
826 so_error);
827 PERROR(_(e_cannot_connect));
828 sock_close(sd);
829 channel_free(channel);
830 return NULL;
831 }
832 }
833
Bram Moolenaar045a2842016-03-08 22:33:07 +0100834 if (FD_ISSET(sd, &wfds) && so_error == 0)
835 /* Did not detect an error, connection is established. */
836 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100837
Bram Moolenaar045a2842016-03-08 22:33:07 +0100838 gettimeofday(&end_tv, NULL);
839 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
840 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100841#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100842 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100843
844#ifndef WIN32
845 if (waittime > 1 && elapsed_msec < waittime)
846 {
847 /* The port isn't ready but we also didn't get an error.
848 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100849 * yet. Select() may return early, wait until the remaining
850 * "waitnow" and try again. */
851 waitnow -= elapsed_msec;
852 waittime -= elapsed_msec;
853 if (waitnow > 0)
854 {
855 mch_delay((long)waitnow, TRUE);
856 ui_breakcheck();
857 waittime -= waitnow;
858 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100859 if (!got_int)
860 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100861 if (waittime <= 0)
862 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100863 waittime = 1;
864 continue;
865 }
866 /* we were interrupted, behave as if timed out */
867 }
868#endif
869
870 /* We timed out. */
871 ch_error(channel, "Connection timed out");
872 sock_close(sd);
873 channel_free(channel);
874 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100875 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100876
Bram Moolenaar045a2842016-03-08 22:33:07 +0100877 ch_log(channel, "Connection made");
878
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100879 if (waittime >= 0)
880 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100881#ifdef _WIN32
882 val = 0;
883 ioctlsocket(sd, FIONBIO, &val);
884#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100885 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100886#endif
887 }
888
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100889 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100890 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100891 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
892 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100893
894#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100895 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100896#endif
897
Bram Moolenaar77073442016-02-13 23:23:53 +0100898 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100899}
900
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100901/*
902 * Implements ch_open().
903 */
904 channel_T *
905channel_open_func(typval_T *argvars)
906{
907 char_u *address;
908 char_u *p;
909 char *rest;
910 int port;
911 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200912 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100913
914 address = get_tv_string(&argvars[0]);
915 if (argvars[1].v_type != VAR_UNKNOWN
916 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
917 {
918 EMSG(_(e_invarg));
919 return NULL;
920 }
921
922 /* parse address */
923 p = vim_strchr(address, ':');
924 if (p == NULL)
925 {
926 EMSG2(_(e_invarg2), address);
927 return NULL;
928 }
929 *p++ = NUL;
930 port = strtol((char *)p, &rest, 10);
931 if (*address == NUL || port <= 0 || *rest != NUL)
932 {
933 p[-1] = ':';
934 EMSG2(_(e_invarg2), address);
935 return NULL;
936 }
937
938 /* parse options */
939 clear_job_options(&opt);
940 opt.jo_mode = MODE_JSON;
941 opt.jo_timeout = 2000;
942 if (get_job_options(&argvars[1], &opt,
943 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200944 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945 if (opt.jo_timeout < 0)
946 {
947 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200948 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100949 }
950
951 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
952 if (channel != NULL)
953 {
954 opt.jo_set = JO_ALL;
955 channel_set_options(channel, &opt);
956 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200957theend:
958 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100959 return channel;
960}
961
Bram Moolenaarde279892016-03-11 22:19:44 +0100962 static void
963may_close_part(sock_T *fd)
964{
965 if (*fd != INVALID_FD)
966 {
967 fd_close(*fd);
968 *fd = INVALID_FD;
969 }
970}
971
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100972 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100973channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100974{
Bram Moolenaarde279892016-03-11 22:19:44 +0100975 if (in != INVALID_FD)
976 {
977 may_close_part(&channel->CH_IN_FD);
978 channel->CH_IN_FD = in;
979 }
980 if (out != INVALID_FD)
981 {
982# if defined(FEAT_GUI)
983 channel_gui_unregister_one(channel, PART_OUT);
984# endif
985 may_close_part(&channel->CH_OUT_FD);
986 channel->CH_OUT_FD = out;
987# if defined(FEAT_GUI)
988 channel_gui_register_one(channel, PART_OUT);
989# endif
990 }
991 if (err != INVALID_FD)
992 {
993# if defined(FEAT_GUI)
994 channel_gui_unregister_one(channel, PART_ERR);
995# endif
996 may_close_part(&channel->CH_ERR_FD);
997 channel->CH_ERR_FD = err;
998# if defined(FEAT_GUI)
999 channel_gui_register_one(channel, PART_ERR);
1000# endif
1001 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001002}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001003
Bram Moolenaard6051b52016-02-28 15:49:03 +01001004/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001005 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001006 * This does not keep a refcount, when the job is freed ch_job is cleared.
1007 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001008 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001009channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010{
Bram Moolenaar77073442016-02-13 23:23:53 +01001011 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001012
1013 channel_set_options(channel, options);
1014
1015 if (job->jv_in_buf != NULL)
1016 {
1017 chanpart_T *in_part = &channel->ch_part[PART_IN];
1018
1019 in_part->ch_buffer = job->jv_in_buf;
1020 ch_logs(channel, "reading from buffer '%s'",
1021 (char *)in_part->ch_buffer->b_ffname);
1022 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001023 {
1024 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1025 {
1026 /* Special mode: send last-but-one line when appending a line
1027 * to the buffer. */
1028 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001029 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001030 in_part->ch_buf_top =
1031 in_part->ch_buffer->b_ml.ml_line_count + 1;
1032 }
1033 else
1034 in_part->ch_buf_top = options->jo_in_top;
1035 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001036 else
1037 in_part->ch_buf_top = 1;
1038 if (options->jo_set & JO_IN_BOT)
1039 in_part->ch_buf_bot = options->jo_in_bot;
1040 else
1041 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1042 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001043}
1044
1045/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001046 * Find a buffer matching "name" or create a new one.
1047 */
1048 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001049find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001050{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001051 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001052 buf_T *save_curbuf = curbuf;
1053
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001054 if (name != NULL && *name != NUL)
1055 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001056 if (buf == NULL)
1057 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001058 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001059 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001060 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001061 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001062#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001063 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1064 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001065#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001066 if (curbuf->b_ml.ml_mfp == NULL)
1067 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001068 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1069 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001070 changed_bytes(1, 0);
1071 curbuf = save_curbuf;
1072 }
1073
1074 return buf;
1075}
1076
1077/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001078 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001079 */
1080 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001081channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001082{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001083 int part;
1084 char_u **cbp;
1085 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001086
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001087 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001088 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001089 channel->ch_part[part].ch_mode = opt->jo_mode;
1090 if (opt->jo_set & JO_IN_MODE)
1091 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1092 if (opt->jo_set & JO_OUT_MODE)
1093 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1094 if (opt->jo_set & JO_ERR_MODE)
1095 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001096
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001097 if (opt->jo_set & JO_TIMEOUT)
1098 for (part = PART_SOCK; part <= PART_IN; ++part)
1099 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1100 if (opt->jo_set & JO_OUT_TIMEOUT)
1101 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1102 if (opt->jo_set & JO_ERR_TIMEOUT)
1103 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001104 if (opt->jo_set & JO_BLOCK_WRITE)
1105 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001106
1107 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001108 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001109 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001110 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001113 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1114 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001115 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001116 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001117 *pp = opt->jo_partial;
1118 if (*pp != NULL)
1119 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001120 }
1121 if (opt->jo_set & JO_OUT_CALLBACK)
1122 {
1123 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001124 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001125 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001126 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001127 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1128 *cbp = vim_strsave(opt->jo_out_cb);
1129 else
1130 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001131 *pp = opt->jo_out_partial;
1132 if (*pp != NULL)
1133 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001134 }
1135 if (opt->jo_set & JO_ERR_CALLBACK)
1136 {
1137 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001138 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001140 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001141 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1142 *cbp = vim_strsave(opt->jo_err_cb);
1143 else
1144 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001145 *pp = opt->jo_err_partial;
1146 if (*pp != NULL)
1147 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001148 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001149 if (opt->jo_set & JO_CLOSE_CALLBACK)
1150 {
1151 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001152 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001153 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001155 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1156 *cbp = vim_strsave(opt->jo_close_cb);
1157 else
1158 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001159 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 if (*pp != NULL)
1161 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001162 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001163
1164 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1165 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001166 /* writing output to a buffer. Default mode is NL. */
1167 if (!(opt->jo_set & JO_OUT_MODE))
1168 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001169 if (opt->jo_set & JO_OUT_BUF)
1170 channel->ch_part[PART_OUT].ch_buffer =
1171 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1172 else
1173 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001174 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1175 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001176 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1177 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001178
1179 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1180 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1181 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1182 {
1183 /* writing err to a buffer. Default mode is NL. */
1184 if (!(opt->jo_set & JO_ERR_MODE))
1185 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1186 if (opt->jo_io[PART_ERR] == JIO_OUT)
1187 channel->ch_part[PART_ERR].ch_buffer =
1188 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001189 else if (opt->jo_set & JO_ERR_BUF)
1190 channel->ch_part[PART_ERR].ch_buffer =
1191 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001192 else
1193 channel->ch_part[PART_ERR].ch_buffer =
1194 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1195 ch_logs(channel, "writing err to buffer '%s'",
1196 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1197 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001198
1199 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1200 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1201 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001202}
1203
1204/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001205 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001206 */
1207 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001208channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001209 channel_T *channel,
1210 int part,
1211 char_u *callback,
1212 partial_T *partial,
1213 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001214{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001215 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001216 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1217
1218 if (item != NULL)
1219 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001220 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001221 item->cq_partial = partial;
1222 if (partial != NULL)
1223 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001224 item->cq_seq_nr = id;
1225 item->cq_prev = head->cq_prev;
1226 head->cq_prev = item;
1227 item->cq_next = NULL;
1228 if (item->cq_prev == NULL)
1229 head->cq_next = item;
1230 else
1231 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001232 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001233}
1234
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001235 static void
1236write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1237{
1238 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001239 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001240 char_u *p;
1241
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001242 if ((p = alloc(len + 2)) == NULL)
1243 return;
1244 STRCPY(p, line);
1245 p[len] = NL;
1246 p[len + 1] = NUL;
1247 channel_send(channel, PART_IN, p, "write_buf_line()");
1248 vim_free(p);
1249}
1250
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001251/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001252 * Return TRUE if "channel" can be written to.
1253 * Returns FALSE if the input is closed or the write would block.
1254 */
1255 static int
1256can_write_buf_line(channel_T *channel)
1257{
1258 chanpart_T *in_part = &channel->ch_part[PART_IN];
1259
1260 if (in_part->ch_fd == INVALID_FD)
1261 return FALSE; /* pipe was closed */
1262
1263 /* for testing: block every other attempt to write */
1264 if (in_part->ch_block_write == 1)
1265 in_part->ch_block_write = -1;
1266 else if (in_part->ch_block_write == -1)
1267 in_part->ch_block_write = 1;
1268
1269 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1270#ifndef WIN32
1271 {
1272# if defined(HAVE_SELECT)
1273 struct timeval tval;
1274 fd_set wfds;
1275 int ret;
1276
1277 FD_ZERO(&wfds);
1278 FD_SET((int)in_part->ch_fd, &wfds);
1279 tval.tv_sec = 0;
1280 tval.tv_usec = 0;
1281 for (;;)
1282 {
1283 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1284# ifdef EINTR
1285 SOCK_ERRNO;
1286 if (ret == -1 && errno == EINTR)
1287 continue;
1288# endif
1289 if (ret <= 0 || in_part->ch_block_write == 1)
1290 {
1291 if (ret > 0)
1292 ch_log(channel, "FAKED Input not ready for writing");
1293 else
1294 ch_log(channel, "Input not ready for writing");
1295 return FALSE;
1296 }
1297 break;
1298 }
1299# else
1300 struct pollfd fds;
1301
1302 fds.fd = in_part->ch_fd;
1303 fds.events = POLLOUT;
1304 if (poll(&fds, 1, 0) <= 0)
1305 {
1306 ch_log(channel, "Input not ready for writing");
1307 return FALSE;
1308 }
1309 if (in_part->ch_block_write == 1)
1310 {
1311 ch_log(channel, "FAKED Input not ready for writing");
1312 return FALSE;
1313 }
1314# endif
1315 }
1316#endif
1317 return TRUE;
1318}
1319
1320/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001321 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001322 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001323 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001324channel_write_in(channel_T *channel)
1325{
1326 chanpart_T *in_part = &channel->ch_part[PART_IN];
1327 linenr_T lnum;
1328 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001329 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001330
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001331 if (buf == NULL || in_part->ch_buf_append)
1332 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001333 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1334 {
1335 /* buffer was wiped out or unloaded */
1336 in_part->ch_buffer = NULL;
1337 return;
1338 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001339
1340 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1341 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1342 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001343 if (!can_write_buf_line(channel))
1344 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001345 write_buf_line(buf, lnum, channel);
1346 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001347 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001348
1349 if (written == 1)
1350 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1351 else if (written > 1)
1352 ch_logn(channel, "written %d lines to channel", written);
1353
Bram Moolenaar014069a2016-03-03 22:51:40 +01001354 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001355 if (lnum > buf->b_ml.ml_line_count)
1356 {
1357 /* Writing is done, no longer need the buffer. */
1358 in_part->ch_buffer = NULL;
1359 ch_log(channel, "Finished writing all lines to channel");
1360 }
1361 else
1362 ch_logn(channel, "Still %d more lines to write",
1363 buf->b_ml.ml_line_count - lnum + 1);
1364}
1365
1366/*
1367 * Write any lines waiting to be written to a channel.
1368 */
1369 void
1370channel_write_any_lines()
1371{
1372 channel_T *channel;
1373
1374 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1375 {
1376 chanpart_T *in_part = &channel->ch_part[PART_IN];
1377
1378 if (in_part->ch_buffer != NULL)
1379 {
1380 if (in_part->ch_buf_append)
1381 channel_write_new_lines(in_part->ch_buffer);
1382 else
1383 channel_write_in(channel);
1384 }
1385 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001386}
1387
1388/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001389 * Write appended lines above the last one in "buf" to the channel.
1390 */
1391 void
1392channel_write_new_lines(buf_T *buf)
1393{
1394 channel_T *channel;
1395 int found_one = FALSE;
1396
1397 /* There could be more than one channel for the buffer, loop over all of
1398 * them. */
1399 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1400 {
1401 chanpart_T *in_part = &channel->ch_part[PART_IN];
1402 linenr_T lnum;
1403 int written = 0;
1404
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001405 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001406 {
1407 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001408 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001409 found_one = TRUE;
1410 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1411 ++lnum)
1412 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001413 if (!can_write_buf_line(channel))
1414 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001415 write_buf_line(buf, lnum, channel);
1416 ++written;
1417 }
1418
1419 if (written == 1)
1420 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1421 else if (written > 1)
1422 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001423 if (lnum < buf->b_ml.ml_line_count)
1424 ch_logn(channel, "Still %d more lines to write",
1425 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001426
1427 in_part->ch_buf_bot = lnum;
1428 }
1429 }
1430 if (!found_one)
1431 buf->b_write_to_channel = FALSE;
1432}
1433
1434/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001435 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001436 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001437 */
1438 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001439invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1440 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001441{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001442 typval_T rettv;
1443 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001444
Bram Moolenaar77073442016-02-13 23:23:53 +01001445 argv[0].v_type = VAR_CHANNEL;
1446 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001447
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001448 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001449 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001450 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001451 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001452}
1453
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001454/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001455 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001456 * The caller must free it.
1457 * Returns NULL if there is nothing.
1458 */
1459 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001460channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001461{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001462 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001463 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001464 char_u *p;
1465
Bram Moolenaar77073442016-02-13 23:23:53 +01001466 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001467 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001468 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001469 p = node->rq_buffer;
1470 head->rq_next = node->rq_next;
1471 if (node->rq_next == NULL)
1472 head->rq_prev = NULL;
1473 else
1474 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001475 vim_free(node);
1476 return p;
1477}
1478
1479/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001480 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001481 */
1482 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001483channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001484{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001485 readq_T *head = &channel->ch_part[part].ch_head;
1486 readq_T *node = head->rq_next;
1487 long_u len = 1;
1488 char_u *res;
1489 char_u *p;
1490
1491 /* If there is only one buffer just get that one. */
1492 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1493 return channel_get(channel, part);
1494
1495 /* Concatenate everything into one buffer. */
1496 for (node = head->rq_next; node != NULL; node = node->rq_next)
1497 len += (long_u)STRLEN(node->rq_buffer);
1498 res = lalloc(len, TRUE);
1499 if (res == NULL)
1500 return NULL;
1501 *res = NUL;
1502 for (node = head->rq_next; node != NULL; node = node->rq_next)
1503 STRCAT(res, node->rq_buffer);
1504
1505 /* Free all buffers */
1506 do
1507 {
1508 p = channel_get(channel, part);
1509 vim_free(p);
1510 } while (p != NULL);
1511
1512 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001513}
1514
1515/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001516 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001517 * Returns FAIL if that is not possible.
1518 */
1519 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001520channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001521{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001522 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001523 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001524 char_u *p;
1525
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001527 return FAIL;
1528
Bram Moolenaar77073442016-02-13 23:23:53 +01001529 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1530 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001531 if (p == NULL)
1532 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001533 STRCPY(p, node->rq_buffer);
1534 STRCAT(p, node->rq_next->rq_buffer);
1535 vim_free(node->rq_next->rq_buffer);
1536 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001537
Bram Moolenaar77073442016-02-13 23:23:53 +01001538 /* dispose of the node and its buffer */
1539 head->rq_next = node->rq_next;
1540 head->rq_next->rq_prev = NULL;
1541 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001542 vim_free(node);
1543 return OK;
1544}
1545
1546/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001547 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001548 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 * Returns OK or FAIL.
1550 */
1551 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001552channel_save(channel_T *channel, int part, char_u *buf, int len,
1553 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001554{
1555 readq_T *node;
1556 readq_T *head = &channel->ch_part[part].ch_head;
1557 char_u *p;
1558 int i;
1559
1560 node = (readq_T *)alloc(sizeof(readq_T));
1561 if (node == NULL)
1562 return FAIL; /* out of memory */
1563 node->rq_buffer = alloc(len + 1);
1564 if (node->rq_buffer == NULL)
1565 {
1566 vim_free(node);
1567 return FAIL; /* out of memory */
1568 }
1569
1570 if (channel->ch_part[part].ch_mode == MODE_NL)
1571 {
1572 /* Drop any CR before a NL. */
1573 p = node->rq_buffer;
1574 for (i = 0; i < len; ++i)
1575 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1576 *p++ = buf[i];
1577 *p = NUL;
1578 }
1579 else
1580 {
1581 mch_memmove(node->rq_buffer, buf, len);
1582 node->rq_buffer[len] = NUL;
1583 }
1584
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001585 if (prepend)
1586 {
1587 /* preend node to the head of the queue */
1588 node->rq_next = head->rq_next;
1589 node->rq_prev = NULL;
1590 if (head->rq_next == NULL)
1591 head->rq_prev = node;
1592 else
1593 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001595 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001597 {
1598 /* append node to the tail of the queue */
1599 node->rq_next = NULL;
1600 node->rq_prev = head->rq_prev;
1601 if (head->rq_prev == NULL)
1602 head->rq_next = node;
1603 else
1604 head->rq_prev->rq_next = node;
1605 head->rq_prev = node;
1606 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001608 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001610 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 fprintf(log_fd, "'");
1612 if (fwrite(buf, len, 1, log_fd) != 1)
1613 return FAIL;
1614 fprintf(log_fd, "'\n");
1615 }
1616 return OK;
1617}
1618
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001619 static int
1620channel_fill(js_read_T *reader)
1621{
1622 channel_T *channel = (channel_T *)reader->js_cookie;
1623 int part = reader->js_cookie_arg;
1624 char_u *next = channel_get(channel, part);
1625 int unused;
1626 int len;
1627 char_u *p;
1628
1629 if (next == NULL)
1630 return FALSE;
1631
1632 unused = reader->js_end - reader->js_buf - reader->js_used;
1633 if (unused > 0)
1634 {
1635 /* Prepend unused text. */
1636 len = (int)STRLEN(next);
1637 p = alloc(unused + len + 1);
1638 if (p == NULL)
1639 {
1640 vim_free(next);
1641 return FALSE;
1642 }
1643 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1644 mch_memmove(p + unused, next, len + 1);
1645 vim_free(next);
1646 next = p;
1647 }
1648
1649 vim_free(reader->js_buf);
1650 reader->js_buf = next;
1651 reader->js_used = 0;
1652 return TRUE;
1653}
1654
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001655/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001656 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001657 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001658 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001660 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001661channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001662{
1663 js_read_T reader;
1664 typval_T listtv;
1665 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001666 chanpart_T *chanpart = &channel->ch_part[part];
1667 jsonq_T *head = &chanpart->ch_json_head;
1668 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001669 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001671 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001672 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001674 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001677 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001678 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001679
1680 /* When a message is incomplete we wait for a short while for more to
1681 * arrive. After the delay drop the input, otherwise a truncated string
1682 * or list will make us hang. */
1683 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001684 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001685 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001687 /* Only accept the response when it is a list with at least two
1688 * items. */
1689 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001690 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001691 if (listtv.v_type != VAR_LIST)
1692 ch_error(channel, "Did not receive a list, discarding");
1693 else
1694 ch_errorn(channel, "Expected list with two items, got %d",
1695 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001697 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 else
1699 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001700 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1701 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 else
1704 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001705 item->jq_value = alloc_tv();
1706 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001707 {
1708 vim_free(item);
1709 clear_tv(&listtv);
1710 }
1711 else
1712 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001713 *item->jq_value = listtv;
1714 item->jq_prev = head->jq_prev;
1715 head->jq_prev = item;
1716 item->jq_next = NULL;
1717 if (item->jq_prev == NULL)
1718 head->jq_next = item;
1719 else
1720 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001721 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001722 }
1723 }
1724 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001725
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001726 if (status == OK)
1727 chanpart->ch_waiting = FALSE;
1728 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001729 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001730 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001731 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001732 /* First time encountering incomplete message, set a deadline of
1733 * 100 msec. */
1734 ch_log(channel, "Incomplete message - wait for more");
1735 reader.js_used = 0;
1736 chanpart->ch_waiting = TRUE;
1737#ifdef WIN32
1738 chanpart->ch_deadline = GetTickCount() + 100L;
1739#else
1740 gettimeofday(&chanpart->ch_deadline, NULL);
1741 chanpart->ch_deadline.tv_usec += 100 * 1000;
1742 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1743 {
1744 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1745 ++chanpart->ch_deadline.tv_sec;
1746 }
1747#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001748 }
1749 else
1750 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001751 int timeout;
1752#ifdef WIN32
1753 timeout = GetTickCount() > chanpart->ch_deadline;
1754#else
1755 {
1756 struct timeval now_tv;
1757
1758 gettimeofday(&now_tv, NULL);
1759 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1760 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1761 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1762 }
1763#endif
1764 if (timeout)
1765 {
1766 status = FAIL;
1767 chanpart->ch_waiting = FALSE;
1768 }
1769 else
1770 {
1771 reader.js_used = 0;
1772 ch_log(channel, "still waiting on incomplete message");
1773 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001774 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001775 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001776
1777 if (status == FAIL)
1778 {
1779 ch_error(channel, "Decoding failed - discarding input");
1780 ret = FALSE;
1781 chanpart->ch_waiting = FALSE;
1782 }
1783 else if (reader.js_buf[reader.js_used] != NUL)
1784 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001785 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001786 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1788 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001789 ret = status == MAYBE ? FALSE: TRUE;
1790 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001791 else
1792 ret = FALSE;
1793
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001794 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001795 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796}
1797
1798/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001799 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001800 */
1801 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001802remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001803{
Bram Moolenaar77073442016-02-13 23:23:53 +01001804 if (node->cq_prev == NULL)
1805 head->cq_next = node->cq_next;
1806 else
1807 node->cq_prev->cq_next = node->cq_next;
1808 if (node->cq_next == NULL)
1809 head->cq_prev = node->cq_prev;
1810 else
1811 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001812}
1813
1814/*
1815 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001816 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001817 */
1818 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001819remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001820{
Bram Moolenaar77073442016-02-13 23:23:53 +01001821 if (node->jq_prev == NULL)
1822 head->jq_next = node->jq_next;
1823 else
1824 node->jq_prev->jq_next = node->jq_next;
1825 if (node->jq_next == NULL)
1826 head->jq_prev = node->jq_prev;
1827 else
1828 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001829 vim_free(node);
1830}
1831
1832/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001833 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001834 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001835 * When "id" is zero or negative jut get the first message. But not the one
1836 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001837 * Return OK when found and return the value in "rettv".
1838 * Return FAIL otherwise.
1839 */
1840 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001842{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001844 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001845
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001848 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849 typval_T *tv = &l->lv_first->li_tv;
1850
1851 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001852 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853 || tv->vval.v_number == 0
1854 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001855 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001856 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001857 if (tv->v_type == VAR_NUMBER)
1858 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001859 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001860 return OK;
1861 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001862 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001863 }
1864 return FAIL;
1865}
1866
Bram Moolenaarece61b02016-02-20 21:39:05 +01001867#define CH_JSON_MAX_ARGS 4
1868
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001869/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001871 * "argv[0]" is the command string.
1872 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001873 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001874 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001875channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001876{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001877 char_u *cmd = argv[0].vval.v_string;
1878 char_u *arg;
1879 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001880
Bram Moolenaarece61b02016-02-20 21:39:05 +01001881 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001886 return;
1887 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001888 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001889 if (arg == NULL)
1890 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001891
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001892 if (STRCMP(cmd, "ex") == 0)
1893 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001894 int save_called_emsg = called_emsg;
1895
1896 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001897 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001898 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001899 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001900 --emsg_silent;
1901 if (called_emsg)
1902 ch_logs(channel, "Ex command error: '%s'",
1903 (char *)get_vim_var_str(VV_ERRMSG));
1904 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001905 }
1906 else if (STRCMP(cmd, "normal") == 0)
1907 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001908 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001909
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001910 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001911 ea.arg = arg;
1912 ea.addr_count = 0;
1913 ea.forceit = TRUE; /* no mapping */
1914 ex_normal(&ea);
1915 }
1916 else if (STRCMP(cmd, "redraw") == 0)
1917 {
1918 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001919
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001920 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001921 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001922 ex_redraw(&ea);
1923 showruler(FALSE);
1924 setcursor();
1925 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001926#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001927 if (gui.in_use)
1928 {
1929 gui_update_cursor(FALSE, FALSE);
1930 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001931 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001932#endif
1933 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001934 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001935 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 int is_call = cmd[0] == 'c';
1937 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001938
Bram Moolenaarece61b02016-02-20 21:39:05 +01001939 if (argv[id_idx].v_type != VAR_UNKNOWN
1940 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001941 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001942 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001943 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 EMSG("E904: last argument for expr/call must be a number");
1945 }
1946 else if (is_call && argv[2].v_type != VAR_LIST)
1947 {
1948 ch_error(channel, "third argument for call must be a list");
1949 if (p_verbose > 2)
1950 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001951 }
1952 else
1953 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001954 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001955 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001957 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001959 /* Don't pollute the display with errors. */
1960 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001961 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001962 {
1963 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001964 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001965 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001966 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001967 {
1968 ch_logs(channel, "Calling '%s'", (char *)arg);
1969 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1970 tv = &res_tv;
1971 else
1972 tv = NULL;
1973 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001974
1975 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001976 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001977 int id = argv[id_idx].vval.v_number;
1978
Bram Moolenaar55fab432016-02-07 16:53:13 +01001979 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001980 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001982 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001983 /* If evaluation failed or the result can't be encoded
1984 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001985 vim_free(json);
1986 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001987 err_tv.v_type = VAR_STRING;
1988 err_tv.vval.v_string = (char_u *)"ERROR";
1989 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001990 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001991 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001992 if (json != NULL)
1993 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001994 channel_send(channel,
1995 part == PART_SOCK ? PART_SOCK : PART_IN,
1996 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001997 vim_free(json);
1998 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001999 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002000 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002001 if (tv == &res_tv)
2002 clear_tv(tv);
2003 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002004 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002005 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002006 }
2007 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008 {
2009 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002010 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002011 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002012}
2013
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002014/*
2015 * Invoke the callback at "cbhead".
2016 * Does not redraw but sets channel_need_redraw.
2017 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002018 static void
2019invoke_one_time_callback(
2020 channel_T *channel,
2021 cbq_T *cbhead,
2022 cbq_T *item,
2023 typval_T *argv)
2024{
2025 ch_logs(channel, "Invoking one-time callback %s",
2026 (char *)item->cq_callback);
2027 /* Remove the item from the list first, if the callback
2028 * invokes ch_close() the list will be cleared. */
2029 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002030 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002031 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002032 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002033 vim_free(item);
2034}
2035
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002036 static void
2037append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2038{
2039 buf_T *save_curbuf = curbuf;
2040 linenr_T lnum = buffer->b_ml.ml_line_count;
2041 int save_write_to = buffer->b_write_to_channel;
2042
2043 /* If the buffer is also used as input insert above the last
2044 * line. Don't write these lines. */
2045 if (save_write_to)
2046 {
2047 --lnum;
2048 buffer->b_write_to_channel = FALSE;
2049 }
2050
2051 /* Append to the buffer */
2052 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2053
2054 curbuf = buffer;
2055 u_sync(TRUE);
2056 /* ignore undo failure, undo is not very useful here */
2057 ignored = u_save(lnum, lnum + 1);
2058
2059 ml_append(lnum, msg, 0, FALSE);
2060 appended_lines_mark(lnum, 1L);
2061 curbuf = save_curbuf;
2062
2063 if (buffer->b_nwindows > 0)
2064 {
2065 win_T *wp;
2066 win_T *save_curwin;
2067
2068 FOR_ALL_WINDOWS(wp)
2069 {
2070 if (wp->w_buffer == buffer
2071 && (save_write_to
2072 ? wp->w_cursor.lnum == lnum + 1
2073 : (wp->w_cursor.lnum == lnum
2074 && wp->w_cursor.col == 0)))
2075 {
2076 ++wp->w_cursor.lnum;
2077 save_curwin = curwin;
2078 curwin = wp;
2079 curbuf = curwin->w_buffer;
2080 scroll_cursor_bot(0, FALSE);
2081 curwin = save_curwin;
2082 curbuf = curwin->w_buffer;
2083 }
2084 }
2085 redraw_buf_later(buffer, VALID);
2086 channel_need_redraw = TRUE;
2087 }
2088
2089 if (save_write_to)
2090 {
2091 channel_T *ch;
2092
2093 /* Find channels reading from this buffer and adjust their
2094 * next-to-read line number. */
2095 buffer->b_write_to_channel = TRUE;
2096 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2097 {
2098 chanpart_T *in_part = &ch->ch_part[PART_IN];
2099
2100 if (in_part->ch_buffer == buffer)
2101 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2102 }
2103 }
2104}
2105
Bram Moolenaar437905c2016-04-26 19:01:05 +02002106 static void
2107drop_messages(channel_T *channel, int part)
2108{
2109 char_u *msg;
2110
2111 while ((msg = channel_get(channel, part)) != NULL)
2112 {
2113 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2114 vim_free(msg);
2115 }
2116}
2117
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002118/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002119 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002120 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002121 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002122 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002123 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002124may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002125{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002126 char_u *msg = NULL;
2127 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002128 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002129 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002130 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002131 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002132 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002133 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002134 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002135 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002136
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002137 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002138 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002139 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002140
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002141 /* Use a message-specific callback, part callback or channel callback */
2142 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2143 if (cbitem->cq_seq_nr == 0)
2144 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002145 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002146 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002147 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002148 partial = cbitem->cq_partial;
2149 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002150 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002151 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002152 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002153 partial = channel->ch_part[part].ch_partial;
2154 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002155 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002156 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002158 partial = channel->ch_partial;
2159 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002160
Bram Moolenaar187db502016-02-27 14:44:26 +01002161 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002162 if (buffer != NULL && !buf_valid(buffer))
2163 {
2164 /* buffer was wiped out */
2165 channel->ch_part[part].ch_buffer = NULL;
2166 buffer = NULL;
2167 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002168
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002169 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002170 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002171 listitem_T *item;
2172 int argc = 0;
2173
Bram Moolenaard7ece102016-02-02 23:23:02 +01002174 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002175 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002176 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002177 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002178 channel_parse_json(channel, part);
2179 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002180 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002181 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002182
Bram Moolenaarece61b02016-02-20 21:39:05 +01002183 for (item = listtv->vval.v_list->lv_first;
2184 item != NULL && argc < CH_JSON_MAX_ARGS;
2185 item = item->li_next)
2186 argv[argc++] = item->li_tv;
2187 while (argc < CH_JSON_MAX_ARGS)
2188 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002189
Bram Moolenaarece61b02016-02-20 21:39:05 +01002190 if (argv[0].v_type == VAR_STRING)
2191 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002194 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002195 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002196 }
2197
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002199 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002200 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002201 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002202 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002203 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002204 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002205 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002206 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002207 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002208 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002209 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002210 return FALSE;
2211 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002212 else
2213 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002214 /* If there is no callback or buffer drop the message. */
2215 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002216 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002217 /* If there is a close callback it may use ch_read() to get the
2218 * messages. */
2219 if (channel->ch_close_cb == NULL)
2220 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002221 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002222 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002223
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002224 if (ch_mode == MODE_NL)
2225 {
2226 char_u *nl;
2227 char_u *buf;
2228
2229 /* See if we have a message ending in NL in the first buffer. If
2230 * not try to concatenate the first and the second buffer. */
2231 while (TRUE)
2232 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002233 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002234 nl = vim_strchr(buf, NL);
2235 if (nl != NULL)
2236 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002237 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002238 return FALSE; /* incomplete message */
2239 }
2240 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002241 {
2242 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002243 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002244 *nl = NUL;
2245 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002246 else
2247 {
2248 /* Copy the message into allocated memory and remove it from
2249 * the buffer. */
2250 msg = vim_strnsave(buf, (int)(nl - buf));
2251 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2252 }
2253 }
2254 else
2255 /* For a raw channel we don't know where the message ends, just
2256 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002257 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002258
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002259 if (msg == NULL)
2260 return FALSE; /* out of memory (and avoids Coverity warning) */
2261
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002262 argv[1].v_type = VAR_STRING;
2263 argv[1].vval.v_string = msg;
2264 }
2265
Bram Moolenaara07fec92016-02-05 21:04:08 +01002266 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002267 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002268 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002269
2270 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002271 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002272 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002273 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002274 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002275 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002276 break;
2277 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002278 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002279 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002280 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002281 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002282 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002283 if (buffer != NULL)
2284 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002285 if (msg == NULL)
2286 /* JSON or JS mode: re-encode the message. */
2287 msg = json_encode(listtv, ch_mode);
2288 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002289 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002290 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002291
Bram Moolenaar187db502016-02-27 14:44:26 +01002292 if (callback != NULL)
2293 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002294 if (cbitem != NULL)
2295 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2296 else
2297 {
2298 /* invoke the channel callback */
2299 ch_logs(channel, "Invoking channel callback %s",
2300 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002301 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002302 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002303 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002304 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002305 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002306 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002307
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002308 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002309 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002310 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002311
2312 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002313}
2314
2315/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002316 * Return TRUE when channel "channel" is open for writing to.
2317 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002318 */
2319 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002320channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002321{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002323 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002324}
2325
2326/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002327 * Return TRUE when channel "channel" is open for reading or writing.
2328 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002329 */
2330 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002331channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002332{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002333 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002334 || channel->CH_IN_FD != INVALID_FD
2335 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002336 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002337}
2338
2339/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002340 * Return TRUE if "channel" has JSON or other typeahead.
2341 */
2342 static int
2343channel_has_readahead(channel_T *channel, int part)
2344{
2345 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2346
2347 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2348 {
2349 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2350 jsonq_T *item = head->jq_next;
2351
2352 return item != NULL;
2353 }
2354 return channel_peek(channel, part) != NULL;
2355}
2356
2357/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002358 * Return a string indicating the status of the channel.
2359 */
2360 char *
2361channel_status(channel_T *channel)
2362{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002363 int part;
2364 int has_readahead = FALSE;
2365
Bram Moolenaar77073442016-02-13 23:23:53 +01002366 if (channel == NULL)
2367 return "fail";
2368 if (channel_is_open(channel))
2369 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002370 for (part = PART_SOCK; part <= PART_ERR; ++part)
2371 if (channel_has_readahead(channel, part))
2372 {
2373 has_readahead = TRUE;
2374 break;
2375 }
2376
2377 if (has_readahead)
2378 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002379 return "closed";
2380}
2381
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002382 static void
2383channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2384{
2385 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002386 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002387 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002388 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002389
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002390 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002391 STRCAT(namebuf, "_");
2392 tail = STRLEN(namebuf);
2393
2394 STRCPY(namebuf + tail, "status");
2395 dict_add_nr_str(dict, namebuf, 0,
2396 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2397
2398 STRCPY(namebuf + tail, "mode");
2399 switch (chanpart->ch_mode)
2400 {
2401 case MODE_NL: s = "NL"; break;
2402 case MODE_RAW: s = "RAW"; break;
2403 case MODE_JSON: s = "JSON"; break;
2404 case MODE_JS: s = "JS"; break;
2405 }
2406 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2407
2408 STRCPY(namebuf + tail, "io");
2409 if (part == PART_SOCK)
2410 s = "socket";
2411 else switch (chanpart->ch_io)
2412 {
2413 case JIO_NULL: s = "null"; break;
2414 case JIO_PIPE: s = "pipe"; break;
2415 case JIO_FILE: s = "file"; break;
2416 case JIO_BUFFER: s = "buffer"; break;
2417 case JIO_OUT: s = "out"; break;
2418 }
2419 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2420
2421 STRCPY(namebuf + tail, "timeout");
2422 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2423}
2424
2425 void
2426channel_info(channel_T *channel, dict_T *dict)
2427{
2428 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2429 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2430
2431 if (channel->ch_hostname != NULL)
2432 {
2433 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2434 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2435 channel_part_info(channel, dict, "sock", PART_SOCK);
2436 }
2437 else
2438 {
2439 channel_part_info(channel, dict, "out", PART_OUT);
2440 channel_part_info(channel, dict, "err", PART_ERR);
2441 channel_part_info(channel, dict, "in", PART_IN);
2442 }
2443}
2444
Bram Moolenaar77073442016-02-13 23:23:53 +01002445/*
2446 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002447 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002448 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002449 */
2450 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002451channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002452{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002453 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002454
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002455#ifdef FEAT_GUI
2456 channel_gui_unregister(channel);
2457#endif
2458
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002459 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002460 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 sock_close(channel->CH_SOCK_FD);
2462 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002463 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002464 may_close_part(&channel->CH_IN_FD);
2465 may_close_part(&channel->CH_OUT_FD);
2466 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002467
Bram Moolenaar8b374212016-02-24 20:43:06 +01002468 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002469 {
2470 typval_T argv[1];
2471 typval_T rettv;
2472 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002473 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002474
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002475 /* Invoke callbacks before the close callback, since it's weird to
2476 * first invoke the close callback. Increment the refcount to avoid
2477 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002478 ++channel->ch_refcount;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002479 for (part = PART_SOCK; part <= PART_ERR; ++part)
2480 while (may_invoke_callback(channel, part))
2481 ;
2482
2483 /* Invoke the close callback, if still set. */
2484 if (channel->ch_close_cb != NULL)
2485 {
2486 ch_logs(channel, "Invoking close callback %s",
2487 (char *)channel->ch_close_cb);
2488 argv[0].v_type = VAR_CHANNEL;
2489 argv[0].vval.v_channel = channel;
2490 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002491 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2492 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002493 clear_tv(&rettv);
2494 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002495 --channel->ch_refcount;
2496
2497 /* the callback is only called once */
2498 vim_free(channel->ch_close_cb);
2499 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002500 partial_unref(channel->ch_close_partial);
2501 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002502
2503 /* any remaining messages are useless now */
2504 for (part = PART_SOCK; part <= PART_ERR; ++part)
2505 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002506 }
2507
2508 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002509}
2510
Bram Moolenaard04a0202016-01-26 23:30:18 +01002511/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002512 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002513 * Returns NULL if there is nothing.
2514 */
2515 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002516channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002517{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002518 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002519
Bram Moolenaar77073442016-02-13 23:23:53 +01002520 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002522 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002523}
2524
2525/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002526 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002527 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002528 static void
2529channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002530{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2532 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 while (channel_peek(channel, part) != NULL)
2535 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002536
2537 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002538 {
2539 cbq_T *node = cb_head->cq_next;
2540
2541 remove_cb_node(cb_head, node);
2542 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002543 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002544 vim_free(node);
2545 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002546
2547 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002548 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002549 free_tv(json_head->jq_next->jq_value);
2550 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002551 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002552
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002553 vim_free(channel->ch_part[part].ch_callback);
2554 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002555 partial_unref(channel->ch_part[part].ch_partial);
2556 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002557}
2558
2559/*
2560 * Clear all the read buffers on "channel".
2561 */
2562 void
2563channel_clear(channel_T *channel)
2564{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002565 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002566 vim_free(channel->ch_hostname);
2567 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002568 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002569 channel_clear_one(channel, PART_OUT);
2570 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002571 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002572 vim_free(channel->ch_callback);
2573 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002574 partial_unref(channel->ch_partial);
2575 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002576 vim_free(channel->ch_close_cb);
2577 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002578 partial_unref(channel->ch_close_partial);
2579 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002580}
2581
Bram Moolenaar77073442016-02-13 23:23:53 +01002582#if defined(EXITFREE) || defined(PROTO)
2583 void
2584channel_free_all(void)
2585{
2586 channel_T *channel;
2587
Bram Moolenaard6051b52016-02-28 15:49:03 +01002588 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002589 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2590 channel_clear(channel);
2591}
2592#endif
2593
2594
Bram Moolenaard04a0202016-01-26 23:30:18 +01002595/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002596#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002597
2598/* Buffer size for reading incoming messages. */
2599#define MAXMSGSIZE 4096
2600
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002601#if defined(HAVE_SELECT)
2602/*
2603 * Add write fds where we are waiting for writing to be possible.
2604 */
2605 static int
2606channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2607{
2608 int maxfd = maxfd_arg;
2609 channel_T *ch;
2610
2611 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2612 {
2613 chanpart_T *in_part = &ch->ch_part[PART_IN];
2614
2615 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2616 {
2617 FD_SET((int)in_part->ch_fd, wfds);
2618 if ((int)in_part->ch_fd >= maxfd)
2619 maxfd = (int)in_part->ch_fd + 1;
2620 }
2621 }
2622 return maxfd;
2623}
2624#else
2625/*
2626 * Add write fds where we are waiting for writing to be possible.
2627 */
2628 static int
2629channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2630{
2631 int nfd = nfd_in;
2632 channel_T *ch;
2633
2634 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2635 {
2636 chanpart_T *in_part = &ch->ch_part[PART_IN];
2637
2638 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2639 {
2640 in_part->ch_poll_idx = nfd;
2641 fds[nfd].fd = in_part->ch_fd;
2642 fds[nfd].events = POLLOUT;
2643 ++nfd;
2644 }
2645 else
2646 in_part->ch_poll_idx = -1;
2647 }
2648 return nfd;
2649}
2650#endif
2651
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002652typedef enum {
2653 CW_READY,
2654 CW_NOT_READY,
2655 CW_ERROR
2656} channel_wait_result;
2657
Bram Moolenaard04a0202016-01-26 23:30:18 +01002658/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002659 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002660 * Return CW_READY when there is something to read.
2661 * Return CW_NOT_READY when there is nothing to read.
2662 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002663 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002664 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002665channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002666{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002667 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002668 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002669
Bram Moolenaard8070362016-02-15 21:56:54 +01002670# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002671 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002672 {
2673 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002674 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002675 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002676 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002677
2678 /* reading from a pipe, not a socket */
2679 while (TRUE)
2680 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002681 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2682
2683 if (r && nread > 0)
2684 return CW_READY;
2685 if (r == 0)
2686 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002687
2688 /* perhaps write some buffer lines */
2689 channel_write_any_lines();
2690
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002691 sleep_time = deadline - GetTickCount();
2692 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002693 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002694 /* Wait for a little while. Very short at first, up to 10 msec
2695 * after looping a few times. */
2696 if (sleep_time > delay)
2697 sleep_time = delay;
2698 Sleep(sleep_time);
2699 delay = delay * 2;
2700 if (delay > 10)
2701 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002702 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002703 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002704 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002705#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002706 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002707#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002708 struct timeval tval;
2709 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002710 fd_set wfds;
2711 int ret;
2712 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002713
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002714 tval.tv_sec = timeout / 1000;
2715 tval.tv_usec = (timeout % 1000) * 1000;
2716 for (;;)
2717 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002718 FD_ZERO(&rfds);
2719 FD_SET((int)fd, &rfds);
2720
2721 /* Write lines to a pipe when a pipe can be written to. Need to
2722 * set this every time, some buffers may be done. */
2723 maxfd = (int)fd + 1;
2724 FD_ZERO(&wfds);
2725 maxfd = channel_fill_wfds(maxfd, &wfds);
2726
2727 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002728# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002729 SOCK_ERRNO;
2730 if (ret == -1 && errno == EINTR)
2731 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002732# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002733 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002734 {
2735 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002736 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002737 channel_write_any_lines();
2738 continue;
2739 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002740 break;
2741 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002742#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002743 for (;;)
2744 {
2745 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2746 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002747
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002748 fds[0].fd = fd;
2749 fds[0].events = POLLIN;
2750 nfd = channel_fill_poll_write(nfd, fds);
2751 if (poll(fds, nfd, timeout) > 0)
2752 {
2753 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002754 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002755 channel_write_any_lines();
2756 continue;
2757 }
2758 break;
2759 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002760#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002761 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002762 return CW_NOT_READY;
2763}
2764
2765 static void
2766channel_close_on_error(channel_T *channel, int part, char *func)
2767{
2768 /* Do not call emsg(), most likely the other end just exited. */
2769 ch_errors(channel, "%s(): Cannot read from channel", func);
2770
2771 /* Queue a "DETACH" netbeans message in the command queue in order to
2772 * terminate the netbeans session later. Do not end the session here
2773 * directly as we may be running in the context of a call to
2774 * netbeans_parse_messages():
2775 * netbeans_parse_messages
2776 * -> autocmd triggered while processing the netbeans cmd
2777 * -> ui_breakcheck
2778 * -> gui event loop or select loop
2779 * -> channel_read()
2780 * Don't send "DETACH" for a JS or JSON channel.
2781 */
2782 if (channel->ch_part[part].ch_mode == MODE_RAW
2783 || channel->ch_part[part].ch_mode == MODE_NL)
2784 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
2785 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2786
2787 /* When reading from stdout is not possible, assume the other side has
2788 * died. */
2789 channel_close(channel, TRUE);
2790 if (channel->ch_nb_close_cb != NULL)
2791 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002792}
2793
2794/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002795 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002796 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002797 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002798 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002799 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002800channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002801{
2802 static char_u *buf = NULL;
2803 int len = 0;
2804 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002805 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002806 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002807
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002808 fd = channel->ch_part[part].ch_fd;
2809 if (fd == INVALID_FD)
2810 {
2811 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002812 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002813 }
2814 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002815
2816 /* Allocate a buffer to read into. */
2817 if (buf == NULL)
2818 {
2819 buf = alloc(MAXMSGSIZE);
2820 if (buf == NULL)
2821 return; /* out of memory! */
2822 }
2823
2824 /* Keep on reading for as long as there is something to read.
2825 * Use select() or poll() to avoid blocking on a message that is exactly
2826 * MAXMSGSIZE long. */
2827 for (;;)
2828 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002829 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002830 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002831 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002832 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002833 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002834 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002835 if (len <= 0)
2836 break; /* error or nothing more to read */
2837
2838 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002839 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002840 readlen += len;
2841 if (len < MAXMSGSIZE)
2842 break; /* did read everything that's available */
2843 }
2844
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002845 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002846 if (readlen <= 0)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002847 channel_close_on_error(channel, part, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002848
2849#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002850 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002851 if (CH_HAS_GUI && gtk_main_level() > 0)
2852 gtk_main_quit();
2853#endif
2854}
2855
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002856/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002857 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002858 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002859 * Returns what was read in allocated memory.
2860 * Returns NULL in case of error or timeout.
2861 */
2862 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002863channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002864{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002865 char_u *buf;
2866 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002867 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002868 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002869 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002870
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002871 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002872 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002873
2874 while (TRUE)
2875 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002876 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002877 if (buf != NULL && (mode == MODE_RAW
2878 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2879 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002880 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002881 continue;
2882
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002883 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002884 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002885 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002886 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002887 {
2888 ch_log(channel, "Timed out");
2889 return NULL;
2890 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002891 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002892 }
2893
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002894 if (mode == MODE_RAW)
2895 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002896 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002897 }
2898 else
2899 {
2900 nl = vim_strchr(buf, NL);
2901 if (nl[1] == NUL)
2902 {
2903 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002904 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002905 *nl = NUL;
2906 }
2907 else
2908 {
2909 /* Copy the message into allocated memory and remove it from the
2910 * buffer. */
2911 msg = vim_strnsave(buf, (int)(nl - buf));
2912 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2913 }
2914 }
2915 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002916 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002917 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002918}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002919
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002920/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002921 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002922 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002923 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002924 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002925 */
2926 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002927channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002928 channel_T *channel,
2929 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002930 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002931 int id,
2932 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002933{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002934 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002935 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002936 int timeout;
2937 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002938
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002939 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002940 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002941 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002942 for (;;)
2943 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002944 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002945
2946 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002947 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002948 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002949 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002950 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002951 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002952
Bram Moolenaard7ece102016-02-02 23:23:02 +01002953 if (!more)
2954 {
2955 /* Handle any other messages in the queue. If done some more
2956 * messages may have arrived. */
2957 if (channel_parse_messages())
2958 continue;
2959
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002960 /* Wait for up to the timeout. If there was an incomplete message
2961 * use the deadline for that. */
2962 timeout = timeout_arg;
2963 if (chanpart->ch_waiting)
2964 {
2965#ifdef WIN32
2966 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2967#else
2968 {
2969 struct timeval now_tv;
2970
2971 gettimeofday(&now_tv, NULL);
2972 timeout = (chanpart->ch_deadline.tv_sec
2973 - now_tv.tv_sec) * 1000
2974 + (chanpart->ch_deadline.tv_usec
2975 - now_tv.tv_usec) / 1000
2976 + 1;
2977 }
2978#endif
2979 if (timeout < 0)
2980 {
2981 /* Something went wrong, channel_parse_json() didn't
2982 * discard message. Cancel waiting. */
2983 chanpart->ch_waiting = FALSE;
2984 timeout = timeout_arg;
2985 }
2986 else if (timeout > timeout_arg)
2987 timeout = timeout_arg;
2988 }
2989 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002990 if (fd == INVALID_FD
2991 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002992 {
2993 if (timeout == timeout_arg)
2994 {
2995 if (fd != INVALID_FD)
2996 ch_log(channel, "Timed out");
2997 break;
2998 }
2999 }
3000 else
3001 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003002 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003003 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003004 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003005 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003006}
3007
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003008/*
3009 * Common for ch_read() and ch_readraw().
3010 */
3011 void
3012common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3013{
3014 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003015 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003016 jobopt_T opt;
3017 int mode;
3018 int timeout;
3019 int id = -1;
3020 typval_T *listtv = NULL;
3021
3022 /* return an empty string by default */
3023 rettv->v_type = VAR_STRING;
3024 rettv->vval.v_string = NULL;
3025
3026 clear_job_options(&opt);
3027 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3028 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003029 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003030
Bram Moolenaar437905c2016-04-26 19:01:05 +02003031 if (opt.jo_set & JO_PART)
3032 part = opt.jo_part;
3033 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003034 if (channel != NULL)
3035 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003036 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003037 part = channel_part_read(channel);
3038 mode = channel_get_mode(channel, part);
3039 timeout = channel_get_timeout(channel, part);
3040 if (opt.jo_set & JO_TIMEOUT)
3041 timeout = opt.jo_timeout;
3042
3043 if (raw || mode == MODE_RAW || mode == MODE_NL)
3044 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3045 else
3046 {
3047 if (opt.jo_set & JO_ID)
3048 id = opt.jo_id;
3049 channel_read_json_block(channel, part, timeout, id, &listtv);
3050 if (listtv != NULL)
3051 {
3052 *rettv = *listtv;
3053 vim_free(listtv);
3054 }
3055 else
3056 {
3057 rettv->v_type = VAR_SPECIAL;
3058 rettv->vval.v_number = VVAL_NONE;
3059 }
3060 }
3061 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003062
3063theend:
3064 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003065}
3066
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003067# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3068 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003069/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003070 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003071 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003072 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003073 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003074channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003075{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003076 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003077 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003078
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003079 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003080 for (channel = first_channel; channel != NULL;
3081 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003082 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003083 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003084 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003085 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003086 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003087 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003088 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003089 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003090 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003091}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003092# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003093
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003094# if defined(WIN32) || defined(PROTO)
3095/*
3096 * Check the channels for anything that is ready to be read.
3097 * The data is put in the read queue.
3098 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003099 void
3100channel_handle_events(void)
3101{
3102 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003103 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003104 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003105
3106 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3107 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003108 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003109 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003110 {
3111 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003112 if (fd != INVALID_FD)
3113 {
3114 int r = channel_wait(channel, fd, 0);
3115
3116 if (r == CW_READY)
3117 channel_read(channel, part, "channel_handle_events");
3118 else if (r == CW_ERROR)
3119 channel_close_on_error(channel, part,
3120 "channel_handle_events()");
3121 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003122 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003123 }
3124}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003125# endif
3126
Bram Moolenaard04a0202016-01-26 23:30:18 +01003127/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003128 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003129 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003130 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003131 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003132 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003133channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003134{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003135 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003136 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003137 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003138
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003139 fd = channel->ch_part[part].ch_fd;
3140 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003141 {
3142 if (!channel->ch_error && fun != NULL)
3143 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003144 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003145 EMSG2("E630: %s(): write while not connected", fun);
3146 }
3147 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003148 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003149 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003150
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003151 if (log_fd != NULL)
3152 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003153 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003154 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003155 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003156 fprintf(log_fd, "'\n");
3157 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003158 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003159 }
3160
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003161 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003162 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003163 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003164 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003165 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003166 {
3167 if (!channel->ch_error && fun != NULL)
3168 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003169 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003170 EMSG2("E631: %s(): write failed", fun);
3171 }
3172 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003173 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003174 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003175
3176 channel->ch_error = FALSE;
3177 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003178}
3179
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003180/*
3181 * Common for "ch_sendexpr()" and "ch_sendraw()".
3182 * Returns the channel if the caller should read the response.
3183 * Sets "part_read" to the the read fd.
3184 * Otherwise returns NULL.
3185 */
3186 channel_T *
3187send_common(
3188 typval_T *argvars,
3189 char_u *text,
3190 int id,
3191 int eval,
3192 jobopt_T *opt,
3193 char *fun,
3194 int *part_read)
3195{
3196 channel_T *channel;
3197 int part_send;
3198
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003199 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003200 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003201 if (channel == NULL)
3202 return NULL;
3203 part_send = channel_part_send(channel);
3204 *part_read = channel_part_read(channel);
3205
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003206 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3207 return NULL;
3208
3209 /* Set the callback. An empty callback means no callback and not reading
3210 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3211 * allowed. */
3212 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3213 {
3214 if (eval)
3215 {
3216 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3217 return NULL;
3218 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003219 channel_set_req_callback(channel, part_send,
3220 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003221 }
3222
3223 if (channel_send(channel, part_send, text, fun) == OK
3224 && opt->jo_callback == NULL)
3225 return channel;
3226 return NULL;
3227}
3228
3229/*
3230 * common for "ch_evalexpr()" and "ch_sendexpr()"
3231 */
3232 void
3233ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3234{
3235 char_u *text;
3236 typval_T *listtv;
3237 channel_T *channel;
3238 int id;
3239 ch_mode_T ch_mode;
3240 int part_send;
3241 int part_read;
3242 jobopt_T opt;
3243 int timeout;
3244
3245 /* return an empty string by default */
3246 rettv->v_type = VAR_STRING;
3247 rettv->vval.v_string = NULL;
3248
Bram Moolenaar437905c2016-04-26 19:01:05 +02003249 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003250 if (channel == NULL)
3251 return;
3252 part_send = channel_part_send(channel);
3253
3254 ch_mode = channel_get_mode(channel, part_send);
3255 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3256 {
3257 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3258 return;
3259 }
3260
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003261 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003262 text = json_encode_nr_expr(id, &argvars[1],
3263 ch_mode == MODE_JS ? JSON_JS : 0);
3264 if (text == NULL)
3265 return;
3266
3267 channel = send_common(argvars, text, id, eval, &opt,
3268 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3269 vim_free(text);
3270 if (channel != NULL && eval)
3271 {
3272 if (opt.jo_set & JO_TIMEOUT)
3273 timeout = opt.jo_timeout;
3274 else
3275 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003276 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3277 == OK)
3278 {
3279 list_T *list = listtv->vval.v_list;
3280
3281 /* Move the item from the list and then change the type to
3282 * avoid the value being freed. */
3283 *rettv = list->lv_last->li_tv;
3284 list->lv_last->li_tv.v_type = VAR_NUMBER;
3285 free_tv(listtv);
3286 }
3287 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003288 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003289}
3290
3291/*
3292 * common for "ch_evalraw()" and "ch_sendraw()"
3293 */
3294 void
3295ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3296{
3297 char_u buf[NUMBUFLEN];
3298 char_u *text;
3299 channel_T *channel;
3300 int part_read;
3301 jobopt_T opt;
3302 int timeout;
3303
3304 /* return an empty string by default */
3305 rettv->v_type = VAR_STRING;
3306 rettv->vval.v_string = NULL;
3307
3308 text = get_tv_string_buf(&argvars[1], buf);
3309 channel = send_common(argvars, text, 0, eval, &opt,
3310 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3311 if (channel != NULL && eval)
3312 {
3313 if (opt.jo_set & JO_TIMEOUT)
3314 timeout = opt.jo_timeout;
3315 else
3316 timeout = channel_get_timeout(channel, part_read);
3317 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3318 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003319 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003320}
3321
Bram Moolenaard04a0202016-01-26 23:30:18 +01003322# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003323/*
3324 * Add open channels to the poll struct.
3325 * Return the adjusted struct index.
3326 * The type of "fds" is hidden to avoid problems with the function proto.
3327 */
3328 int
3329channel_poll_setup(int nfd_in, void *fds_in)
3330{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003331 int nfd = nfd_in;
3332 channel_T *channel;
3333 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003334 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003335
Bram Moolenaar77073442016-02-13 23:23:53 +01003336 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003337 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003338 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003339 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003340 chanpart_T *ch_part = &channel->ch_part[part];
3341
3342 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003343 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003344 ch_part->ch_poll_idx = nfd;
3345 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003346 fds[nfd].events = POLLIN;
3347 nfd++;
3348 }
3349 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003350 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003351 }
3352 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003353
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003354 nfd = channel_fill_poll_write(nfd, fds);
3355
Bram Moolenaare0874f82016-01-24 20:36:41 +01003356 return nfd;
3357}
3358
3359/*
3360 * The type of "fds" is hidden to avoid problems with the function proto.
3361 */
3362 int
3363channel_poll_check(int ret_in, void *fds_in)
3364{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003365 int ret = ret_in;
3366 channel_T *channel;
3367 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003368 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003369 int idx;
3370 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003371
Bram Moolenaar77073442016-02-13 23:23:53 +01003372 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003373 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003374 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003375 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003376 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003377
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003378 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003379 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003380 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003381 --ret;
3382 }
3383 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003384
3385 in_part = &channel->ch_part[PART_IN];
3386 idx = in_part->ch_poll_idx;
3387 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3388 {
3389 if (in_part->ch_buf_append)
3390 {
3391 if (in_part->ch_buffer != NULL)
3392 channel_write_new_lines(in_part->ch_buffer);
3393 }
3394 else
3395 channel_write_in(channel);
3396 --ret;
3397 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003398 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003399
3400 return ret;
3401}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003402# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003403
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003404# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003405/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003406 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003407 */
3408 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003409channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003410{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003411 int maxfd = maxfd_in;
3412 channel_T *channel;
3413 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003414 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003415 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003416
Bram Moolenaar77073442016-02-13 23:23:53 +01003417 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003418 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003419 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003420 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003421 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003422
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003423 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003424 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003425 FD_SET((int)fd, rfds);
3426 if (maxfd < (int)fd)
3427 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003428 }
3429 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003430 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003431
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003432 maxfd = channel_fill_wfds(maxfd, wfds);
3433
Bram Moolenaare0874f82016-01-24 20:36:41 +01003434 return maxfd;
3435}
3436
3437/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003438 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003439 */
3440 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003441channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003442{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003443 int ret = ret_in;
3444 channel_T *channel;
3445 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003446 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003447 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003448 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003449
Bram Moolenaar77073442016-02-13 23:23:53 +01003450 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003451 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003452 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003453 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003454 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003455
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003456 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003457 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003458 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003459 --ret;
3460 }
3461 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003462
3463 in_part = &channel->ch_part[PART_IN];
3464 if (ret > 0 && in_part->ch_fd != INVALID_FD
3465 && FD_ISSET(in_part->ch_fd, wfds))
3466 {
3467 if (in_part->ch_buf_append)
3468 {
3469 if (in_part->ch_buffer != NULL)
3470 channel_write_new_lines(in_part->ch_buffer);
3471 }
3472 else
3473 channel_write_in(channel);
3474 --ret;
3475 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003476 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003477
3478 return ret;
3479}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003480# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003481
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003482/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003483 * Execute queued up commands.
3484 * Invoked from the main loop when it's safe to execute received commands.
3485 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003486 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003487 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003488channel_parse_messages(void)
3489{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003490 channel_T *channel = first_channel;
3491 int ret = FALSE;
3492 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003493 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003494
Bram Moolenaard0b65022016-03-06 21:50:33 +01003495 /* Only do this message when another message was given, otherwise we get
3496 * lots of them. */
3497 if (did_log_msg)
3498 {
3499 ch_log(NULL, "looking for messages on channels");
3500 did_log_msg = FALSE;
3501 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003502 while (channel != NULL)
3503 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003504 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003505 {
3506 /* channel is no longer useful, free it */
3507 channel_free(channel);
3508 channel = first_channel;
3509 part = PART_SOCK;
3510 continue;
3511 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003512 if (channel->ch_part[part].ch_fd != INVALID_FD
3513 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003514 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003515 /* Increase the refcount, in case the handler causes the channel
3516 * to be unreferenced or closed. */
3517 ++channel->ch_refcount;
3518 r = may_invoke_callback(channel, part);
3519 if (r == OK)
3520 ret = TRUE;
3521 if (channel_unref(channel) || r == OK)
3522 {
3523 /* channel was freed or something was done, start over */
3524 channel = first_channel;
3525 part = PART_SOCK;
3526 continue;
3527 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003528 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003529 if (part < PART_ERR)
3530 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003531 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003532 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003533 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003534 part = PART_SOCK;
3535 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003536 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003537
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003538 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003539 {
3540 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003541 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003542 }
3543
Bram Moolenaard7ece102016-02-02 23:23:02 +01003544 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003545}
3546
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003547/*
3548 * Mark references to lists used in channels.
3549 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003550 int
3551set_ref_in_channel(int copyID)
3552{
Bram Moolenaar77073442016-02-13 23:23:53 +01003553 int abort = FALSE;
3554 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003555 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003556
Bram Moolenaar77073442016-02-13 23:23:53 +01003557 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003558 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003560 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003561 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3562 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003563
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003564 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003565 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003566 list_T *l = item->jq_value->vval.v_list;
3567
3568 if (l->lv_copyID != copyID)
3569 {
3570 l->lv_copyID = copyID;
3571 abort = abort || set_ref_in_list(l, copyID, NULL);
3572 }
3573 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003574 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003575 }
3576 }
3577 return abort;
3578}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003579
3580/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003581 * Return the "part" to write to for "channel".
3582 */
3583 int
3584channel_part_send(channel_T *channel)
3585{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003586 if (channel->CH_SOCK_FD == INVALID_FD)
3587 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003588 return PART_SOCK;
3589}
3590
3591/*
3592 * Return the default "part" to read from for "channel".
3593 */
3594 int
3595channel_part_read(channel_T *channel)
3596{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003597 if (channel->CH_SOCK_FD == INVALID_FD)
3598 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003599 return PART_SOCK;
3600}
3601
3602/*
3603 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003604 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003605 */
3606 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003607channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003608{
Bram Moolenaar77073442016-02-13 23:23:53 +01003609 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003610 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003611 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003612}
3613
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003614/*
3615 * Return the timeout of "channel"/"part"
3616 */
3617 int
3618channel_get_timeout(channel_T *channel, int part)
3619{
3620 return channel->ch_part[part].ch_timeout;
3621}
3622
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003623 static int
3624handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3625{
3626 char_u *val = get_tv_string(item);
3627
3628 opt->jo_set |= jo;
3629 if (STRCMP(val, "nl") == 0)
3630 *modep = MODE_NL;
3631 else if (STRCMP(val, "raw") == 0)
3632 *modep = MODE_RAW;
3633 else if (STRCMP(val, "js") == 0)
3634 *modep = MODE_JS;
3635 else if (STRCMP(val, "json") == 0)
3636 *modep = MODE_JSON;
3637 else
3638 {
3639 EMSG2(_(e_invarg2), val);
3640 return FAIL;
3641 }
3642 return OK;
3643}
3644
3645 static int
3646handle_io(typval_T *item, int part, jobopt_T *opt)
3647{
3648 char_u *val = get_tv_string(item);
3649
3650 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3651 if (STRCMP(val, "null") == 0)
3652 opt->jo_io[part] = JIO_NULL;
3653 else if (STRCMP(val, "pipe") == 0)
3654 opt->jo_io[part] = JIO_PIPE;
3655 else if (STRCMP(val, "file") == 0)
3656 opt->jo_io[part] = JIO_FILE;
3657 else if (STRCMP(val, "buffer") == 0)
3658 opt->jo_io[part] = JIO_BUFFER;
3659 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3660 opt->jo_io[part] = JIO_OUT;
3661 else
3662 {
3663 EMSG2(_(e_invarg2), val);
3664 return FAIL;
3665 }
3666 return OK;
3667}
3668
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003669/*
3670 * Clear a jobopt_T before using it.
3671 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003672 void
3673clear_job_options(jobopt_T *opt)
3674{
3675 vim_memset(opt, 0, sizeof(jobopt_T));
3676}
3677
3678/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003679 * Free any members of a jobopt_T.
3680 */
3681 void
3682free_job_options(jobopt_T *opt)
3683{
3684 if (opt->jo_partial != NULL)
3685 partial_unref(opt->jo_partial);
3686 if (opt->jo_out_partial != NULL)
3687 partial_unref(opt->jo_out_partial);
3688 if (opt->jo_err_partial != NULL)
3689 partial_unref(opt->jo_err_partial);
3690 if (opt->jo_close_partial != NULL)
3691 partial_unref(opt->jo_close_partial);
3692}
3693
3694/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003695 * Get the PART_ number from the first character of an option name.
3696 */
3697 static int
3698part_from_char(int c)
3699{
3700 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3701}
3702
3703/*
3704 * Get the option entries from the dict in "tv", parse them and put the result
3705 * in "opt".
3706 * Only accept options in "supported".
3707 * If an option value is invalid return FAIL.
3708 */
3709 int
3710get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3711{
3712 typval_T *item;
3713 char_u *val;
3714 dict_T *dict;
3715 int todo;
3716 hashitem_T *hi;
3717 int part;
3718
3719 opt->jo_set = 0;
3720 if (tv->v_type == VAR_UNKNOWN)
3721 return OK;
3722 if (tv->v_type != VAR_DICT)
3723 {
3724 EMSG(_(e_invarg));
3725 return FAIL;
3726 }
3727 dict = tv->vval.v_dict;
3728 if (dict == NULL)
3729 return OK;
3730
3731 todo = (int)dict->dv_hashtab.ht_used;
3732 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3733 if (!HASHITEM_EMPTY(hi))
3734 {
3735 item = &dict_lookup(hi)->di_tv;
3736
3737 if (STRCMP(hi->hi_key, "mode") == 0)
3738 {
3739 if (!(supported & JO_MODE))
3740 break;
3741 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3742 return FAIL;
3743 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003744 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003745 {
3746 if (!(supported & JO_IN_MODE))
3747 break;
3748 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3749 == FAIL)
3750 return FAIL;
3751 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003752 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003753 {
3754 if (!(supported & JO_OUT_MODE))
3755 break;
3756 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3757 == FAIL)
3758 return FAIL;
3759 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003760 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003761 {
3762 if (!(supported & JO_ERR_MODE))
3763 break;
3764 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3765 == FAIL)
3766 return FAIL;
3767 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003768 else if (STRCMP(hi->hi_key, "in_io") == 0
3769 || STRCMP(hi->hi_key, "out_io") == 0
3770 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003771 {
3772 if (!(supported & JO_OUT_IO))
3773 break;
3774 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3775 return FAIL;
3776 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003777 else if (STRCMP(hi->hi_key, "in_name") == 0
3778 || STRCMP(hi->hi_key, "out_name") == 0
3779 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003780 {
3781 part = part_from_char(*hi->hi_key);
3782
3783 if (!(supported & JO_OUT_IO))
3784 break;
3785 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3786 opt->jo_io_name[part] =
3787 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3788 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003789 else if (STRCMP(hi->hi_key, "in_buf") == 0
3790 || STRCMP(hi->hi_key, "out_buf") == 0
3791 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003792 {
3793 part = part_from_char(*hi->hi_key);
3794
3795 if (!(supported & JO_OUT_IO))
3796 break;
3797 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3798 opt->jo_io_buf[part] = get_tv_number(item);
3799 if (opt->jo_io_buf[part] <= 0)
3800 {
3801 EMSG2(_(e_invarg2), get_tv_string(item));
3802 return FAIL;
3803 }
3804 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3805 {
3806 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3807 return FAIL;
3808 }
3809 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003810 else if (STRCMP(hi->hi_key, "in_top") == 0
3811 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003812 {
3813 linenr_T *lp;
3814
3815 if (!(supported & JO_OUT_IO))
3816 break;
3817 if (hi->hi_key[3] == 't')
3818 {
3819 lp = &opt->jo_in_top;
3820 opt->jo_set |= JO_IN_TOP;
3821 }
3822 else
3823 {
3824 lp = &opt->jo_in_bot;
3825 opt->jo_set |= JO_IN_BOT;
3826 }
3827 *lp = get_tv_number(item);
3828 if (*lp < 0)
3829 {
3830 EMSG2(_(e_invarg2), get_tv_string(item));
3831 return FAIL;
3832 }
3833 }
3834 else if (STRCMP(hi->hi_key, "channel") == 0)
3835 {
3836 if (!(supported & JO_OUT_IO))
3837 break;
3838 opt->jo_set |= JO_CHANNEL;
3839 if (item->v_type != VAR_CHANNEL)
3840 {
3841 EMSG2(_(e_invarg2), "channel");
3842 return FAIL;
3843 }
3844 opt->jo_channel = item->vval.v_channel;
3845 }
3846 else if (STRCMP(hi->hi_key, "callback") == 0)
3847 {
3848 if (!(supported & JO_CALLBACK))
3849 break;
3850 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003851 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852 if (opt->jo_callback == NULL)
3853 {
3854 EMSG2(_(e_invarg2), "callback");
3855 return FAIL;
3856 }
3857 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003858 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003859 {
3860 if (!(supported & JO_OUT_CALLBACK))
3861 break;
3862 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003863 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003864 if (opt->jo_out_cb == NULL)
3865 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003866 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003867 return FAIL;
3868 }
3869 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003870 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003871 {
3872 if (!(supported & JO_ERR_CALLBACK))
3873 break;
3874 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003875 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003876 if (opt->jo_err_cb == NULL)
3877 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003878 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003879 return FAIL;
3880 }
3881 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003882 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003883 {
3884 if (!(supported & JO_CLOSE_CALLBACK))
3885 break;
3886 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003887 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003888 if (opt->jo_close_cb == NULL)
3889 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003890 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003891 return FAIL;
3892 }
3893 }
3894 else if (STRCMP(hi->hi_key, "waittime") == 0)
3895 {
3896 if (!(supported & JO_WAITTIME))
3897 break;
3898 opt->jo_set |= JO_WAITTIME;
3899 opt->jo_waittime = get_tv_number(item);
3900 }
3901 else if (STRCMP(hi->hi_key, "timeout") == 0)
3902 {
3903 if (!(supported & JO_TIMEOUT))
3904 break;
3905 opt->jo_set |= JO_TIMEOUT;
3906 opt->jo_timeout = get_tv_number(item);
3907 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003908 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003909 {
3910 if (!(supported & JO_OUT_TIMEOUT))
3911 break;
3912 opt->jo_set |= JO_OUT_TIMEOUT;
3913 opt->jo_out_timeout = get_tv_number(item);
3914 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003915 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003916 {
3917 if (!(supported & JO_ERR_TIMEOUT))
3918 break;
3919 opt->jo_set |= JO_ERR_TIMEOUT;
3920 opt->jo_err_timeout = get_tv_number(item);
3921 }
3922 else if (STRCMP(hi->hi_key, "part") == 0)
3923 {
3924 if (!(supported & JO_PART))
3925 break;
3926 opt->jo_set |= JO_PART;
3927 val = get_tv_string(item);
3928 if (STRCMP(val, "err") == 0)
3929 opt->jo_part = PART_ERR;
3930 else
3931 {
3932 EMSG2(_(e_invarg2), val);
3933 return FAIL;
3934 }
3935 }
3936 else if (STRCMP(hi->hi_key, "id") == 0)
3937 {
3938 if (!(supported & JO_ID))
3939 break;
3940 opt->jo_set |= JO_ID;
3941 opt->jo_id = get_tv_number(item);
3942 }
3943 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3944 {
3945 if (!(supported & JO_STOPONEXIT))
3946 break;
3947 opt->jo_set |= JO_STOPONEXIT;
3948 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3949 opt->jo_soe_buf);
3950 if (opt->jo_stoponexit == NULL)
3951 {
3952 EMSG2(_(e_invarg2), "stoponexit");
3953 return FAIL;
3954 }
3955 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003956 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003957 {
3958 if (!(supported & JO_EXIT_CB))
3959 break;
3960 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003961 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3962 {
3963 opt->jo_exit_partial = item->vval.v_partial;
3964 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3965 }
3966 else
3967 opt->jo_exit_cb = get_tv_string_buf_chk(
3968 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003969 if (opt->jo_exit_cb == NULL)
3970 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003971 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003972 return FAIL;
3973 }
3974 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003975 else if (STRCMP(hi->hi_key, "block_write") == 0)
3976 {
3977 if (!(supported & JO_BLOCK_WRITE))
3978 break;
3979 opt->jo_set |= JO_BLOCK_WRITE;
3980 opt->jo_block_write = get_tv_number(item);
3981 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003982 else
3983 break;
3984 --todo;
3985 }
3986 if (todo > 0)
3987 {
3988 EMSG2(_(e_invarg2), hi->hi_key);
3989 return FAIL;
3990 }
3991
3992 return OK;
3993}
3994
3995/*
3996 * Get the channel from the argument.
3997 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02003998 * When "check_open" is TRUE check that the channel can be used.
3999 * When "reading" is TRUE "check_open" considers typeahead useful.
4000 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004001 */
4002 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004003get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004004{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004005 channel_T *channel = NULL;
4006 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007
4008 if (tv->v_type == VAR_JOB)
4009 {
4010 if (tv->vval.v_job != NULL)
4011 channel = tv->vval.v_job->jv_channel;
4012 }
4013 else if (tv->v_type == VAR_CHANNEL)
4014 {
4015 channel = tv->vval.v_channel;
4016 }
4017 else
4018 {
4019 EMSG2(_(e_invarg2), get_tv_string(tv));
4020 return NULL;
4021 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004022 if (channel != NULL && reading)
4023 has_readahead = channel_has_readahead(channel,
4024 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004025
Bram Moolenaar437905c2016-04-26 19:01:05 +02004026 if (check_open && (channel == NULL || (!channel_is_open(channel)
4027 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004028 {
4029 EMSG(_("E906: not an open channel"));
4030 return NULL;
4031 }
4032 return channel;
4033}
4034
4035static job_T *first_job = NULL;
4036
4037 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004038job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004039{
4040 ch_log(job->jv_channel, "Freeing job");
4041 if (job->jv_channel != NULL)
4042 {
4043 /* The link from the channel to the job doesn't count as a reference,
4044 * thus don't decrement the refcount of the job. The reference from
4045 * the job to the channel does count the refrence, decrement it and
4046 * NULL the reference. We don't set ch_job_killed, unreferencing the
4047 * job doesn't mean it stops running. */
4048 job->jv_channel->ch_job = NULL;
4049 channel_unref(job->jv_channel);
4050 }
4051 mch_clear_job(job);
4052
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004053 vim_free(job->jv_stoponexit);
4054 vim_free(job->jv_exit_cb);
4055 partial_unref(job->jv_exit_partial);
4056}
4057
4058 static void
4059job_free_job(job_T *job)
4060{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004061 if (job->jv_next != NULL)
4062 job->jv_next->jv_prev = job->jv_prev;
4063 if (job->jv_prev == NULL)
4064 first_job = job->jv_next;
4065 else
4066 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004067 vim_free(job);
4068}
4069
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004070 static void
4071job_free(job_T *job)
4072{
4073 if (!in_free_unref_items)
4074 {
4075 job_free_contents(job);
4076 job_free_job(job);
4077 }
4078}
4079
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004080/*
4081 * Return TRUE if the job should not be freed yet. Do not free the job when
4082 * it has not ended yet and there is a "stoponexit" flag or an exit callback.
4083 */
4084 static int
4085job_still_useful(job_T *job)
4086{
4087 return job->jv_status == JOB_STARTED
4088 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4089}
4090
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004091 void
4092job_unref(job_T *job)
4093{
4094 if (job != NULL && --job->jv_refcount <= 0)
4095 {
4096 /* Do not free the job when it has not ended yet and there is a
4097 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004098 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004099 {
4100 job_free(job);
4101 }
4102 else if (job->jv_channel != NULL)
4103 {
4104 /* Do remove the link to the channel, otherwise it hangs
4105 * around until Vim exits. See job_free() for refcount. */
4106 job->jv_channel->ch_job = NULL;
4107 channel_unref(job->jv_channel);
4108 job->jv_channel = NULL;
4109 }
4110 }
4111}
4112
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004113 int
4114free_unused_jobs_contents(int copyID, int mask)
4115{
4116 int did_free = FALSE;
4117 job_T *job;
4118
4119 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004120 if ((job->jv_copyID & mask) != (copyID & mask)
4121 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004122 {
4123 /* Free the channel and ordinary items it contains, but don't
4124 * recurse into Lists, Dictionaries etc. */
4125 job_free_contents(job);
4126 did_free = TRUE;
4127 }
4128 return did_free;
4129}
4130
4131 void
4132free_unused_jobs(int copyID, int mask)
4133{
4134 job_T *job;
4135 job_T *job_next;
4136
4137 for (job = first_job; job != NULL; job = job_next)
4138 {
4139 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004140 if ((job->jv_copyID & mask) != (copyID & mask)
4141 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004142 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004143 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 job_free_job(job);
4145 }
4146 }
4147}
4148
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004149/*
4150 * Allocate a job. Sets the refcount to one and sets options default.
4151 */
4152 static job_T *
4153job_alloc(void)
4154{
4155 job_T *job;
4156
4157 job = (job_T *)alloc_clear(sizeof(job_T));
4158 if (job != NULL)
4159 {
4160 job->jv_refcount = 1;
4161 job->jv_stoponexit = vim_strsave((char_u *)"term");
4162
4163 if (first_job != NULL)
4164 {
4165 first_job->jv_prev = job;
4166 job->jv_next = first_job;
4167 }
4168 first_job = job;
4169 }
4170 return job;
4171}
4172
4173 void
4174job_set_options(job_T *job, jobopt_T *opt)
4175{
4176 if (opt->jo_set & JO_STOPONEXIT)
4177 {
4178 vim_free(job->jv_stoponexit);
4179 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4180 job->jv_stoponexit = NULL;
4181 else
4182 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4183 }
4184 if (opt->jo_set & JO_EXIT_CB)
4185 {
4186 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004187 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004188 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004189 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004190 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004191 job->jv_exit_partial = NULL;
4192 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004193 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004194 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004195 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004196 job->jv_exit_partial = opt->jo_exit_partial;
4197 if (job->jv_exit_partial != NULL)
4198 ++job->jv_exit_partial->pt_refcount;
4199 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 }
4201}
4202
4203/*
4204 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4205 */
4206 void
4207job_stop_on_exit()
4208{
4209 job_T *job;
4210
4211 for (job = first_job; job != NULL; job = job->jv_next)
4212 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4213 mch_stop_job(job, job->jv_stoponexit);
4214}
4215
4216/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004217 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 */
4219 void
4220job_check_ended(void)
4221{
4222 static time_t last_check = 0;
4223 time_t now;
4224 job_T *job;
4225 job_T *next;
4226
4227 /* Only do this once in 10 seconds. */
4228 now = time(NULL);
4229 if (last_check + 10 < now)
4230 {
4231 last_check = now;
4232 for (job = first_job; job != NULL; job = next)
4233 {
4234 next = job->jv_next;
4235 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4236 job_status(job); /* may free "job" */
4237 }
4238 }
4239}
4240
4241/*
4242 * "job_start()" function
4243 */
4244 job_T *
4245job_start(typval_T *argvars)
4246{
4247 job_T *job;
4248 char_u *cmd = NULL;
4249#if defined(UNIX)
4250# define USE_ARGV
4251 char **argv = NULL;
4252 int argc = 0;
4253#else
4254 garray_T ga;
4255#endif
4256 jobopt_T opt;
4257 int part;
4258
4259 job = job_alloc();
4260 if (job == NULL)
4261 return NULL;
4262
4263 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004264#ifndef USE_ARGV
4265 ga_init2(&ga, (int)sizeof(char*), 20);
4266#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004267
4268 /* Default mode is NL. */
4269 clear_job_options(&opt);
4270 opt.jo_mode = MODE_NL;
4271 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004272 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4273 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004274 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004275
4276 /* Check that when io is "file" that there is a file name. */
4277 for (part = PART_OUT; part <= PART_IN; ++part)
4278 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4279 && opt.jo_io[part] == JIO_FILE
4280 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4281 || *opt.jo_io_name[part] == NUL))
4282 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004283 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004284 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004285 }
4286
4287 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4288 {
4289 buf_T *buf = NULL;
4290
4291 /* check that we can find the buffer before starting the job */
4292 if (opt.jo_set & JO_IN_BUF)
4293 {
4294 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4295 if (buf == NULL)
4296 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4297 }
4298 else if (!(opt.jo_set & JO_IN_NAME))
4299 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004300 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004301 }
4302 else
4303 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4304 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004305 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004306 if (buf->b_ml.ml_mfp == NULL)
4307 {
4308 char_u numbuf[NUMBUFLEN];
4309 char_u *s;
4310
4311 if (opt.jo_set & JO_IN_BUF)
4312 {
4313 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4314 s = numbuf;
4315 }
4316 else
4317 s = opt.jo_io_name[PART_IN];
4318 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004319 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004320 }
4321 job->jv_in_buf = buf;
4322 }
4323
4324 job_set_options(job, &opt);
4325
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 if (argvars[0].v_type == VAR_STRING)
4327 {
4328 /* Command is a string. */
4329 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004330 if (cmd == NULL || *cmd == NUL)
4331 {
4332 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004333 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004334 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004335#ifdef USE_ARGV
4336 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004337 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004338 argv[argc] = NULL;
4339#endif
4340 }
4341 else if (argvars[0].v_type != VAR_LIST
4342 || argvars[0].vval.v_list == NULL
4343 || argvars[0].vval.v_list->lv_len < 1)
4344 {
4345 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004346 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004347 }
4348 else
4349 {
4350 list_T *l = argvars[0].vval.v_list;
4351 listitem_T *li;
4352 char_u *s;
4353
4354#ifdef USE_ARGV
4355 /* Pass argv[] to mch_call_shell(). */
4356 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4357 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004358 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004359#endif
4360 for (li = l->lv_first; li != NULL; li = li->li_next)
4361 {
4362 s = get_tv_string_chk(&li->li_tv);
4363 if (s == NULL)
4364 goto theend;
4365#ifdef USE_ARGV
4366 argv[argc++] = (char *)s;
4367#else
4368 /* Only escape when needed, double quotes are not always allowed. */
4369 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4370 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004371# ifdef WIN32
4372 int old_ssl = p_ssl;
4373
4374 /* This is using CreateProcess, not cmd.exe. Always use
4375 * double quote and backslashes. */
4376 p_ssl = 0;
4377# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004378 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004379# ifdef WIN32
4380 p_ssl = old_ssl;
4381# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004382 if (s == NULL)
4383 goto theend;
4384 ga_concat(&ga, s);
4385 vim_free(s);
4386 }
4387 else
4388 ga_concat(&ga, s);
4389 if (li->li_next != NULL)
4390 ga_append(&ga, ' ');
4391#endif
4392 }
4393#ifdef USE_ARGV
4394 argv[argc] = NULL;
4395#else
4396 cmd = ga.ga_data;
4397#endif
4398 }
4399
4400#ifdef USE_ARGV
4401 if (ch_log_active())
4402 {
4403 garray_T ga;
4404 int i;
4405
4406 ga_init2(&ga, (int)sizeof(char), 200);
4407 for (i = 0; i < argc; ++i)
4408 {
4409 if (i > 0)
4410 ga_concat(&ga, (char_u *)" ");
4411 ga_concat(&ga, (char_u *)argv[i]);
4412 }
4413 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4414 ga_clear(&ga);
4415 }
4416 mch_start_job(argv, job, &opt);
4417#else
4418 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4419 mch_start_job((char *)cmd, job, &opt);
4420#endif
4421
4422 /* If the channel is reading from a buffer, write lines now. */
4423 if (job->jv_channel != NULL)
4424 channel_write_in(job->jv_channel);
4425
4426theend:
4427#ifdef USE_ARGV
4428 vim_free(argv);
4429#else
4430 vim_free(ga.ga_data);
4431#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004432 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004433 return job;
4434}
4435
4436/*
4437 * Get the status of "job" and invoke the exit callback when needed.
4438 * The returned string is not allocated.
4439 */
4440 char *
4441job_status(job_T *job)
4442{
4443 char *result;
4444
4445 if (job->jv_status == JOB_ENDED)
4446 /* No need to check, dead is dead. */
4447 result = "dead";
4448 else if (job->jv_status == JOB_FAILED)
4449 result = "fail";
4450 else
4451 {
4452 result = mch_job_status(job);
4453 if (job->jv_status == JOB_ENDED)
4454 ch_log(job->jv_channel, "Job ended");
4455 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4456 {
4457 typval_T argv[3];
4458 typval_T rettv;
4459 int dummy;
4460
4461 /* invoke the exit callback; make sure the refcount is > 0 */
4462 ++job->jv_refcount;
4463 argv[0].v_type = VAR_JOB;
4464 argv[0].vval.v_job = job;
4465 argv[1].v_type = VAR_NUMBER;
4466 argv[1].vval.v_number = job->jv_exitval;
4467 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004468 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4469 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004470 clear_tv(&rettv);
4471 --job->jv_refcount;
4472 }
4473 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4474 {
4475 /* The job was already unreferenced, now that it ended it can be
4476 * freed. Careful: caller must not use "job" after this! */
4477 job_free(job);
4478 }
4479 }
4480 return result;
4481}
4482
Bram Moolenaar8950a562016-03-12 15:22:55 +01004483/*
4484 * Implementation of job_info().
4485 */
4486 void
4487job_info(job_T *job, dict_T *dict)
4488{
4489 dictitem_T *item;
4490 varnumber_T nr;
4491
4492 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4493
4494 item = dictitem_alloc((char_u *)"channel");
4495 if (item == NULL)
4496 return;
4497 item->di_tv.v_lock = 0;
4498 item->di_tv.v_type = VAR_CHANNEL;
4499 item->di_tv.vval.v_channel = job->jv_channel;
4500 if (job->jv_channel != NULL)
4501 ++job->jv_channel->ch_refcount;
4502 if (dict_add(dict, item) == FAIL)
4503 dictitem_free(item);
4504
4505#ifdef UNIX
4506 nr = job->jv_pid;
4507#else
4508 nr = job->jv_proc_info.dwProcessId;
4509#endif
4510 dict_add_nr_str(dict, "process", nr, NULL);
4511
4512 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004513 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004514 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4515}
4516
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004517 int
4518job_stop(job_T *job, typval_T *argvars)
4519{
4520 char_u *arg;
4521
4522 if (argvars[1].v_type == VAR_UNKNOWN)
4523 arg = (char_u *)"";
4524 else
4525 {
4526 arg = get_tv_string_chk(&argvars[1]);
4527 if (arg == NULL)
4528 {
4529 EMSG(_(e_invarg));
4530 return 0;
4531 }
4532 }
4533 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4534 if (mch_stop_job(job, arg) == FAIL)
4535 return 0;
4536
4537 /* Assume that "hup" does not kill the job. */
4538 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4539 job->jv_channel->ch_job_killed = TRUE;
4540
4541 /* We don't try freeing the job, obviously the caller still has a
4542 * reference to it. */
4543 return 1;
4544}
4545
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004546#endif /* FEAT_JOB_CHANNEL */