blob: aa588031c1f72446f7a262e60bbb9618089215a4 [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);
2497 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002498 --channel->ch_refcount;
2499
2500 /* the callback is only called once */
2501 vim_free(channel->ch_close_cb);
2502 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002503 partial_unref(channel->ch_close_partial);
2504 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002505
2506 /* any remaining messages are useless now */
2507 for (part = PART_SOCK; part <= PART_ERR; ++part)
2508 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002509 }
2510
2511 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002512}
2513
Bram Moolenaard04a0202016-01-26 23:30:18 +01002514/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002515 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002516 * Returns NULL if there is nothing.
2517 */
2518 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002519channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002520{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002521 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002522
Bram Moolenaar77073442016-02-13 23:23:53 +01002523 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002524 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002525 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002526}
2527
2528/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002529 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002530 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 static void
2532channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2535 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002536
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002537 while (channel_peek(channel, part) != NULL)
2538 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002539
2540 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002541 {
2542 cbq_T *node = cb_head->cq_next;
2543
2544 remove_cb_node(cb_head, node);
2545 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002546 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002547 vim_free(node);
2548 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002549
2550 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002551 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002552 free_tv(json_head->jq_next->jq_value);
2553 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002554 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002555
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002556 vim_free(channel->ch_part[part].ch_callback);
2557 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002558 partial_unref(channel->ch_part[part].ch_partial);
2559 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002560}
2561
2562/*
2563 * Clear all the read buffers on "channel".
2564 */
2565 void
2566channel_clear(channel_T *channel)
2567{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002568 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002569 vim_free(channel->ch_hostname);
2570 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002571 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002572 channel_clear_one(channel, PART_OUT);
2573 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002574 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002575 vim_free(channel->ch_callback);
2576 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002577 partial_unref(channel->ch_partial);
2578 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002579 vim_free(channel->ch_close_cb);
2580 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002581 partial_unref(channel->ch_close_partial);
2582 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002583}
2584
Bram Moolenaar77073442016-02-13 23:23:53 +01002585#if defined(EXITFREE) || defined(PROTO)
2586 void
2587channel_free_all(void)
2588{
2589 channel_T *channel;
2590
Bram Moolenaard6051b52016-02-28 15:49:03 +01002591 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002592 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2593 channel_clear(channel);
2594}
2595#endif
2596
2597
Bram Moolenaard04a0202016-01-26 23:30:18 +01002598/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002599#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002600
2601/* Buffer size for reading incoming messages. */
2602#define MAXMSGSIZE 4096
2603
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002604#if defined(HAVE_SELECT)
2605/*
2606 * Add write fds where we are waiting for writing to be possible.
2607 */
2608 static int
2609channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2610{
2611 int maxfd = maxfd_arg;
2612 channel_T *ch;
2613
2614 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2615 {
2616 chanpart_T *in_part = &ch->ch_part[PART_IN];
2617
2618 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2619 {
2620 FD_SET((int)in_part->ch_fd, wfds);
2621 if ((int)in_part->ch_fd >= maxfd)
2622 maxfd = (int)in_part->ch_fd + 1;
2623 }
2624 }
2625 return maxfd;
2626}
2627#else
2628/*
2629 * Add write fds where we are waiting for writing to be possible.
2630 */
2631 static int
2632channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2633{
2634 int nfd = nfd_in;
2635 channel_T *ch;
2636
2637 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2638 {
2639 chanpart_T *in_part = &ch->ch_part[PART_IN];
2640
2641 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2642 {
2643 in_part->ch_poll_idx = nfd;
2644 fds[nfd].fd = in_part->ch_fd;
2645 fds[nfd].events = POLLOUT;
2646 ++nfd;
2647 }
2648 else
2649 in_part->ch_poll_idx = -1;
2650 }
2651 return nfd;
2652}
2653#endif
2654
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002655typedef enum {
2656 CW_READY,
2657 CW_NOT_READY,
2658 CW_ERROR
2659} channel_wait_result;
2660
Bram Moolenaard04a0202016-01-26 23:30:18 +01002661/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002662 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002663 * Return CW_READY when there is something to read.
2664 * Return CW_NOT_READY when there is nothing to read.
2665 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002666 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002667 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002668channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002669{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002670 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002671 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002672
Bram Moolenaard8070362016-02-15 21:56:54 +01002673# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002675 {
2676 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002677 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002678 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002679 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002680
2681 /* reading from a pipe, not a socket */
2682 while (TRUE)
2683 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002684 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2685
2686 if (r && nread > 0)
2687 return CW_READY;
2688 if (r == 0)
2689 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002690
2691 /* perhaps write some buffer lines */
2692 channel_write_any_lines();
2693
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002694 sleep_time = deadline - GetTickCount();
2695 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002696 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002697 /* Wait for a little while. Very short at first, up to 10 msec
2698 * after looping a few times. */
2699 if (sleep_time > delay)
2700 sleep_time = delay;
2701 Sleep(sleep_time);
2702 delay = delay * 2;
2703 if (delay > 10)
2704 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002705 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002706 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002707 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002708#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002709 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002710#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002711 struct timeval tval;
2712 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002713 fd_set wfds;
2714 int ret;
2715 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002716
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002717 tval.tv_sec = timeout / 1000;
2718 tval.tv_usec = (timeout % 1000) * 1000;
2719 for (;;)
2720 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002721 FD_ZERO(&rfds);
2722 FD_SET((int)fd, &rfds);
2723
2724 /* Write lines to a pipe when a pipe can be written to. Need to
2725 * set this every time, some buffers may be done. */
2726 maxfd = (int)fd + 1;
2727 FD_ZERO(&wfds);
2728 maxfd = channel_fill_wfds(maxfd, &wfds);
2729
2730 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002731# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002732 SOCK_ERRNO;
2733 if (ret == -1 && errno == EINTR)
2734 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002735# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002736 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002737 {
2738 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002739 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002740 channel_write_any_lines();
2741 continue;
2742 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002743 break;
2744 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002745#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002746 for (;;)
2747 {
2748 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2749 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002750
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002751 fds[0].fd = fd;
2752 fds[0].events = POLLIN;
2753 nfd = channel_fill_poll_write(nfd, fds);
2754 if (poll(fds, nfd, timeout) > 0)
2755 {
2756 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002757 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002758 channel_write_any_lines();
2759 continue;
2760 }
2761 break;
2762 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002763#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002764 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002765 return CW_NOT_READY;
2766}
2767
2768 static void
2769channel_close_on_error(channel_T *channel, int part, char *func)
2770{
2771 /* Do not call emsg(), most likely the other end just exited. */
2772 ch_errors(channel, "%s(): Cannot read from channel", func);
2773
2774 /* Queue a "DETACH" netbeans message in the command queue in order to
2775 * terminate the netbeans session later. Do not end the session here
2776 * directly as we may be running in the context of a call to
2777 * netbeans_parse_messages():
2778 * netbeans_parse_messages
2779 * -> autocmd triggered while processing the netbeans cmd
2780 * -> ui_breakcheck
2781 * -> gui event loop or select loop
2782 * -> channel_read()
2783 * Don't send "DETACH" for a JS or JSON channel.
2784 */
2785 if (channel->ch_part[part].ch_mode == MODE_RAW
2786 || channel->ch_part[part].ch_mode == MODE_NL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002787 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002788 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2789
2790 /* When reading from stdout is not possible, assume the other side has
2791 * died. */
2792 channel_close(channel, TRUE);
2793 if (channel->ch_nb_close_cb != NULL)
2794 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002795}
2796
2797/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002798 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002799 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002800 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002801 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002802 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002803channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002804{
2805 static char_u *buf = NULL;
2806 int len = 0;
2807 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002808 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002809 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002810
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002811 fd = channel->ch_part[part].ch_fd;
2812 if (fd == INVALID_FD)
2813 {
2814 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002815 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002816 }
2817 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002818
2819 /* Allocate a buffer to read into. */
2820 if (buf == NULL)
2821 {
2822 buf = alloc(MAXMSGSIZE);
2823 if (buf == NULL)
2824 return; /* out of memory! */
2825 }
2826
2827 /* Keep on reading for as long as there is something to read.
2828 * Use select() or poll() to avoid blocking on a message that is exactly
2829 * MAXMSGSIZE long. */
2830 for (;;)
2831 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002832 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002833 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002834 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002835 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002836 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002837 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002838 if (len <= 0)
2839 break; /* error or nothing more to read */
2840
2841 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002842 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002843 readlen += len;
2844 if (len < MAXMSGSIZE)
2845 break; /* did read everything that's available */
2846 }
2847
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002848 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002849 if (readlen <= 0)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002850 channel_close_on_error(channel, part, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002851
2852#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002853 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002854 if (CH_HAS_GUI && gtk_main_level() > 0)
2855 gtk_main_quit();
2856#endif
2857}
2858
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002859/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002860 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002861 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002862 * Returns what was read in allocated memory.
2863 * Returns NULL in case of error or timeout.
2864 */
2865 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002866channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002867{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002868 char_u *buf;
2869 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002870 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002871 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002872 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002873
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002874 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002875 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002876
2877 while (TRUE)
2878 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002879 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002880 if (buf != NULL && (mode == MODE_RAW
2881 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2882 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002883 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002884 continue;
2885
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002886 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002887 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002888 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002889 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002890 {
2891 ch_log(channel, "Timed out");
2892 return NULL;
2893 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002894 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002895 }
2896
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002897 if (mode == MODE_RAW)
2898 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002899 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002900 }
2901 else
2902 {
2903 nl = vim_strchr(buf, NL);
2904 if (nl[1] == NUL)
2905 {
2906 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002907 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002908 *nl = NUL;
2909 }
2910 else
2911 {
2912 /* Copy the message into allocated memory and remove it from the
2913 * buffer. */
2914 msg = vim_strnsave(buf, (int)(nl - buf));
2915 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2916 }
2917 }
2918 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002919 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002920 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002921}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002922
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002923/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002924 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002925 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002926 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002927 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002928 */
2929 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002930channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002931 channel_T *channel,
2932 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002933 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002934 int id,
2935 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002936{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002937 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002938 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002939 int timeout;
2940 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002941
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002942 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002943 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002944 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002945 for (;;)
2946 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002947 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002948
2949 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002950 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002951 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002952 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002953 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002954 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002955
Bram Moolenaard7ece102016-02-02 23:23:02 +01002956 if (!more)
2957 {
2958 /* Handle any other messages in the queue. If done some more
2959 * messages may have arrived. */
2960 if (channel_parse_messages())
2961 continue;
2962
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002963 /* Wait for up to the timeout. If there was an incomplete message
2964 * use the deadline for that. */
2965 timeout = timeout_arg;
2966 if (chanpart->ch_waiting)
2967 {
2968#ifdef WIN32
2969 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2970#else
2971 {
2972 struct timeval now_tv;
2973
2974 gettimeofday(&now_tv, NULL);
2975 timeout = (chanpart->ch_deadline.tv_sec
2976 - now_tv.tv_sec) * 1000
2977 + (chanpart->ch_deadline.tv_usec
2978 - now_tv.tv_usec) / 1000
2979 + 1;
2980 }
2981#endif
2982 if (timeout < 0)
2983 {
2984 /* Something went wrong, channel_parse_json() didn't
2985 * discard message. Cancel waiting. */
2986 chanpart->ch_waiting = FALSE;
2987 timeout = timeout_arg;
2988 }
2989 else if (timeout > timeout_arg)
2990 timeout = timeout_arg;
2991 }
2992 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002993 if (fd == INVALID_FD
2994 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002995 {
2996 if (timeout == timeout_arg)
2997 {
2998 if (fd != INVALID_FD)
2999 ch_log(channel, "Timed out");
3000 break;
3001 }
3002 }
3003 else
3004 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003005 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003006 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003007 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003008 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003009}
3010
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003011/*
3012 * Common for ch_read() and ch_readraw().
3013 */
3014 void
3015common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3016{
3017 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003018 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003019 jobopt_T opt;
3020 int mode;
3021 int timeout;
3022 int id = -1;
3023 typval_T *listtv = NULL;
3024
3025 /* return an empty string by default */
3026 rettv->v_type = VAR_STRING;
3027 rettv->vval.v_string = NULL;
3028
3029 clear_job_options(&opt);
3030 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3031 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003032 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003033
Bram Moolenaar437905c2016-04-26 19:01:05 +02003034 if (opt.jo_set & JO_PART)
3035 part = opt.jo_part;
3036 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003037 if (channel != NULL)
3038 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003039 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003040 part = channel_part_read(channel);
3041 mode = channel_get_mode(channel, part);
3042 timeout = channel_get_timeout(channel, part);
3043 if (opt.jo_set & JO_TIMEOUT)
3044 timeout = opt.jo_timeout;
3045
3046 if (raw || mode == MODE_RAW || mode == MODE_NL)
3047 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3048 else
3049 {
3050 if (opt.jo_set & JO_ID)
3051 id = opt.jo_id;
3052 channel_read_json_block(channel, part, timeout, id, &listtv);
3053 if (listtv != NULL)
3054 {
3055 *rettv = *listtv;
3056 vim_free(listtv);
3057 }
3058 else
3059 {
3060 rettv->v_type = VAR_SPECIAL;
3061 rettv->vval.v_number = VVAL_NONE;
3062 }
3063 }
3064 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003065
3066theend:
3067 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003068}
3069
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003070# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3071 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003072/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003073 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003074 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003075 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003076 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003077channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003078{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003079 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003080 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003081
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003082 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003083 for (channel = first_channel; channel != NULL;
3084 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003085 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003087 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003088 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003089 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003090 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003091 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003092 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003093 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003094}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003095# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003096
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003097# if defined(WIN32) || defined(PROTO)
3098/*
3099 * Check the channels for anything that is ready to be read.
3100 * The data is put in the read queue.
3101 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003102 void
3103channel_handle_events(void)
3104{
3105 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003106 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003107 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003108
3109 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3110 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003111 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003112 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003113 {
3114 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003115 if (fd != INVALID_FD)
3116 {
3117 int r = channel_wait(channel, fd, 0);
3118
3119 if (r == CW_READY)
3120 channel_read(channel, part, "channel_handle_events");
3121 else if (r == CW_ERROR)
3122 channel_close_on_error(channel, part,
3123 "channel_handle_events()");
3124 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003125 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003126 }
3127}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003128# endif
3129
Bram Moolenaard04a0202016-01-26 23:30:18 +01003130/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003131 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003132 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003133 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003134 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003135 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003136channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003137{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003138 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003139 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003140 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003141
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003142 fd = channel->ch_part[part].ch_fd;
3143 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003144 {
3145 if (!channel->ch_error && fun != NULL)
3146 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003147 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003148 EMSG2("E630: %s(): write while not connected", fun);
3149 }
3150 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003151 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003152 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003153
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003154 if (log_fd != NULL)
3155 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003156 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003157 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003158 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003159 fprintf(log_fd, "'\n");
3160 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003161 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003162 }
3163
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003164 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003165 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003166 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003167 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003168 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003169 {
3170 if (!channel->ch_error && fun != NULL)
3171 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003172 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003173 EMSG2("E631: %s(): write failed", fun);
3174 }
3175 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003176 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003177 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003178
3179 channel->ch_error = FALSE;
3180 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003181}
3182
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003183/*
3184 * Common for "ch_sendexpr()" and "ch_sendraw()".
3185 * Returns the channel if the caller should read the response.
3186 * Sets "part_read" to the the read fd.
3187 * Otherwise returns NULL.
3188 */
3189 channel_T *
3190send_common(
3191 typval_T *argvars,
3192 char_u *text,
3193 int id,
3194 int eval,
3195 jobopt_T *opt,
3196 char *fun,
3197 int *part_read)
3198{
3199 channel_T *channel;
3200 int part_send;
3201
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003202 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003203 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003204 if (channel == NULL)
3205 return NULL;
3206 part_send = channel_part_send(channel);
3207 *part_read = channel_part_read(channel);
3208
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003209 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3210 return NULL;
3211
3212 /* Set the callback. An empty callback means no callback and not reading
3213 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3214 * allowed. */
3215 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3216 {
3217 if (eval)
3218 {
3219 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3220 return NULL;
3221 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003222 channel_set_req_callback(channel, part_send,
3223 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003224 }
3225
3226 if (channel_send(channel, part_send, text, fun) == OK
3227 && opt->jo_callback == NULL)
3228 return channel;
3229 return NULL;
3230}
3231
3232/*
3233 * common for "ch_evalexpr()" and "ch_sendexpr()"
3234 */
3235 void
3236ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3237{
3238 char_u *text;
3239 typval_T *listtv;
3240 channel_T *channel;
3241 int id;
3242 ch_mode_T ch_mode;
3243 int part_send;
3244 int part_read;
3245 jobopt_T opt;
3246 int timeout;
3247
3248 /* return an empty string by default */
3249 rettv->v_type = VAR_STRING;
3250 rettv->vval.v_string = NULL;
3251
Bram Moolenaar437905c2016-04-26 19:01:05 +02003252 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003253 if (channel == NULL)
3254 return;
3255 part_send = channel_part_send(channel);
3256
3257 ch_mode = channel_get_mode(channel, part_send);
3258 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3259 {
3260 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3261 return;
3262 }
3263
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003264 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003265 text = json_encode_nr_expr(id, &argvars[1],
3266 ch_mode == MODE_JS ? JSON_JS : 0);
3267 if (text == NULL)
3268 return;
3269
3270 channel = send_common(argvars, text, id, eval, &opt,
3271 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3272 vim_free(text);
3273 if (channel != NULL && eval)
3274 {
3275 if (opt.jo_set & JO_TIMEOUT)
3276 timeout = opt.jo_timeout;
3277 else
3278 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003279 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3280 == OK)
3281 {
3282 list_T *list = listtv->vval.v_list;
3283
3284 /* Move the item from the list and then change the type to
3285 * avoid the value being freed. */
3286 *rettv = list->lv_last->li_tv;
3287 list->lv_last->li_tv.v_type = VAR_NUMBER;
3288 free_tv(listtv);
3289 }
3290 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003291 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003292}
3293
3294/*
3295 * common for "ch_evalraw()" and "ch_sendraw()"
3296 */
3297 void
3298ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3299{
3300 char_u buf[NUMBUFLEN];
3301 char_u *text;
3302 channel_T *channel;
3303 int part_read;
3304 jobopt_T opt;
3305 int timeout;
3306
3307 /* return an empty string by default */
3308 rettv->v_type = VAR_STRING;
3309 rettv->vval.v_string = NULL;
3310
3311 text = get_tv_string_buf(&argvars[1], buf);
3312 channel = send_common(argvars, text, 0, eval, &opt,
3313 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3314 if (channel != NULL && eval)
3315 {
3316 if (opt.jo_set & JO_TIMEOUT)
3317 timeout = opt.jo_timeout;
3318 else
3319 timeout = channel_get_timeout(channel, part_read);
3320 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3321 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003322 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003323}
3324
Bram Moolenaard04a0202016-01-26 23:30:18 +01003325# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003326/*
3327 * Add open channels to the poll struct.
3328 * Return the adjusted struct index.
3329 * The type of "fds" is hidden to avoid problems with the function proto.
3330 */
3331 int
3332channel_poll_setup(int nfd_in, void *fds_in)
3333{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003334 int nfd = nfd_in;
3335 channel_T *channel;
3336 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003338
Bram Moolenaar77073442016-02-13 23:23:53 +01003339 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003340 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003341 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003342 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003343 chanpart_T *ch_part = &channel->ch_part[part];
3344
3345 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003346 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003347 ch_part->ch_poll_idx = nfd;
3348 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003349 fds[nfd].events = POLLIN;
3350 nfd++;
3351 }
3352 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003353 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003354 }
3355 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003356
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003357 nfd = channel_fill_poll_write(nfd, fds);
3358
Bram Moolenaare0874f82016-01-24 20:36:41 +01003359 return nfd;
3360}
3361
3362/*
3363 * The type of "fds" is hidden to avoid problems with the function proto.
3364 */
3365 int
3366channel_poll_check(int ret_in, void *fds_in)
3367{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003368 int ret = ret_in;
3369 channel_T *channel;
3370 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003371 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003372 int idx;
3373 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003374
Bram Moolenaar77073442016-02-13 23:23:53 +01003375 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003376 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003377 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003378 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003379 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003380
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003381 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003382 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003383 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003384 --ret;
3385 }
3386 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003387
3388 in_part = &channel->ch_part[PART_IN];
3389 idx = in_part->ch_poll_idx;
3390 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3391 {
3392 if (in_part->ch_buf_append)
3393 {
3394 if (in_part->ch_buffer != NULL)
3395 channel_write_new_lines(in_part->ch_buffer);
3396 }
3397 else
3398 channel_write_in(channel);
3399 --ret;
3400 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003401 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003402
3403 return ret;
3404}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003405# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003406
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003407# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003408/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003409 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003410 */
3411 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003412channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003413{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003414 int maxfd = maxfd_in;
3415 channel_T *channel;
3416 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003417 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003418 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003419
Bram Moolenaar77073442016-02-13 23:23:53 +01003420 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003421 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003422 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003423 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003424 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003425
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003426 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003427 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003428 FD_SET((int)fd, rfds);
3429 if (maxfd < (int)fd)
3430 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003431 }
3432 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003433 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003434
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003435 maxfd = channel_fill_wfds(maxfd, wfds);
3436
Bram Moolenaare0874f82016-01-24 20:36:41 +01003437 return maxfd;
3438}
3439
3440/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003441 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003442 */
3443 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003444channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003445{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003446 int ret = ret_in;
3447 channel_T *channel;
3448 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003449 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003450 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003451 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003452
Bram Moolenaar77073442016-02-13 23:23:53 +01003453 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003454 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003455 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003456 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003457 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003458
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003459 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003460 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003461 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003462 --ret;
3463 }
3464 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003465
3466 in_part = &channel->ch_part[PART_IN];
3467 if (ret > 0 && in_part->ch_fd != INVALID_FD
3468 && FD_ISSET(in_part->ch_fd, wfds))
3469 {
3470 if (in_part->ch_buf_append)
3471 {
3472 if (in_part->ch_buffer != NULL)
3473 channel_write_new_lines(in_part->ch_buffer);
3474 }
3475 else
3476 channel_write_in(channel);
3477 --ret;
3478 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003479 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003480
3481 return ret;
3482}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003483# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003484
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003485/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003486 * Execute queued up commands.
3487 * Invoked from the main loop when it's safe to execute received commands.
3488 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003489 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003490 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003491channel_parse_messages(void)
3492{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003493 channel_T *channel = first_channel;
3494 int ret = FALSE;
3495 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003497
Bram Moolenaard0b65022016-03-06 21:50:33 +01003498 /* Only do this message when another message was given, otherwise we get
3499 * lots of them. */
3500 if (did_log_msg)
3501 {
3502 ch_log(NULL, "looking for messages on channels");
3503 did_log_msg = FALSE;
3504 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003505 while (channel != NULL)
3506 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003507 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003508 {
3509 /* channel is no longer useful, free it */
3510 channel_free(channel);
3511 channel = first_channel;
3512 part = PART_SOCK;
3513 continue;
3514 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003515 if (channel->ch_part[part].ch_fd != INVALID_FD
3516 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003517 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003518 /* Increase the refcount, in case the handler causes the channel
3519 * to be unreferenced or closed. */
3520 ++channel->ch_refcount;
3521 r = may_invoke_callback(channel, part);
3522 if (r == OK)
3523 ret = TRUE;
3524 if (channel_unref(channel) || r == OK)
3525 {
3526 /* channel was freed or something was done, start over */
3527 channel = first_channel;
3528 part = PART_SOCK;
3529 continue;
3530 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003531 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003532 if (part < PART_ERR)
3533 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003534 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003535 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003536 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 part = PART_SOCK;
3538 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003539 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003540
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003541 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003542 {
3543 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003544 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003545 }
3546
Bram Moolenaard7ece102016-02-02 23:23:02 +01003547 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003548}
3549
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003550/*
3551 * Mark references to lists used in channels.
3552 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003553 int
3554set_ref_in_channel(int copyID)
3555{
Bram Moolenaar77073442016-02-13 23:23:53 +01003556 int abort = FALSE;
3557 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003558 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003559
Bram Moolenaar77073442016-02-13 23:23:53 +01003560 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003561 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003562 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003563 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003564 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3565 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003566
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003567 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003568 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003569 list_T *l = item->jq_value->vval.v_list;
3570
3571 if (l->lv_copyID != copyID)
3572 {
3573 l->lv_copyID = copyID;
3574 abort = abort || set_ref_in_list(l, copyID, NULL);
3575 }
3576 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003577 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003578 }
3579 }
3580 return abort;
3581}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003582
3583/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003584 * Return the "part" to write to for "channel".
3585 */
3586 int
3587channel_part_send(channel_T *channel)
3588{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003589 if (channel->CH_SOCK_FD == INVALID_FD)
3590 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003591 return PART_SOCK;
3592}
3593
3594/*
3595 * Return the default "part" to read from for "channel".
3596 */
3597 int
3598channel_part_read(channel_T *channel)
3599{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003600 if (channel->CH_SOCK_FD == INVALID_FD)
3601 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003602 return PART_SOCK;
3603}
3604
3605/*
3606 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003607 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003608 */
3609 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003610channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003611{
Bram Moolenaar77073442016-02-13 23:23:53 +01003612 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003613 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003614 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003615}
3616
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003617/*
3618 * Return the timeout of "channel"/"part"
3619 */
3620 int
3621channel_get_timeout(channel_T *channel, int part)
3622{
3623 return channel->ch_part[part].ch_timeout;
3624}
3625
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003626 static int
3627handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3628{
3629 char_u *val = get_tv_string(item);
3630
3631 opt->jo_set |= jo;
3632 if (STRCMP(val, "nl") == 0)
3633 *modep = MODE_NL;
3634 else if (STRCMP(val, "raw") == 0)
3635 *modep = MODE_RAW;
3636 else if (STRCMP(val, "js") == 0)
3637 *modep = MODE_JS;
3638 else if (STRCMP(val, "json") == 0)
3639 *modep = MODE_JSON;
3640 else
3641 {
3642 EMSG2(_(e_invarg2), val);
3643 return FAIL;
3644 }
3645 return OK;
3646}
3647
3648 static int
3649handle_io(typval_T *item, int part, jobopt_T *opt)
3650{
3651 char_u *val = get_tv_string(item);
3652
3653 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3654 if (STRCMP(val, "null") == 0)
3655 opt->jo_io[part] = JIO_NULL;
3656 else if (STRCMP(val, "pipe") == 0)
3657 opt->jo_io[part] = JIO_PIPE;
3658 else if (STRCMP(val, "file") == 0)
3659 opt->jo_io[part] = JIO_FILE;
3660 else if (STRCMP(val, "buffer") == 0)
3661 opt->jo_io[part] = JIO_BUFFER;
3662 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3663 opt->jo_io[part] = JIO_OUT;
3664 else
3665 {
3666 EMSG2(_(e_invarg2), val);
3667 return FAIL;
3668 }
3669 return OK;
3670}
3671
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003672/*
3673 * Clear a jobopt_T before using it.
3674 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003675 void
3676clear_job_options(jobopt_T *opt)
3677{
3678 vim_memset(opt, 0, sizeof(jobopt_T));
3679}
3680
3681/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003682 * Free any members of a jobopt_T.
3683 */
3684 void
3685free_job_options(jobopt_T *opt)
3686{
3687 if (opt->jo_partial != NULL)
3688 partial_unref(opt->jo_partial);
3689 if (opt->jo_out_partial != NULL)
3690 partial_unref(opt->jo_out_partial);
3691 if (opt->jo_err_partial != NULL)
3692 partial_unref(opt->jo_err_partial);
3693 if (opt->jo_close_partial != NULL)
3694 partial_unref(opt->jo_close_partial);
3695}
3696
3697/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698 * Get the PART_ number from the first character of an option name.
3699 */
3700 static int
3701part_from_char(int c)
3702{
3703 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3704}
3705
3706/*
3707 * Get the option entries from the dict in "tv", parse them and put the result
3708 * in "opt".
3709 * Only accept options in "supported".
3710 * If an option value is invalid return FAIL.
3711 */
3712 int
3713get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3714{
3715 typval_T *item;
3716 char_u *val;
3717 dict_T *dict;
3718 int todo;
3719 hashitem_T *hi;
3720 int part;
3721
3722 opt->jo_set = 0;
3723 if (tv->v_type == VAR_UNKNOWN)
3724 return OK;
3725 if (tv->v_type != VAR_DICT)
3726 {
3727 EMSG(_(e_invarg));
3728 return FAIL;
3729 }
3730 dict = tv->vval.v_dict;
3731 if (dict == NULL)
3732 return OK;
3733
3734 todo = (int)dict->dv_hashtab.ht_used;
3735 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3736 if (!HASHITEM_EMPTY(hi))
3737 {
3738 item = &dict_lookup(hi)->di_tv;
3739
3740 if (STRCMP(hi->hi_key, "mode") == 0)
3741 {
3742 if (!(supported & JO_MODE))
3743 break;
3744 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3745 return FAIL;
3746 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003747 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003748 {
3749 if (!(supported & JO_IN_MODE))
3750 break;
3751 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3752 == FAIL)
3753 return FAIL;
3754 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003755 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003756 {
3757 if (!(supported & JO_OUT_MODE))
3758 break;
3759 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3760 == FAIL)
3761 return FAIL;
3762 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003763 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003764 {
3765 if (!(supported & JO_ERR_MODE))
3766 break;
3767 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3768 == FAIL)
3769 return FAIL;
3770 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003771 else if (STRCMP(hi->hi_key, "in_io") == 0
3772 || STRCMP(hi->hi_key, "out_io") == 0
3773 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003774 {
3775 if (!(supported & JO_OUT_IO))
3776 break;
3777 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3778 return FAIL;
3779 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003780 else if (STRCMP(hi->hi_key, "in_name") == 0
3781 || STRCMP(hi->hi_key, "out_name") == 0
3782 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003783 {
3784 part = part_from_char(*hi->hi_key);
3785
3786 if (!(supported & JO_OUT_IO))
3787 break;
3788 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3789 opt->jo_io_name[part] =
3790 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3791 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003792 else if (STRCMP(hi->hi_key, "in_buf") == 0
3793 || STRCMP(hi->hi_key, "out_buf") == 0
3794 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003795 {
3796 part = part_from_char(*hi->hi_key);
3797
3798 if (!(supported & JO_OUT_IO))
3799 break;
3800 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3801 opt->jo_io_buf[part] = get_tv_number(item);
3802 if (opt->jo_io_buf[part] <= 0)
3803 {
3804 EMSG2(_(e_invarg2), get_tv_string(item));
3805 return FAIL;
3806 }
3807 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3808 {
3809 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3810 return FAIL;
3811 }
3812 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003813 else if (STRCMP(hi->hi_key, "in_top") == 0
3814 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003815 {
3816 linenr_T *lp;
3817
3818 if (!(supported & JO_OUT_IO))
3819 break;
3820 if (hi->hi_key[3] == 't')
3821 {
3822 lp = &opt->jo_in_top;
3823 opt->jo_set |= JO_IN_TOP;
3824 }
3825 else
3826 {
3827 lp = &opt->jo_in_bot;
3828 opt->jo_set |= JO_IN_BOT;
3829 }
3830 *lp = get_tv_number(item);
3831 if (*lp < 0)
3832 {
3833 EMSG2(_(e_invarg2), get_tv_string(item));
3834 return FAIL;
3835 }
3836 }
3837 else if (STRCMP(hi->hi_key, "channel") == 0)
3838 {
3839 if (!(supported & JO_OUT_IO))
3840 break;
3841 opt->jo_set |= JO_CHANNEL;
3842 if (item->v_type != VAR_CHANNEL)
3843 {
3844 EMSG2(_(e_invarg2), "channel");
3845 return FAIL;
3846 }
3847 opt->jo_channel = item->vval.v_channel;
3848 }
3849 else if (STRCMP(hi->hi_key, "callback") == 0)
3850 {
3851 if (!(supported & JO_CALLBACK))
3852 break;
3853 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003854 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003855 if (opt->jo_callback == NULL)
3856 {
3857 EMSG2(_(e_invarg2), "callback");
3858 return FAIL;
3859 }
3860 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003861 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003862 {
3863 if (!(supported & JO_OUT_CALLBACK))
3864 break;
3865 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003866 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003867 if (opt->jo_out_cb == NULL)
3868 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003869 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003870 return FAIL;
3871 }
3872 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003873 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003874 {
3875 if (!(supported & JO_ERR_CALLBACK))
3876 break;
3877 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003878 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003879 if (opt->jo_err_cb == NULL)
3880 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003881 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003882 return FAIL;
3883 }
3884 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003885 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003886 {
3887 if (!(supported & JO_CLOSE_CALLBACK))
3888 break;
3889 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003890 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003891 if (opt->jo_close_cb == NULL)
3892 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003893 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003894 return FAIL;
3895 }
3896 }
3897 else if (STRCMP(hi->hi_key, "waittime") == 0)
3898 {
3899 if (!(supported & JO_WAITTIME))
3900 break;
3901 opt->jo_set |= JO_WAITTIME;
3902 opt->jo_waittime = get_tv_number(item);
3903 }
3904 else if (STRCMP(hi->hi_key, "timeout") == 0)
3905 {
3906 if (!(supported & JO_TIMEOUT))
3907 break;
3908 opt->jo_set |= JO_TIMEOUT;
3909 opt->jo_timeout = get_tv_number(item);
3910 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003911 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003912 {
3913 if (!(supported & JO_OUT_TIMEOUT))
3914 break;
3915 opt->jo_set |= JO_OUT_TIMEOUT;
3916 opt->jo_out_timeout = get_tv_number(item);
3917 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003918 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003919 {
3920 if (!(supported & JO_ERR_TIMEOUT))
3921 break;
3922 opt->jo_set |= JO_ERR_TIMEOUT;
3923 opt->jo_err_timeout = get_tv_number(item);
3924 }
3925 else if (STRCMP(hi->hi_key, "part") == 0)
3926 {
3927 if (!(supported & JO_PART))
3928 break;
3929 opt->jo_set |= JO_PART;
3930 val = get_tv_string(item);
3931 if (STRCMP(val, "err") == 0)
3932 opt->jo_part = PART_ERR;
3933 else
3934 {
3935 EMSG2(_(e_invarg2), val);
3936 return FAIL;
3937 }
3938 }
3939 else if (STRCMP(hi->hi_key, "id") == 0)
3940 {
3941 if (!(supported & JO_ID))
3942 break;
3943 opt->jo_set |= JO_ID;
3944 opt->jo_id = get_tv_number(item);
3945 }
3946 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3947 {
3948 if (!(supported & JO_STOPONEXIT))
3949 break;
3950 opt->jo_set |= JO_STOPONEXIT;
3951 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3952 opt->jo_soe_buf);
3953 if (opt->jo_stoponexit == NULL)
3954 {
3955 EMSG2(_(e_invarg2), "stoponexit");
3956 return FAIL;
3957 }
3958 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003959 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 {
3961 if (!(supported & JO_EXIT_CB))
3962 break;
3963 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003964 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3965 {
3966 opt->jo_exit_partial = item->vval.v_partial;
3967 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3968 }
3969 else
3970 opt->jo_exit_cb = get_tv_string_buf_chk(
3971 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003972 if (opt->jo_exit_cb == NULL)
3973 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003974 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975 return FAIL;
3976 }
3977 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003978 else if (STRCMP(hi->hi_key, "block_write") == 0)
3979 {
3980 if (!(supported & JO_BLOCK_WRITE))
3981 break;
3982 opt->jo_set |= JO_BLOCK_WRITE;
3983 opt->jo_block_write = get_tv_number(item);
3984 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003985 else
3986 break;
3987 --todo;
3988 }
3989 if (todo > 0)
3990 {
3991 EMSG2(_(e_invarg2), hi->hi_key);
3992 return FAIL;
3993 }
3994
3995 return OK;
3996}
3997
3998/*
3999 * Get the channel from the argument.
4000 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004001 * When "check_open" is TRUE check that the channel can be used.
4002 * When "reading" is TRUE "check_open" considers typeahead useful.
4003 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004004 */
4005 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004006get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004008 channel_T *channel = NULL;
4009 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004010
4011 if (tv->v_type == VAR_JOB)
4012 {
4013 if (tv->vval.v_job != NULL)
4014 channel = tv->vval.v_job->jv_channel;
4015 }
4016 else if (tv->v_type == VAR_CHANNEL)
4017 {
4018 channel = tv->vval.v_channel;
4019 }
4020 else
4021 {
4022 EMSG2(_(e_invarg2), get_tv_string(tv));
4023 return NULL;
4024 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004025 if (channel != NULL && reading)
4026 has_readahead = channel_has_readahead(channel,
4027 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004028
Bram Moolenaar437905c2016-04-26 19:01:05 +02004029 if (check_open && (channel == NULL || (!channel_is_open(channel)
4030 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004031 {
4032 EMSG(_("E906: not an open channel"));
4033 return NULL;
4034 }
4035 return channel;
4036}
4037
4038static job_T *first_job = NULL;
4039
4040 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004041job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004042{
4043 ch_log(job->jv_channel, "Freeing job");
4044 if (job->jv_channel != NULL)
4045 {
4046 /* The link from the channel to the job doesn't count as a reference,
4047 * thus don't decrement the refcount of the job. The reference from
4048 * the job to the channel does count the refrence, decrement it and
4049 * NULL the reference. We don't set ch_job_killed, unreferencing the
4050 * job doesn't mean it stops running. */
4051 job->jv_channel->ch_job = NULL;
4052 channel_unref(job->jv_channel);
4053 }
4054 mch_clear_job(job);
4055
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004056 vim_free(job->jv_stoponexit);
4057 vim_free(job->jv_exit_cb);
4058 partial_unref(job->jv_exit_partial);
4059}
4060
4061 static void
4062job_free_job(job_T *job)
4063{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 if (job->jv_next != NULL)
4065 job->jv_next->jv_prev = job->jv_prev;
4066 if (job->jv_prev == NULL)
4067 first_job = job->jv_next;
4068 else
4069 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070 vim_free(job);
4071}
4072
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004073 static void
4074job_free(job_T *job)
4075{
4076 if (!in_free_unref_items)
4077 {
4078 job_free_contents(job);
4079 job_free_job(job);
4080 }
4081}
4082
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004083/*
4084 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004085 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4086 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004087 */
4088 static int
4089job_still_useful(job_T *job)
4090{
4091 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004092 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4093 || (job->jv_channel != NULL
4094 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004095}
4096
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004097 void
4098job_unref(job_T *job)
4099{
4100 if (job != NULL && --job->jv_refcount <= 0)
4101 {
4102 /* Do not free the job when it has not ended yet and there is a
4103 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004104 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004105 {
4106 job_free(job);
4107 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004108 else if (job->jv_channel != NULL
4109 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004110 {
4111 /* Do remove the link to the channel, otherwise it hangs
4112 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004113 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114 job->jv_channel->ch_job = NULL;
4115 channel_unref(job->jv_channel);
4116 job->jv_channel = NULL;
4117 }
4118 }
4119}
4120
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004121 int
4122free_unused_jobs_contents(int copyID, int mask)
4123{
4124 int did_free = FALSE;
4125 job_T *job;
4126
4127 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004128 if ((job->jv_copyID & mask) != (copyID & mask)
4129 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004130 {
4131 /* Free the channel and ordinary items it contains, but don't
4132 * recurse into Lists, Dictionaries etc. */
4133 job_free_contents(job);
4134 did_free = TRUE;
4135 }
4136 return did_free;
4137}
4138
4139 void
4140free_unused_jobs(int copyID, int mask)
4141{
4142 job_T *job;
4143 job_T *job_next;
4144
4145 for (job = first_job; job != NULL; job = job_next)
4146 {
4147 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004148 if ((job->jv_copyID & mask) != (copyID & mask)
4149 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004150 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004151 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004152 job_free_job(job);
4153 }
4154 }
4155}
4156
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004157/*
4158 * Allocate a job. Sets the refcount to one and sets options default.
4159 */
4160 static job_T *
4161job_alloc(void)
4162{
4163 job_T *job;
4164
4165 job = (job_T *)alloc_clear(sizeof(job_T));
4166 if (job != NULL)
4167 {
4168 job->jv_refcount = 1;
4169 job->jv_stoponexit = vim_strsave((char_u *)"term");
4170
4171 if (first_job != NULL)
4172 {
4173 first_job->jv_prev = job;
4174 job->jv_next = first_job;
4175 }
4176 first_job = job;
4177 }
4178 return job;
4179}
4180
4181 void
4182job_set_options(job_T *job, jobopt_T *opt)
4183{
4184 if (opt->jo_set & JO_STOPONEXIT)
4185 {
4186 vim_free(job->jv_stoponexit);
4187 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4188 job->jv_stoponexit = NULL;
4189 else
4190 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4191 }
4192 if (opt->jo_set & JO_EXIT_CB)
4193 {
4194 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004195 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004196 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004197 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004198 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004199 job->jv_exit_partial = NULL;
4200 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004201 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004202 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004203 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004204 job->jv_exit_partial = opt->jo_exit_partial;
4205 if (job->jv_exit_partial != NULL)
4206 ++job->jv_exit_partial->pt_refcount;
4207 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004208 }
4209}
4210
4211/*
4212 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4213 */
4214 void
4215job_stop_on_exit()
4216{
4217 job_T *job;
4218
4219 for (job = first_job; job != NULL; job = job->jv_next)
4220 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4221 mch_stop_job(job, job->jv_stoponexit);
4222}
4223
4224/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004225 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004226 */
4227 void
4228job_check_ended(void)
4229{
4230 static time_t last_check = 0;
4231 time_t now;
4232 job_T *job;
4233 job_T *next;
4234
4235 /* Only do this once in 10 seconds. */
4236 now = time(NULL);
4237 if (last_check + 10 < now)
4238 {
4239 last_check = now;
4240 for (job = first_job; job != NULL; job = next)
4241 {
4242 next = job->jv_next;
4243 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4244 job_status(job); /* may free "job" */
4245 }
4246 }
4247}
4248
4249/*
4250 * "job_start()" function
4251 */
4252 job_T *
4253job_start(typval_T *argvars)
4254{
4255 job_T *job;
4256 char_u *cmd = NULL;
4257#if defined(UNIX)
4258# define USE_ARGV
4259 char **argv = NULL;
4260 int argc = 0;
4261#else
4262 garray_T ga;
4263#endif
4264 jobopt_T opt;
4265 int part;
4266
4267 job = job_alloc();
4268 if (job == NULL)
4269 return NULL;
4270
4271 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004272#ifndef USE_ARGV
4273 ga_init2(&ga, (int)sizeof(char*), 20);
4274#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004275
4276 /* Default mode is NL. */
4277 clear_job_options(&opt);
4278 opt.jo_mode = MODE_NL;
4279 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004280 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4281 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004282 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004283
4284 /* Check that when io is "file" that there is a file name. */
4285 for (part = PART_OUT; part <= PART_IN; ++part)
4286 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4287 && opt.jo_io[part] == JIO_FILE
4288 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4289 || *opt.jo_io_name[part] == NUL))
4290 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004291 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004292 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004293 }
4294
4295 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4296 {
4297 buf_T *buf = NULL;
4298
4299 /* check that we can find the buffer before starting the job */
4300 if (opt.jo_set & JO_IN_BUF)
4301 {
4302 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4303 if (buf == NULL)
4304 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4305 }
4306 else if (!(opt.jo_set & JO_IN_NAME))
4307 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004308 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004309 }
4310 else
4311 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4312 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004313 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004314 if (buf->b_ml.ml_mfp == NULL)
4315 {
4316 char_u numbuf[NUMBUFLEN];
4317 char_u *s;
4318
4319 if (opt.jo_set & JO_IN_BUF)
4320 {
4321 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4322 s = numbuf;
4323 }
4324 else
4325 s = opt.jo_io_name[PART_IN];
4326 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004327 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004328 }
4329 job->jv_in_buf = buf;
4330 }
4331
4332 job_set_options(job, &opt);
4333
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004334 if (argvars[0].v_type == VAR_STRING)
4335 {
4336 /* Command is a string. */
4337 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004338 if (cmd == NULL || *cmd == NUL)
4339 {
4340 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004341 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004342 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004343#ifdef USE_ARGV
4344 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004345 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004346 argv[argc] = NULL;
4347#endif
4348 }
4349 else if (argvars[0].v_type != VAR_LIST
4350 || argvars[0].vval.v_list == NULL
4351 || argvars[0].vval.v_list->lv_len < 1)
4352 {
4353 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004354 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004355 }
4356 else
4357 {
4358 list_T *l = argvars[0].vval.v_list;
4359 listitem_T *li;
4360 char_u *s;
4361
4362#ifdef USE_ARGV
4363 /* Pass argv[] to mch_call_shell(). */
4364 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4365 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004366 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367#endif
4368 for (li = l->lv_first; li != NULL; li = li->li_next)
4369 {
4370 s = get_tv_string_chk(&li->li_tv);
4371 if (s == NULL)
4372 goto theend;
4373#ifdef USE_ARGV
4374 argv[argc++] = (char *)s;
4375#else
4376 /* Only escape when needed, double quotes are not always allowed. */
4377 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4378 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004379# ifdef WIN32
4380 int old_ssl = p_ssl;
4381
4382 /* This is using CreateProcess, not cmd.exe. Always use
4383 * double quote and backslashes. */
4384 p_ssl = 0;
4385# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004386 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004387# ifdef WIN32
4388 p_ssl = old_ssl;
4389# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 if (s == NULL)
4391 goto theend;
4392 ga_concat(&ga, s);
4393 vim_free(s);
4394 }
4395 else
4396 ga_concat(&ga, s);
4397 if (li->li_next != NULL)
4398 ga_append(&ga, ' ');
4399#endif
4400 }
4401#ifdef USE_ARGV
4402 argv[argc] = NULL;
4403#else
4404 cmd = ga.ga_data;
4405#endif
4406 }
4407
4408#ifdef USE_ARGV
4409 if (ch_log_active())
4410 {
4411 garray_T ga;
4412 int i;
4413
4414 ga_init2(&ga, (int)sizeof(char), 200);
4415 for (i = 0; i < argc; ++i)
4416 {
4417 if (i > 0)
4418 ga_concat(&ga, (char_u *)" ");
4419 ga_concat(&ga, (char_u *)argv[i]);
4420 }
4421 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4422 ga_clear(&ga);
4423 }
4424 mch_start_job(argv, job, &opt);
4425#else
4426 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4427 mch_start_job((char *)cmd, job, &opt);
4428#endif
4429
4430 /* If the channel is reading from a buffer, write lines now. */
4431 if (job->jv_channel != NULL)
4432 channel_write_in(job->jv_channel);
4433
4434theend:
4435#ifdef USE_ARGV
4436 vim_free(argv);
4437#else
4438 vim_free(ga.ga_data);
4439#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004440 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004441 return job;
4442}
4443
4444/*
4445 * Get the status of "job" and invoke the exit callback when needed.
4446 * The returned string is not allocated.
4447 */
4448 char *
4449job_status(job_T *job)
4450{
4451 char *result;
4452
4453 if (job->jv_status == JOB_ENDED)
4454 /* No need to check, dead is dead. */
4455 result = "dead";
4456 else if (job->jv_status == JOB_FAILED)
4457 result = "fail";
4458 else
4459 {
4460 result = mch_job_status(job);
4461 if (job->jv_status == JOB_ENDED)
4462 ch_log(job->jv_channel, "Job ended");
4463 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4464 {
4465 typval_T argv[3];
4466 typval_T rettv;
4467 int dummy;
4468
4469 /* invoke the exit callback; make sure the refcount is > 0 */
4470 ++job->jv_refcount;
4471 argv[0].v_type = VAR_JOB;
4472 argv[0].vval.v_job = job;
4473 argv[1].v_type = VAR_NUMBER;
4474 argv[1].vval.v_number = job->jv_exitval;
4475 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004476 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4477 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004478 clear_tv(&rettv);
4479 --job->jv_refcount;
4480 }
4481 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4482 {
4483 /* The job was already unreferenced, now that it ended it can be
4484 * freed. Careful: caller must not use "job" after this! */
4485 job_free(job);
4486 }
4487 }
4488 return result;
4489}
4490
Bram Moolenaar8950a562016-03-12 15:22:55 +01004491/*
4492 * Implementation of job_info().
4493 */
4494 void
4495job_info(job_T *job, dict_T *dict)
4496{
4497 dictitem_T *item;
4498 varnumber_T nr;
4499
4500 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4501
4502 item = dictitem_alloc((char_u *)"channel");
4503 if (item == NULL)
4504 return;
4505 item->di_tv.v_lock = 0;
4506 item->di_tv.v_type = VAR_CHANNEL;
4507 item->di_tv.vval.v_channel = job->jv_channel;
4508 if (job->jv_channel != NULL)
4509 ++job->jv_channel->ch_refcount;
4510 if (dict_add(dict, item) == FAIL)
4511 dictitem_free(item);
4512
4513#ifdef UNIX
4514 nr = job->jv_pid;
4515#else
4516 nr = job->jv_proc_info.dwProcessId;
4517#endif
4518 dict_add_nr_str(dict, "process", nr, NULL);
4519
4520 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004521 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004522 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4523}
4524
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004525 int
4526job_stop(job_T *job, typval_T *argvars)
4527{
4528 char_u *arg;
4529
4530 if (argvars[1].v_type == VAR_UNKNOWN)
4531 arg = (char_u *)"";
4532 else
4533 {
4534 arg = get_tv_string_chk(&argvars[1]);
4535 if (arg == NULL)
4536 {
4537 EMSG(_(e_invarg));
4538 return 0;
4539 }
4540 }
4541 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4542 if (mch_stop_job(job, arg) == FAIL)
4543 return 0;
4544
4545 /* Assume that "hup" does not kill the job. */
4546 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4547 job->jv_channel->ch_job_killed = TRUE;
4548
4549 /* We don't try freeing the job, obviously the caller still has a
4550 * reference to it. */
4551 return 1;
4552}
4553
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004554#endif /* FEAT_JOB_CHANNEL */