blob: 444599c7c2812406a45d5731d9587bf70dd89769 [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)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200442 if (!channel_still_useful(ch)
443 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200444 {
445 /* Free the channel and ordinary items it contains, but don't
446 * recurse into Lists, Dictionaries etc. */
447 channel_free_contents(ch);
448 did_free = TRUE;
449 }
450 return did_free;
451}
452
453 void
454free_unused_channels(int copyID, int mask)
455{
456 channel_T *ch;
457 channel_T *ch_next;
458
459 for (ch = first_channel; ch != NULL; ch = ch_next)
460 {
461 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200462 if (!channel_still_useful(ch)
463 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200465 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 channel_free_channel(ch);
467 }
468 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100469}
470
Bram Moolenaard04a0202016-01-26 23:30:18 +0100471#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100472
473#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
474 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100476{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100477 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100478 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100479
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100480 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100481 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100482 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100483 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100484 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100485}
486#endif
487
Bram Moolenaare0874f82016-01-24 20:36:41 +0100488/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100489 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100490 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100491#ifdef FEAT_GUI_X11
492 static void
493messageFromNetbeans(XtPointer clientData,
494 int *unused1 UNUSED,
495 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100502# if GTK_CHECK_VERSION(3,0,0)
503 static gboolean
504messageFromNetbeans(GIOChannel *unused1 UNUSED,
505 GIOCondition unused2 UNUSED,
506 gpointer clientData)
507{
508 channel_read_fd(GPOINTER_TO_INT(clientData));
509 return TRUE; /* Return FALSE instead in case the event source is to
510 * be removed after this function returns. */
511}
512# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 static void
514messageFromNetbeans(gpointer clientData,
515 gint unused1 UNUSED,
516 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100518 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519}
Bram Moolenaar98921892016-02-23 17:14:37 +0100520# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100521#endif
522
523 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100524channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100525{
Bram Moolenaarde279892016-03-11 22:19:44 +0100526 if (!CH_HAS_GUI)
527 return;
528
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100529# ifdef FEAT_GUI_X11
530 /* Tell notifier we are interested in being called
531 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100532 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
533 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100534 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100535 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100536 (XtPointer)(XtInputReadMask + XtInputExceptMask),
537 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100538 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100539# else
540# ifdef FEAT_GUI_GTK
541 /* Tell gdk we are interested in being called when there
542 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100543 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100544# if GTK_CHECK_VERSION(3,0,0)
545 {
546 GIOChannel *chnnl = g_io_channel_unix_new(
547 (gint)channel->ch_part[part].ch_fd);
548
549 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
550 chnnl,
551 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
552 messageFromNetbeans,
553 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
554
555 g_io_channel_unref(chnnl);
556 }
557# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100558 channel->ch_part[part].ch_inputHandler = gdk_input_add(
559 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100560 (GdkInputCondition)
561 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100562 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100563 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100564# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100565# endif
566# endif
567}
568
Bram Moolenaarde279892016-03-11 22:19:44 +0100569 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100570channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100571{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100572 if (channel->CH_SOCK_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100574 if (channel->CH_OUT_FD != INVALID_FD)
575 channel_gui_register_one(channel, PART_OUT);
576 if (channel->CH_ERR_FD != INVALID_FD)
577 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100578}
579
580/*
581 * Register any of our file descriptors with the GUI event handling system.
582 * Called when the GUI has started.
583 */
584 void
585channel_gui_register_all(void)
586{
Bram Moolenaar77073442016-02-13 23:23:53 +0100587 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100588
Bram Moolenaar77073442016-02-13 23:23:53 +0100589 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100590 channel_gui_register(channel);
591}
592
593 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100594channel_gui_unregister_one(channel_T *channel, int part)
595{
596# ifdef FEAT_GUI_X11
597 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
598 {
599 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
600 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
601 }
602# else
603# ifdef FEAT_GUI_GTK
604 if (channel->ch_part[part].ch_inputHandler != 0)
605 {
606# if GTK_CHECK_VERSION(3,0,0)
607 g_source_remove(channel->ch_part[part].ch_inputHandler);
608# else
609 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
610# endif
611 channel->ch_part[part].ch_inputHandler = 0;
612 }
613# endif
614# endif
615}
616
617 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100618channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100619{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100620 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100621
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100622 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100623 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100624}
625
626#endif
627
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100628static char *e_cannot_connect = N_("E902: Cannot connect to port");
629
Bram Moolenaard04a0202016-01-26 23:30:18 +0100630/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100631 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100632 * "waittime" is the time in msec to wait for the connection.
633 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100634 * Returns the channel for success.
635 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100636 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100638channel_open(
639 char *hostname,
640 int port_in,
641 int waittime,
642 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100643{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100645 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100646 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100647#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100649 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100650#else
651 int port = port_in;
652#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100653 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100654 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100656#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100657 channel_init_winsock();
658#endif
659
Bram Moolenaar77073442016-02-13 23:23:53 +0100660 channel = add_channel();
661 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100663 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100664 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100665 }
666
667 /* Get the server internet address and put into addr structure */
668 /* fill in the socket address structure and connect to server */
669 vim_memset((char *)&server, 0, sizeof(server));
670 server.sin_family = AF_INET;
671 server.sin_port = htons(port);
672 if ((host = gethostbyname(hostname)) == NULL)
673 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100675 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100676 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678 }
679 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
680
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100681 /* On Mac and Solaris a zero timeout almost never works. At least wait
682 * one millisecond. Let's do it for all systems, because we don't know why
683 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684 if (waittime == 0)
685 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100686
687 /*
688 * For Unix we need to call connect() again after connect() failed.
689 * On Win32 one time is sufficient.
690 */
691 while (TRUE)
692 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100693 long elapsed_msec = 0;
694 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100695
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 sd = socket(AF_INET, SOCK_STREAM, 0);
699 if (sd == -1)
700 {
701 ch_error(channel, "in socket() in channel_open().");
702 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100703 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100704 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100706
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100707 if (waittime >= 0)
708 {
709 /* Make connect() non-blocking. */
710 if (
711#ifdef _WIN32
712 ioctlsocket(sd, FIONBIO, &val) < 0
713#else
714 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
715#endif
716 )
717 {
718 SOCK_ERRNO;
719 ch_errorn(channel,
720 "channel_open: Connect failed with errno %d", errno);
721 sock_close(sd);
722 channel_free(channel);
723 return NULL;
724 }
725 }
726
727 /* Try connecting to the server. */
728 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
729 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
730
Bram Moolenaar045a2842016-03-08 22:33:07 +0100731 if (ret == 0)
732 /* The connection could be established. */
733 break;
734
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100735 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736 if (waittime < 0 || (errno != EWOULDBLOCK
737 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100738#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100740#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100741 ))
742 {
743 ch_errorn(channel,
744 "channel_open: Connect failed with errno %d", errno);
745 PERROR(_(e_cannot_connect));
746 sock_close(sd);
747 channel_free(channel);
748 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100749 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100751 /* Limit the waittime to 50 msec. If it doesn't work within this
752 * time we close the socket and try creating it again. */
753 waitnow = waittime > 50 ? 50 : waittime;
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100756 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100757#ifndef WIN32
758 if (errno != ECONNREFUSED)
759#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100760 {
761 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100762 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100764#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100765 int so_error = 0;
766 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 struct timeval start_tv;
768 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100769#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 FD_ZERO(&rfds);
771 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100772 FD_ZERO(&wfds);
773 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100774
Bram Moolenaar562ca712016-03-09 21:50:05 +0100775 tv.tv_sec = waitnow / 1000;
776 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100777#ifndef WIN32
778 gettimeofday(&start_tv, NULL);
779#endif
780 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100781 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100782 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100783
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 if (ret < 0)
785 {
786 SOCK_ERRNO;
787 ch_errorn(channel,
788 "channel_open: Connect failed with errno %d", errno);
789 PERROR(_(e_cannot_connect));
790 sock_close(sd);
791 channel_free(channel);
792 return NULL;
793 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100794
Bram Moolenaare081e212016-02-28 22:33:46 +0100795#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100796 /* On Win32: select() is expected to work and wait for up to
797 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798 if (FD_ISSET(sd, &wfds))
799 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100800 elapsed_msec = waitnow;
801 if (waittime > 1 && elapsed_msec < waittime)
802 {
803 waittime -= elapsed_msec;
804 continue;
805 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100806#else
807 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 * After putting the socket in non-blocking mode, connect() will
809 * return EINPROGRESS, select() will not wait (as if writing is
810 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100811 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100814 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 {
816 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 if (ret < 0 || (so_error != 0
819 && so_error != EWOULDBLOCK
820 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100821# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100823# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824 ))
825 {
826 ch_errorn(channel,
827 "channel_open: Connect failed with errno %d",
828 so_error);
829 PERROR(_(e_cannot_connect));
830 sock_close(sd);
831 channel_free(channel);
832 return NULL;
833 }
834 }
835
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 if (FD_ISSET(sd, &wfds) && so_error == 0)
837 /* Did not detect an error, connection is established. */
838 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839
Bram Moolenaar045a2842016-03-08 22:33:07 +0100840 gettimeofday(&end_tv, NULL);
841 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
842 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100843#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100844 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845
846#ifndef WIN32
847 if (waittime > 1 && elapsed_msec < waittime)
848 {
849 /* The port isn't ready but we also didn't get an error.
850 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100851 * yet. Select() may return early, wait until the remaining
852 * "waitnow" and try again. */
853 waitnow -= elapsed_msec;
854 waittime -= elapsed_msec;
855 if (waitnow > 0)
856 {
857 mch_delay((long)waitnow, TRUE);
858 ui_breakcheck();
859 waittime -= waitnow;
860 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100861 if (!got_int)
862 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100863 if (waittime <= 0)
864 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100865 waittime = 1;
866 continue;
867 }
868 /* we were interrupted, behave as if timed out */
869 }
870#endif
871
872 /* We timed out. */
873 ch_error(channel, "Connection timed out");
874 sock_close(sd);
875 channel_free(channel);
876 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100877 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100878
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879 ch_log(channel, "Connection made");
880
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100881 if (waittime >= 0)
882 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100883#ifdef _WIN32
884 val = 0;
885 ioctlsocket(sd, FIONBIO, &val);
886#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100887 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100888#endif
889 }
890
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100891 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100892 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100893 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
894 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100895
896#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100897 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100898#endif
899
Bram Moolenaar77073442016-02-13 23:23:53 +0100900 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100901}
902
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100903/*
904 * Implements ch_open().
905 */
906 channel_T *
907channel_open_func(typval_T *argvars)
908{
909 char_u *address;
910 char_u *p;
911 char *rest;
912 int port;
913 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200914 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100915
916 address = get_tv_string(&argvars[0]);
917 if (argvars[1].v_type != VAR_UNKNOWN
918 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
919 {
920 EMSG(_(e_invarg));
921 return NULL;
922 }
923
924 /* parse address */
925 p = vim_strchr(address, ':');
926 if (p == NULL)
927 {
928 EMSG2(_(e_invarg2), address);
929 return NULL;
930 }
931 *p++ = NUL;
932 port = strtol((char *)p, &rest, 10);
933 if (*address == NUL || port <= 0 || *rest != NUL)
934 {
935 p[-1] = ':';
936 EMSG2(_(e_invarg2), address);
937 return NULL;
938 }
939
940 /* parse options */
941 clear_job_options(&opt);
942 opt.jo_mode = MODE_JSON;
943 opt.jo_timeout = 2000;
944 if (get_job_options(&argvars[1], &opt,
945 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200946 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100947 if (opt.jo_timeout < 0)
948 {
949 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200950 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100951 }
952
953 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
954 if (channel != NULL)
955 {
956 opt.jo_set = JO_ALL;
957 channel_set_options(channel, &opt);
958 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200959theend:
960 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100961 return channel;
962}
963
Bram Moolenaarde279892016-03-11 22:19:44 +0100964 static void
965may_close_part(sock_T *fd)
966{
967 if (*fd != INVALID_FD)
968 {
969 fd_close(*fd);
970 *fd = INVALID_FD;
971 }
972}
973
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100974 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100975channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100976{
Bram Moolenaarde279892016-03-11 22:19:44 +0100977 if (in != INVALID_FD)
978 {
979 may_close_part(&channel->CH_IN_FD);
980 channel->CH_IN_FD = in;
981 }
982 if (out != INVALID_FD)
983 {
984# if defined(FEAT_GUI)
985 channel_gui_unregister_one(channel, PART_OUT);
986# endif
987 may_close_part(&channel->CH_OUT_FD);
988 channel->CH_OUT_FD = out;
989# if defined(FEAT_GUI)
990 channel_gui_register_one(channel, PART_OUT);
991# endif
992 }
993 if (err != INVALID_FD)
994 {
995# if defined(FEAT_GUI)
996 channel_gui_unregister_one(channel, PART_ERR);
997# endif
998 may_close_part(&channel->CH_ERR_FD);
999 channel->CH_ERR_FD = err;
1000# if defined(FEAT_GUI)
1001 channel_gui_register_one(channel, PART_ERR);
1002# endif
1003 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001004}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001005
Bram Moolenaard6051b52016-02-28 15:49:03 +01001006/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001007 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001008 * This does not keep a refcount, when the job is freed ch_job is cleared.
1009 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001011channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001012{
Bram Moolenaar77073442016-02-13 23:23:53 +01001013 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001014
1015 channel_set_options(channel, options);
1016
1017 if (job->jv_in_buf != NULL)
1018 {
1019 chanpart_T *in_part = &channel->ch_part[PART_IN];
1020
1021 in_part->ch_buffer = job->jv_in_buf;
1022 ch_logs(channel, "reading from buffer '%s'",
1023 (char *)in_part->ch_buffer->b_ffname);
1024 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001025 {
1026 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1027 {
1028 /* Special mode: send last-but-one line when appending a line
1029 * to the buffer. */
1030 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001031 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001032 in_part->ch_buf_top =
1033 in_part->ch_buffer->b_ml.ml_line_count + 1;
1034 }
1035 else
1036 in_part->ch_buf_top = options->jo_in_top;
1037 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038 else
1039 in_part->ch_buf_top = 1;
1040 if (options->jo_set & JO_IN_BOT)
1041 in_part->ch_buf_bot = options->jo_in_bot;
1042 else
1043 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1044 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001045}
1046
1047/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001048 * Find a buffer matching "name" or create a new one.
1049 */
1050 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001051find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001052{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001053 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001054 buf_T *save_curbuf = curbuf;
1055
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001056 if (name != NULL && *name != NUL)
1057 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001058 if (buf == NULL)
1059 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001060 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001061 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001062 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001063 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001064#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001065 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1066 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001067#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001068 if (curbuf->b_ml.ml_mfp == NULL)
1069 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001070 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1071 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 changed_bytes(1, 0);
1073 curbuf = save_curbuf;
1074 }
1075
1076 return buf;
1077}
1078
1079/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001080 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001081 */
1082 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001083channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001084{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001085 int part;
1086 char_u **cbp;
1087 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001088
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001089 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001090 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001091 channel->ch_part[part].ch_mode = opt->jo_mode;
1092 if (opt->jo_set & JO_IN_MODE)
1093 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1094 if (opt->jo_set & JO_OUT_MODE)
1095 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1096 if (opt->jo_set & JO_ERR_MODE)
1097 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001098
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001099 if (opt->jo_set & JO_TIMEOUT)
1100 for (part = PART_SOCK; part <= PART_IN; ++part)
1101 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1102 if (opt->jo_set & JO_OUT_TIMEOUT)
1103 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1104 if (opt->jo_set & JO_ERR_TIMEOUT)
1105 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001106 if (opt->jo_set & JO_BLOCK_WRITE)
1107 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001108
1109 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001110 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001113 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001114 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001115 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1116 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001117 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001119 *pp = opt->jo_partial;
1120 if (*pp != NULL)
1121 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001122 }
1123 if (opt->jo_set & JO_OUT_CALLBACK)
1124 {
1125 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001126 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001127 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001128 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001129 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1130 *cbp = vim_strsave(opt->jo_out_cb);
1131 else
1132 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001133 *pp = opt->jo_out_partial;
1134 if (*pp != NULL)
1135 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001136 }
1137 if (opt->jo_set & JO_ERR_CALLBACK)
1138 {
1139 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001140 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001141 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001142 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1144 *cbp = vim_strsave(opt->jo_err_cb);
1145 else
1146 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001147 *pp = opt->jo_err_partial;
1148 if (*pp != NULL)
1149 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001150 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001151 if (opt->jo_set & JO_CLOSE_CALLBACK)
1152 {
1153 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001155 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001156 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001157 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1158 *cbp = vim_strsave(opt->jo_close_cb);
1159 else
1160 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001161 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001162 if (*pp != NULL)
1163 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001164 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001165
1166 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1167 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001168 /* writing output to a buffer. Default mode is NL. */
1169 if (!(opt->jo_set & JO_OUT_MODE))
1170 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001171 if (opt->jo_set & JO_OUT_BUF)
1172 channel->ch_part[PART_OUT].ch_buffer =
1173 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1174 else
1175 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001176 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1177 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001178 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1179 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001180
1181 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1182 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1183 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1184 {
1185 /* writing err to a buffer. Default mode is NL. */
1186 if (!(opt->jo_set & JO_ERR_MODE))
1187 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1188 if (opt->jo_io[PART_ERR] == JIO_OUT)
1189 channel->ch_part[PART_ERR].ch_buffer =
1190 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001191 else if (opt->jo_set & JO_ERR_BUF)
1192 channel->ch_part[PART_ERR].ch_buffer =
1193 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001194 else
1195 channel->ch_part[PART_ERR].ch_buffer =
1196 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1197 ch_logs(channel, "writing err to buffer '%s'",
1198 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1199 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001200
1201 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1202 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1203 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001204}
1205
1206/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001207 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001208 */
1209 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001210channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001211 channel_T *channel,
1212 int part,
1213 char_u *callback,
1214 partial_T *partial,
1215 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001216{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001217 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001218 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1219
1220 if (item != NULL)
1221 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001222 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001223 item->cq_partial = partial;
1224 if (partial != NULL)
1225 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001226 item->cq_seq_nr = id;
1227 item->cq_prev = head->cq_prev;
1228 head->cq_prev = item;
1229 item->cq_next = NULL;
1230 if (item->cq_prev == NULL)
1231 head->cq_next = item;
1232 else
1233 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001234 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001235}
1236
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001237 static void
1238write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1239{
1240 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001241 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001242 char_u *p;
1243
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001244 if ((p = alloc(len + 2)) == NULL)
1245 return;
1246 STRCPY(p, line);
1247 p[len] = NL;
1248 p[len + 1] = NUL;
1249 channel_send(channel, PART_IN, p, "write_buf_line()");
1250 vim_free(p);
1251}
1252
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001253/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001254 * Return TRUE if "channel" can be written to.
1255 * Returns FALSE if the input is closed or the write would block.
1256 */
1257 static int
1258can_write_buf_line(channel_T *channel)
1259{
1260 chanpart_T *in_part = &channel->ch_part[PART_IN];
1261
1262 if (in_part->ch_fd == INVALID_FD)
1263 return FALSE; /* pipe was closed */
1264
1265 /* for testing: block every other attempt to write */
1266 if (in_part->ch_block_write == 1)
1267 in_part->ch_block_write = -1;
1268 else if (in_part->ch_block_write == -1)
1269 in_part->ch_block_write = 1;
1270
1271 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1272#ifndef WIN32
1273 {
1274# if defined(HAVE_SELECT)
1275 struct timeval tval;
1276 fd_set wfds;
1277 int ret;
1278
1279 FD_ZERO(&wfds);
1280 FD_SET((int)in_part->ch_fd, &wfds);
1281 tval.tv_sec = 0;
1282 tval.tv_usec = 0;
1283 for (;;)
1284 {
1285 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1286# ifdef EINTR
1287 SOCK_ERRNO;
1288 if (ret == -1 && errno == EINTR)
1289 continue;
1290# endif
1291 if (ret <= 0 || in_part->ch_block_write == 1)
1292 {
1293 if (ret > 0)
1294 ch_log(channel, "FAKED Input not ready for writing");
1295 else
1296 ch_log(channel, "Input not ready for writing");
1297 return FALSE;
1298 }
1299 break;
1300 }
1301# else
1302 struct pollfd fds;
1303
1304 fds.fd = in_part->ch_fd;
1305 fds.events = POLLOUT;
1306 if (poll(&fds, 1, 0) <= 0)
1307 {
1308 ch_log(channel, "Input not ready for writing");
1309 return FALSE;
1310 }
1311 if (in_part->ch_block_write == 1)
1312 {
1313 ch_log(channel, "FAKED Input not ready for writing");
1314 return FALSE;
1315 }
1316# endif
1317 }
1318#endif
1319 return TRUE;
1320}
1321
1322/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001323 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001324 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001325 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001326channel_write_in(channel_T *channel)
1327{
1328 chanpart_T *in_part = &channel->ch_part[PART_IN];
1329 linenr_T lnum;
1330 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001331 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001332
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001333 if (buf == NULL || in_part->ch_buf_append)
1334 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001335 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1336 {
1337 /* buffer was wiped out or unloaded */
1338 in_part->ch_buffer = NULL;
1339 return;
1340 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001341
1342 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1343 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1344 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001345 if (!can_write_buf_line(channel))
1346 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001347 write_buf_line(buf, lnum, channel);
1348 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001349 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001350
1351 if (written == 1)
1352 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1353 else if (written > 1)
1354 ch_logn(channel, "written %d lines to channel", written);
1355
Bram Moolenaar014069a2016-03-03 22:51:40 +01001356 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001357 if (lnum > buf->b_ml.ml_line_count)
1358 {
1359 /* Writing is done, no longer need the buffer. */
1360 in_part->ch_buffer = NULL;
1361 ch_log(channel, "Finished writing all lines to channel");
1362 }
1363 else
1364 ch_logn(channel, "Still %d more lines to write",
1365 buf->b_ml.ml_line_count - lnum + 1);
1366}
1367
1368/*
1369 * Write any lines waiting to be written to a channel.
1370 */
1371 void
1372channel_write_any_lines()
1373{
1374 channel_T *channel;
1375
1376 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1377 {
1378 chanpart_T *in_part = &channel->ch_part[PART_IN];
1379
1380 if (in_part->ch_buffer != NULL)
1381 {
1382 if (in_part->ch_buf_append)
1383 channel_write_new_lines(in_part->ch_buffer);
1384 else
1385 channel_write_in(channel);
1386 }
1387 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001388}
1389
1390/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 * Write appended lines above the last one in "buf" to the channel.
1392 */
1393 void
1394channel_write_new_lines(buf_T *buf)
1395{
1396 channel_T *channel;
1397 int found_one = FALSE;
1398
1399 /* There could be more than one channel for the buffer, loop over all of
1400 * them. */
1401 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1402 {
1403 chanpart_T *in_part = &channel->ch_part[PART_IN];
1404 linenr_T lnum;
1405 int written = 0;
1406
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001408 {
1409 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001410 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 found_one = TRUE;
1412 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1413 ++lnum)
1414 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 if (!can_write_buf_line(channel))
1416 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 write_buf_line(buf, lnum, channel);
1418 ++written;
1419 }
1420
1421 if (written == 1)
1422 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1423 else if (written > 1)
1424 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001425 if (lnum < buf->b_ml.ml_line_count)
1426 ch_logn(channel, "Still %d more lines to write",
1427 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001428
1429 in_part->ch_buf_bot = lnum;
1430 }
1431 }
1432 if (!found_one)
1433 buf->b_write_to_channel = FALSE;
1434}
1435
1436/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001437 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001438 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001439 */
1440 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001441invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1442 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001443{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001444 typval_T rettv;
1445 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001446
Bram Moolenaar77073442016-02-13 23:23:53 +01001447 argv[0].v_type = VAR_CHANNEL;
1448 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001449
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001450 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001451 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001452 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001453 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001454}
1455
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001456/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001457 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001458 * The caller must free it.
1459 * Returns NULL if there is nothing.
1460 */
1461 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001462channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001463{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001464 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001465 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 char_u *p;
1467
Bram Moolenaar77073442016-02-13 23:23:53 +01001468 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001469 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001470 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001471 p = node->rq_buffer;
1472 head->rq_next = node->rq_next;
1473 if (node->rq_next == NULL)
1474 head->rq_prev = NULL;
1475 else
1476 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001477 vim_free(node);
1478 return p;
1479}
1480
1481/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001482 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001483 */
1484 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001485channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001486{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001487 readq_T *head = &channel->ch_part[part].ch_head;
1488 readq_T *node = head->rq_next;
1489 long_u len = 1;
1490 char_u *res;
1491 char_u *p;
1492
1493 /* If there is only one buffer just get that one. */
1494 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1495 return channel_get(channel, part);
1496
1497 /* Concatenate everything into one buffer. */
1498 for (node = head->rq_next; node != NULL; node = node->rq_next)
1499 len += (long_u)STRLEN(node->rq_buffer);
1500 res = lalloc(len, TRUE);
1501 if (res == NULL)
1502 return NULL;
1503 *res = NUL;
1504 for (node = head->rq_next; node != NULL; node = node->rq_next)
1505 STRCAT(res, node->rq_buffer);
1506
1507 /* Free all buffers */
1508 do
1509 {
1510 p = channel_get(channel, part);
1511 vim_free(p);
1512 } while (p != NULL);
1513
1514 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001515}
1516
1517/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001518 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001519 * Returns FAIL if that is not possible.
1520 */
1521 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001522channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001523{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001524 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001525 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001526 char_u *p;
1527
Bram Moolenaar77073442016-02-13 23:23:53 +01001528 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529 return FAIL;
1530
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1532 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533 if (p == NULL)
1534 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 STRCPY(p, node->rq_buffer);
1536 STRCAT(p, node->rq_next->rq_buffer);
1537 vim_free(node->rq_next->rq_buffer);
1538 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539
Bram Moolenaar77073442016-02-13 23:23:53 +01001540 /* dispose of the node and its buffer */
1541 head->rq_next = node->rq_next;
1542 head->rq_next->rq_prev = NULL;
1543 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001544 vim_free(node);
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001550 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001551 * Returns OK or FAIL.
1552 */
1553 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001554channel_save(channel_T *channel, int part, char_u *buf, int len,
1555 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556{
1557 readq_T *node;
1558 readq_T *head = &channel->ch_part[part].ch_head;
1559 char_u *p;
1560 int i;
1561
1562 node = (readq_T *)alloc(sizeof(readq_T));
1563 if (node == NULL)
1564 return FAIL; /* out of memory */
1565 node->rq_buffer = alloc(len + 1);
1566 if (node->rq_buffer == NULL)
1567 {
1568 vim_free(node);
1569 return FAIL; /* out of memory */
1570 }
1571
1572 if (channel->ch_part[part].ch_mode == MODE_NL)
1573 {
1574 /* Drop any CR before a NL. */
1575 p = node->rq_buffer;
1576 for (i = 0; i < len; ++i)
1577 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1578 *p++ = buf[i];
1579 *p = NUL;
1580 }
1581 else
1582 {
1583 mch_memmove(node->rq_buffer, buf, len);
1584 node->rq_buffer[len] = NUL;
1585 }
1586
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001587 if (prepend)
1588 {
1589 /* preend node to the head of the queue */
1590 node->rq_next = head->rq_next;
1591 node->rq_prev = NULL;
1592 if (head->rq_next == NULL)
1593 head->rq_prev = node;
1594 else
1595 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001597 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001598 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001599 {
1600 /* append node to the tail of the queue */
1601 node->rq_next = NULL;
1602 node->rq_prev = head->rq_prev;
1603 if (head->rq_prev == NULL)
1604 head->rq_next = node;
1605 else
1606 head->rq_prev->rq_next = node;
1607 head->rq_prev = node;
1608 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001610 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001612 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001613 fprintf(log_fd, "'");
1614 if (fwrite(buf, len, 1, log_fd) != 1)
1615 return FAIL;
1616 fprintf(log_fd, "'\n");
1617 }
1618 return OK;
1619}
1620
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001621 static int
1622channel_fill(js_read_T *reader)
1623{
1624 channel_T *channel = (channel_T *)reader->js_cookie;
1625 int part = reader->js_cookie_arg;
1626 char_u *next = channel_get(channel, part);
1627 int unused;
1628 int len;
1629 char_u *p;
1630
1631 if (next == NULL)
1632 return FALSE;
1633
1634 unused = reader->js_end - reader->js_buf - reader->js_used;
1635 if (unused > 0)
1636 {
1637 /* Prepend unused text. */
1638 len = (int)STRLEN(next);
1639 p = alloc(unused + len + 1);
1640 if (p == NULL)
1641 {
1642 vim_free(next);
1643 return FALSE;
1644 }
1645 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1646 mch_memmove(p + unused, next, len + 1);
1647 vim_free(next);
1648 next = p;
1649 }
1650
1651 vim_free(reader->js_buf);
1652 reader->js_buf = next;
1653 reader->js_used = 0;
1654 return TRUE;
1655}
1656
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001657/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001658 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001660 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001661 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001662 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001663channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001664{
1665 js_read_T reader;
1666 typval_T listtv;
1667 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001668 chanpart_T *chanpart = &channel->ch_part[part];
1669 jsonq_T *head = &chanpart->ch_json_head;
1670 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001671 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001673 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001674 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001678 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001679 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001680 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001681
1682 /* When a message is incomplete we wait for a short while for more to
1683 * arrive. After the delay drop the input, otherwise a truncated string
1684 * or list will make us hang. */
1685 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001686 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001687 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001688 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001689 /* Only accept the response when it is a list with at least two
1690 * items. */
1691 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001692 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001693 if (listtv.v_type != VAR_LIST)
1694 ch_error(channel, "Did not receive a list, discarding");
1695 else
1696 ch_errorn(channel, "Expected list with two items, got %d",
1697 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001699 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 else
1701 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001702 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1703 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001704 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705 else
1706 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001707 item->jq_value = alloc_tv();
1708 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001709 {
1710 vim_free(item);
1711 clear_tv(&listtv);
1712 }
1713 else
1714 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 *item->jq_value = listtv;
1716 item->jq_prev = head->jq_prev;
1717 head->jq_prev = item;
1718 item->jq_next = NULL;
1719 if (item->jq_prev == NULL)
1720 head->jq_next = item;
1721 else
1722 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001723 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001724 }
1725 }
1726 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001727
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 if (status == OK)
1729 chanpart->ch_waiting = FALSE;
1730 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001731 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001732 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001733 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001734 /* First time encountering incomplete message, set a deadline of
1735 * 100 msec. */
1736 ch_log(channel, "Incomplete message - wait for more");
1737 reader.js_used = 0;
1738 chanpart->ch_waiting = TRUE;
1739#ifdef WIN32
1740 chanpart->ch_deadline = GetTickCount() + 100L;
1741#else
1742 gettimeofday(&chanpart->ch_deadline, NULL);
1743 chanpart->ch_deadline.tv_usec += 100 * 1000;
1744 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1745 {
1746 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1747 ++chanpart->ch_deadline.tv_sec;
1748 }
1749#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001750 }
1751 else
1752 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001753 int timeout;
1754#ifdef WIN32
1755 timeout = GetTickCount() > chanpart->ch_deadline;
1756#else
1757 {
1758 struct timeval now_tv;
1759
1760 gettimeofday(&now_tv, NULL);
1761 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1762 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1763 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1764 }
1765#endif
1766 if (timeout)
1767 {
1768 status = FAIL;
1769 chanpart->ch_waiting = FALSE;
1770 }
1771 else
1772 {
1773 reader.js_used = 0;
1774 ch_log(channel, "still waiting on incomplete message");
1775 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001776 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001777 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001778
1779 if (status == FAIL)
1780 {
1781 ch_error(channel, "Decoding failed - discarding input");
1782 ret = FALSE;
1783 chanpart->ch_waiting = FALSE;
1784 }
1785 else if (reader.js_buf[reader.js_used] != NUL)
1786 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001788 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001789 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1790 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001791 ret = status == MAYBE ? FALSE: TRUE;
1792 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 else
1794 ret = FALSE;
1795
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001796 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001797 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798}
1799
1800/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001801 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001802 */
1803 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001804remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001805{
Bram Moolenaar77073442016-02-13 23:23:53 +01001806 if (node->cq_prev == NULL)
1807 head->cq_next = node->cq_next;
1808 else
1809 node->cq_prev->cq_next = node->cq_next;
1810 if (node->cq_next == NULL)
1811 head->cq_prev = node->cq_prev;
1812 else
1813 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001814}
1815
1816/*
1817 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001818 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001819 */
1820 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001821remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001822{
Bram Moolenaar77073442016-02-13 23:23:53 +01001823 if (node->jq_prev == NULL)
1824 head->jq_next = node->jq_next;
1825 else
1826 node->jq_prev->jq_next = node->jq_next;
1827 if (node->jq_next == NULL)
1828 head->jq_prev = node->jq_prev;
1829 else
1830 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001831 vim_free(node);
1832}
1833
1834/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001835 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001837 * When "id" is zero or negative jut get the first message. But not the one
1838 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001839 * Return OK when found and return the value in "rettv".
1840 * Return FAIL otherwise.
1841 */
1842 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001845 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847
Bram Moolenaar77073442016-02-13 23:23:53 +01001848 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001850 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001851 typval_T *tv = &l->lv_first->li_tv;
1852
1853 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001854 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001855 || tv->vval.v_number == 0
1856 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001858 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001859 if (tv->v_type == VAR_NUMBER)
1860 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001861 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 return OK;
1863 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001864 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001865 }
1866 return FAIL;
1867}
1868
Bram Moolenaarece61b02016-02-20 21:39:05 +01001869#define CH_JSON_MAX_ARGS 4
1870
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001871/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001873 * "argv[0]" is the command string.
1874 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001875 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001876 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001877channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001878{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001879 char_u *cmd = argv[0].vval.v_string;
1880 char_u *arg;
1881 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001886 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001887 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001888 return;
1889 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001890 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001891 if (arg == NULL)
1892 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001893
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001894 if (STRCMP(cmd, "ex") == 0)
1895 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001896 int save_called_emsg = called_emsg;
1897
1898 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001899 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001900 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001901 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001902 --emsg_silent;
1903 if (called_emsg)
1904 ch_logs(channel, "Ex command error: '%s'",
1905 (char *)get_vim_var_str(VV_ERRMSG));
1906 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001907 }
1908 else if (STRCMP(cmd, "normal") == 0)
1909 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001910 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001911
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001912 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001913 ea.arg = arg;
1914 ea.addr_count = 0;
1915 ea.forceit = TRUE; /* no mapping */
1916 ex_normal(&ea);
1917 }
1918 else if (STRCMP(cmd, "redraw") == 0)
1919 {
1920 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001921
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001922 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001923 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001924 ex_redraw(&ea);
1925 showruler(FALSE);
1926 setcursor();
1927 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001928#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001929 if (gui.in_use)
1930 {
1931 gui_update_cursor(FALSE, FALSE);
1932 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001933 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001934#endif
1935 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001937 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001938 int is_call = cmd[0] == 'c';
1939 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001940
Bram Moolenaarece61b02016-02-20 21:39:05 +01001941 if (argv[id_idx].v_type != VAR_UNKNOWN
1942 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001943 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001945 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001946 EMSG("E904: last argument for expr/call must be a number");
1947 }
1948 else if (is_call && argv[2].v_type != VAR_LIST)
1949 {
1950 ch_error(channel, "third argument for call must be a list");
1951 if (p_verbose > 2)
1952 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001953 }
1954 else
1955 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001956 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001959 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001960
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001961 /* Don't pollute the display with errors. */
1962 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001963 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001964 {
1965 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001966 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001967 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001968 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 {
1970 ch_logs(channel, "Calling '%s'", (char *)arg);
1971 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1972 tv = &res_tv;
1973 else
1974 tv = NULL;
1975 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001976
1977 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001978 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001979 int id = argv[id_idx].vval.v_number;
1980
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001982 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001983 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001984 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001985 /* If evaluation failed or the result can't be encoded
1986 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001987 vim_free(json);
1988 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001989 err_tv.v_type = VAR_STRING;
1990 err_tv.vval.v_string = (char_u *)"ERROR";
1991 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001992 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001993 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001994 if (json != NULL)
1995 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001996 channel_send(channel,
1997 part == PART_SOCK ? PART_SOCK : PART_IN,
1998 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001999 vim_free(json);
2000 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002001 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002002 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002003 if (tv == &res_tv)
2004 clear_tv(tv);
2005 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002006 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002008 }
2009 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002010 {
2011 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002012 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002013 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002014}
2015
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002016/*
2017 * Invoke the callback at "cbhead".
2018 * Does not redraw but sets channel_need_redraw.
2019 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002020 static void
2021invoke_one_time_callback(
2022 channel_T *channel,
2023 cbq_T *cbhead,
2024 cbq_T *item,
2025 typval_T *argv)
2026{
2027 ch_logs(channel, "Invoking one-time callback %s",
2028 (char *)item->cq_callback);
2029 /* Remove the item from the list first, if the callback
2030 * invokes ch_close() the list will be cleared. */
2031 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002032 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002033 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002034 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002035 vim_free(item);
2036}
2037
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002038 static void
2039append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2040{
2041 buf_T *save_curbuf = curbuf;
2042 linenr_T lnum = buffer->b_ml.ml_line_count;
2043 int save_write_to = buffer->b_write_to_channel;
2044
2045 /* If the buffer is also used as input insert above the last
2046 * line. Don't write these lines. */
2047 if (save_write_to)
2048 {
2049 --lnum;
2050 buffer->b_write_to_channel = FALSE;
2051 }
2052
2053 /* Append to the buffer */
2054 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2055
2056 curbuf = buffer;
2057 u_sync(TRUE);
2058 /* ignore undo failure, undo is not very useful here */
2059 ignored = u_save(lnum, lnum + 1);
2060
2061 ml_append(lnum, msg, 0, FALSE);
2062 appended_lines_mark(lnum, 1L);
2063 curbuf = save_curbuf;
2064
2065 if (buffer->b_nwindows > 0)
2066 {
2067 win_T *wp;
2068 win_T *save_curwin;
2069
2070 FOR_ALL_WINDOWS(wp)
2071 {
2072 if (wp->w_buffer == buffer
2073 && (save_write_to
2074 ? wp->w_cursor.lnum == lnum + 1
2075 : (wp->w_cursor.lnum == lnum
2076 && wp->w_cursor.col == 0)))
2077 {
2078 ++wp->w_cursor.lnum;
2079 save_curwin = curwin;
2080 curwin = wp;
2081 curbuf = curwin->w_buffer;
2082 scroll_cursor_bot(0, FALSE);
2083 curwin = save_curwin;
2084 curbuf = curwin->w_buffer;
2085 }
2086 }
2087 redraw_buf_later(buffer, VALID);
2088 channel_need_redraw = TRUE;
2089 }
2090
2091 if (save_write_to)
2092 {
2093 channel_T *ch;
2094
2095 /* Find channels reading from this buffer and adjust their
2096 * next-to-read line number. */
2097 buffer->b_write_to_channel = TRUE;
2098 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2099 {
2100 chanpart_T *in_part = &ch->ch_part[PART_IN];
2101
2102 if (in_part->ch_buffer == buffer)
2103 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2104 }
2105 }
2106}
2107
Bram Moolenaar437905c2016-04-26 19:01:05 +02002108 static void
2109drop_messages(channel_T *channel, int part)
2110{
2111 char_u *msg;
2112
2113 while ((msg = channel_get(channel, part)) != NULL)
2114 {
2115 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2116 vim_free(msg);
2117 }
2118}
2119
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002120/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002121 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002122 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002123 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002124 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002125 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002126may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002127{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 char_u *msg = NULL;
2129 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002131 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002132 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002133 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002134 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002135 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002136 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002137 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002139 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002140 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002141 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002142
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002143 /* Use a message-specific callback, part callback or channel callback */
2144 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2145 if (cbitem->cq_seq_nr == 0)
2146 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002147 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002148 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002149 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002150 partial = cbitem->cq_partial;
2151 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002152 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002153 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002154 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002155 partial = channel->ch_part[part].ch_partial;
2156 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002158 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002159 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002160 partial = channel->ch_partial;
2161 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002162
Bram Moolenaar187db502016-02-27 14:44:26 +01002163 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002164 if (buffer != NULL && !buf_valid(buffer))
2165 {
2166 /* buffer was wiped out */
2167 channel->ch_part[part].ch_buffer = NULL;
2168 buffer = NULL;
2169 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002170
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002171 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002172 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002173 listitem_T *item;
2174 int argc = 0;
2175
Bram Moolenaard7ece102016-02-02 23:23:02 +01002176 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002177 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002178 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002179 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002180 channel_parse_json(channel, part);
2181 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002182 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002183 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002184
Bram Moolenaarece61b02016-02-20 21:39:05 +01002185 for (item = listtv->vval.v_list->lv_first;
2186 item != NULL && argc < CH_JSON_MAX_ARGS;
2187 item = item->li_next)
2188 argv[argc++] = item->li_tv;
2189 while (argc < CH_JSON_MAX_ARGS)
2190 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002191
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 if (argv[0].v_type == VAR_STRING)
2193 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002194 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002195 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002196 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002197 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002198 }
2199
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002201 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002202 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002203 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002204 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002205 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002206 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002207 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002209 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002210 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002211 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002212 return FALSE;
2213 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002214 else
2215 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002216 /* If there is no callback or buffer drop the message. */
2217 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002218 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002219 /* If there is a close callback it may use ch_read() to get the
2220 * messages. */
2221 if (channel->ch_close_cb == NULL)
2222 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002223 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002224 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002225
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002226 if (ch_mode == MODE_NL)
2227 {
2228 char_u *nl;
2229 char_u *buf;
2230
2231 /* See if we have a message ending in NL in the first buffer. If
2232 * not try to concatenate the first and the second buffer. */
2233 while (TRUE)
2234 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002235 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002236 nl = vim_strchr(buf, NL);
2237 if (nl != NULL)
2238 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002239 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002240 return FALSE; /* incomplete message */
2241 }
2242 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002243 {
2244 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002245 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002246 *nl = NUL;
2247 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002248 else
2249 {
2250 /* Copy the message into allocated memory and remove it from
2251 * the buffer. */
2252 msg = vim_strnsave(buf, (int)(nl - buf));
2253 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2254 }
2255 }
2256 else
2257 /* For a raw channel we don't know where the message ends, just
2258 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002259 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002260
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002261 if (msg == NULL)
2262 return FALSE; /* out of memory (and avoids Coverity warning) */
2263
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002264 argv[1].v_type = VAR_STRING;
2265 argv[1].vval.v_string = msg;
2266 }
2267
Bram Moolenaara07fec92016-02-05 21:04:08 +01002268 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002269 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002270 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002271
2272 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002273 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002274 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002275 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002276 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002277 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002278 break;
2279 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002280 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002281 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002282 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002283 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002284 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002285 if (buffer != NULL)
2286 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002287 if (msg == NULL)
2288 /* JSON or JS mode: re-encode the message. */
2289 msg = json_encode(listtv, ch_mode);
2290 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002291 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002292 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002293
Bram Moolenaar187db502016-02-27 14:44:26 +01002294 if (callback != NULL)
2295 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002296 if (cbitem != NULL)
2297 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2298 else
2299 {
2300 /* invoke the channel callback */
2301 ch_logs(channel, "Invoking channel callback %s",
2302 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002303 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002304 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002305 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002306 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002307 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002308 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002309
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002310 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002311 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002312 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002313
2314 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002315}
2316
2317/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002318 * Return TRUE when channel "channel" is open for writing to.
2319 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002320 */
2321 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002322channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002323{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002325 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002326}
2327
2328/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002329 * Return TRUE when channel "channel" is open for reading or writing.
2330 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002331 */
2332 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002333channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002334{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002335 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002336 || channel->CH_IN_FD != INVALID_FD
2337 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002338 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002339}
2340
2341/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002342 * Return TRUE if "channel" has JSON or other typeahead.
2343 */
2344 static int
2345channel_has_readahead(channel_T *channel, int part)
2346{
2347 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2348
2349 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2350 {
2351 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2352 jsonq_T *item = head->jq_next;
2353
2354 return item != NULL;
2355 }
2356 return channel_peek(channel, part) != NULL;
2357}
2358
2359/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002360 * Return a string indicating the status of the channel.
2361 */
2362 char *
2363channel_status(channel_T *channel)
2364{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002365 int part;
2366 int has_readahead = FALSE;
2367
Bram Moolenaar77073442016-02-13 23:23:53 +01002368 if (channel == NULL)
2369 return "fail";
2370 if (channel_is_open(channel))
2371 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002372 for (part = PART_SOCK; part <= PART_ERR; ++part)
2373 if (channel_has_readahead(channel, part))
2374 {
2375 has_readahead = TRUE;
2376 break;
2377 }
2378
2379 if (has_readahead)
2380 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002381 return "closed";
2382}
2383
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002384 static void
2385channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2386{
2387 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002388 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002389 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002390 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002391
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002392 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002393 STRCAT(namebuf, "_");
2394 tail = STRLEN(namebuf);
2395
2396 STRCPY(namebuf + tail, "status");
2397 dict_add_nr_str(dict, namebuf, 0,
2398 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2399
2400 STRCPY(namebuf + tail, "mode");
2401 switch (chanpart->ch_mode)
2402 {
2403 case MODE_NL: s = "NL"; break;
2404 case MODE_RAW: s = "RAW"; break;
2405 case MODE_JSON: s = "JSON"; break;
2406 case MODE_JS: s = "JS"; break;
2407 }
2408 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2409
2410 STRCPY(namebuf + tail, "io");
2411 if (part == PART_SOCK)
2412 s = "socket";
2413 else switch (chanpart->ch_io)
2414 {
2415 case JIO_NULL: s = "null"; break;
2416 case JIO_PIPE: s = "pipe"; break;
2417 case JIO_FILE: s = "file"; break;
2418 case JIO_BUFFER: s = "buffer"; break;
2419 case JIO_OUT: s = "out"; break;
2420 }
2421 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2422
2423 STRCPY(namebuf + tail, "timeout");
2424 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2425}
2426
2427 void
2428channel_info(channel_T *channel, dict_T *dict)
2429{
2430 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2431 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2432
2433 if (channel->ch_hostname != NULL)
2434 {
2435 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2436 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2437 channel_part_info(channel, dict, "sock", PART_SOCK);
2438 }
2439 else
2440 {
2441 channel_part_info(channel, dict, "out", PART_OUT);
2442 channel_part_info(channel, dict, "err", PART_ERR);
2443 channel_part_info(channel, dict, "in", PART_IN);
2444 }
2445}
2446
Bram Moolenaar77073442016-02-13 23:23:53 +01002447/*
2448 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002449 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002450 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002451 */
2452 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002453channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002454{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002455 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002456
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002457#ifdef FEAT_GUI
2458 channel_gui_unregister(channel);
2459#endif
2460
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002462 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002463 sock_close(channel->CH_SOCK_FD);
2464 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002465 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002466 may_close_part(&channel->CH_IN_FD);
2467 may_close_part(&channel->CH_OUT_FD);
2468 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002469
Bram Moolenaar8b374212016-02-24 20:43:06 +01002470 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002471 {
2472 typval_T argv[1];
2473 typval_T rettv;
2474 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002475 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002476
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002477 /* Invoke callbacks before the close callback, since it's weird to
2478 * first invoke the close callback. Increment the refcount to avoid
2479 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002480 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002481 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002482 for (part = PART_SOCK; part <= PART_ERR; ++part)
2483 while (may_invoke_callback(channel, part))
2484 ;
2485
2486 /* Invoke the close callback, if still set. */
2487 if (channel->ch_close_cb != NULL)
2488 {
2489 ch_logs(channel, "Invoking close callback %s",
2490 (char *)channel->ch_close_cb);
2491 argv[0].v_type = VAR_CHANNEL;
2492 argv[0].vval.v_channel = channel;
2493 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002494 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2495 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002496 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002497 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002498 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002499 --channel->ch_refcount;
2500
2501 /* the callback is only called once */
2502 vim_free(channel->ch_close_cb);
2503 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002504 partial_unref(channel->ch_close_partial);
2505 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002506
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002507 if (channel_need_redraw)
2508 {
2509 channel_need_redraw = FALSE;
2510 redraw_after_callback();
2511 }
2512
Bram Moolenaar437905c2016-04-26 19:01:05 +02002513 /* any remaining messages are useless now */
2514 for (part = PART_SOCK; part <= PART_ERR; ++part)
2515 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002516 }
2517
2518 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002519}
2520
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002522 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002523 * Returns NULL if there is nothing.
2524 */
2525 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002526channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002527{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002528 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002529
Bram Moolenaar77073442016-02-13 23:23:53 +01002530 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002531 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002532 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533}
2534
2535/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002536 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002537 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002538 static void
2539channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002540{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002541 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2542 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002543
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002544 while (channel_peek(channel, part) != NULL)
2545 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002546
2547 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002548 {
2549 cbq_T *node = cb_head->cq_next;
2550
2551 remove_cb_node(cb_head, node);
2552 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002553 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002554 vim_free(node);
2555 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002556
2557 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002558 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002559 free_tv(json_head->jq_next->jq_value);
2560 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002561 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002562
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002563 vim_free(channel->ch_part[part].ch_callback);
2564 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002565 partial_unref(channel->ch_part[part].ch_partial);
2566 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002567}
2568
2569/*
2570 * Clear all the read buffers on "channel".
2571 */
2572 void
2573channel_clear(channel_T *channel)
2574{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002575 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002576 vim_free(channel->ch_hostname);
2577 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002578 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002579 channel_clear_one(channel, PART_OUT);
2580 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002581 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002582 vim_free(channel->ch_callback);
2583 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002584 partial_unref(channel->ch_partial);
2585 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002586 vim_free(channel->ch_close_cb);
2587 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002588 partial_unref(channel->ch_close_partial);
2589 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002590}
2591
Bram Moolenaar77073442016-02-13 23:23:53 +01002592#if defined(EXITFREE) || defined(PROTO)
2593 void
2594channel_free_all(void)
2595{
2596 channel_T *channel;
2597
Bram Moolenaard6051b52016-02-28 15:49:03 +01002598 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002599 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2600 channel_clear(channel);
2601}
2602#endif
2603
2604
Bram Moolenaar715d2852016-04-30 17:06:31 +02002605/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002606#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002607
2608/* Buffer size for reading incoming messages. */
2609#define MAXMSGSIZE 4096
2610
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002611#if defined(HAVE_SELECT)
2612/*
2613 * Add write fds where we are waiting for writing to be possible.
2614 */
2615 static int
2616channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2617{
2618 int maxfd = maxfd_arg;
2619 channel_T *ch;
2620
2621 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2622 {
2623 chanpart_T *in_part = &ch->ch_part[PART_IN];
2624
2625 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2626 {
2627 FD_SET((int)in_part->ch_fd, wfds);
2628 if ((int)in_part->ch_fd >= maxfd)
2629 maxfd = (int)in_part->ch_fd + 1;
2630 }
2631 }
2632 return maxfd;
2633}
2634#else
2635/*
2636 * Add write fds where we are waiting for writing to be possible.
2637 */
2638 static int
2639channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2640{
2641 int nfd = nfd_in;
2642 channel_T *ch;
2643
2644 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2645 {
2646 chanpart_T *in_part = &ch->ch_part[PART_IN];
2647
2648 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2649 {
2650 in_part->ch_poll_idx = nfd;
2651 fds[nfd].fd = in_part->ch_fd;
2652 fds[nfd].events = POLLOUT;
2653 ++nfd;
2654 }
2655 else
2656 in_part->ch_poll_idx = -1;
2657 }
2658 return nfd;
2659}
2660#endif
2661
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002662typedef enum {
2663 CW_READY,
2664 CW_NOT_READY,
2665 CW_ERROR
2666} channel_wait_result;
2667
Bram Moolenaard04a0202016-01-26 23:30:18 +01002668/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002669 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002670 * Return CW_READY when there is something to read.
2671 * Return CW_NOT_READY when there is nothing to read.
2672 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002673 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002674 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002675channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002676{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002677 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002678 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002679
Bram Moolenaard8070362016-02-15 21:56:54 +01002680# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002681 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002682 {
2683 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002684 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002685 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002686 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002687
2688 /* reading from a pipe, not a socket */
2689 while (TRUE)
2690 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002691 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2692
2693 if (r && nread > 0)
2694 return CW_READY;
2695 if (r == 0)
2696 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002697
2698 /* perhaps write some buffer lines */
2699 channel_write_any_lines();
2700
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002701 sleep_time = deadline - GetTickCount();
2702 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002703 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002704 /* Wait for a little while. Very short at first, up to 10 msec
2705 * after looping a few times. */
2706 if (sleep_time > delay)
2707 sleep_time = delay;
2708 Sleep(sleep_time);
2709 delay = delay * 2;
2710 if (delay > 10)
2711 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002712 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002713 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002714 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002715#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002716 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002717#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002718 struct timeval tval;
2719 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002720 fd_set wfds;
2721 int ret;
2722 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002723
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002724 tval.tv_sec = timeout / 1000;
2725 tval.tv_usec = (timeout % 1000) * 1000;
2726 for (;;)
2727 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002728 FD_ZERO(&rfds);
2729 FD_SET((int)fd, &rfds);
2730
2731 /* Write lines to a pipe when a pipe can be written to. Need to
2732 * set this every time, some buffers may be done. */
2733 maxfd = (int)fd + 1;
2734 FD_ZERO(&wfds);
2735 maxfd = channel_fill_wfds(maxfd, &wfds);
2736
2737 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002738# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002739 SOCK_ERRNO;
2740 if (ret == -1 && errno == EINTR)
2741 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002742# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002743 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002744 {
2745 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002746 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002747 channel_write_any_lines();
2748 continue;
2749 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002750 break;
2751 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002752#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002753 for (;;)
2754 {
2755 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2756 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002757
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002758 fds[0].fd = fd;
2759 fds[0].events = POLLIN;
2760 nfd = channel_fill_poll_write(nfd, fds);
2761 if (poll(fds, nfd, timeout) > 0)
2762 {
2763 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002764 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002765 channel_write_any_lines();
2766 continue;
2767 }
2768 break;
2769 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002770#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002771 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002772 return CW_NOT_READY;
2773}
2774
2775 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002776channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002777{
2778 /* Do not call emsg(), most likely the other end just exited. */
2779 ch_errors(channel, "%s(): Cannot read from channel", func);
2780
2781 /* Queue a "DETACH" netbeans message in the command queue in order to
2782 * terminate the netbeans session later. Do not end the session here
2783 * directly as we may be running in the context of a call to
2784 * netbeans_parse_messages():
2785 * netbeans_parse_messages
2786 * -> autocmd triggered while processing the netbeans cmd
2787 * -> ui_breakcheck
2788 * -> gui event loop or select loop
2789 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002790 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002791 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002792 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002793 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002794 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2795
2796 /* When reading from stdout is not possible, assume the other side has
2797 * died. */
2798 channel_close(channel, TRUE);
2799 if (channel->ch_nb_close_cb != NULL)
2800 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002801}
2802
2803/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002804 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002805 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002806 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002807 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002808 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002809channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002810{
2811 static char_u *buf = NULL;
2812 int len = 0;
2813 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002814 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002815 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002816
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002817 fd = channel->ch_part[part].ch_fd;
2818 if (fd == INVALID_FD)
2819 {
2820 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002821 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002822 }
2823 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002824
2825 /* Allocate a buffer to read into. */
2826 if (buf == NULL)
2827 {
2828 buf = alloc(MAXMSGSIZE);
2829 if (buf == NULL)
2830 return; /* out of memory! */
2831 }
2832
2833 /* Keep on reading for as long as there is something to read.
2834 * Use select() or poll() to avoid blocking on a message that is exactly
2835 * MAXMSGSIZE long. */
2836 for (;;)
2837 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002838 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002839 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002840 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002841 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002842 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002843 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002844 if (len <= 0)
2845 break; /* error or nothing more to read */
2846
2847 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002848 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002849 readlen += len;
2850 if (len < MAXMSGSIZE)
2851 break; /* did read everything that's available */
2852 }
2853
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002854 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002855 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002856 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002857
2858#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002859 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002860 if (CH_HAS_GUI && gtk_main_level() > 0)
2861 gtk_main_quit();
2862#endif
2863}
2864
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002865/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002866 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002867 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002868 * Returns what was read in allocated memory.
2869 * Returns NULL in case of error or timeout.
2870 */
2871 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002872channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002873{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002874 char_u *buf;
2875 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002876 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002877 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002878 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002879
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002880 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002881 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002882
2883 while (TRUE)
2884 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002885 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002886 if (buf != NULL && (mode == MODE_RAW
2887 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2888 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002889 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002890 continue;
2891
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002892 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002893 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002894 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002895 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002896 {
2897 ch_log(channel, "Timed out");
2898 return NULL;
2899 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002900 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002901 }
2902
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002903 if (mode == MODE_RAW)
2904 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002905 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002906 }
2907 else
2908 {
2909 nl = vim_strchr(buf, NL);
2910 if (nl[1] == NUL)
2911 {
2912 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002913 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002914 *nl = NUL;
2915 }
2916 else
2917 {
2918 /* Copy the message into allocated memory and remove it from the
2919 * buffer. */
2920 msg = vim_strnsave(buf, (int)(nl - buf));
2921 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2922 }
2923 }
2924 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002925 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002926 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002927}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002928
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002929/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002930 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002931 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002932 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002933 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002934 */
2935 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002936channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002937 channel_T *channel,
2938 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002939 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002940 int id,
2941 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002942{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002943 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002944 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002945 int timeout;
2946 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002947
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002948 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002949 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002950 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002951 for (;;)
2952 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002953 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002954
2955 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002957 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002958 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002959 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002960 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002961
Bram Moolenaard7ece102016-02-02 23:23:02 +01002962 if (!more)
2963 {
2964 /* Handle any other messages in the queue. If done some more
2965 * messages may have arrived. */
2966 if (channel_parse_messages())
2967 continue;
2968
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002969 /* Wait for up to the timeout. If there was an incomplete message
2970 * use the deadline for that. */
2971 timeout = timeout_arg;
2972 if (chanpart->ch_waiting)
2973 {
2974#ifdef WIN32
2975 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2976#else
2977 {
2978 struct timeval now_tv;
2979
2980 gettimeofday(&now_tv, NULL);
2981 timeout = (chanpart->ch_deadline.tv_sec
2982 - now_tv.tv_sec) * 1000
2983 + (chanpart->ch_deadline.tv_usec
2984 - now_tv.tv_usec) / 1000
2985 + 1;
2986 }
2987#endif
2988 if (timeout < 0)
2989 {
2990 /* Something went wrong, channel_parse_json() didn't
2991 * discard message. Cancel waiting. */
2992 chanpart->ch_waiting = FALSE;
2993 timeout = timeout_arg;
2994 }
2995 else if (timeout > timeout_arg)
2996 timeout = timeout_arg;
2997 }
2998 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002999 if (fd == INVALID_FD
3000 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003001 {
3002 if (timeout == timeout_arg)
3003 {
3004 if (fd != INVALID_FD)
3005 ch_log(channel, "Timed out");
3006 break;
3007 }
3008 }
3009 else
3010 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003011 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003012 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003013 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003014 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003015}
3016
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003017/*
3018 * Common for ch_read() and ch_readraw().
3019 */
3020 void
3021common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3022{
3023 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003024 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003025 jobopt_T opt;
3026 int mode;
3027 int timeout;
3028 int id = -1;
3029 typval_T *listtv = NULL;
3030
3031 /* return an empty string by default */
3032 rettv->v_type = VAR_STRING;
3033 rettv->vval.v_string = NULL;
3034
3035 clear_job_options(&opt);
3036 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3037 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003038 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003039
Bram Moolenaar437905c2016-04-26 19:01:05 +02003040 if (opt.jo_set & JO_PART)
3041 part = opt.jo_part;
3042 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003043 if (channel != NULL)
3044 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003045 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003046 part = channel_part_read(channel);
3047 mode = channel_get_mode(channel, part);
3048 timeout = channel_get_timeout(channel, part);
3049 if (opt.jo_set & JO_TIMEOUT)
3050 timeout = opt.jo_timeout;
3051
3052 if (raw || mode == MODE_RAW || mode == MODE_NL)
3053 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3054 else
3055 {
3056 if (opt.jo_set & JO_ID)
3057 id = opt.jo_id;
3058 channel_read_json_block(channel, part, timeout, id, &listtv);
3059 if (listtv != NULL)
3060 {
3061 *rettv = *listtv;
3062 vim_free(listtv);
3063 }
3064 else
3065 {
3066 rettv->v_type = VAR_SPECIAL;
3067 rettv->vval.v_number = VVAL_NONE;
3068 }
3069 }
3070 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003071
3072theend:
3073 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003074}
3075
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003076# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3077 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003078/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003079 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003080 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003081 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003082 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003083channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003084{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003085 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003087
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003088 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003089 for (channel = first_channel; channel != NULL;
3090 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003091 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003092 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003093 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003094 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003095 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003096 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003097 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003098 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003099 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003100}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003101# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003102
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003103# if defined(WIN32) || defined(PROTO)
3104/*
3105 * Check the channels for anything that is ready to be read.
3106 * The data is put in the read queue.
3107 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003108 void
3109channel_handle_events(void)
3110{
3111 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003112 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003113 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003114
3115 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3116 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003117 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003118 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003119 {
3120 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003121 if (fd != INVALID_FD)
3122 {
3123 int r = channel_wait(channel, fd, 0);
3124
3125 if (r == CW_READY)
3126 channel_read(channel, part, "channel_handle_events");
3127 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003128 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003129 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003130 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003131 }
3132}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003133# endif
3134
Bram Moolenaard04a0202016-01-26 23:30:18 +01003135/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003136 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003137 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003138 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003139 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003140 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003141channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003142{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003143 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003144 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003145 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003146
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003147 fd = channel->ch_part[part].ch_fd;
3148 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003149 {
3150 if (!channel->ch_error && fun != NULL)
3151 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003152 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003153 EMSG2("E630: %s(): write while not connected", fun);
3154 }
3155 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003156 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003157 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003158
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003159 if (log_fd != NULL)
3160 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003161 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003162 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003163 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003164 fprintf(log_fd, "'\n");
3165 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003166 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003167 }
3168
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003169 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003170 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003171 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003172 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003173 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003174 {
3175 if (!channel->ch_error && fun != NULL)
3176 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003177 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003178 EMSG2("E631: %s(): write failed", fun);
3179 }
3180 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003181 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003182 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003183
3184 channel->ch_error = FALSE;
3185 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003186}
3187
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003188/*
3189 * Common for "ch_sendexpr()" and "ch_sendraw()".
3190 * Returns the channel if the caller should read the response.
3191 * Sets "part_read" to the the read fd.
3192 * Otherwise returns NULL.
3193 */
3194 channel_T *
3195send_common(
3196 typval_T *argvars,
3197 char_u *text,
3198 int id,
3199 int eval,
3200 jobopt_T *opt,
3201 char *fun,
3202 int *part_read)
3203{
3204 channel_T *channel;
3205 int part_send;
3206
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003207 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003208 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003209 if (channel == NULL)
3210 return NULL;
3211 part_send = channel_part_send(channel);
3212 *part_read = channel_part_read(channel);
3213
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003214 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3215 return NULL;
3216
3217 /* Set the callback. An empty callback means no callback and not reading
3218 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3219 * allowed. */
3220 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3221 {
3222 if (eval)
3223 {
3224 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3225 return NULL;
3226 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003227 channel_set_req_callback(channel, part_send,
3228 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003229 }
3230
3231 if (channel_send(channel, part_send, text, fun) == OK
3232 && opt->jo_callback == NULL)
3233 return channel;
3234 return NULL;
3235}
3236
3237/*
3238 * common for "ch_evalexpr()" and "ch_sendexpr()"
3239 */
3240 void
3241ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3242{
3243 char_u *text;
3244 typval_T *listtv;
3245 channel_T *channel;
3246 int id;
3247 ch_mode_T ch_mode;
3248 int part_send;
3249 int part_read;
3250 jobopt_T opt;
3251 int timeout;
3252
3253 /* return an empty string by default */
3254 rettv->v_type = VAR_STRING;
3255 rettv->vval.v_string = NULL;
3256
Bram Moolenaar437905c2016-04-26 19:01:05 +02003257 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003258 if (channel == NULL)
3259 return;
3260 part_send = channel_part_send(channel);
3261
3262 ch_mode = channel_get_mode(channel, part_send);
3263 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3264 {
3265 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3266 return;
3267 }
3268
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003269 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003270 text = json_encode_nr_expr(id, &argvars[1],
3271 ch_mode == MODE_JS ? JSON_JS : 0);
3272 if (text == NULL)
3273 return;
3274
3275 channel = send_common(argvars, text, id, eval, &opt,
3276 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3277 vim_free(text);
3278 if (channel != NULL && eval)
3279 {
3280 if (opt.jo_set & JO_TIMEOUT)
3281 timeout = opt.jo_timeout;
3282 else
3283 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003284 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3285 == OK)
3286 {
3287 list_T *list = listtv->vval.v_list;
3288
3289 /* Move the item from the list and then change the type to
3290 * avoid the value being freed. */
3291 *rettv = list->lv_last->li_tv;
3292 list->lv_last->li_tv.v_type = VAR_NUMBER;
3293 free_tv(listtv);
3294 }
3295 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003296 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003297}
3298
3299/*
3300 * common for "ch_evalraw()" and "ch_sendraw()"
3301 */
3302 void
3303ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3304{
3305 char_u buf[NUMBUFLEN];
3306 char_u *text;
3307 channel_T *channel;
3308 int part_read;
3309 jobopt_T opt;
3310 int timeout;
3311
3312 /* return an empty string by default */
3313 rettv->v_type = VAR_STRING;
3314 rettv->vval.v_string = NULL;
3315
3316 text = get_tv_string_buf(&argvars[1], buf);
3317 channel = send_common(argvars, text, 0, eval, &opt,
3318 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3319 if (channel != NULL && eval)
3320 {
3321 if (opt.jo_set & JO_TIMEOUT)
3322 timeout = opt.jo_timeout;
3323 else
3324 timeout = channel_get_timeout(channel, part_read);
3325 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3326 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003327 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003328}
3329
Bram Moolenaard04a0202016-01-26 23:30:18 +01003330# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003331/*
3332 * Add open channels to the poll struct.
3333 * Return the adjusted struct index.
3334 * The type of "fds" is hidden to avoid problems with the function proto.
3335 */
3336 int
3337channel_poll_setup(int nfd_in, void *fds_in)
3338{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003339 int nfd = nfd_in;
3340 channel_T *channel;
3341 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003342 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003343
Bram Moolenaar77073442016-02-13 23:23:53 +01003344 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003345 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003346 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003347 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003348 chanpart_T *ch_part = &channel->ch_part[part];
3349
3350 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003351 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003352 ch_part->ch_poll_idx = nfd;
3353 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003354 fds[nfd].events = POLLIN;
3355 nfd++;
3356 }
3357 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003358 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003359 }
3360 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003361
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003362 nfd = channel_fill_poll_write(nfd, fds);
3363
Bram Moolenaare0874f82016-01-24 20:36:41 +01003364 return nfd;
3365}
3366
3367/*
3368 * The type of "fds" is hidden to avoid problems with the function proto.
3369 */
3370 int
3371channel_poll_check(int ret_in, void *fds_in)
3372{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003373 int ret = ret_in;
3374 channel_T *channel;
3375 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003376 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003377 int idx;
3378 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003379
Bram Moolenaar77073442016-02-13 23:23:53 +01003380 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003381 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003382 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003383 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003384 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003385
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003386 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003387 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003388 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003389 --ret;
3390 }
3391 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003392
3393 in_part = &channel->ch_part[PART_IN];
3394 idx = in_part->ch_poll_idx;
3395 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3396 {
3397 if (in_part->ch_buf_append)
3398 {
3399 if (in_part->ch_buffer != NULL)
3400 channel_write_new_lines(in_part->ch_buffer);
3401 }
3402 else
3403 channel_write_in(channel);
3404 --ret;
3405 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003406 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003407
3408 return ret;
3409}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003410# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003411
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003412# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003413/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003414 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003415 */
3416 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003417channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003418{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003419 int maxfd = maxfd_in;
3420 channel_T *channel;
3421 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003422 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003423 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003424
Bram Moolenaar77073442016-02-13 23:23:53 +01003425 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003426 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003427 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003428 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003429 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003430
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003431 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003432 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003433 FD_SET((int)fd, rfds);
3434 if (maxfd < (int)fd)
3435 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003436 }
3437 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003438 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003439
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003440 maxfd = channel_fill_wfds(maxfd, wfds);
3441
Bram Moolenaare0874f82016-01-24 20:36:41 +01003442 return maxfd;
3443}
3444
3445/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003446 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003447 */
3448 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003449channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003450{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003451 int ret = ret_in;
3452 channel_T *channel;
3453 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003454 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003455 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003456 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003457
Bram Moolenaar77073442016-02-13 23:23:53 +01003458 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003459 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003460 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003461 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003462 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003463
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003464 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003465 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003466 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003467 --ret;
3468 }
3469 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003470
3471 in_part = &channel->ch_part[PART_IN];
3472 if (ret > 0 && in_part->ch_fd != INVALID_FD
3473 && FD_ISSET(in_part->ch_fd, wfds))
3474 {
3475 if (in_part->ch_buf_append)
3476 {
3477 if (in_part->ch_buffer != NULL)
3478 channel_write_new_lines(in_part->ch_buffer);
3479 }
3480 else
3481 channel_write_in(channel);
3482 --ret;
3483 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003484 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003485
3486 return ret;
3487}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003488# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003489
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003490/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003491 * Execute queued up commands.
3492 * Invoked from the main loop when it's safe to execute received commands.
3493 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003494 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003495 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003496channel_parse_messages(void)
3497{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003498 channel_T *channel = first_channel;
3499 int ret = FALSE;
3500 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003501 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003502
Bram Moolenaard0b65022016-03-06 21:50:33 +01003503 /* Only do this message when another message was given, otherwise we get
3504 * lots of them. */
3505 if (did_log_msg)
3506 {
3507 ch_log(NULL, "looking for messages on channels");
3508 did_log_msg = FALSE;
3509 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003510 while (channel != NULL)
3511 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003512 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003513 {
3514 /* channel is no longer useful, free it */
3515 channel_free(channel);
3516 channel = first_channel;
3517 part = PART_SOCK;
3518 continue;
3519 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003520 if (channel->ch_part[part].ch_fd != INVALID_FD
3521 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003522 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003523 /* Increase the refcount, in case the handler causes the channel
3524 * to be unreferenced or closed. */
3525 ++channel->ch_refcount;
3526 r = may_invoke_callback(channel, part);
3527 if (r == OK)
3528 ret = TRUE;
3529 if (channel_unref(channel) || r == OK)
3530 {
3531 /* channel was freed or something was done, start over */
3532 channel = first_channel;
3533 part = PART_SOCK;
3534 continue;
3535 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003536 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 if (part < PART_ERR)
3538 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003539 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003540 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003541 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003542 part = PART_SOCK;
3543 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003544 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003545
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003546 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003547 {
3548 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003549 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003550 }
3551
Bram Moolenaard7ece102016-02-02 23:23:02 +01003552 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003553}
3554
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003555/*
3556 * Mark references to lists used in channels.
3557 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003558 int
3559set_ref_in_channel(int copyID)
3560{
Bram Moolenaar77073442016-02-13 23:23:53 +01003561 int abort = FALSE;
3562 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003563 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003564
Bram Moolenaar77073442016-02-13 23:23:53 +01003565 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003566 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003567 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003568 tv.v_type = VAR_CHANNEL;
3569 tv.vval.v_channel = channel;
3570 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003571 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003572 return abort;
3573}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003574
3575/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003576 * Return the "part" to write to for "channel".
3577 */
3578 int
3579channel_part_send(channel_T *channel)
3580{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003581 if (channel->CH_SOCK_FD == INVALID_FD)
3582 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003583 return PART_SOCK;
3584}
3585
3586/*
3587 * Return the default "part" to read from for "channel".
3588 */
3589 int
3590channel_part_read(channel_T *channel)
3591{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003592 if (channel->CH_SOCK_FD == INVALID_FD)
3593 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003594 return PART_SOCK;
3595}
3596
3597/*
3598 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003599 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003600 */
3601 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003602channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003603{
Bram Moolenaar77073442016-02-13 23:23:53 +01003604 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003605 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003606 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003607}
3608
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003609/*
3610 * Return the timeout of "channel"/"part"
3611 */
3612 int
3613channel_get_timeout(channel_T *channel, int part)
3614{
3615 return channel->ch_part[part].ch_timeout;
3616}
3617
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003618 static int
3619handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3620{
3621 char_u *val = get_tv_string(item);
3622
3623 opt->jo_set |= jo;
3624 if (STRCMP(val, "nl") == 0)
3625 *modep = MODE_NL;
3626 else if (STRCMP(val, "raw") == 0)
3627 *modep = MODE_RAW;
3628 else if (STRCMP(val, "js") == 0)
3629 *modep = MODE_JS;
3630 else if (STRCMP(val, "json") == 0)
3631 *modep = MODE_JSON;
3632 else
3633 {
3634 EMSG2(_(e_invarg2), val);
3635 return FAIL;
3636 }
3637 return OK;
3638}
3639
3640 static int
3641handle_io(typval_T *item, int part, jobopt_T *opt)
3642{
3643 char_u *val = get_tv_string(item);
3644
3645 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3646 if (STRCMP(val, "null") == 0)
3647 opt->jo_io[part] = JIO_NULL;
3648 else if (STRCMP(val, "pipe") == 0)
3649 opt->jo_io[part] = JIO_PIPE;
3650 else if (STRCMP(val, "file") == 0)
3651 opt->jo_io[part] = JIO_FILE;
3652 else if (STRCMP(val, "buffer") == 0)
3653 opt->jo_io[part] = JIO_BUFFER;
3654 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3655 opt->jo_io[part] = JIO_OUT;
3656 else
3657 {
3658 EMSG2(_(e_invarg2), val);
3659 return FAIL;
3660 }
3661 return OK;
3662}
3663
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003664/*
3665 * Clear a jobopt_T before using it.
3666 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003667 void
3668clear_job_options(jobopt_T *opt)
3669{
3670 vim_memset(opt, 0, sizeof(jobopt_T));
3671}
3672
3673/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003674 * Free any members of a jobopt_T.
3675 */
3676 void
3677free_job_options(jobopt_T *opt)
3678{
3679 if (opt->jo_partial != NULL)
3680 partial_unref(opt->jo_partial);
3681 if (opt->jo_out_partial != NULL)
3682 partial_unref(opt->jo_out_partial);
3683 if (opt->jo_err_partial != NULL)
3684 partial_unref(opt->jo_err_partial);
3685 if (opt->jo_close_partial != NULL)
3686 partial_unref(opt->jo_close_partial);
3687}
3688
3689/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003690 * Get the PART_ number from the first character of an option name.
3691 */
3692 static int
3693part_from_char(int c)
3694{
3695 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3696}
3697
3698/*
3699 * Get the option entries from the dict in "tv", parse them and put the result
3700 * in "opt".
3701 * Only accept options in "supported".
3702 * If an option value is invalid return FAIL.
3703 */
3704 int
3705get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3706{
3707 typval_T *item;
3708 char_u *val;
3709 dict_T *dict;
3710 int todo;
3711 hashitem_T *hi;
3712 int part;
3713
3714 opt->jo_set = 0;
3715 if (tv->v_type == VAR_UNKNOWN)
3716 return OK;
3717 if (tv->v_type != VAR_DICT)
3718 {
3719 EMSG(_(e_invarg));
3720 return FAIL;
3721 }
3722 dict = tv->vval.v_dict;
3723 if (dict == NULL)
3724 return OK;
3725
3726 todo = (int)dict->dv_hashtab.ht_used;
3727 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3728 if (!HASHITEM_EMPTY(hi))
3729 {
3730 item = &dict_lookup(hi)->di_tv;
3731
3732 if (STRCMP(hi->hi_key, "mode") == 0)
3733 {
3734 if (!(supported & JO_MODE))
3735 break;
3736 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3737 return FAIL;
3738 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003739 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003740 {
3741 if (!(supported & JO_IN_MODE))
3742 break;
3743 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3744 == FAIL)
3745 return FAIL;
3746 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003747 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003748 {
3749 if (!(supported & JO_OUT_MODE))
3750 break;
3751 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3752 == FAIL)
3753 return FAIL;
3754 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003755 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003756 {
3757 if (!(supported & JO_ERR_MODE))
3758 break;
3759 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3760 == FAIL)
3761 return FAIL;
3762 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003763 else if (STRCMP(hi->hi_key, "in_io") == 0
3764 || STRCMP(hi->hi_key, "out_io") == 0
3765 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003766 {
3767 if (!(supported & JO_OUT_IO))
3768 break;
3769 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3770 return FAIL;
3771 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003772 else if (STRCMP(hi->hi_key, "in_name") == 0
3773 || STRCMP(hi->hi_key, "out_name") == 0
3774 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003775 {
3776 part = part_from_char(*hi->hi_key);
3777
3778 if (!(supported & JO_OUT_IO))
3779 break;
3780 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3781 opt->jo_io_name[part] =
3782 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3783 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003784 else if (STRCMP(hi->hi_key, "in_buf") == 0
3785 || STRCMP(hi->hi_key, "out_buf") == 0
3786 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003787 {
3788 part = part_from_char(*hi->hi_key);
3789
3790 if (!(supported & JO_OUT_IO))
3791 break;
3792 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3793 opt->jo_io_buf[part] = get_tv_number(item);
3794 if (opt->jo_io_buf[part] <= 0)
3795 {
3796 EMSG2(_(e_invarg2), get_tv_string(item));
3797 return FAIL;
3798 }
3799 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3800 {
3801 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3802 return FAIL;
3803 }
3804 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003805 else if (STRCMP(hi->hi_key, "in_top") == 0
3806 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003807 {
3808 linenr_T *lp;
3809
3810 if (!(supported & JO_OUT_IO))
3811 break;
3812 if (hi->hi_key[3] == 't')
3813 {
3814 lp = &opt->jo_in_top;
3815 opt->jo_set |= JO_IN_TOP;
3816 }
3817 else
3818 {
3819 lp = &opt->jo_in_bot;
3820 opt->jo_set |= JO_IN_BOT;
3821 }
3822 *lp = get_tv_number(item);
3823 if (*lp < 0)
3824 {
3825 EMSG2(_(e_invarg2), get_tv_string(item));
3826 return FAIL;
3827 }
3828 }
3829 else if (STRCMP(hi->hi_key, "channel") == 0)
3830 {
3831 if (!(supported & JO_OUT_IO))
3832 break;
3833 opt->jo_set |= JO_CHANNEL;
3834 if (item->v_type != VAR_CHANNEL)
3835 {
3836 EMSG2(_(e_invarg2), "channel");
3837 return FAIL;
3838 }
3839 opt->jo_channel = item->vval.v_channel;
3840 }
3841 else if (STRCMP(hi->hi_key, "callback") == 0)
3842 {
3843 if (!(supported & JO_CALLBACK))
3844 break;
3845 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003846 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003847 if (opt->jo_callback == NULL)
3848 {
3849 EMSG2(_(e_invarg2), "callback");
3850 return FAIL;
3851 }
3852 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003853 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003854 {
3855 if (!(supported & JO_OUT_CALLBACK))
3856 break;
3857 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003858 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003859 if (opt->jo_out_cb == NULL)
3860 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003861 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003862 return FAIL;
3863 }
3864 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003865 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003866 {
3867 if (!(supported & JO_ERR_CALLBACK))
3868 break;
3869 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003870 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003871 if (opt->jo_err_cb == NULL)
3872 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003873 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003874 return FAIL;
3875 }
3876 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003877 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878 {
3879 if (!(supported & JO_CLOSE_CALLBACK))
3880 break;
3881 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003882 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003883 if (opt->jo_close_cb == NULL)
3884 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003885 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003886 return FAIL;
3887 }
3888 }
3889 else if (STRCMP(hi->hi_key, "waittime") == 0)
3890 {
3891 if (!(supported & JO_WAITTIME))
3892 break;
3893 opt->jo_set |= JO_WAITTIME;
3894 opt->jo_waittime = get_tv_number(item);
3895 }
3896 else if (STRCMP(hi->hi_key, "timeout") == 0)
3897 {
3898 if (!(supported & JO_TIMEOUT))
3899 break;
3900 opt->jo_set |= JO_TIMEOUT;
3901 opt->jo_timeout = get_tv_number(item);
3902 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003903 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003904 {
3905 if (!(supported & JO_OUT_TIMEOUT))
3906 break;
3907 opt->jo_set |= JO_OUT_TIMEOUT;
3908 opt->jo_out_timeout = get_tv_number(item);
3909 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003910 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003911 {
3912 if (!(supported & JO_ERR_TIMEOUT))
3913 break;
3914 opt->jo_set |= JO_ERR_TIMEOUT;
3915 opt->jo_err_timeout = get_tv_number(item);
3916 }
3917 else if (STRCMP(hi->hi_key, "part") == 0)
3918 {
3919 if (!(supported & JO_PART))
3920 break;
3921 opt->jo_set |= JO_PART;
3922 val = get_tv_string(item);
3923 if (STRCMP(val, "err") == 0)
3924 opt->jo_part = PART_ERR;
3925 else
3926 {
3927 EMSG2(_(e_invarg2), val);
3928 return FAIL;
3929 }
3930 }
3931 else if (STRCMP(hi->hi_key, "id") == 0)
3932 {
3933 if (!(supported & JO_ID))
3934 break;
3935 opt->jo_set |= JO_ID;
3936 opt->jo_id = get_tv_number(item);
3937 }
3938 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3939 {
3940 if (!(supported & JO_STOPONEXIT))
3941 break;
3942 opt->jo_set |= JO_STOPONEXIT;
3943 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3944 opt->jo_soe_buf);
3945 if (opt->jo_stoponexit == NULL)
3946 {
3947 EMSG2(_(e_invarg2), "stoponexit");
3948 return FAIL;
3949 }
3950 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003951 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003952 {
3953 if (!(supported & JO_EXIT_CB))
3954 break;
3955 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003956 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3957 {
3958 opt->jo_exit_partial = item->vval.v_partial;
3959 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3960 }
3961 else
3962 opt->jo_exit_cb = get_tv_string_buf_chk(
3963 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003964 if (opt->jo_exit_cb == NULL)
3965 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003966 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967 return FAIL;
3968 }
3969 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003970 else if (STRCMP(hi->hi_key, "block_write") == 0)
3971 {
3972 if (!(supported & JO_BLOCK_WRITE))
3973 break;
3974 opt->jo_set |= JO_BLOCK_WRITE;
3975 opt->jo_block_write = get_tv_number(item);
3976 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003977 else
3978 break;
3979 --todo;
3980 }
3981 if (todo > 0)
3982 {
3983 EMSG2(_(e_invarg2), hi->hi_key);
3984 return FAIL;
3985 }
3986
3987 return OK;
3988}
3989
3990/*
3991 * Get the channel from the argument.
3992 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02003993 * When "check_open" is TRUE check that the channel can be used.
3994 * When "reading" is TRUE "check_open" considers typeahead useful.
3995 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003996 */
3997 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02003998get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003999{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004000 channel_T *channel = NULL;
4001 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004002
4003 if (tv->v_type == VAR_JOB)
4004 {
4005 if (tv->vval.v_job != NULL)
4006 channel = tv->vval.v_job->jv_channel;
4007 }
4008 else if (tv->v_type == VAR_CHANNEL)
4009 {
4010 channel = tv->vval.v_channel;
4011 }
4012 else
4013 {
4014 EMSG2(_(e_invarg2), get_tv_string(tv));
4015 return NULL;
4016 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004017 if (channel != NULL && reading)
4018 has_readahead = channel_has_readahead(channel,
4019 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004020
Bram Moolenaar437905c2016-04-26 19:01:05 +02004021 if (check_open && (channel == NULL || (!channel_is_open(channel)
4022 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004023 {
4024 EMSG(_("E906: not an open channel"));
4025 return NULL;
4026 }
4027 return channel;
4028}
4029
4030static job_T *first_job = NULL;
4031
4032 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004033job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004034{
4035 ch_log(job->jv_channel, "Freeing job");
4036 if (job->jv_channel != NULL)
4037 {
4038 /* The link from the channel to the job doesn't count as a reference,
4039 * thus don't decrement the refcount of the job. The reference from
4040 * the job to the channel does count the refrence, decrement it and
4041 * NULL the reference. We don't set ch_job_killed, unreferencing the
4042 * job doesn't mean it stops running. */
4043 job->jv_channel->ch_job = NULL;
4044 channel_unref(job->jv_channel);
4045 }
4046 mch_clear_job(job);
4047
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004048 vim_free(job->jv_stoponexit);
4049 vim_free(job->jv_exit_cb);
4050 partial_unref(job->jv_exit_partial);
4051}
4052
4053 static void
4054job_free_job(job_T *job)
4055{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056 if (job->jv_next != NULL)
4057 job->jv_next->jv_prev = job->jv_prev;
4058 if (job->jv_prev == NULL)
4059 first_job = job->jv_next;
4060 else
4061 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 vim_free(job);
4063}
4064
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004065 static void
4066job_free(job_T *job)
4067{
4068 if (!in_free_unref_items)
4069 {
4070 job_free_contents(job);
4071 job_free_job(job);
4072 }
4073}
4074
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004075/*
4076 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004077 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4078 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004079 */
4080 static int
4081job_still_useful(job_T *job)
4082{
4083 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004084 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4085 || (job->jv_channel != NULL
4086 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004087}
4088
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004089/*
4090 * Mark references in jobs that are still useful.
4091 */
4092 int
4093set_ref_in_job(int copyID)
4094{
4095 int abort = FALSE;
4096 job_T *job;
4097 typval_T tv;
4098
4099 for (job = first_job; job != NULL; job = job->jv_next)
4100 if (job_still_useful(job))
4101 {
4102 tv.v_type = VAR_JOB;
4103 tv.vval.v_job = job;
4104 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4105 }
4106 return abort;
4107}
4108
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004109 void
4110job_unref(job_T *job)
4111{
4112 if (job != NULL && --job->jv_refcount <= 0)
4113 {
4114 /* Do not free the job when it has not ended yet and there is a
4115 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004116 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004117 {
4118 job_free(job);
4119 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004120 else if (job->jv_channel != NULL
4121 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004122 {
4123 /* Do remove the link to the channel, otherwise it hangs
4124 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004125 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004126 job->jv_channel->ch_job = NULL;
4127 channel_unref(job->jv_channel);
4128 job->jv_channel = NULL;
4129 }
4130 }
4131}
4132
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004133 int
4134free_unused_jobs_contents(int copyID, int mask)
4135{
4136 int did_free = FALSE;
4137 job_T *job;
4138
4139 for (job = first_job; job != NULL; job = 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 {
4143 /* Free the channel and ordinary items it contains, but don't
4144 * recurse into Lists, Dictionaries etc. */
4145 job_free_contents(job);
4146 did_free = TRUE;
4147 }
4148 return did_free;
4149}
4150
4151 void
4152free_unused_jobs(int copyID, int mask)
4153{
4154 job_T *job;
4155 job_T *job_next;
4156
4157 for (job = first_job; job != NULL; job = job_next)
4158 {
4159 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004160 if ((job->jv_copyID & mask) != (copyID & mask)
4161 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004162 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004163 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 job_free_job(job);
4165 }
4166 }
4167}
4168
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004169/*
4170 * Allocate a job. Sets the refcount to one and sets options default.
4171 */
4172 static job_T *
4173job_alloc(void)
4174{
4175 job_T *job;
4176
4177 job = (job_T *)alloc_clear(sizeof(job_T));
4178 if (job != NULL)
4179 {
4180 job->jv_refcount = 1;
4181 job->jv_stoponexit = vim_strsave((char_u *)"term");
4182
4183 if (first_job != NULL)
4184 {
4185 first_job->jv_prev = job;
4186 job->jv_next = first_job;
4187 }
4188 first_job = job;
4189 }
4190 return job;
4191}
4192
4193 void
4194job_set_options(job_T *job, jobopt_T *opt)
4195{
4196 if (opt->jo_set & JO_STOPONEXIT)
4197 {
4198 vim_free(job->jv_stoponexit);
4199 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4200 job->jv_stoponexit = NULL;
4201 else
4202 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4203 }
4204 if (opt->jo_set & JO_EXIT_CB)
4205 {
4206 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004207 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004208 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004209 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004210 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004211 job->jv_exit_partial = NULL;
4212 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004213 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004214 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004215 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004216 job->jv_exit_partial = opt->jo_exit_partial;
4217 if (job->jv_exit_partial != NULL)
4218 ++job->jv_exit_partial->pt_refcount;
4219 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004220 }
4221}
4222
4223/*
4224 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4225 */
4226 void
4227job_stop_on_exit()
4228{
4229 job_T *job;
4230
4231 for (job = first_job; job != NULL; job = job->jv_next)
4232 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4233 mch_stop_job(job, job->jv_stoponexit);
4234}
4235
4236/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004237 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004238 */
4239 void
4240job_check_ended(void)
4241{
4242 static time_t last_check = 0;
4243 time_t now;
4244 job_T *job;
4245 job_T *next;
4246
4247 /* Only do this once in 10 seconds. */
4248 now = time(NULL);
4249 if (last_check + 10 < now)
4250 {
4251 last_check = now;
4252 for (job = first_job; job != NULL; job = next)
4253 {
4254 next = job->jv_next;
4255 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4256 job_status(job); /* may free "job" */
4257 }
4258 }
4259}
4260
4261/*
4262 * "job_start()" function
4263 */
4264 job_T *
4265job_start(typval_T *argvars)
4266{
4267 job_T *job;
4268 char_u *cmd = NULL;
4269#if defined(UNIX)
4270# define USE_ARGV
4271 char **argv = NULL;
4272 int argc = 0;
4273#else
4274 garray_T ga;
4275#endif
4276 jobopt_T opt;
4277 int part;
4278
4279 job = job_alloc();
4280 if (job == NULL)
4281 return NULL;
4282
4283 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004284#ifndef USE_ARGV
4285 ga_init2(&ga, (int)sizeof(char*), 20);
4286#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004287
4288 /* Default mode is NL. */
4289 clear_job_options(&opt);
4290 opt.jo_mode = MODE_NL;
4291 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004292 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4293 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004294 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004295
4296 /* Check that when io is "file" that there is a file name. */
4297 for (part = PART_OUT; part <= PART_IN; ++part)
4298 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4299 && opt.jo_io[part] == JIO_FILE
4300 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4301 || *opt.jo_io_name[part] == NUL))
4302 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004303 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004304 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004305 }
4306
4307 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4308 {
4309 buf_T *buf = NULL;
4310
4311 /* check that we can find the buffer before starting the job */
4312 if (opt.jo_set & JO_IN_BUF)
4313 {
4314 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4315 if (buf == NULL)
4316 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4317 }
4318 else if (!(opt.jo_set & JO_IN_NAME))
4319 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004320 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004321 }
4322 else
4323 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4324 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004325 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 if (buf->b_ml.ml_mfp == NULL)
4327 {
4328 char_u numbuf[NUMBUFLEN];
4329 char_u *s;
4330
4331 if (opt.jo_set & JO_IN_BUF)
4332 {
4333 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4334 s = numbuf;
4335 }
4336 else
4337 s = opt.jo_io_name[PART_IN];
4338 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004339 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004340 }
4341 job->jv_in_buf = buf;
4342 }
4343
4344 job_set_options(job, &opt);
4345
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004346 if (argvars[0].v_type == VAR_STRING)
4347 {
4348 /* Command is a string. */
4349 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004350 if (cmd == NULL || *cmd == NUL)
4351 {
4352 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004353 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004354 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004355#ifdef USE_ARGV
4356 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004357 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 argv[argc] = NULL;
4359#endif
4360 }
4361 else if (argvars[0].v_type != VAR_LIST
4362 || argvars[0].vval.v_list == NULL
4363 || argvars[0].vval.v_list->lv_len < 1)
4364 {
4365 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004366 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367 }
4368 else
4369 {
4370 list_T *l = argvars[0].vval.v_list;
4371 listitem_T *li;
4372 char_u *s;
4373
4374#ifdef USE_ARGV
4375 /* Pass argv[] to mch_call_shell(). */
4376 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4377 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004378 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379#endif
4380 for (li = l->lv_first; li != NULL; li = li->li_next)
4381 {
4382 s = get_tv_string_chk(&li->li_tv);
4383 if (s == NULL)
4384 goto theend;
4385#ifdef USE_ARGV
4386 argv[argc++] = (char *)s;
4387#else
4388 /* Only escape when needed, double quotes are not always allowed. */
4389 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4390 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004391# ifdef WIN32
4392 int old_ssl = p_ssl;
4393
4394 /* This is using CreateProcess, not cmd.exe. Always use
4395 * double quote and backslashes. */
4396 p_ssl = 0;
4397# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004399# ifdef WIN32
4400 p_ssl = old_ssl;
4401# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004402 if (s == NULL)
4403 goto theend;
4404 ga_concat(&ga, s);
4405 vim_free(s);
4406 }
4407 else
4408 ga_concat(&ga, s);
4409 if (li->li_next != NULL)
4410 ga_append(&ga, ' ');
4411#endif
4412 }
4413#ifdef USE_ARGV
4414 argv[argc] = NULL;
4415#else
4416 cmd = ga.ga_data;
4417#endif
4418 }
4419
4420#ifdef USE_ARGV
4421 if (ch_log_active())
4422 {
4423 garray_T ga;
4424 int i;
4425
4426 ga_init2(&ga, (int)sizeof(char), 200);
4427 for (i = 0; i < argc; ++i)
4428 {
4429 if (i > 0)
4430 ga_concat(&ga, (char_u *)" ");
4431 ga_concat(&ga, (char_u *)argv[i]);
4432 }
4433 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4434 ga_clear(&ga);
4435 }
4436 mch_start_job(argv, job, &opt);
4437#else
4438 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4439 mch_start_job((char *)cmd, job, &opt);
4440#endif
4441
4442 /* If the channel is reading from a buffer, write lines now. */
4443 if (job->jv_channel != NULL)
4444 channel_write_in(job->jv_channel);
4445
4446theend:
4447#ifdef USE_ARGV
4448 vim_free(argv);
4449#else
4450 vim_free(ga.ga_data);
4451#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004452 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004453 return job;
4454}
4455
4456/*
4457 * Get the status of "job" and invoke the exit callback when needed.
4458 * The returned string is not allocated.
4459 */
4460 char *
4461job_status(job_T *job)
4462{
4463 char *result;
4464
4465 if (job->jv_status == JOB_ENDED)
4466 /* No need to check, dead is dead. */
4467 result = "dead";
4468 else if (job->jv_status == JOB_FAILED)
4469 result = "fail";
4470 else
4471 {
4472 result = mch_job_status(job);
4473 if (job->jv_status == JOB_ENDED)
4474 ch_log(job->jv_channel, "Job ended");
4475 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4476 {
4477 typval_T argv[3];
4478 typval_T rettv;
4479 int dummy;
4480
4481 /* invoke the exit callback; make sure the refcount is > 0 */
4482 ++job->jv_refcount;
4483 argv[0].v_type = VAR_JOB;
4484 argv[0].vval.v_job = job;
4485 argv[1].v_type = VAR_NUMBER;
4486 argv[1].vval.v_number = job->jv_exitval;
4487 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004488 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4489 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490 clear_tv(&rettv);
4491 --job->jv_refcount;
4492 }
4493 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4494 {
4495 /* The job was already unreferenced, now that it ended it can be
4496 * freed. Careful: caller must not use "job" after this! */
4497 job_free(job);
4498 }
4499 }
4500 return result;
4501}
4502
Bram Moolenaar8950a562016-03-12 15:22:55 +01004503/*
4504 * Implementation of job_info().
4505 */
4506 void
4507job_info(job_T *job, dict_T *dict)
4508{
4509 dictitem_T *item;
4510 varnumber_T nr;
4511
4512 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4513
4514 item = dictitem_alloc((char_u *)"channel");
4515 if (item == NULL)
4516 return;
4517 item->di_tv.v_lock = 0;
4518 item->di_tv.v_type = VAR_CHANNEL;
4519 item->di_tv.vval.v_channel = job->jv_channel;
4520 if (job->jv_channel != NULL)
4521 ++job->jv_channel->ch_refcount;
4522 if (dict_add(dict, item) == FAIL)
4523 dictitem_free(item);
4524
4525#ifdef UNIX
4526 nr = job->jv_pid;
4527#else
4528 nr = job->jv_proc_info.dwProcessId;
4529#endif
4530 dict_add_nr_str(dict, "process", nr, NULL);
4531
4532 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004533 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004534 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4535}
4536
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004537 int
4538job_stop(job_T *job, typval_T *argvars)
4539{
4540 char_u *arg;
4541
4542 if (argvars[1].v_type == VAR_UNKNOWN)
4543 arg = (char_u *)"";
4544 else
4545 {
4546 arg = get_tv_string_chk(&argvars[1]);
4547 if (arg == NULL)
4548 {
4549 EMSG(_(e_invarg));
4550 return 0;
4551 }
4552 }
4553 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4554 if (mch_stop_job(job, arg) == FAIL)
4555 return 0;
4556
4557 /* Assume that "hup" does not kill the job. */
4558 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4559 job->jv_channel->ch_job_killed = TRUE;
4560
4561 /* We don't try freeing the job, obviously the caller still has a
4562 * reference to it. */
4563 return 1;
4564}
4565
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004566#endif /* FEAT_JOB_CHANNEL */