blob: e1b0304c38a1461f57d2ba272e7845db8bbce1b8 [file] [log] [blame]
Bram Moolenaare0874f82016-01-24 20:36:41 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
22/* Note: when making changes here also adjust configure.in. */
23#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
62
Bram Moolenaard8070362016-02-15 21:56:54 +010063#ifdef WIN32
64 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010065fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010066{
67 HANDLE h = (HANDLE)fd;
68 DWORD nread;
69
70 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
71 return -1;
72 return (int)nread;
73}
74
75 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010076fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010077{
78 HANDLE h = (HANDLE)fd;
79 DWORD nwrite;
80
81 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
82 return -1;
83 return (int)nwrite;
84}
85
86 static void
87fd_close(sock_T fd)
88{
89 HANDLE h = (HANDLE)fd;
90
91 CloseHandle(h);
92}
93#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010094
Bram Moolenaar6463ca22016-02-13 17:04:46 +010095/* Log file opened with ch_logfile(). */
96static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010097#ifdef FEAT_RELTIME
98static proftime_T log_start;
99#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100
101 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100103{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100104 FILE *file = NULL;
105
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100106 if (log_fd != NULL)
107 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100108
109 if (*fname != NUL)
110 {
111 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
112 if (file == NULL)
113 {
114 EMSG2(_(e_notopen), fname);
115 return;
116 }
117 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100119
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100122 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100123#ifdef FEAT_RELTIME
124 profile_start(&log_start);
125#endif
126 }
127}
128
129 int
130ch_log_active()
131{
132 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100133}
134
135 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100136ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137{
138 if (log_fd != NULL)
139 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100140#ifdef FEAT_RELTIME
141 proftime_T log_now;
142
143 profile_start(&log_now);
144 profile_sub(&log_now, &log_start);
145 fprintf(log_fd, "%s ", profile_msg(&log_now));
146#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100147 if (ch != NULL)
148 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100149 else
150 fprintf(log_fd, "%s: ", what);
151 }
152}
153
Bram Moolenaard0b65022016-03-06 21:50:33 +0100154static int did_log_msg = TRUE;
155
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100156 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100157ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100158{
159 if (log_fd != NULL)
160 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100165 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100166 }
167}
168
169 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100170ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171{
172 if (log_fd != NULL)
173 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100174 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100176 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100178 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100179 }
180}
181
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100182 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100183ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184{
185 if (log_fd != NULL)
186 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100187 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
194
195 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100196ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100200 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100202 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100204 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100205 }
206}
207
208 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100209ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210{
211 if (log_fd != NULL)
212 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100213 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100215 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100217 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100218 }
219}
220
221 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100222ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223{
224 if (log_fd != NULL)
225 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100226 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100228 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100230 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100231 }
232}
233
234 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100235ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236{
237 if (log_fd != NULL)
238 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100239 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100241 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100243 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100244 }
245}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100246
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100247#ifdef _WIN32
248# undef PERROR
249# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
250 (char_u *)msg, (char_u *)strerror_win32(errno))
251
252 static char *
253strerror_win32(int eno)
254{
255 static LPVOID msgbuf = NULL;
256 char_u *ptr;
257
258 if (msgbuf)
259 LocalFree(msgbuf);
260 FormatMessage(
261 FORMAT_MESSAGE_ALLOCATE_BUFFER |
262 FORMAT_MESSAGE_FROM_SYSTEM |
263 FORMAT_MESSAGE_IGNORE_INSERTS,
264 NULL,
265 eno,
266 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
267 (LPTSTR) &msgbuf,
268 0,
269 NULL);
270 /* chomp \r or \n */
271 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
272 switch (*ptr)
273 {
274 case '\r':
275 STRMOVE(ptr, ptr + 1);
276 ptr--;
277 break;
278 case '\n':
279 if (*(ptr + 1) == '\0')
280 *ptr = '\0';
281 else
282 *ptr = ' ';
283 break;
284 }
285 return msgbuf;
286}
287#endif
288
Bram Moolenaar77073442016-02-13 23:23:53 +0100289/*
290 * The list of all allocated channels.
291 */
292static channel_T *first_channel = NULL;
293static int next_ch_id = 0;
294
295/*
296 * Allocate a new channel. The refcount is set to 1.
297 * The channel isn't actually used until it is opened.
298 * Returns NULL if out of memory.
299 */
300 channel_T *
301add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100302{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100303 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100304 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100305
Bram Moolenaar77073442016-02-13 23:23:53 +0100306 if (channel == NULL)
307 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308
Bram Moolenaar77073442016-02-13 23:23:53 +0100309 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100310 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100311
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100316 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317#endif
318#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100323
Bram Moolenaar77073442016-02-13 23:23:53 +0100324 if (first_channel != NULL)
325 {
326 first_channel->ch_prev = channel;
327 channel->ch_next = first_channel;
328 }
329 first_channel = channel;
330
331 channel->ch_refcount = 1;
332 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100334
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100335/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100336 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100337 * Return TRUE if "channel" has a callback and the associated job wasn't
338 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100339 */
340 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100341channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100342{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344 int has_out_msg;
345 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100346
347 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100348 if (channel->ch_job_killed && channel->ch_job == NULL)
349 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100350
351 /* If there is a close callback it may still need to be invoked. */
352 if (channel->ch_close_cb != NULL)
353 return TRUE;
354
355 /* If there is no callback then nobody can get readahead. If the fd is
356 * closed and there is no readahead then the callback won't be called. */
357 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
358 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
359 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100360 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
361 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
362 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
363 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
364 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
365 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100369 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100370}
371
372/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 * Close a channel and free all its resources.
374 */
375 static void
376channel_free_contents(channel_T *channel)
377{
378 channel_close(channel, TRUE);
379 channel_clear(channel);
380 ch_log(channel, "Freeing channel");
381}
382
383 static void
384channel_free_channel(channel_T *channel)
385{
386 if (channel->ch_next != NULL)
387 channel->ch_next->ch_prev = channel->ch_prev;
388 if (channel->ch_prev == NULL)
389 first_channel = channel->ch_next;
390 else
391 channel->ch_prev->ch_next = channel->ch_next;
392 vim_free(channel);
393}
394
395 static void
396channel_free(channel_T *channel)
397{
398 if (!in_free_unref_items)
399 {
400 channel_free_contents(channel);
401 channel_free_channel(channel);
402 }
403}
404
405/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100406 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100407 * possible, there is no callback to be invoked or the associated job was
408 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100409 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100411 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412channel_may_free(channel_T *channel)
413{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100414 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100417 return TRUE;
418 }
419 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100420}
421
422/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100423 * Decrement the reference count on "channel" and maybe free it when it goes
424 * down to zero. Don't free it if there is a pending action.
425 * Returns TRUE when the channel is no longer referenced.
426 */
427 int
428channel_unref(channel_T *channel)
429{
430 if (channel != NULL && --channel->ch_refcount <= 0)
431 return channel_may_free(channel);
432 return FALSE;
433}
434
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200435 int
436free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100437{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200438 int did_free = FALSE;
439 channel_T *ch;
440
441 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
442 if ((ch->ch_copyID & mask) != (copyID & mask))
443 {
444 /* Free the channel and ordinary items it contains, but don't
445 * recurse into Lists, Dictionaries etc. */
446 channel_free_contents(ch);
447 did_free = TRUE;
448 }
449 return did_free;
450}
451
452 void
453free_unused_channels(int copyID, int mask)
454{
455 channel_T *ch;
456 channel_T *ch_next;
457
458 for (ch = first_channel; ch != NULL; ch = ch_next)
459 {
460 ch_next = ch->ch_next;
461 if ((ch->ch_copyID & mask) != (copyID & mask))
462 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200463 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 channel_free_channel(ch);
465 }
466 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467}
468
Bram Moolenaard04a0202016-01-26 23:30:18 +0100469#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100470
471#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
472 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100473channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100474{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100476 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100477
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100478 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100479 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100480 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100481 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100482 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100483}
484#endif
485
Bram Moolenaare0874f82016-01-24 20:36:41 +0100486/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100487 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100488 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100489#ifdef FEAT_GUI_X11
490 static void
491messageFromNetbeans(XtPointer clientData,
492 int *unused1 UNUSED,
493 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100495 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100497#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100500# if GTK_CHECK_VERSION(3,0,0)
501 static gboolean
502messageFromNetbeans(GIOChannel *unused1 UNUSED,
503 GIOCondition unused2 UNUSED,
504 gpointer clientData)
505{
506 channel_read_fd(GPOINTER_TO_INT(clientData));
507 return TRUE; /* Return FALSE instead in case the event source is to
508 * be removed after this function returns. */
509}
510# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511 static void
512messageFromNetbeans(gpointer clientData,
513 gint unused1 UNUSED,
514 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517}
Bram Moolenaar98921892016-02-23 17:14:37 +0100518# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519#endif
520
521 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100522channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100523{
Bram Moolenaarde279892016-03-11 22:19:44 +0100524 if (!CH_HAS_GUI)
525 return;
526
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100527# ifdef FEAT_GUI_X11
528 /* Tell notifier we are interested in being called
529 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100530 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
531 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100532 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100533 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100534 (XtPointer)(XtInputReadMask + XtInputExceptMask),
535 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100536 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100537# else
538# ifdef FEAT_GUI_GTK
539 /* Tell gdk we are interested in being called when there
540 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100541 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100542# if GTK_CHECK_VERSION(3,0,0)
543 {
544 GIOChannel *chnnl = g_io_channel_unix_new(
545 (gint)channel->ch_part[part].ch_fd);
546
547 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
548 chnnl,
549 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
550 messageFromNetbeans,
551 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
552
553 g_io_channel_unref(chnnl);
554 }
555# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100556 channel->ch_part[part].ch_inputHandler = gdk_input_add(
557 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100558 (GdkInputCondition)
559 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100561 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100562# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# endif
564# endif
565}
566
Bram Moolenaarde279892016-03-11 22:19:44 +0100567 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100568channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100569{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 if (channel->CH_SOCK_FD != INVALID_FD)
571 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100572 if (channel->CH_OUT_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_OUT);
574 if (channel->CH_ERR_FD != INVALID_FD)
575 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100576}
577
578/*
579 * Register any of our file descriptors with the GUI event handling system.
580 * Called when the GUI has started.
581 */
582 void
583channel_gui_register_all(void)
584{
Bram Moolenaar77073442016-02-13 23:23:53 +0100585 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100586
Bram Moolenaar77073442016-02-13 23:23:53 +0100587 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100588 channel_gui_register(channel);
589}
590
591 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100592channel_gui_unregister_one(channel_T *channel, int part)
593{
594# ifdef FEAT_GUI_X11
595 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
596 {
597 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
598 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
599 }
600# else
601# ifdef FEAT_GUI_GTK
602 if (channel->ch_part[part].ch_inputHandler != 0)
603 {
604# if GTK_CHECK_VERSION(3,0,0)
605 g_source_remove(channel->ch_part[part].ch_inputHandler);
606# else
607 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
608# endif
609 channel->ch_part[part].ch_inputHandler = 0;
610 }
611# endif
612# endif
613}
614
615 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100616channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100617{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100619
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100620 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100621 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622}
623
624#endif
625
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100626static char *e_cannot_connect = N_("E902: Cannot connect to port");
627
Bram Moolenaard04a0202016-01-26 23:30:18 +0100628/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100629 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100630 * "waittime" is the time in msec to wait for the connection.
631 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100632 * Returns the channel for success.
633 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100634 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100635 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100636channel_open(
637 char *hostname,
638 int port_in,
639 int waittime,
640 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100642 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100643 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100645#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100647 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648#else
649 int port = port_in;
650#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100651 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100652 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100654#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655 channel_init_winsock();
656#endif
657
Bram Moolenaar77073442016-02-13 23:23:53 +0100658 channel = add_channel();
659 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100661 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100662 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100663 }
664
665 /* Get the server internet address and put into addr structure */
666 /* fill in the socket address structure and connect to server */
667 vim_memset((char *)&server, 0, sizeof(server));
668 server.sin_family = AF_INET;
669 server.sin_port = htons(port);
670 if ((host = gethostbyname(hostname)) == NULL)
671 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100672 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100673 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100674 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100675 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100676 }
677 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
678
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100679 /* On Mac and Solaris a zero timeout almost never works. At least wait
680 * one millisecond. Let's do it for all systems, because we don't know why
681 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 if (waittime == 0)
683 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684
685 /*
686 * For Unix we need to call connect() again after connect() failed.
687 * On Win32 one time is sufficient.
688 */
689 while (TRUE)
690 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100691 long elapsed_msec = 0;
692 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100693
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100694 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 sd = socket(AF_INET, SOCK_STREAM, 0);
697 if (sd == -1)
698 {
699 ch_error(channel, "in socket() in channel_open().");
700 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100701 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100702 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100703 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100704
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100705 if (waittime >= 0)
706 {
707 /* Make connect() non-blocking. */
708 if (
709#ifdef _WIN32
710 ioctlsocket(sd, FIONBIO, &val) < 0
711#else
712 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
713#endif
714 )
715 {
716 SOCK_ERRNO;
717 ch_errorn(channel,
718 "channel_open: Connect failed with errno %d", errno);
719 sock_close(sd);
720 channel_free(channel);
721 return NULL;
722 }
723 }
724
725 /* Try connecting to the server. */
726 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
727 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
728
Bram Moolenaar045a2842016-03-08 22:33:07 +0100729 if (ret == 0)
730 /* The connection could be established. */
731 break;
732
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100733 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100734 if (waittime < 0 || (errno != EWOULDBLOCK
735 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100736#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100737 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100738#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 ))
740 {
741 ch_errorn(channel,
742 "channel_open: Connect failed with errno %d", errno);
743 PERROR(_(e_cannot_connect));
744 sock_close(sd);
745 channel_free(channel);
746 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100747 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100748
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100749 /* Limit the waittime to 50 msec. If it doesn't work within this
750 * time we close the socket and try creating it again. */
751 waitnow = waittime > 50 ? 50 : waittime;
752
Bram Moolenaar045a2842016-03-08 22:33:07 +0100753 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100754 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755#ifndef WIN32
756 if (errno != ECONNREFUSED)
757#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100758 {
759 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100760 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100762#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100763 int so_error = 0;
764 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 struct timeval start_tv;
766 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100767#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768 FD_ZERO(&rfds);
769 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 FD_ZERO(&wfds);
771 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100772
Bram Moolenaar562ca712016-03-09 21:50:05 +0100773 tv.tv_sec = waitnow / 1000;
774 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100775#ifndef WIN32
776 gettimeofday(&start_tv, NULL);
777#endif
778 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100779 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100780 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100781
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100782 if (ret < 0)
783 {
784 SOCK_ERRNO;
785 ch_errorn(channel,
786 "channel_open: Connect failed with errno %d", errno);
787 PERROR(_(e_cannot_connect));
788 sock_close(sd);
789 channel_free(channel);
790 return NULL;
791 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100792
Bram Moolenaare081e212016-02-28 22:33:46 +0100793#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100794 /* On Win32: select() is expected to work and wait for up to
795 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 if (FD_ISSET(sd, &wfds))
797 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100798 elapsed_msec = waitnow;
799 if (waittime > 1 && elapsed_msec < waittime)
800 {
801 waittime -= elapsed_msec;
802 continue;
803 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100804#else
805 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 * After putting the socket in non-blocking mode, connect() will
807 * return EINPROGRESS, select() will not wait (as if writing is
808 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100809 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100810 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100811 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100812 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 {
814 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100815 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100816 if (ret < 0 || (so_error != 0
817 && so_error != EWOULDBLOCK
818 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100819# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100820 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100821# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822 ))
823 {
824 ch_errorn(channel,
825 "channel_open: Connect failed with errno %d",
826 so_error);
827 PERROR(_(e_cannot_connect));
828 sock_close(sd);
829 channel_free(channel);
830 return NULL;
831 }
832 }
833
Bram Moolenaar045a2842016-03-08 22:33:07 +0100834 if (FD_ISSET(sd, &wfds) && so_error == 0)
835 /* Did not detect an error, connection is established. */
836 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100837
Bram Moolenaar045a2842016-03-08 22:33:07 +0100838 gettimeofday(&end_tv, NULL);
839 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
840 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100841#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100842 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100843
844#ifndef WIN32
845 if (waittime > 1 && elapsed_msec < waittime)
846 {
847 /* The port isn't ready but we also didn't get an error.
848 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100849 * yet. Select() may return early, wait until the remaining
850 * "waitnow" and try again. */
851 waitnow -= elapsed_msec;
852 waittime -= elapsed_msec;
853 if (waitnow > 0)
854 {
855 mch_delay((long)waitnow, TRUE);
856 ui_breakcheck();
857 waittime -= waitnow;
858 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100859 if (!got_int)
860 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100861 if (waittime <= 0)
862 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100863 waittime = 1;
864 continue;
865 }
866 /* we were interrupted, behave as if timed out */
867 }
868#endif
869
870 /* We timed out. */
871 ch_error(channel, "Connection timed out");
872 sock_close(sd);
873 channel_free(channel);
874 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100875 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100876
Bram Moolenaar045a2842016-03-08 22:33:07 +0100877 ch_log(channel, "Connection made");
878
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100879 if (waittime >= 0)
880 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100881#ifdef _WIN32
882 val = 0;
883 ioctlsocket(sd, FIONBIO, &val);
884#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100885 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100886#endif
887 }
888
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100889 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100890 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100891 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
892 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100893
894#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100895 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100896#endif
897
Bram Moolenaar77073442016-02-13 23:23:53 +0100898 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100899}
900
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100901/*
902 * Implements ch_open().
903 */
904 channel_T *
905channel_open_func(typval_T *argvars)
906{
907 char_u *address;
908 char_u *p;
909 char *rest;
910 int port;
911 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200912 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100913
914 address = get_tv_string(&argvars[0]);
915 if (argvars[1].v_type != VAR_UNKNOWN
916 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
917 {
918 EMSG(_(e_invarg));
919 return NULL;
920 }
921
922 /* parse address */
923 p = vim_strchr(address, ':');
924 if (p == NULL)
925 {
926 EMSG2(_(e_invarg2), address);
927 return NULL;
928 }
929 *p++ = NUL;
930 port = strtol((char *)p, &rest, 10);
931 if (*address == NUL || port <= 0 || *rest != NUL)
932 {
933 p[-1] = ':';
934 EMSG2(_(e_invarg2), address);
935 return NULL;
936 }
937
938 /* parse options */
939 clear_job_options(&opt);
940 opt.jo_mode = MODE_JSON;
941 opt.jo_timeout = 2000;
942 if (get_job_options(&argvars[1], &opt,
943 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200944 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945 if (opt.jo_timeout < 0)
946 {
947 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200948 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100949 }
950
951 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
952 if (channel != NULL)
953 {
954 opt.jo_set = JO_ALL;
955 channel_set_options(channel, &opt);
956 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200957theend:
958 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100959 return channel;
960}
961
Bram Moolenaarde279892016-03-11 22:19:44 +0100962 static void
963may_close_part(sock_T *fd)
964{
965 if (*fd != INVALID_FD)
966 {
967 fd_close(*fd);
968 *fd = INVALID_FD;
969 }
970}
971
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100972 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100973channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100974{
Bram Moolenaarde279892016-03-11 22:19:44 +0100975 if (in != INVALID_FD)
976 {
977 may_close_part(&channel->CH_IN_FD);
978 channel->CH_IN_FD = in;
979 }
980 if (out != INVALID_FD)
981 {
982# if defined(FEAT_GUI)
983 channel_gui_unregister_one(channel, PART_OUT);
984# endif
985 may_close_part(&channel->CH_OUT_FD);
986 channel->CH_OUT_FD = out;
987# if defined(FEAT_GUI)
988 channel_gui_register_one(channel, PART_OUT);
989# endif
990 }
991 if (err != INVALID_FD)
992 {
993# if defined(FEAT_GUI)
994 channel_gui_unregister_one(channel, PART_ERR);
995# endif
996 may_close_part(&channel->CH_ERR_FD);
997 channel->CH_ERR_FD = err;
998# if defined(FEAT_GUI)
999 channel_gui_register_one(channel, PART_ERR);
1000# endif
1001 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001002}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001003
Bram Moolenaard6051b52016-02-28 15:49:03 +01001004/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001005 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001006 * This does not keep a refcount, when the job is freed ch_job is cleared.
1007 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001008 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001009channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010{
Bram Moolenaar77073442016-02-13 23:23:53 +01001011 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001012
1013 channel_set_options(channel, options);
1014
1015 if (job->jv_in_buf != NULL)
1016 {
1017 chanpart_T *in_part = &channel->ch_part[PART_IN];
1018
1019 in_part->ch_buffer = job->jv_in_buf;
1020 ch_logs(channel, "reading from buffer '%s'",
1021 (char *)in_part->ch_buffer->b_ffname);
1022 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001023 {
1024 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1025 {
1026 /* Special mode: send last-but-one line when appending a line
1027 * to the buffer. */
1028 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001029 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001030 in_part->ch_buf_top =
1031 in_part->ch_buffer->b_ml.ml_line_count + 1;
1032 }
1033 else
1034 in_part->ch_buf_top = options->jo_in_top;
1035 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001036 else
1037 in_part->ch_buf_top = 1;
1038 if (options->jo_set & JO_IN_BOT)
1039 in_part->ch_buf_bot = options->jo_in_bot;
1040 else
1041 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1042 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001043}
1044
1045/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001046 * Find a buffer matching "name" or create a new one.
1047 */
1048 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001049find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001050{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001051 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001052 buf_T *save_curbuf = curbuf;
1053
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001054 if (name != NULL && *name != NUL)
1055 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001056 if (buf == NULL)
1057 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001058 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001059 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001060 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001061 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001062#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001063 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1064 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001065#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001066 if (curbuf->b_ml.ml_mfp == NULL)
1067 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001068 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1069 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001070 changed_bytes(1, 0);
1071 curbuf = save_curbuf;
1072 }
1073
1074 return buf;
1075}
1076
1077/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001078 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001079 */
1080 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001081channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001082{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001083 int part;
1084 char_u **cbp;
1085 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001086
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001087 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001088 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001089 channel->ch_part[part].ch_mode = opt->jo_mode;
1090 if (opt->jo_set & JO_IN_MODE)
1091 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1092 if (opt->jo_set & JO_OUT_MODE)
1093 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1094 if (opt->jo_set & JO_ERR_MODE)
1095 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001096
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001097 if (opt->jo_set & JO_TIMEOUT)
1098 for (part = PART_SOCK; part <= PART_IN; ++part)
1099 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1100 if (opt->jo_set & JO_OUT_TIMEOUT)
1101 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1102 if (opt->jo_set & JO_ERR_TIMEOUT)
1103 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001104 if (opt->jo_set & JO_BLOCK_WRITE)
1105 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001106
1107 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001108 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001109 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001110 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001113 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1114 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001115 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001116 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001117 *pp = opt->jo_partial;
1118 if (*pp != NULL)
1119 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001120 }
1121 if (opt->jo_set & JO_OUT_CALLBACK)
1122 {
1123 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001124 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001125 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001126 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001127 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1128 *cbp = vim_strsave(opt->jo_out_cb);
1129 else
1130 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001131 *pp = opt->jo_out_partial;
1132 if (*pp != NULL)
1133 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001134 }
1135 if (opt->jo_set & JO_ERR_CALLBACK)
1136 {
1137 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001138 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001140 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001141 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1142 *cbp = vim_strsave(opt->jo_err_cb);
1143 else
1144 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001145 *pp = opt->jo_err_partial;
1146 if (*pp != NULL)
1147 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001148 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001149 if (opt->jo_set & JO_CLOSE_CALLBACK)
1150 {
1151 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001152 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001153 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001155 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1156 *cbp = vim_strsave(opt->jo_close_cb);
1157 else
1158 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001159 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 if (*pp != NULL)
1161 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001162 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001163
1164 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1165 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001166 /* writing output to a buffer. Default mode is NL. */
1167 if (!(opt->jo_set & JO_OUT_MODE))
1168 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001169 if (opt->jo_set & JO_OUT_BUF)
1170 channel->ch_part[PART_OUT].ch_buffer =
1171 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1172 else
1173 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001174 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1175 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001176 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1177 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001178
1179 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1180 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1181 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1182 {
1183 /* writing err to a buffer. Default mode is NL. */
1184 if (!(opt->jo_set & JO_ERR_MODE))
1185 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1186 if (opt->jo_io[PART_ERR] == JIO_OUT)
1187 channel->ch_part[PART_ERR].ch_buffer =
1188 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001189 else if (opt->jo_set & JO_ERR_BUF)
1190 channel->ch_part[PART_ERR].ch_buffer =
1191 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001192 else
1193 channel->ch_part[PART_ERR].ch_buffer =
1194 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1195 ch_logs(channel, "writing err to buffer '%s'",
1196 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1197 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001198
1199 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1200 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1201 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001202}
1203
1204/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001205 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001206 */
1207 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001208channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001209 channel_T *channel,
1210 int part,
1211 char_u *callback,
1212 partial_T *partial,
1213 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001214{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001215 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001216 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1217
1218 if (item != NULL)
1219 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001220 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001221 item->cq_partial = partial;
1222 if (partial != NULL)
1223 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001224 item->cq_seq_nr = id;
1225 item->cq_prev = head->cq_prev;
1226 head->cq_prev = item;
1227 item->cq_next = NULL;
1228 if (item->cq_prev == NULL)
1229 head->cq_next = item;
1230 else
1231 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001232 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001233}
1234
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001235 static void
1236write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1237{
1238 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001239 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001240 char_u *p;
1241
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001242 if ((p = alloc(len + 2)) == NULL)
1243 return;
1244 STRCPY(p, line);
1245 p[len] = NL;
1246 p[len + 1] = NUL;
1247 channel_send(channel, PART_IN, p, "write_buf_line()");
1248 vim_free(p);
1249}
1250
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001251/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001252 * Return TRUE if "channel" can be written to.
1253 * Returns FALSE if the input is closed or the write would block.
1254 */
1255 static int
1256can_write_buf_line(channel_T *channel)
1257{
1258 chanpart_T *in_part = &channel->ch_part[PART_IN];
1259
1260 if (in_part->ch_fd == INVALID_FD)
1261 return FALSE; /* pipe was closed */
1262
1263 /* for testing: block every other attempt to write */
1264 if (in_part->ch_block_write == 1)
1265 in_part->ch_block_write = -1;
1266 else if (in_part->ch_block_write == -1)
1267 in_part->ch_block_write = 1;
1268
1269 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1270#ifndef WIN32
1271 {
1272# if defined(HAVE_SELECT)
1273 struct timeval tval;
1274 fd_set wfds;
1275 int ret;
1276
1277 FD_ZERO(&wfds);
1278 FD_SET((int)in_part->ch_fd, &wfds);
1279 tval.tv_sec = 0;
1280 tval.tv_usec = 0;
1281 for (;;)
1282 {
1283 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1284# ifdef EINTR
1285 SOCK_ERRNO;
1286 if (ret == -1 && errno == EINTR)
1287 continue;
1288# endif
1289 if (ret <= 0 || in_part->ch_block_write == 1)
1290 {
1291 if (ret > 0)
1292 ch_log(channel, "FAKED Input not ready for writing");
1293 else
1294 ch_log(channel, "Input not ready for writing");
1295 return FALSE;
1296 }
1297 break;
1298 }
1299# else
1300 struct pollfd fds;
1301
1302 fds.fd = in_part->ch_fd;
1303 fds.events = POLLOUT;
1304 if (poll(&fds, 1, 0) <= 0)
1305 {
1306 ch_log(channel, "Input not ready for writing");
1307 return FALSE;
1308 }
1309 if (in_part->ch_block_write == 1)
1310 {
1311 ch_log(channel, "FAKED Input not ready for writing");
1312 return FALSE;
1313 }
1314# endif
1315 }
1316#endif
1317 return TRUE;
1318}
1319
1320/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001321 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001322 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001323 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001324channel_write_in(channel_T *channel)
1325{
1326 chanpart_T *in_part = &channel->ch_part[PART_IN];
1327 linenr_T lnum;
1328 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001329 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001330
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001331 if (buf == NULL || in_part->ch_buf_append)
1332 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001333 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1334 {
1335 /* buffer was wiped out or unloaded */
1336 in_part->ch_buffer = NULL;
1337 return;
1338 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001339
1340 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1341 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1342 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001343 if (!can_write_buf_line(channel))
1344 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001345 write_buf_line(buf, lnum, channel);
1346 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001347 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001348
1349 if (written == 1)
1350 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1351 else if (written > 1)
1352 ch_logn(channel, "written %d lines to channel", written);
1353
Bram Moolenaar014069a2016-03-03 22:51:40 +01001354 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001355 if (lnum > buf->b_ml.ml_line_count)
1356 {
1357 /* Writing is done, no longer need the buffer. */
1358 in_part->ch_buffer = NULL;
1359 ch_log(channel, "Finished writing all lines to channel");
1360 }
1361 else
1362 ch_logn(channel, "Still %d more lines to write",
1363 buf->b_ml.ml_line_count - lnum + 1);
1364}
1365
1366/*
1367 * Write any lines waiting to be written to a channel.
1368 */
1369 void
1370channel_write_any_lines()
1371{
1372 channel_T *channel;
1373
1374 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1375 {
1376 chanpart_T *in_part = &channel->ch_part[PART_IN];
1377
1378 if (in_part->ch_buffer != NULL)
1379 {
1380 if (in_part->ch_buf_append)
1381 channel_write_new_lines(in_part->ch_buffer);
1382 else
1383 channel_write_in(channel);
1384 }
1385 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001386}
1387
1388/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001389 * Write appended lines above the last one in "buf" to the channel.
1390 */
1391 void
1392channel_write_new_lines(buf_T *buf)
1393{
1394 channel_T *channel;
1395 int found_one = FALSE;
1396
1397 /* There could be more than one channel for the buffer, loop over all of
1398 * them. */
1399 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1400 {
1401 chanpart_T *in_part = &channel->ch_part[PART_IN];
1402 linenr_T lnum;
1403 int written = 0;
1404
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001405 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001406 {
1407 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001408 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001409 found_one = TRUE;
1410 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1411 ++lnum)
1412 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001413 if (!can_write_buf_line(channel))
1414 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001415 write_buf_line(buf, lnum, channel);
1416 ++written;
1417 }
1418
1419 if (written == 1)
1420 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1421 else if (written > 1)
1422 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001423 if (lnum < buf->b_ml.ml_line_count)
1424 ch_logn(channel, "Still %d more lines to write",
1425 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001426
1427 in_part->ch_buf_bot = lnum;
1428 }
1429 }
1430 if (!found_one)
1431 buf->b_write_to_channel = FALSE;
1432}
1433
1434/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001435 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001436 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001437 */
1438 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001439invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1440 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001441{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001442 typval_T rettv;
1443 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001444
Bram Moolenaar77073442016-02-13 23:23:53 +01001445 argv[0].v_type = VAR_CHANNEL;
1446 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001447
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001448 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001449 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001450 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001451 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001452}
1453
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001454/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001455 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001456 * The caller must free it.
1457 * Returns NULL if there is nothing.
1458 */
1459 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001460channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001461{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001462 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001463 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001464 char_u *p;
1465
Bram Moolenaar77073442016-02-13 23:23:53 +01001466 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001467 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001468 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001469 p = node->rq_buffer;
1470 head->rq_next = node->rq_next;
1471 if (node->rq_next == NULL)
1472 head->rq_prev = NULL;
1473 else
1474 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001475 vim_free(node);
1476 return p;
1477}
1478
1479/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001480 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001481 */
1482 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001483channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001484{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001485 readq_T *head = &channel->ch_part[part].ch_head;
1486 readq_T *node = head->rq_next;
1487 long_u len = 1;
1488 char_u *res;
1489 char_u *p;
1490
1491 /* If there is only one buffer just get that one. */
1492 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1493 return channel_get(channel, part);
1494
1495 /* Concatenate everything into one buffer. */
1496 for (node = head->rq_next; node != NULL; node = node->rq_next)
1497 len += (long_u)STRLEN(node->rq_buffer);
1498 res = lalloc(len, TRUE);
1499 if (res == NULL)
1500 return NULL;
1501 *res = NUL;
1502 for (node = head->rq_next; node != NULL; node = node->rq_next)
1503 STRCAT(res, node->rq_buffer);
1504
1505 /* Free all buffers */
1506 do
1507 {
1508 p = channel_get(channel, part);
1509 vim_free(p);
1510 } while (p != NULL);
1511
1512 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001513}
1514
1515/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001516 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001517 * Returns FAIL if that is not possible.
1518 */
1519 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001520channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001521{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001522 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001523 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001524 char_u *p;
1525
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001527 return FAIL;
1528
Bram Moolenaar77073442016-02-13 23:23:53 +01001529 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1530 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001531 if (p == NULL)
1532 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001533 STRCPY(p, node->rq_buffer);
1534 STRCAT(p, node->rq_next->rq_buffer);
1535 vim_free(node->rq_next->rq_buffer);
1536 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001537
Bram Moolenaar77073442016-02-13 23:23:53 +01001538 /* dispose of the node and its buffer */
1539 head->rq_next = node->rq_next;
1540 head->rq_next->rq_prev = NULL;
1541 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001542 vim_free(node);
1543 return OK;
1544}
1545
1546/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001547 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001548 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 * Returns OK or FAIL.
1550 */
1551 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001552channel_save(channel_T *channel, int part, char_u *buf, int len,
1553 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001554{
1555 readq_T *node;
1556 readq_T *head = &channel->ch_part[part].ch_head;
1557 char_u *p;
1558 int i;
1559
1560 node = (readq_T *)alloc(sizeof(readq_T));
1561 if (node == NULL)
1562 return FAIL; /* out of memory */
1563 node->rq_buffer = alloc(len + 1);
1564 if (node->rq_buffer == NULL)
1565 {
1566 vim_free(node);
1567 return FAIL; /* out of memory */
1568 }
1569
1570 if (channel->ch_part[part].ch_mode == MODE_NL)
1571 {
1572 /* Drop any CR before a NL. */
1573 p = node->rq_buffer;
1574 for (i = 0; i < len; ++i)
1575 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1576 *p++ = buf[i];
1577 *p = NUL;
1578 }
1579 else
1580 {
1581 mch_memmove(node->rq_buffer, buf, len);
1582 node->rq_buffer[len] = NUL;
1583 }
1584
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001585 if (prepend)
1586 {
1587 /* preend node to the head of the queue */
1588 node->rq_next = head->rq_next;
1589 node->rq_prev = NULL;
1590 if (head->rq_next == NULL)
1591 head->rq_prev = node;
1592 else
1593 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001595 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001597 {
1598 /* append node to the tail of the queue */
1599 node->rq_next = NULL;
1600 node->rq_prev = head->rq_prev;
1601 if (head->rq_prev == NULL)
1602 head->rq_next = node;
1603 else
1604 head->rq_prev->rq_next = node;
1605 head->rq_prev = node;
1606 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001608 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001610 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 fprintf(log_fd, "'");
1612 if (fwrite(buf, len, 1, log_fd) != 1)
1613 return FAIL;
1614 fprintf(log_fd, "'\n");
1615 }
1616 return OK;
1617}
1618
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001619 static int
1620channel_fill(js_read_T *reader)
1621{
1622 channel_T *channel = (channel_T *)reader->js_cookie;
1623 int part = reader->js_cookie_arg;
1624 char_u *next = channel_get(channel, part);
1625 int unused;
1626 int len;
1627 char_u *p;
1628
1629 if (next == NULL)
1630 return FALSE;
1631
1632 unused = reader->js_end - reader->js_buf - reader->js_used;
1633 if (unused > 0)
1634 {
1635 /* Prepend unused text. */
1636 len = (int)STRLEN(next);
1637 p = alloc(unused + len + 1);
1638 if (p == NULL)
1639 {
1640 vim_free(next);
1641 return FALSE;
1642 }
1643 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1644 mch_memmove(p + unused, next, len + 1);
1645 vim_free(next);
1646 next = p;
1647 }
1648
1649 vim_free(reader->js_buf);
1650 reader->js_buf = next;
1651 reader->js_used = 0;
1652 return TRUE;
1653}
1654
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001655/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001656 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001657 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001658 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001660 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001661channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001662{
1663 js_read_T reader;
1664 typval_T listtv;
1665 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001666 chanpart_T *chanpart = &channel->ch_part[part];
1667 jsonq_T *head = &chanpart->ch_json_head;
1668 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001669 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001671 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001672 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001674 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001677 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001678 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001679
1680 /* When a message is incomplete we wait for a short while for more to
1681 * arrive. After the delay drop the input, otherwise a truncated string
1682 * or list will make us hang. */
1683 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001684 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001685 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001687 /* Only accept the response when it is a list with at least two
1688 * items. */
1689 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001690 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001691 if (listtv.v_type != VAR_LIST)
1692 ch_error(channel, "Did not receive a list, discarding");
1693 else
1694 ch_errorn(channel, "Expected list with two items, got %d",
1695 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001697 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 else
1699 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001700 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1701 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 else
1704 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001705 item->jq_value = alloc_tv();
1706 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001707 {
1708 vim_free(item);
1709 clear_tv(&listtv);
1710 }
1711 else
1712 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001713 *item->jq_value = listtv;
1714 item->jq_prev = head->jq_prev;
1715 head->jq_prev = item;
1716 item->jq_next = NULL;
1717 if (item->jq_prev == NULL)
1718 head->jq_next = item;
1719 else
1720 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001721 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001722 }
1723 }
1724 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001725
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001726 if (status == OK)
1727 chanpart->ch_waiting = FALSE;
1728 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001729 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001730 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001731 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001732 /* First time encountering incomplete message, set a deadline of
1733 * 100 msec. */
1734 ch_log(channel, "Incomplete message - wait for more");
1735 reader.js_used = 0;
1736 chanpart->ch_waiting = TRUE;
1737#ifdef WIN32
1738 chanpart->ch_deadline = GetTickCount() + 100L;
1739#else
1740 gettimeofday(&chanpart->ch_deadline, NULL);
1741 chanpart->ch_deadline.tv_usec += 100 * 1000;
1742 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1743 {
1744 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1745 ++chanpart->ch_deadline.tv_sec;
1746 }
1747#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001748 }
1749 else
1750 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001751 int timeout;
1752#ifdef WIN32
1753 timeout = GetTickCount() > chanpart->ch_deadline;
1754#else
1755 {
1756 struct timeval now_tv;
1757
1758 gettimeofday(&now_tv, NULL);
1759 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1760 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1761 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1762 }
1763#endif
1764 if (timeout)
1765 {
1766 status = FAIL;
1767 chanpart->ch_waiting = FALSE;
1768 }
1769 else
1770 {
1771 reader.js_used = 0;
1772 ch_log(channel, "still waiting on incomplete message");
1773 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001774 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001775 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001776
1777 if (status == FAIL)
1778 {
1779 ch_error(channel, "Decoding failed - discarding input");
1780 ret = FALSE;
1781 chanpart->ch_waiting = FALSE;
1782 }
1783 else if (reader.js_buf[reader.js_used] != NUL)
1784 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001785 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001786 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1788 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001789 ret = status == MAYBE ? FALSE: TRUE;
1790 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001791 else
1792 ret = FALSE;
1793
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001794 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001795 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796}
1797
1798/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001799 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001800 */
1801 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001802remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001803{
Bram Moolenaar77073442016-02-13 23:23:53 +01001804 if (node->cq_prev == NULL)
1805 head->cq_next = node->cq_next;
1806 else
1807 node->cq_prev->cq_next = node->cq_next;
1808 if (node->cq_next == NULL)
1809 head->cq_prev = node->cq_prev;
1810 else
1811 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001812}
1813
1814/*
1815 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001816 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001817 */
1818 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001819remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001820{
Bram Moolenaar77073442016-02-13 23:23:53 +01001821 if (node->jq_prev == NULL)
1822 head->jq_next = node->jq_next;
1823 else
1824 node->jq_prev->jq_next = node->jq_next;
1825 if (node->jq_next == NULL)
1826 head->jq_prev = node->jq_prev;
1827 else
1828 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001829 vim_free(node);
1830}
1831
1832/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001833 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001834 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001835 * When "id" is zero or negative jut get the first message. But not the one
1836 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001837 * Return OK when found and return the value in "rettv".
1838 * Return FAIL otherwise.
1839 */
1840 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001842{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001844 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001845
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001848 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849 typval_T *tv = &l->lv_first->li_tv;
1850
1851 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001852 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853 || tv->vval.v_number == 0
1854 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001855 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001856 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001857 if (tv->v_type == VAR_NUMBER)
1858 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001859 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001860 return OK;
1861 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001862 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001863 }
1864 return FAIL;
1865}
1866
Bram Moolenaarece61b02016-02-20 21:39:05 +01001867#define CH_JSON_MAX_ARGS 4
1868
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001869/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001871 * "argv[0]" is the command string.
1872 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001873 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001874 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001875channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001876{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001877 char_u *cmd = argv[0].vval.v_string;
1878 char_u *arg;
1879 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001880
Bram Moolenaarece61b02016-02-20 21:39:05 +01001881 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001886 return;
1887 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001888 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001889 if (arg == NULL)
1890 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001891
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001892 if (STRCMP(cmd, "ex") == 0)
1893 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001894 int save_called_emsg = called_emsg;
1895
1896 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001897 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001898 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001899 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001900 --emsg_silent;
1901 if (called_emsg)
1902 ch_logs(channel, "Ex command error: '%s'",
1903 (char *)get_vim_var_str(VV_ERRMSG));
1904 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001905 }
1906 else if (STRCMP(cmd, "normal") == 0)
1907 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001908 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001909
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001910 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001911 ea.arg = arg;
1912 ea.addr_count = 0;
1913 ea.forceit = TRUE; /* no mapping */
1914 ex_normal(&ea);
1915 }
1916 else if (STRCMP(cmd, "redraw") == 0)
1917 {
1918 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001919
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001920 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001921 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001922 ex_redraw(&ea);
1923 showruler(FALSE);
1924 setcursor();
1925 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001926#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001927 if (gui.in_use)
1928 {
1929 gui_update_cursor(FALSE, FALSE);
1930 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001931 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001932#endif
1933 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001934 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001935 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 int is_call = cmd[0] == 'c';
1937 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001938
Bram Moolenaarece61b02016-02-20 21:39:05 +01001939 if (argv[id_idx].v_type != VAR_UNKNOWN
1940 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001941 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001942 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001943 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 EMSG("E904: last argument for expr/call must be a number");
1945 }
1946 else if (is_call && argv[2].v_type != VAR_LIST)
1947 {
1948 ch_error(channel, "third argument for call must be a list");
1949 if (p_verbose > 2)
1950 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001951 }
1952 else
1953 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001954 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001955 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001957 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001959 /* Don't pollute the display with errors. */
1960 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001961 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001962 {
1963 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001964 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001965 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001966 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001967 {
1968 ch_logs(channel, "Calling '%s'", (char *)arg);
1969 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1970 tv = &res_tv;
1971 else
1972 tv = NULL;
1973 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001974
1975 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001976 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001977 int id = argv[id_idx].vval.v_number;
1978
Bram Moolenaar55fab432016-02-07 16:53:13 +01001979 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001980 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001982 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001983 /* If evaluation failed or the result can't be encoded
1984 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001985 vim_free(json);
1986 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001987 err_tv.v_type = VAR_STRING;
1988 err_tv.vval.v_string = (char_u *)"ERROR";
1989 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001990 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001991 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001992 if (json != NULL)
1993 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001994 channel_send(channel,
1995 part == PART_SOCK ? PART_SOCK : PART_IN,
1996 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001997 vim_free(json);
1998 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001999 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002000 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002001 if (tv == &res_tv)
2002 clear_tv(tv);
2003 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002004 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002005 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002006 }
2007 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008 {
2009 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002010 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002011 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002012}
2013
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002014/*
2015 * Invoke the callback at "cbhead".
2016 * Does not redraw but sets channel_need_redraw.
2017 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002018 static void
2019invoke_one_time_callback(
2020 channel_T *channel,
2021 cbq_T *cbhead,
2022 cbq_T *item,
2023 typval_T *argv)
2024{
2025 ch_logs(channel, "Invoking one-time callback %s",
2026 (char *)item->cq_callback);
2027 /* Remove the item from the list first, if the callback
2028 * invokes ch_close() the list will be cleared. */
2029 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002030 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002031 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002032 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002033 vim_free(item);
2034}
2035
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002036 static void
2037append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2038{
2039 buf_T *save_curbuf = curbuf;
2040 linenr_T lnum = buffer->b_ml.ml_line_count;
2041 int save_write_to = buffer->b_write_to_channel;
2042
2043 /* If the buffer is also used as input insert above the last
2044 * line. Don't write these lines. */
2045 if (save_write_to)
2046 {
2047 --lnum;
2048 buffer->b_write_to_channel = FALSE;
2049 }
2050
2051 /* Append to the buffer */
2052 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2053
2054 curbuf = buffer;
2055 u_sync(TRUE);
2056 /* ignore undo failure, undo is not very useful here */
2057 ignored = u_save(lnum, lnum + 1);
2058
2059 ml_append(lnum, msg, 0, FALSE);
2060 appended_lines_mark(lnum, 1L);
2061 curbuf = save_curbuf;
2062
2063 if (buffer->b_nwindows > 0)
2064 {
2065 win_T *wp;
2066 win_T *save_curwin;
2067
2068 FOR_ALL_WINDOWS(wp)
2069 {
2070 if (wp->w_buffer == buffer
2071 && (save_write_to
2072 ? wp->w_cursor.lnum == lnum + 1
2073 : (wp->w_cursor.lnum == lnum
2074 && wp->w_cursor.col == 0)))
2075 {
2076 ++wp->w_cursor.lnum;
2077 save_curwin = curwin;
2078 curwin = wp;
2079 curbuf = curwin->w_buffer;
2080 scroll_cursor_bot(0, FALSE);
2081 curwin = save_curwin;
2082 curbuf = curwin->w_buffer;
2083 }
2084 }
2085 redraw_buf_later(buffer, VALID);
2086 channel_need_redraw = TRUE;
2087 }
2088
2089 if (save_write_to)
2090 {
2091 channel_T *ch;
2092
2093 /* Find channels reading from this buffer and adjust their
2094 * next-to-read line number. */
2095 buffer->b_write_to_channel = TRUE;
2096 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2097 {
2098 chanpart_T *in_part = &ch->ch_part[PART_IN];
2099
2100 if (in_part->ch_buffer == buffer)
2101 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2102 }
2103 }
2104}
2105
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002106/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002107 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002108 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002109 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002110 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002111 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002112may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002113{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002114 char_u *msg = NULL;
2115 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002116 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002117 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002118 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002119 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002120 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002121 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002122 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002123 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002124
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002125 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002126 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002127 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002128
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002129 /* Use a message-specific callback, part callback or channel callback */
2130 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2131 if (cbitem->cq_seq_nr == 0)
2132 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002133 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002134 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002135 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002136 partial = cbitem->cq_partial;
2137 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002138 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002139 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002140 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002141 partial = channel->ch_part[part].ch_partial;
2142 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002143 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002144 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002145 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002146 partial = channel->ch_partial;
2147 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002148
Bram Moolenaar187db502016-02-27 14:44:26 +01002149 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002150 if (buffer != NULL && !buf_valid(buffer))
2151 {
2152 /* buffer was wiped out */
2153 channel->ch_part[part].ch_buffer = NULL;
2154 buffer = NULL;
2155 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002156
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002157 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002158 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002159 listitem_T *item;
2160 int argc = 0;
2161
Bram Moolenaard7ece102016-02-02 23:23:02 +01002162 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002163 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002164 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002165 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002166 channel_parse_json(channel, part);
2167 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002168 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002169 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002170
Bram Moolenaarece61b02016-02-20 21:39:05 +01002171 for (item = listtv->vval.v_list->lv_first;
2172 item != NULL && argc < CH_JSON_MAX_ARGS;
2173 item = item->li_next)
2174 argv[argc++] = item->li_tv;
2175 while (argc < CH_JSON_MAX_ARGS)
2176 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002177
Bram Moolenaarece61b02016-02-20 21:39:05 +01002178 if (argv[0].v_type == VAR_STRING)
2179 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002180 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002181 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002182 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002183 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002184 }
2185
Bram Moolenaarece61b02016-02-20 21:39:05 +01002186 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002187 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002188 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002189 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002190 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002191 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002192 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002195 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002196 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002197 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002198 return FALSE;
2199 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002200 else
2201 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002202 /* If there is no callback or buffer drop the message. */
2203 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002204 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002205 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002206 {
2207 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002208 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01002209 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002210 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002211 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002212
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002213 if (ch_mode == MODE_NL)
2214 {
2215 char_u *nl;
2216 char_u *buf;
2217
2218 /* See if we have a message ending in NL in the first buffer. If
2219 * not try to concatenate the first and the second buffer. */
2220 while (TRUE)
2221 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002222 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002223 nl = vim_strchr(buf, NL);
2224 if (nl != NULL)
2225 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002226 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002227 return FALSE; /* incomplete message */
2228 }
2229 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002230 {
2231 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002232 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002233 *nl = NUL;
2234 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002235 else
2236 {
2237 /* Copy the message into allocated memory and remove it from
2238 * the buffer. */
2239 msg = vim_strnsave(buf, (int)(nl - buf));
2240 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2241 }
2242 }
2243 else
2244 /* For a raw channel we don't know where the message ends, just
2245 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002246 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002247
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002248 if (msg == NULL)
2249 return FALSE; /* out of memory (and avoids Coverity warning) */
2250
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002251 argv[1].v_type = VAR_STRING;
2252 argv[1].vval.v_string = msg;
2253 }
2254
Bram Moolenaara07fec92016-02-05 21:04:08 +01002255 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002256 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002257 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002258
2259 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002260 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002261 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002262 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002263 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002264 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002265 break;
2266 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002267 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002268 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002269 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002270 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002271 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002272 if (buffer != NULL)
2273 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002274 if (msg == NULL)
2275 /* JSON or JS mode: re-encode the message. */
2276 msg = json_encode(listtv, ch_mode);
2277 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002278 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002279 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002280
Bram Moolenaar187db502016-02-27 14:44:26 +01002281 if (callback != NULL)
2282 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002283 if (cbitem != NULL)
2284 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2285 else
2286 {
2287 /* invoke the channel callback */
2288 ch_logs(channel, "Invoking channel callback %s",
2289 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002290 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002291 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002292 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002293 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002294 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002295 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002296
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002297 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002298 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002299 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002300
2301 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002302}
2303
2304/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002305 * Return TRUE when channel "channel" is open for writing to.
2306 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002307 */
2308 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002309channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002310{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002311 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002312 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002313}
2314
2315/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002316 * Return TRUE when channel "channel" is open for reading or writing.
2317 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002318 */
2319 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002320channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002321{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002323 || channel->CH_IN_FD != INVALID_FD
2324 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002325 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002326}
2327
2328/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002329 * Return a string indicating the status of the channel.
2330 */
2331 char *
2332channel_status(channel_T *channel)
2333{
2334 if (channel == NULL)
2335 return "fail";
2336 if (channel_is_open(channel))
2337 return "open";
2338 return "closed";
2339}
2340
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002341 static void
2342channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2343{
2344 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002345 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002346 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002347 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002348
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002349 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002350 STRCAT(namebuf, "_");
2351 tail = STRLEN(namebuf);
2352
2353 STRCPY(namebuf + tail, "status");
2354 dict_add_nr_str(dict, namebuf, 0,
2355 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2356
2357 STRCPY(namebuf + tail, "mode");
2358 switch (chanpart->ch_mode)
2359 {
2360 case MODE_NL: s = "NL"; break;
2361 case MODE_RAW: s = "RAW"; break;
2362 case MODE_JSON: s = "JSON"; break;
2363 case MODE_JS: s = "JS"; break;
2364 }
2365 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2366
2367 STRCPY(namebuf + tail, "io");
2368 if (part == PART_SOCK)
2369 s = "socket";
2370 else switch (chanpart->ch_io)
2371 {
2372 case JIO_NULL: s = "null"; break;
2373 case JIO_PIPE: s = "pipe"; break;
2374 case JIO_FILE: s = "file"; break;
2375 case JIO_BUFFER: s = "buffer"; break;
2376 case JIO_OUT: s = "out"; break;
2377 }
2378 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2379
2380 STRCPY(namebuf + tail, "timeout");
2381 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2382}
2383
2384 void
2385channel_info(channel_T *channel, dict_T *dict)
2386{
2387 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2388 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2389
2390 if (channel->ch_hostname != NULL)
2391 {
2392 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2393 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2394 channel_part_info(channel, dict, "sock", PART_SOCK);
2395 }
2396 else
2397 {
2398 channel_part_info(channel, dict, "out", PART_OUT);
2399 channel_part_info(channel, dict, "err", PART_ERR);
2400 channel_part_info(channel, dict, "in", PART_IN);
2401 }
2402}
2403
Bram Moolenaar77073442016-02-13 23:23:53 +01002404/*
2405 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002406 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002407 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002408 */
2409 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002410channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002411{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002412 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002413
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002414#ifdef FEAT_GUI
2415 channel_gui_unregister(channel);
2416#endif
2417
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002418 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002419 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002420 sock_close(channel->CH_SOCK_FD);
2421 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002422 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002423 may_close_part(&channel->CH_IN_FD);
2424 may_close_part(&channel->CH_OUT_FD);
2425 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002426
Bram Moolenaar8b374212016-02-24 20:43:06 +01002427 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002428 {
2429 typval_T argv[1];
2430 typval_T rettv;
2431 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002432 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002433
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002434 /* Invoke callbacks before the close callback, since it's weird to
2435 * first invoke the close callback. Increment the refcount to avoid
2436 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002437 ++channel->ch_refcount;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002438 for (part = PART_SOCK; part <= PART_ERR; ++part)
2439 while (may_invoke_callback(channel, part))
2440 ;
2441
2442 /* Invoke the close callback, if still set. */
2443 if (channel->ch_close_cb != NULL)
2444 {
2445 ch_logs(channel, "Invoking close callback %s",
2446 (char *)channel->ch_close_cb);
2447 argv[0].v_type = VAR_CHANNEL;
2448 argv[0].vval.v_channel = channel;
2449 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002450 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2451 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002452 clear_tv(&rettv);
2453 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002454 --channel->ch_refcount;
2455
2456 /* the callback is only called once */
2457 vim_free(channel->ch_close_cb);
2458 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002459 partial_unref(channel->ch_close_partial);
2460 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002461 }
2462
2463 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002464}
2465
Bram Moolenaard04a0202016-01-26 23:30:18 +01002466/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002467 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002468 * Returns NULL if there is nothing.
2469 */
2470 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002472{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002473 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002474
Bram Moolenaar77073442016-02-13 23:23:53 +01002475 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002476 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002477 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002478}
2479
2480/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002481 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002482 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002483 static void
2484channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002485{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002486 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2487 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002488
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002489 while (channel_peek(channel, part) != NULL)
2490 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002491
2492 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002493 {
2494 cbq_T *node = cb_head->cq_next;
2495
2496 remove_cb_node(cb_head, node);
2497 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002498 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002499 vim_free(node);
2500 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002501
2502 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002503 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002504 free_tv(json_head->jq_next->jq_value);
2505 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002506 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002507
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002508 vim_free(channel->ch_part[part].ch_callback);
2509 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002510 partial_unref(channel->ch_part[part].ch_partial);
2511 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002512}
2513
2514/*
2515 * Clear all the read buffers on "channel".
2516 */
2517 void
2518channel_clear(channel_T *channel)
2519{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002520 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002521 vim_free(channel->ch_hostname);
2522 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002523 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002524 channel_clear_one(channel, PART_OUT);
2525 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002526 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002527 vim_free(channel->ch_callback);
2528 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002529 partial_unref(channel->ch_partial);
2530 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002531 vim_free(channel->ch_close_cb);
2532 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002533 partial_unref(channel->ch_close_partial);
2534 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002535}
2536
Bram Moolenaar77073442016-02-13 23:23:53 +01002537#if defined(EXITFREE) || defined(PROTO)
2538 void
2539channel_free_all(void)
2540{
2541 channel_T *channel;
2542
Bram Moolenaard6051b52016-02-28 15:49:03 +01002543 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002544 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2545 channel_clear(channel);
2546}
2547#endif
2548
2549
Bram Moolenaard04a0202016-01-26 23:30:18 +01002550/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002551#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002552
2553/* Buffer size for reading incoming messages. */
2554#define MAXMSGSIZE 4096
2555
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002556#if defined(HAVE_SELECT)
2557/*
2558 * Add write fds where we are waiting for writing to be possible.
2559 */
2560 static int
2561channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2562{
2563 int maxfd = maxfd_arg;
2564 channel_T *ch;
2565
2566 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2567 {
2568 chanpart_T *in_part = &ch->ch_part[PART_IN];
2569
2570 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2571 {
2572 FD_SET((int)in_part->ch_fd, wfds);
2573 if ((int)in_part->ch_fd >= maxfd)
2574 maxfd = (int)in_part->ch_fd + 1;
2575 }
2576 }
2577 return maxfd;
2578}
2579#else
2580/*
2581 * Add write fds where we are waiting for writing to be possible.
2582 */
2583 static int
2584channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2585{
2586 int nfd = nfd_in;
2587 channel_T *ch;
2588
2589 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2590 {
2591 chanpart_T *in_part = &ch->ch_part[PART_IN];
2592
2593 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2594 {
2595 in_part->ch_poll_idx = nfd;
2596 fds[nfd].fd = in_part->ch_fd;
2597 fds[nfd].events = POLLOUT;
2598 ++nfd;
2599 }
2600 else
2601 in_part->ch_poll_idx = -1;
2602 }
2603 return nfd;
2604}
2605#endif
2606
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002607typedef enum {
2608 CW_READY,
2609 CW_NOT_READY,
2610 CW_ERROR
2611} channel_wait_result;
2612
Bram Moolenaard04a0202016-01-26 23:30:18 +01002613/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002614 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002615 * Return CW_READY when there is something to read.
2616 * Return CW_NOT_READY when there is nothing to read.
2617 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002618 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002619 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002620channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002621{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002622 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002623 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002624
Bram Moolenaard8070362016-02-15 21:56:54 +01002625# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002626 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002627 {
2628 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002629 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002630 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002631 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002632
2633 /* reading from a pipe, not a socket */
2634 while (TRUE)
2635 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002636 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2637
2638 if (r && nread > 0)
2639 return CW_READY;
2640 if (r == 0)
2641 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002642
2643 /* perhaps write some buffer lines */
2644 channel_write_any_lines();
2645
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002646 sleep_time = deadline - GetTickCount();
2647 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002648 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002649 /* Wait for a little while. Very short at first, up to 10 msec
2650 * after looping a few times. */
2651 if (sleep_time > delay)
2652 sleep_time = delay;
2653 Sleep(sleep_time);
2654 delay = delay * 2;
2655 if (delay > 10)
2656 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002657 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002658 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002659 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002660#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002661 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002662#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002663 struct timeval tval;
2664 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002665 fd_set wfds;
2666 int ret;
2667 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002668
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002669 tval.tv_sec = timeout / 1000;
2670 tval.tv_usec = (timeout % 1000) * 1000;
2671 for (;;)
2672 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002673 FD_ZERO(&rfds);
2674 FD_SET((int)fd, &rfds);
2675
2676 /* Write lines to a pipe when a pipe can be written to. Need to
2677 * set this every time, some buffers may be done. */
2678 maxfd = (int)fd + 1;
2679 FD_ZERO(&wfds);
2680 maxfd = channel_fill_wfds(maxfd, &wfds);
2681
2682 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002683# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002684 SOCK_ERRNO;
2685 if (ret == -1 && errno == EINTR)
2686 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002687# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002688 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002689 {
2690 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002691 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002692 channel_write_any_lines();
2693 continue;
2694 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002695 break;
2696 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002697#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002698 for (;;)
2699 {
2700 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2701 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002702
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002703 fds[0].fd = fd;
2704 fds[0].events = POLLIN;
2705 nfd = channel_fill_poll_write(nfd, fds);
2706 if (poll(fds, nfd, timeout) > 0)
2707 {
2708 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002709 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002710 channel_write_any_lines();
2711 continue;
2712 }
2713 break;
2714 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002715#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002716 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002717 return CW_NOT_READY;
2718}
2719
2720 static void
2721channel_close_on_error(channel_T *channel, int part, char *func)
2722{
2723 /* Do not call emsg(), most likely the other end just exited. */
2724 ch_errors(channel, "%s(): Cannot read from channel", func);
2725
2726 /* Queue a "DETACH" netbeans message in the command queue in order to
2727 * terminate the netbeans session later. Do not end the session here
2728 * directly as we may be running in the context of a call to
2729 * netbeans_parse_messages():
2730 * netbeans_parse_messages
2731 * -> autocmd triggered while processing the netbeans cmd
2732 * -> ui_breakcheck
2733 * -> gui event loop or select loop
2734 * -> channel_read()
2735 * Don't send "DETACH" for a JS or JSON channel.
2736 */
2737 if (channel->ch_part[part].ch_mode == MODE_RAW
2738 || channel->ch_part[part].ch_mode == MODE_NL)
2739 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
2740 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2741
2742 /* When reading from stdout is not possible, assume the other side has
2743 * died. */
2744 channel_close(channel, TRUE);
2745 if (channel->ch_nb_close_cb != NULL)
2746 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002747}
2748
2749/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002750 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002751 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002752 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002753 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002754 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002755channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002756{
2757 static char_u *buf = NULL;
2758 int len = 0;
2759 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002760 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002761 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002762
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002763 fd = channel->ch_part[part].ch_fd;
2764 if (fd == INVALID_FD)
2765 {
2766 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002767 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002768 }
2769 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002770
2771 /* Allocate a buffer to read into. */
2772 if (buf == NULL)
2773 {
2774 buf = alloc(MAXMSGSIZE);
2775 if (buf == NULL)
2776 return; /* out of memory! */
2777 }
2778
2779 /* Keep on reading for as long as there is something to read.
2780 * Use select() or poll() to avoid blocking on a message that is exactly
2781 * MAXMSGSIZE long. */
2782 for (;;)
2783 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002784 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002785 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002786 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002787 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002788 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002789 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002790 if (len <= 0)
2791 break; /* error or nothing more to read */
2792
2793 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002794 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002795 readlen += len;
2796 if (len < MAXMSGSIZE)
2797 break; /* did read everything that's available */
2798 }
2799
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002800 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002801 if (readlen <= 0)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002802 channel_close_on_error(channel, part, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002803
2804#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002805 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002806 if (CH_HAS_GUI && gtk_main_level() > 0)
2807 gtk_main_quit();
2808#endif
2809}
2810
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002811/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002812 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002813 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002814 * Returns what was read in allocated memory.
2815 * Returns NULL in case of error or timeout.
2816 */
2817 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002818channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002819{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002820 char_u *buf;
2821 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002822 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002823 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002824 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002825
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002826 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002827 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002828
2829 while (TRUE)
2830 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002831 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002832 if (buf != NULL && (mode == MODE_RAW
2833 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2834 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002835 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002836 continue;
2837
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002838 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002839 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002840 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002841 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002842 {
2843 ch_log(channel, "Timed out");
2844 return NULL;
2845 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002846 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002847 }
2848
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002849 if (mode == MODE_RAW)
2850 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002851 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002852 }
2853 else
2854 {
2855 nl = vim_strchr(buf, NL);
2856 if (nl[1] == NUL)
2857 {
2858 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002859 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002860 *nl = NUL;
2861 }
2862 else
2863 {
2864 /* Copy the message into allocated memory and remove it from the
2865 * buffer. */
2866 msg = vim_strnsave(buf, (int)(nl - buf));
2867 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2868 }
2869 }
2870 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002871 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002872 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002873}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002874
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002875/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002876 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002877 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002878 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002879 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002880 */
2881 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002882channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002883 channel_T *channel,
2884 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002885 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002886 int id,
2887 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002888{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002889 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002890 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002891 int timeout;
2892 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002893
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002894 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002895 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002896 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002897 for (;;)
2898 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002899 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002900
2901 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002902 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002903 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002904 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002905 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002906 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002907
Bram Moolenaard7ece102016-02-02 23:23:02 +01002908 if (!more)
2909 {
2910 /* Handle any other messages in the queue. If done some more
2911 * messages may have arrived. */
2912 if (channel_parse_messages())
2913 continue;
2914
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002915 /* Wait for up to the timeout. If there was an incomplete message
2916 * use the deadline for that. */
2917 timeout = timeout_arg;
2918 if (chanpart->ch_waiting)
2919 {
2920#ifdef WIN32
2921 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2922#else
2923 {
2924 struct timeval now_tv;
2925
2926 gettimeofday(&now_tv, NULL);
2927 timeout = (chanpart->ch_deadline.tv_sec
2928 - now_tv.tv_sec) * 1000
2929 + (chanpart->ch_deadline.tv_usec
2930 - now_tv.tv_usec) / 1000
2931 + 1;
2932 }
2933#endif
2934 if (timeout < 0)
2935 {
2936 /* Something went wrong, channel_parse_json() didn't
2937 * discard message. Cancel waiting. */
2938 chanpart->ch_waiting = FALSE;
2939 timeout = timeout_arg;
2940 }
2941 else if (timeout > timeout_arg)
2942 timeout = timeout_arg;
2943 }
2944 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002945 if (fd == INVALID_FD
2946 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002947 {
2948 if (timeout == timeout_arg)
2949 {
2950 if (fd != INVALID_FD)
2951 ch_log(channel, "Timed out");
2952 break;
2953 }
2954 }
2955 else
2956 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002957 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002958 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002959 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002960 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002961}
2962
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002963/*
2964 * Common for ch_read() and ch_readraw().
2965 */
2966 void
2967common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2968{
2969 channel_T *channel;
2970 int part;
2971 jobopt_T opt;
2972 int mode;
2973 int timeout;
2974 int id = -1;
2975 typval_T *listtv = NULL;
2976
2977 /* return an empty string by default */
2978 rettv->v_type = VAR_STRING;
2979 rettv->vval.v_string = NULL;
2980
2981 clear_job_options(&opt);
2982 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2983 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02002984 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002985
2986 channel = get_channel_arg(&argvars[0], TRUE);
2987 if (channel != NULL)
2988 {
2989 if (opt.jo_set & JO_PART)
2990 part = opt.jo_part;
2991 else
2992 part = channel_part_read(channel);
2993 mode = channel_get_mode(channel, part);
2994 timeout = channel_get_timeout(channel, part);
2995 if (opt.jo_set & JO_TIMEOUT)
2996 timeout = opt.jo_timeout;
2997
2998 if (raw || mode == MODE_RAW || mode == MODE_NL)
2999 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3000 else
3001 {
3002 if (opt.jo_set & JO_ID)
3003 id = opt.jo_id;
3004 channel_read_json_block(channel, part, timeout, id, &listtv);
3005 if (listtv != NULL)
3006 {
3007 *rettv = *listtv;
3008 vim_free(listtv);
3009 }
3010 else
3011 {
3012 rettv->v_type = VAR_SPECIAL;
3013 rettv->vval.v_number = VVAL_NONE;
3014 }
3015 }
3016 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003017
3018theend:
3019 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003020}
3021
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003022# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3023 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003024/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003025 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003026 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003027 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003028 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003029channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003030{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003031 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003032 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003033
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003034 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003035 for (channel = first_channel; channel != NULL;
3036 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003037 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003038 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003039 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003040 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003041 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003042 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003043 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003044 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003045 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003046}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003047# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003048
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003049# if defined(WIN32) || defined(PROTO)
3050/*
3051 * Check the channels for anything that is ready to be read.
3052 * The data is put in the read queue.
3053 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003054 void
3055channel_handle_events(void)
3056{
3057 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003058 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003059 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003060
3061 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3062 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003063 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003064 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003065 {
3066 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003067 if (fd != INVALID_FD)
3068 {
3069 int r = channel_wait(channel, fd, 0);
3070
3071 if (r == CW_READY)
3072 channel_read(channel, part, "channel_handle_events");
3073 else if (r == CW_ERROR)
3074 channel_close_on_error(channel, part,
3075 "channel_handle_events()");
3076 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003077 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003078 }
3079}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003080# endif
3081
Bram Moolenaard04a0202016-01-26 23:30:18 +01003082/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003083 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003084 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003085 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003086 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003087 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003088channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003089{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003091 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003092 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003093
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003094 fd = channel->ch_part[part].ch_fd;
3095 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003096 {
3097 if (!channel->ch_error && fun != NULL)
3098 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003099 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003100 EMSG2("E630: %s(): write while not connected", fun);
3101 }
3102 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003103 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003104 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003105
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003106 if (log_fd != NULL)
3107 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003108 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003109 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003110 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003111 fprintf(log_fd, "'\n");
3112 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003113 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003114 }
3115
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003116 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003117 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003118 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003119 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003120 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003121 {
3122 if (!channel->ch_error && fun != NULL)
3123 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003124 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003125 EMSG2("E631: %s(): write failed", fun);
3126 }
3127 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003128 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003129 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003130
3131 channel->ch_error = FALSE;
3132 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003133}
3134
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003135/*
3136 * Common for "ch_sendexpr()" and "ch_sendraw()".
3137 * Returns the channel if the caller should read the response.
3138 * Sets "part_read" to the the read fd.
3139 * Otherwise returns NULL.
3140 */
3141 channel_T *
3142send_common(
3143 typval_T *argvars,
3144 char_u *text,
3145 int id,
3146 int eval,
3147 jobopt_T *opt,
3148 char *fun,
3149 int *part_read)
3150{
3151 channel_T *channel;
3152 int part_send;
3153
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003154 clear_job_options(opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003155 channel = get_channel_arg(&argvars[0], TRUE);
3156 if (channel == NULL)
3157 return NULL;
3158 part_send = channel_part_send(channel);
3159 *part_read = channel_part_read(channel);
3160
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003161 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3162 return NULL;
3163
3164 /* Set the callback. An empty callback means no callback and not reading
3165 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3166 * allowed. */
3167 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3168 {
3169 if (eval)
3170 {
3171 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3172 return NULL;
3173 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003174 channel_set_req_callback(channel, part_send,
3175 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003176 }
3177
3178 if (channel_send(channel, part_send, text, fun) == OK
3179 && opt->jo_callback == NULL)
3180 return channel;
3181 return NULL;
3182}
3183
3184/*
3185 * common for "ch_evalexpr()" and "ch_sendexpr()"
3186 */
3187 void
3188ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3189{
3190 char_u *text;
3191 typval_T *listtv;
3192 channel_T *channel;
3193 int id;
3194 ch_mode_T ch_mode;
3195 int part_send;
3196 int part_read;
3197 jobopt_T opt;
3198 int timeout;
3199
3200 /* return an empty string by default */
3201 rettv->v_type = VAR_STRING;
3202 rettv->vval.v_string = NULL;
3203
3204 channel = get_channel_arg(&argvars[0], TRUE);
3205 if (channel == NULL)
3206 return;
3207 part_send = channel_part_send(channel);
3208
3209 ch_mode = channel_get_mode(channel, part_send);
3210 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3211 {
3212 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3213 return;
3214 }
3215
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003216 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003217 text = json_encode_nr_expr(id, &argvars[1],
3218 ch_mode == MODE_JS ? JSON_JS : 0);
3219 if (text == NULL)
3220 return;
3221
3222 channel = send_common(argvars, text, id, eval, &opt,
3223 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3224 vim_free(text);
3225 if (channel != NULL && eval)
3226 {
3227 if (opt.jo_set & JO_TIMEOUT)
3228 timeout = opt.jo_timeout;
3229 else
3230 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003231 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3232 == OK)
3233 {
3234 list_T *list = listtv->vval.v_list;
3235
3236 /* Move the item from the list and then change the type to
3237 * avoid the value being freed. */
3238 *rettv = list->lv_last->li_tv;
3239 list->lv_last->li_tv.v_type = VAR_NUMBER;
3240 free_tv(listtv);
3241 }
3242 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003243 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003244}
3245
3246/*
3247 * common for "ch_evalraw()" and "ch_sendraw()"
3248 */
3249 void
3250ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3251{
3252 char_u buf[NUMBUFLEN];
3253 char_u *text;
3254 channel_T *channel;
3255 int part_read;
3256 jobopt_T opt;
3257 int timeout;
3258
3259 /* return an empty string by default */
3260 rettv->v_type = VAR_STRING;
3261 rettv->vval.v_string = NULL;
3262
3263 text = get_tv_string_buf(&argvars[1], buf);
3264 channel = send_common(argvars, text, 0, eval, &opt,
3265 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3266 if (channel != NULL && eval)
3267 {
3268 if (opt.jo_set & JO_TIMEOUT)
3269 timeout = opt.jo_timeout;
3270 else
3271 timeout = channel_get_timeout(channel, part_read);
3272 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3273 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003274 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003275}
3276
Bram Moolenaard04a0202016-01-26 23:30:18 +01003277# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003278/*
3279 * Add open channels to the poll struct.
3280 * Return the adjusted struct index.
3281 * The type of "fds" is hidden to avoid problems with the function proto.
3282 */
3283 int
3284channel_poll_setup(int nfd_in, void *fds_in)
3285{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003286 int nfd = nfd_in;
3287 channel_T *channel;
3288 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003289 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003290
Bram Moolenaar77073442016-02-13 23:23:53 +01003291 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003292 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003293 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003294 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003295 chanpart_T *ch_part = &channel->ch_part[part];
3296
3297 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003298 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003299 ch_part->ch_poll_idx = nfd;
3300 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003301 fds[nfd].events = POLLIN;
3302 nfd++;
3303 }
3304 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003305 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003306 }
3307 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003308
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003309 nfd = channel_fill_poll_write(nfd, fds);
3310
Bram Moolenaare0874f82016-01-24 20:36:41 +01003311 return nfd;
3312}
3313
3314/*
3315 * The type of "fds" is hidden to avoid problems with the function proto.
3316 */
3317 int
3318channel_poll_check(int ret_in, void *fds_in)
3319{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003320 int ret = ret_in;
3321 channel_T *channel;
3322 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003323 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003324 int idx;
3325 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003326
Bram Moolenaar77073442016-02-13 23:23:53 +01003327 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003328 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003329 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003330 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003331 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003332
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003333 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003334 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003335 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003336 --ret;
3337 }
3338 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003339
3340 in_part = &channel->ch_part[PART_IN];
3341 idx = in_part->ch_poll_idx;
3342 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3343 {
3344 if (in_part->ch_buf_append)
3345 {
3346 if (in_part->ch_buffer != NULL)
3347 channel_write_new_lines(in_part->ch_buffer);
3348 }
3349 else
3350 channel_write_in(channel);
3351 --ret;
3352 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003353 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003354
3355 return ret;
3356}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003357# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003358
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003359# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003360/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003361 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003362 */
3363 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003364channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003365{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003366 int maxfd = maxfd_in;
3367 channel_T *channel;
3368 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003369 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003370 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003371
Bram Moolenaar77073442016-02-13 23:23:53 +01003372 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003373 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003374 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003375 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003376 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003377
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003378 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003379 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003380 FD_SET((int)fd, rfds);
3381 if (maxfd < (int)fd)
3382 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003383 }
3384 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003385 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003386
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003387 maxfd = channel_fill_wfds(maxfd, wfds);
3388
Bram Moolenaare0874f82016-01-24 20:36:41 +01003389 return maxfd;
3390}
3391
3392/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003393 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003394 */
3395 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003396channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003397{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003398 int ret = ret_in;
3399 channel_T *channel;
3400 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003401 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003402 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003403 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003404
Bram Moolenaar77073442016-02-13 23:23:53 +01003405 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003406 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003407 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003408 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003409 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003410
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003411 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003412 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003413 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003414 --ret;
3415 }
3416 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003417
3418 in_part = &channel->ch_part[PART_IN];
3419 if (ret > 0 && in_part->ch_fd != INVALID_FD
3420 && FD_ISSET(in_part->ch_fd, wfds))
3421 {
3422 if (in_part->ch_buf_append)
3423 {
3424 if (in_part->ch_buffer != NULL)
3425 channel_write_new_lines(in_part->ch_buffer);
3426 }
3427 else
3428 channel_write_in(channel);
3429 --ret;
3430 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003431 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003432
3433 return ret;
3434}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003435# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003436
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003437/*
Bram Moolenaar187db502016-02-27 14:44:26 +01003438 * Return TRUE if "channel" has JSON or other typeahead.
3439 */
3440 static int
3441channel_has_readahead(channel_T *channel, int part)
3442{
3443 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
3444
3445 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
3446 {
3447 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3448 jsonq_T *item = head->jq_next;
3449
3450 return item != NULL;
3451 }
3452 return channel_peek(channel, part) != NULL;
3453}
3454
3455/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003456 * Execute queued up commands.
3457 * Invoked from the main loop when it's safe to execute received commands.
3458 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003459 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003460 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003461channel_parse_messages(void)
3462{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003463 channel_T *channel = first_channel;
3464 int ret = FALSE;
3465 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003466 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003467
Bram Moolenaard0b65022016-03-06 21:50:33 +01003468 /* Only do this message when another message was given, otherwise we get
3469 * lots of them. */
3470 if (did_log_msg)
3471 {
3472 ch_log(NULL, "looking for messages on channels");
3473 did_log_msg = FALSE;
3474 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003475 while (channel != NULL)
3476 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003477 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003478 {
3479 /* channel is no longer useful, free it */
3480 channel_free(channel);
3481 channel = first_channel;
3482 part = PART_SOCK;
3483 continue;
3484 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003485 if (channel->ch_part[part].ch_fd != INVALID_FD
3486 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003487 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003488 /* Increase the refcount, in case the handler causes the channel
3489 * to be unreferenced or closed. */
3490 ++channel->ch_refcount;
3491 r = may_invoke_callback(channel, part);
3492 if (r == OK)
3493 ret = TRUE;
3494 if (channel_unref(channel) || r == OK)
3495 {
3496 /* channel was freed or something was done, start over */
3497 channel = first_channel;
3498 part = PART_SOCK;
3499 continue;
3500 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003501 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 if (part < PART_ERR)
3503 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003504 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003505 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003506 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003507 part = PART_SOCK;
3508 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003509 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003510
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003511 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003512 {
3513 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003514 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003515 }
3516
Bram Moolenaard7ece102016-02-02 23:23:02 +01003517 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003518}
3519
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003520/*
3521 * Mark references to lists used in channels.
3522 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003523 int
3524set_ref_in_channel(int copyID)
3525{
Bram Moolenaar77073442016-02-13 23:23:53 +01003526 int abort = FALSE;
3527 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003528 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003529
Bram Moolenaar77073442016-02-13 23:23:53 +01003530 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003531 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003532 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003533 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003534 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3535 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003536
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003538 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003539 list_T *l = item->jq_value->vval.v_list;
3540
3541 if (l->lv_copyID != copyID)
3542 {
3543 l->lv_copyID = copyID;
3544 abort = abort || set_ref_in_list(l, copyID, NULL);
3545 }
3546 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003547 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003548 }
3549 }
3550 return abort;
3551}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003552
3553/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003554 * Return the "part" to write to for "channel".
3555 */
3556 int
3557channel_part_send(channel_T *channel)
3558{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 if (channel->CH_SOCK_FD == INVALID_FD)
3560 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003561 return PART_SOCK;
3562}
3563
3564/*
3565 * Return the default "part" to read from for "channel".
3566 */
3567 int
3568channel_part_read(channel_T *channel)
3569{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003570 if (channel->CH_SOCK_FD == INVALID_FD)
3571 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003572 return PART_SOCK;
3573}
3574
3575/*
3576 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003577 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003578 */
3579 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003580channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003581{
Bram Moolenaar77073442016-02-13 23:23:53 +01003582 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003583 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003584 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003585}
3586
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003587/*
3588 * Return the timeout of "channel"/"part"
3589 */
3590 int
3591channel_get_timeout(channel_T *channel, int part)
3592{
3593 return channel->ch_part[part].ch_timeout;
3594}
3595
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003596 static int
3597handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3598{
3599 char_u *val = get_tv_string(item);
3600
3601 opt->jo_set |= jo;
3602 if (STRCMP(val, "nl") == 0)
3603 *modep = MODE_NL;
3604 else if (STRCMP(val, "raw") == 0)
3605 *modep = MODE_RAW;
3606 else if (STRCMP(val, "js") == 0)
3607 *modep = MODE_JS;
3608 else if (STRCMP(val, "json") == 0)
3609 *modep = MODE_JSON;
3610 else
3611 {
3612 EMSG2(_(e_invarg2), val);
3613 return FAIL;
3614 }
3615 return OK;
3616}
3617
3618 static int
3619handle_io(typval_T *item, int part, jobopt_T *opt)
3620{
3621 char_u *val = get_tv_string(item);
3622
3623 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3624 if (STRCMP(val, "null") == 0)
3625 opt->jo_io[part] = JIO_NULL;
3626 else if (STRCMP(val, "pipe") == 0)
3627 opt->jo_io[part] = JIO_PIPE;
3628 else if (STRCMP(val, "file") == 0)
3629 opt->jo_io[part] = JIO_FILE;
3630 else if (STRCMP(val, "buffer") == 0)
3631 opt->jo_io[part] = JIO_BUFFER;
3632 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3633 opt->jo_io[part] = JIO_OUT;
3634 else
3635 {
3636 EMSG2(_(e_invarg2), val);
3637 return FAIL;
3638 }
3639 return OK;
3640}
3641
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003642/*
3643 * Clear a jobopt_T before using it.
3644 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003645 void
3646clear_job_options(jobopt_T *opt)
3647{
3648 vim_memset(opt, 0, sizeof(jobopt_T));
3649}
3650
3651/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003652 * Free any members of a jobopt_T.
3653 */
3654 void
3655free_job_options(jobopt_T *opt)
3656{
3657 if (opt->jo_partial != NULL)
3658 partial_unref(opt->jo_partial);
3659 if (opt->jo_out_partial != NULL)
3660 partial_unref(opt->jo_out_partial);
3661 if (opt->jo_err_partial != NULL)
3662 partial_unref(opt->jo_err_partial);
3663 if (opt->jo_close_partial != NULL)
3664 partial_unref(opt->jo_close_partial);
3665}
3666
3667/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003668 * Get the PART_ number from the first character of an option name.
3669 */
3670 static int
3671part_from_char(int c)
3672{
3673 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3674}
3675
3676/*
3677 * Get the option entries from the dict in "tv", parse them and put the result
3678 * in "opt".
3679 * Only accept options in "supported".
3680 * If an option value is invalid return FAIL.
3681 */
3682 int
3683get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3684{
3685 typval_T *item;
3686 char_u *val;
3687 dict_T *dict;
3688 int todo;
3689 hashitem_T *hi;
3690 int part;
3691
3692 opt->jo_set = 0;
3693 if (tv->v_type == VAR_UNKNOWN)
3694 return OK;
3695 if (tv->v_type != VAR_DICT)
3696 {
3697 EMSG(_(e_invarg));
3698 return FAIL;
3699 }
3700 dict = tv->vval.v_dict;
3701 if (dict == NULL)
3702 return OK;
3703
3704 todo = (int)dict->dv_hashtab.ht_used;
3705 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3706 if (!HASHITEM_EMPTY(hi))
3707 {
3708 item = &dict_lookup(hi)->di_tv;
3709
3710 if (STRCMP(hi->hi_key, "mode") == 0)
3711 {
3712 if (!(supported & JO_MODE))
3713 break;
3714 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3715 return FAIL;
3716 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003717 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003718 {
3719 if (!(supported & JO_IN_MODE))
3720 break;
3721 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3722 == FAIL)
3723 return FAIL;
3724 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003725 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003726 {
3727 if (!(supported & JO_OUT_MODE))
3728 break;
3729 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3730 == FAIL)
3731 return FAIL;
3732 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003733 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003734 {
3735 if (!(supported & JO_ERR_MODE))
3736 break;
3737 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3738 == FAIL)
3739 return FAIL;
3740 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003741 else if (STRCMP(hi->hi_key, "in_io") == 0
3742 || STRCMP(hi->hi_key, "out_io") == 0
3743 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003744 {
3745 if (!(supported & JO_OUT_IO))
3746 break;
3747 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3748 return FAIL;
3749 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003750 else if (STRCMP(hi->hi_key, "in_name") == 0
3751 || STRCMP(hi->hi_key, "out_name") == 0
3752 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003753 {
3754 part = part_from_char(*hi->hi_key);
3755
3756 if (!(supported & JO_OUT_IO))
3757 break;
3758 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3759 opt->jo_io_name[part] =
3760 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3761 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003762 else if (STRCMP(hi->hi_key, "in_buf") == 0
3763 || STRCMP(hi->hi_key, "out_buf") == 0
3764 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003765 {
3766 part = part_from_char(*hi->hi_key);
3767
3768 if (!(supported & JO_OUT_IO))
3769 break;
3770 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3771 opt->jo_io_buf[part] = get_tv_number(item);
3772 if (opt->jo_io_buf[part] <= 0)
3773 {
3774 EMSG2(_(e_invarg2), get_tv_string(item));
3775 return FAIL;
3776 }
3777 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3778 {
3779 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3780 return FAIL;
3781 }
3782 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003783 else if (STRCMP(hi->hi_key, "in_top") == 0
3784 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003785 {
3786 linenr_T *lp;
3787
3788 if (!(supported & JO_OUT_IO))
3789 break;
3790 if (hi->hi_key[3] == 't')
3791 {
3792 lp = &opt->jo_in_top;
3793 opt->jo_set |= JO_IN_TOP;
3794 }
3795 else
3796 {
3797 lp = &opt->jo_in_bot;
3798 opt->jo_set |= JO_IN_BOT;
3799 }
3800 *lp = get_tv_number(item);
3801 if (*lp < 0)
3802 {
3803 EMSG2(_(e_invarg2), get_tv_string(item));
3804 return FAIL;
3805 }
3806 }
3807 else if (STRCMP(hi->hi_key, "channel") == 0)
3808 {
3809 if (!(supported & JO_OUT_IO))
3810 break;
3811 opt->jo_set |= JO_CHANNEL;
3812 if (item->v_type != VAR_CHANNEL)
3813 {
3814 EMSG2(_(e_invarg2), "channel");
3815 return FAIL;
3816 }
3817 opt->jo_channel = item->vval.v_channel;
3818 }
3819 else if (STRCMP(hi->hi_key, "callback") == 0)
3820 {
3821 if (!(supported & JO_CALLBACK))
3822 break;
3823 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003824 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003825 if (opt->jo_callback == NULL)
3826 {
3827 EMSG2(_(e_invarg2), "callback");
3828 return FAIL;
3829 }
3830 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003831 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003832 {
3833 if (!(supported & JO_OUT_CALLBACK))
3834 break;
3835 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003836 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003837 if (opt->jo_out_cb == NULL)
3838 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003839 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003840 return FAIL;
3841 }
3842 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003843 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003844 {
3845 if (!(supported & JO_ERR_CALLBACK))
3846 break;
3847 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003848 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003849 if (opt->jo_err_cb == NULL)
3850 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003851 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852 return FAIL;
3853 }
3854 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003855 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003856 {
3857 if (!(supported & JO_CLOSE_CALLBACK))
3858 break;
3859 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003860 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003861 if (opt->jo_close_cb == NULL)
3862 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003863 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003864 return FAIL;
3865 }
3866 }
3867 else if (STRCMP(hi->hi_key, "waittime") == 0)
3868 {
3869 if (!(supported & JO_WAITTIME))
3870 break;
3871 opt->jo_set |= JO_WAITTIME;
3872 opt->jo_waittime = get_tv_number(item);
3873 }
3874 else if (STRCMP(hi->hi_key, "timeout") == 0)
3875 {
3876 if (!(supported & JO_TIMEOUT))
3877 break;
3878 opt->jo_set |= JO_TIMEOUT;
3879 opt->jo_timeout = get_tv_number(item);
3880 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003881 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003882 {
3883 if (!(supported & JO_OUT_TIMEOUT))
3884 break;
3885 opt->jo_set |= JO_OUT_TIMEOUT;
3886 opt->jo_out_timeout = get_tv_number(item);
3887 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003888 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003889 {
3890 if (!(supported & JO_ERR_TIMEOUT))
3891 break;
3892 opt->jo_set |= JO_ERR_TIMEOUT;
3893 opt->jo_err_timeout = get_tv_number(item);
3894 }
3895 else if (STRCMP(hi->hi_key, "part") == 0)
3896 {
3897 if (!(supported & JO_PART))
3898 break;
3899 opt->jo_set |= JO_PART;
3900 val = get_tv_string(item);
3901 if (STRCMP(val, "err") == 0)
3902 opt->jo_part = PART_ERR;
3903 else
3904 {
3905 EMSG2(_(e_invarg2), val);
3906 return FAIL;
3907 }
3908 }
3909 else if (STRCMP(hi->hi_key, "id") == 0)
3910 {
3911 if (!(supported & JO_ID))
3912 break;
3913 opt->jo_set |= JO_ID;
3914 opt->jo_id = get_tv_number(item);
3915 }
3916 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3917 {
3918 if (!(supported & JO_STOPONEXIT))
3919 break;
3920 opt->jo_set |= JO_STOPONEXIT;
3921 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3922 opt->jo_soe_buf);
3923 if (opt->jo_stoponexit == NULL)
3924 {
3925 EMSG2(_(e_invarg2), "stoponexit");
3926 return FAIL;
3927 }
3928 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003929 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003930 {
3931 if (!(supported & JO_EXIT_CB))
3932 break;
3933 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003934 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3935 {
3936 opt->jo_exit_partial = item->vval.v_partial;
3937 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3938 }
3939 else
3940 opt->jo_exit_cb = get_tv_string_buf_chk(
3941 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003942 if (opt->jo_exit_cb == NULL)
3943 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003944 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003945 return FAIL;
3946 }
3947 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003948 else if (STRCMP(hi->hi_key, "block_write") == 0)
3949 {
3950 if (!(supported & JO_BLOCK_WRITE))
3951 break;
3952 opt->jo_set |= JO_BLOCK_WRITE;
3953 opt->jo_block_write = get_tv_number(item);
3954 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003955 else
3956 break;
3957 --todo;
3958 }
3959 if (todo > 0)
3960 {
3961 EMSG2(_(e_invarg2), hi->hi_key);
3962 return FAIL;
3963 }
3964
3965 return OK;
3966}
3967
3968/*
3969 * Get the channel from the argument.
3970 * Returns NULL if the handle is invalid.
3971 */
3972 channel_T *
3973get_channel_arg(typval_T *tv, int check_open)
3974{
3975 channel_T *channel = NULL;
3976
3977 if (tv->v_type == VAR_JOB)
3978 {
3979 if (tv->vval.v_job != NULL)
3980 channel = tv->vval.v_job->jv_channel;
3981 }
3982 else if (tv->v_type == VAR_CHANNEL)
3983 {
3984 channel = tv->vval.v_channel;
3985 }
3986 else
3987 {
3988 EMSG2(_(e_invarg2), get_tv_string(tv));
3989 return NULL;
3990 }
3991
3992 if (check_open && (channel == NULL || !channel_is_open(channel)))
3993 {
3994 EMSG(_("E906: not an open channel"));
3995 return NULL;
3996 }
3997 return channel;
3998}
3999
4000static job_T *first_job = NULL;
4001
4002 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004003job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004004{
4005 ch_log(job->jv_channel, "Freeing job");
4006 if (job->jv_channel != NULL)
4007 {
4008 /* The link from the channel to the job doesn't count as a reference,
4009 * thus don't decrement the refcount of the job. The reference from
4010 * the job to the channel does count the refrence, decrement it and
4011 * NULL the reference. We don't set ch_job_killed, unreferencing the
4012 * job doesn't mean it stops running. */
4013 job->jv_channel->ch_job = NULL;
4014 channel_unref(job->jv_channel);
4015 }
4016 mch_clear_job(job);
4017
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004018 vim_free(job->jv_stoponexit);
4019 vim_free(job->jv_exit_cb);
4020 partial_unref(job->jv_exit_partial);
4021}
4022
4023 static void
4024job_free_job(job_T *job)
4025{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004026 if (job->jv_next != NULL)
4027 job->jv_next->jv_prev = job->jv_prev;
4028 if (job->jv_prev == NULL)
4029 first_job = job->jv_next;
4030 else
4031 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004032 vim_free(job);
4033}
4034
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004035 static void
4036job_free(job_T *job)
4037{
4038 if (!in_free_unref_items)
4039 {
4040 job_free_contents(job);
4041 job_free_job(job);
4042 }
4043}
4044
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004045/*
4046 * Return TRUE if the job should not be freed yet. Do not free the job when
4047 * it has not ended yet and there is a "stoponexit" flag or an exit callback.
4048 */
4049 static int
4050job_still_useful(job_T *job)
4051{
4052 return job->jv_status == JOB_STARTED
4053 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4054}
4055
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056 void
4057job_unref(job_T *job)
4058{
4059 if (job != NULL && --job->jv_refcount <= 0)
4060 {
4061 /* Do not free the job when it has not ended yet and there is a
4062 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004063 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 {
4065 job_free(job);
4066 }
4067 else if (job->jv_channel != NULL)
4068 {
4069 /* Do remove the link to the channel, otherwise it hangs
4070 * around until Vim exits. See job_free() for refcount. */
4071 job->jv_channel->ch_job = NULL;
4072 channel_unref(job->jv_channel);
4073 job->jv_channel = NULL;
4074 }
4075 }
4076}
4077
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004078 int
4079free_unused_jobs_contents(int copyID, int mask)
4080{
4081 int did_free = FALSE;
4082 job_T *job;
4083
4084 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004085 if ((job->jv_copyID & mask) != (copyID & mask)
4086 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004087 {
4088 /* Free the channel and ordinary items it contains, but don't
4089 * recurse into Lists, Dictionaries etc. */
4090 job_free_contents(job);
4091 did_free = TRUE;
4092 }
4093 return did_free;
4094}
4095
4096 void
4097free_unused_jobs(int copyID, int mask)
4098{
4099 job_T *job;
4100 job_T *job_next;
4101
4102 for (job = first_job; job != NULL; job = job_next)
4103 {
4104 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004105 if ((job->jv_copyID & mask) != (copyID & mask)
4106 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004107 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004108 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004109 job_free_job(job);
4110 }
4111 }
4112}
4113
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114/*
4115 * Allocate a job. Sets the refcount to one and sets options default.
4116 */
4117 static job_T *
4118job_alloc(void)
4119{
4120 job_T *job;
4121
4122 job = (job_T *)alloc_clear(sizeof(job_T));
4123 if (job != NULL)
4124 {
4125 job->jv_refcount = 1;
4126 job->jv_stoponexit = vim_strsave((char_u *)"term");
4127
4128 if (first_job != NULL)
4129 {
4130 first_job->jv_prev = job;
4131 job->jv_next = first_job;
4132 }
4133 first_job = job;
4134 }
4135 return job;
4136}
4137
4138 void
4139job_set_options(job_T *job, jobopt_T *opt)
4140{
4141 if (opt->jo_set & JO_STOPONEXIT)
4142 {
4143 vim_free(job->jv_stoponexit);
4144 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4145 job->jv_stoponexit = NULL;
4146 else
4147 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4148 }
4149 if (opt->jo_set & JO_EXIT_CB)
4150 {
4151 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004152 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004153 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004154 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004155 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004156 job->jv_exit_partial = NULL;
4157 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004158 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004159 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004160 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004161 job->jv_exit_partial = opt->jo_exit_partial;
4162 if (job->jv_exit_partial != NULL)
4163 ++job->jv_exit_partial->pt_refcount;
4164 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004165 }
4166}
4167
4168/*
4169 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4170 */
4171 void
4172job_stop_on_exit()
4173{
4174 job_T *job;
4175
4176 for (job = first_job; job != NULL; job = job->jv_next)
4177 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4178 mch_stop_job(job, job->jv_stoponexit);
4179}
4180
4181/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004182 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004183 */
4184 void
4185job_check_ended(void)
4186{
4187 static time_t last_check = 0;
4188 time_t now;
4189 job_T *job;
4190 job_T *next;
4191
4192 /* Only do this once in 10 seconds. */
4193 now = time(NULL);
4194 if (last_check + 10 < now)
4195 {
4196 last_check = now;
4197 for (job = first_job; job != NULL; job = next)
4198 {
4199 next = job->jv_next;
4200 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4201 job_status(job); /* may free "job" */
4202 }
4203 }
4204}
4205
4206/*
4207 * "job_start()" function
4208 */
4209 job_T *
4210job_start(typval_T *argvars)
4211{
4212 job_T *job;
4213 char_u *cmd = NULL;
4214#if defined(UNIX)
4215# define USE_ARGV
4216 char **argv = NULL;
4217 int argc = 0;
4218#else
4219 garray_T ga;
4220#endif
4221 jobopt_T opt;
4222 int part;
4223
4224 job = job_alloc();
4225 if (job == NULL)
4226 return NULL;
4227
4228 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004229#ifndef USE_ARGV
4230 ga_init2(&ga, (int)sizeof(char*), 20);
4231#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004232
4233 /* Default mode is NL. */
4234 clear_job_options(&opt);
4235 opt.jo_mode = MODE_NL;
4236 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004237 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4238 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004239 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004240
4241 /* Check that when io is "file" that there is a file name. */
4242 for (part = PART_OUT; part <= PART_IN; ++part)
4243 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4244 && opt.jo_io[part] == JIO_FILE
4245 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4246 || *opt.jo_io_name[part] == NUL))
4247 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004248 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004249 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004250 }
4251
4252 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4253 {
4254 buf_T *buf = NULL;
4255
4256 /* check that we can find the buffer before starting the job */
4257 if (opt.jo_set & JO_IN_BUF)
4258 {
4259 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4260 if (buf == NULL)
4261 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4262 }
4263 else if (!(opt.jo_set & JO_IN_NAME))
4264 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004265 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004266 }
4267 else
4268 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4269 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004270 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004271 if (buf->b_ml.ml_mfp == NULL)
4272 {
4273 char_u numbuf[NUMBUFLEN];
4274 char_u *s;
4275
4276 if (opt.jo_set & JO_IN_BUF)
4277 {
4278 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4279 s = numbuf;
4280 }
4281 else
4282 s = opt.jo_io_name[PART_IN];
4283 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004284 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004285 }
4286 job->jv_in_buf = buf;
4287 }
4288
4289 job_set_options(job, &opt);
4290
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004291 if (argvars[0].v_type == VAR_STRING)
4292 {
4293 /* Command is a string. */
4294 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004295 if (cmd == NULL || *cmd == NUL)
4296 {
4297 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004298 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004299 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004300#ifdef USE_ARGV
4301 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004302 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004303 argv[argc] = NULL;
4304#endif
4305 }
4306 else if (argvars[0].v_type != VAR_LIST
4307 || argvars[0].vval.v_list == NULL
4308 || argvars[0].vval.v_list->lv_len < 1)
4309 {
4310 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004311 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004312 }
4313 else
4314 {
4315 list_T *l = argvars[0].vval.v_list;
4316 listitem_T *li;
4317 char_u *s;
4318
4319#ifdef USE_ARGV
4320 /* Pass argv[] to mch_call_shell(). */
4321 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4322 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004323 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004324#endif
4325 for (li = l->lv_first; li != NULL; li = li->li_next)
4326 {
4327 s = get_tv_string_chk(&li->li_tv);
4328 if (s == NULL)
4329 goto theend;
4330#ifdef USE_ARGV
4331 argv[argc++] = (char *)s;
4332#else
4333 /* Only escape when needed, double quotes are not always allowed. */
4334 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4335 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004336# ifdef WIN32
4337 int old_ssl = p_ssl;
4338
4339 /* This is using CreateProcess, not cmd.exe. Always use
4340 * double quote and backslashes. */
4341 p_ssl = 0;
4342# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004343 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004344# ifdef WIN32
4345 p_ssl = old_ssl;
4346# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004347 if (s == NULL)
4348 goto theend;
4349 ga_concat(&ga, s);
4350 vim_free(s);
4351 }
4352 else
4353 ga_concat(&ga, s);
4354 if (li->li_next != NULL)
4355 ga_append(&ga, ' ');
4356#endif
4357 }
4358#ifdef USE_ARGV
4359 argv[argc] = NULL;
4360#else
4361 cmd = ga.ga_data;
4362#endif
4363 }
4364
4365#ifdef USE_ARGV
4366 if (ch_log_active())
4367 {
4368 garray_T ga;
4369 int i;
4370
4371 ga_init2(&ga, (int)sizeof(char), 200);
4372 for (i = 0; i < argc; ++i)
4373 {
4374 if (i > 0)
4375 ga_concat(&ga, (char_u *)" ");
4376 ga_concat(&ga, (char_u *)argv[i]);
4377 }
4378 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4379 ga_clear(&ga);
4380 }
4381 mch_start_job(argv, job, &opt);
4382#else
4383 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4384 mch_start_job((char *)cmd, job, &opt);
4385#endif
4386
4387 /* If the channel is reading from a buffer, write lines now. */
4388 if (job->jv_channel != NULL)
4389 channel_write_in(job->jv_channel);
4390
4391theend:
4392#ifdef USE_ARGV
4393 vim_free(argv);
4394#else
4395 vim_free(ga.ga_data);
4396#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004397 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398 return job;
4399}
4400
4401/*
4402 * Get the status of "job" and invoke the exit callback when needed.
4403 * The returned string is not allocated.
4404 */
4405 char *
4406job_status(job_T *job)
4407{
4408 char *result;
4409
4410 if (job->jv_status == JOB_ENDED)
4411 /* No need to check, dead is dead. */
4412 result = "dead";
4413 else if (job->jv_status == JOB_FAILED)
4414 result = "fail";
4415 else
4416 {
4417 result = mch_job_status(job);
4418 if (job->jv_status == JOB_ENDED)
4419 ch_log(job->jv_channel, "Job ended");
4420 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4421 {
4422 typval_T argv[3];
4423 typval_T rettv;
4424 int dummy;
4425
4426 /* invoke the exit callback; make sure the refcount is > 0 */
4427 ++job->jv_refcount;
4428 argv[0].v_type = VAR_JOB;
4429 argv[0].vval.v_job = job;
4430 argv[1].v_type = VAR_NUMBER;
4431 argv[1].vval.v_number = job->jv_exitval;
4432 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004433 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4434 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004435 clear_tv(&rettv);
4436 --job->jv_refcount;
4437 }
4438 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4439 {
4440 /* The job was already unreferenced, now that it ended it can be
4441 * freed. Careful: caller must not use "job" after this! */
4442 job_free(job);
4443 }
4444 }
4445 return result;
4446}
4447
Bram Moolenaar8950a562016-03-12 15:22:55 +01004448/*
4449 * Implementation of job_info().
4450 */
4451 void
4452job_info(job_T *job, dict_T *dict)
4453{
4454 dictitem_T *item;
4455 varnumber_T nr;
4456
4457 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4458
4459 item = dictitem_alloc((char_u *)"channel");
4460 if (item == NULL)
4461 return;
4462 item->di_tv.v_lock = 0;
4463 item->di_tv.v_type = VAR_CHANNEL;
4464 item->di_tv.vval.v_channel = job->jv_channel;
4465 if (job->jv_channel != NULL)
4466 ++job->jv_channel->ch_refcount;
4467 if (dict_add(dict, item) == FAIL)
4468 dictitem_free(item);
4469
4470#ifdef UNIX
4471 nr = job->jv_pid;
4472#else
4473 nr = job->jv_proc_info.dwProcessId;
4474#endif
4475 dict_add_nr_str(dict, "process", nr, NULL);
4476
4477 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004478 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004479 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4480}
4481
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004482 int
4483job_stop(job_T *job, typval_T *argvars)
4484{
4485 char_u *arg;
4486
4487 if (argvars[1].v_type == VAR_UNKNOWN)
4488 arg = (char_u *)"";
4489 else
4490 {
4491 arg = get_tv_string_chk(&argvars[1]);
4492 if (arg == NULL)
4493 {
4494 EMSG(_(e_invarg));
4495 return 0;
4496 }
4497 }
4498 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4499 if (mch_stop_job(job, arg) == FAIL)
4500 return 0;
4501
4502 /* Assume that "hup" does not kill the job. */
4503 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4504 job->jv_channel->ch_job_killed = TRUE;
4505
4506 /* We don't try freeing the job, obviously the caller still has a
4507 * reference to it. */
4508 return 1;
4509}
4510
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004511#endif /* FEAT_JOB_CHANNEL */