blob: 4b7b6f72c62b8890d75f512cbcbf149f3870b0d7 [file] [log] [blame]
Bram Moolenaare0874f82016-01-24 20:36:41 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
22/* Note: when making changes here also adjust configure.in. */
23#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
62
Bram Moolenaard8070362016-02-15 21:56:54 +010063#ifdef WIN32
64 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010065fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010066{
67 HANDLE h = (HANDLE)fd;
68 DWORD nread;
69
70 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
71 return -1;
72 return (int)nread;
73}
74
75 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010076fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010077{
78 HANDLE h = (HANDLE)fd;
79 DWORD nwrite;
80
81 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
82 return -1;
83 return (int)nwrite;
84}
85
86 static void
87fd_close(sock_T fd)
88{
89 HANDLE h = (HANDLE)fd;
90
91 CloseHandle(h);
92}
93#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010094
Bram Moolenaar6463ca22016-02-13 17:04:46 +010095/* Log file opened with ch_logfile(). */
96static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010097#ifdef FEAT_RELTIME
98static proftime_T log_start;
99#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100
101 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100103{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100104 FILE *file = NULL;
105
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100106 if (log_fd != NULL)
107 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100108
109 if (*fname != NUL)
110 {
111 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
112 if (file == NULL)
113 {
114 EMSG2(_(e_notopen), fname);
115 return;
116 }
117 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100119
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100122 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100123#ifdef FEAT_RELTIME
124 profile_start(&log_start);
125#endif
126 }
127}
128
129 int
130ch_log_active()
131{
132 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100133}
134
135 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100136ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137{
138 if (log_fd != NULL)
139 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100140#ifdef FEAT_RELTIME
141 proftime_T log_now;
142
143 profile_start(&log_now);
144 profile_sub(&log_now, &log_start);
145 fprintf(log_fd, "%s ", profile_msg(&log_now));
146#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100147 if (ch != NULL)
148 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100149 else
150 fprintf(log_fd, "%s: ", what);
151 }
152}
153
Bram Moolenaard0b65022016-03-06 21:50:33 +0100154static int did_log_msg = TRUE;
155
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100156 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100157ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100158{
159 if (log_fd != NULL)
160 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100165 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100166 }
167}
168
169 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100170ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171{
172 if (log_fd != NULL)
173 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100174 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100176 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100178 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100179 }
180}
181
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100182 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100183ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184{
185 if (log_fd != NULL)
186 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100187 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
194
195 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100196ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100200 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100202 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100204 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100205 }
206}
207
208 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100209ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210{
211 if (log_fd != NULL)
212 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100213 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100215 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100217 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100218 }
219}
220
221 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100222ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223{
224 if (log_fd != NULL)
225 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100226 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100228 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100230 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100231 }
232}
233
234 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100235ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236{
237 if (log_fd != NULL)
238 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100239 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100241 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100243 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100244 }
245}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100246
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100247#ifdef _WIN32
248# undef PERROR
249# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
250 (char_u *)msg, (char_u *)strerror_win32(errno))
251
252 static char *
253strerror_win32(int eno)
254{
255 static LPVOID msgbuf = NULL;
256 char_u *ptr;
257
258 if (msgbuf)
259 LocalFree(msgbuf);
260 FormatMessage(
261 FORMAT_MESSAGE_ALLOCATE_BUFFER |
262 FORMAT_MESSAGE_FROM_SYSTEM |
263 FORMAT_MESSAGE_IGNORE_INSERTS,
264 NULL,
265 eno,
266 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
267 (LPTSTR) &msgbuf,
268 0,
269 NULL);
270 /* chomp \r or \n */
271 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
272 switch (*ptr)
273 {
274 case '\r':
275 STRMOVE(ptr, ptr + 1);
276 ptr--;
277 break;
278 case '\n':
279 if (*(ptr + 1) == '\0')
280 *ptr = '\0';
281 else
282 *ptr = ' ';
283 break;
284 }
285 return msgbuf;
286}
287#endif
288
Bram Moolenaar77073442016-02-13 23:23:53 +0100289/*
290 * The list of all allocated channels.
291 */
292static channel_T *first_channel = NULL;
293static int next_ch_id = 0;
294
295/*
296 * Allocate a new channel. The refcount is set to 1.
297 * The channel isn't actually used until it is opened.
298 * Returns NULL if out of memory.
299 */
300 channel_T *
301add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100302{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100303 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100304 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100305
Bram Moolenaar77073442016-02-13 23:23:53 +0100306 if (channel == NULL)
307 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308
Bram Moolenaar77073442016-02-13 23:23:53 +0100309 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100310 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100311
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100316 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317#endif
318#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100323
Bram Moolenaar77073442016-02-13 23:23:53 +0100324 if (first_channel != NULL)
325 {
326 first_channel->ch_prev = channel;
327 channel->ch_next = first_channel;
328 }
329 first_channel = channel;
330
331 channel->ch_refcount = 1;
332 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100334
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100335/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100336 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100337 * Return TRUE if "channel" has a callback and the associated job wasn't
338 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100339 */
340 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100341channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100342{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344 int has_out_msg;
345 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100346
347 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100348 if (channel->ch_job_killed && channel->ch_job == NULL)
349 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100350
351 /* If there is a close callback it may still need to be invoked. */
352 if (channel->ch_close_cb != NULL)
353 return TRUE;
354
355 /* If there is no callback then nobody can get readahead. If the fd is
356 * closed and there is no readahead then the callback won't be called. */
357 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
358 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
359 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100360 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
361 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
362 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
363 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
364 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
365 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100369 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100370}
371
372/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 * Close a channel and free all its resources.
374 */
375 static void
376channel_free_contents(channel_T *channel)
377{
378 channel_close(channel, TRUE);
379 channel_clear(channel);
380 ch_log(channel, "Freeing channel");
381}
382
383 static void
384channel_free_channel(channel_T *channel)
385{
386 if (channel->ch_next != NULL)
387 channel->ch_next->ch_prev = channel->ch_prev;
388 if (channel->ch_prev == NULL)
389 first_channel = channel->ch_next;
390 else
391 channel->ch_prev->ch_next = channel->ch_next;
392 vim_free(channel);
393}
394
395 static void
396channel_free(channel_T *channel)
397{
398 if (!in_free_unref_items)
399 {
400 channel_free_contents(channel);
401 channel_free_channel(channel);
402 }
403}
404
405/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100406 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100407 * possible, there is no callback to be invoked or the associated job was
408 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100409 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100411 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412channel_may_free(channel_T *channel)
413{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100414 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100417 return TRUE;
418 }
419 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100420}
421
422/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100423 * Decrement the reference count on "channel" and maybe free it when it goes
424 * down to zero. Don't free it if there is a pending action.
425 * Returns TRUE when the channel is no longer referenced.
426 */
427 int
428channel_unref(channel_T *channel)
429{
430 if (channel != NULL && --channel->ch_refcount <= 0)
431 return channel_may_free(channel);
432 return FALSE;
433}
434
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200435 int
436free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100437{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200438 int did_free = FALSE;
439 channel_T *ch;
440
441 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200442 if (!channel_still_useful(ch)
443 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200444 {
445 /* Free the channel and ordinary items it contains, but don't
446 * recurse into Lists, Dictionaries etc. */
447 channel_free_contents(ch);
448 did_free = TRUE;
449 }
450 return did_free;
451}
452
453 void
454free_unused_channels(int copyID, int mask)
455{
456 channel_T *ch;
457 channel_T *ch_next;
458
459 for (ch = first_channel; ch != NULL; ch = ch_next)
460 {
461 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200462 if (!channel_still_useful(ch)
463 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200465 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 channel_free_channel(ch);
467 }
468 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100469}
470
Bram Moolenaard04a0202016-01-26 23:30:18 +0100471#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100472
473#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
474 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100476{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100477 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100478 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100479
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100480 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100481 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100482 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100483 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100484 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100485}
486#endif
487
Bram Moolenaare0874f82016-01-24 20:36:41 +0100488/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100489 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100490 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100491#ifdef FEAT_GUI_X11
492 static void
493messageFromNetbeans(XtPointer clientData,
494 int *unused1 UNUSED,
495 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100502# if GTK_CHECK_VERSION(3,0,0)
503 static gboolean
504messageFromNetbeans(GIOChannel *unused1 UNUSED,
505 GIOCondition unused2 UNUSED,
506 gpointer clientData)
507{
508 channel_read_fd(GPOINTER_TO_INT(clientData));
509 return TRUE; /* Return FALSE instead in case the event source is to
510 * be removed after this function returns. */
511}
512# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 static void
514messageFromNetbeans(gpointer clientData,
515 gint unused1 UNUSED,
516 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100518 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519}
Bram Moolenaar98921892016-02-23 17:14:37 +0100520# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100521#endif
522
523 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100524channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100525{
Bram Moolenaarde279892016-03-11 22:19:44 +0100526 if (!CH_HAS_GUI)
527 return;
528
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100529# ifdef FEAT_GUI_X11
530 /* Tell notifier we are interested in being called
531 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100532 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
533 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100534 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100535 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100536 (XtPointer)(XtInputReadMask + XtInputExceptMask),
537 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100538 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100539# else
540# ifdef FEAT_GUI_GTK
541 /* Tell gdk we are interested in being called when there
542 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100543 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100544# if GTK_CHECK_VERSION(3,0,0)
545 {
546 GIOChannel *chnnl = g_io_channel_unix_new(
547 (gint)channel->ch_part[part].ch_fd);
548
549 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
550 chnnl,
551 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
552 messageFromNetbeans,
553 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
554
555 g_io_channel_unref(chnnl);
556 }
557# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100558 channel->ch_part[part].ch_inputHandler = gdk_input_add(
559 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100560 (GdkInputCondition)
561 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100562 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100563 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100564# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100565# endif
566# endif
567}
568
Bram Moolenaarde279892016-03-11 22:19:44 +0100569 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100570channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100571{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100572 if (channel->CH_SOCK_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100574 if (channel->CH_OUT_FD != INVALID_FD)
575 channel_gui_register_one(channel, PART_OUT);
576 if (channel->CH_ERR_FD != INVALID_FD)
577 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100578}
579
580/*
581 * Register any of our file descriptors with the GUI event handling system.
582 * Called when the GUI has started.
583 */
584 void
585channel_gui_register_all(void)
586{
Bram Moolenaar77073442016-02-13 23:23:53 +0100587 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100588
Bram Moolenaar77073442016-02-13 23:23:53 +0100589 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100590 channel_gui_register(channel);
591}
592
593 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100594channel_gui_unregister_one(channel_T *channel, int part)
595{
596# ifdef FEAT_GUI_X11
597 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
598 {
599 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
600 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
601 }
602# else
603# ifdef FEAT_GUI_GTK
604 if (channel->ch_part[part].ch_inputHandler != 0)
605 {
606# if GTK_CHECK_VERSION(3,0,0)
607 g_source_remove(channel->ch_part[part].ch_inputHandler);
608# else
609 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
610# endif
611 channel->ch_part[part].ch_inputHandler = 0;
612 }
613# endif
614# endif
615}
616
617 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100618channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100619{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100620 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100621
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100622 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100623 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100624}
625
626#endif
627
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100628static char *e_cannot_connect = N_("E902: Cannot connect to port");
629
Bram Moolenaard04a0202016-01-26 23:30:18 +0100630/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100631 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100632 * "waittime" is the time in msec to wait for the connection.
633 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100634 * Returns the channel for success.
635 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100636 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100638channel_open(
639 char *hostname,
640 int port_in,
641 int waittime,
642 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100643{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100645 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100646 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100647#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100649 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100650#else
651 int port = port_in;
652#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100653 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100654 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100656#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100657 channel_init_winsock();
658#endif
659
Bram Moolenaar77073442016-02-13 23:23:53 +0100660 channel = add_channel();
661 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100663 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100664 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100665 }
666
667 /* Get the server internet address and put into addr structure */
668 /* fill in the socket address structure and connect to server */
669 vim_memset((char *)&server, 0, sizeof(server));
670 server.sin_family = AF_INET;
671 server.sin_port = htons(port);
672 if ((host = gethostbyname(hostname)) == NULL)
673 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100675 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100676 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678 }
679 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
680
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100681 /* On Mac and Solaris a zero timeout almost never works. At least wait
682 * one millisecond. Let's do it for all systems, because we don't know why
683 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684 if (waittime == 0)
685 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100686
687 /*
688 * For Unix we need to call connect() again after connect() failed.
689 * On Win32 one time is sufficient.
690 */
691 while (TRUE)
692 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100693 long elapsed_msec = 0;
694 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100695
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 sd = socket(AF_INET, SOCK_STREAM, 0);
699 if (sd == -1)
700 {
701 ch_error(channel, "in socket() in channel_open().");
702 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100703 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100704 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100706
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100707 if (waittime >= 0)
708 {
709 /* Make connect() non-blocking. */
710 if (
711#ifdef _WIN32
712 ioctlsocket(sd, FIONBIO, &val) < 0
713#else
714 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
715#endif
716 )
717 {
718 SOCK_ERRNO;
719 ch_errorn(channel,
720 "channel_open: Connect failed with errno %d", errno);
721 sock_close(sd);
722 channel_free(channel);
723 return NULL;
724 }
725 }
726
727 /* Try connecting to the server. */
728 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
729 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
730
Bram Moolenaar045a2842016-03-08 22:33:07 +0100731 if (ret == 0)
732 /* The connection could be established. */
733 break;
734
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100735 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736 if (waittime < 0 || (errno != EWOULDBLOCK
737 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100738#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100740#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100741 ))
742 {
743 ch_errorn(channel,
744 "channel_open: Connect failed with errno %d", errno);
745 PERROR(_(e_cannot_connect));
746 sock_close(sd);
747 channel_free(channel);
748 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100749 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100751 /* Limit the waittime to 50 msec. If it doesn't work within this
752 * time we close the socket and try creating it again. */
753 waitnow = waittime > 50 ? 50 : waittime;
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100756 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100757#ifndef WIN32
758 if (errno != ECONNREFUSED)
759#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100760 {
761 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100762 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100764#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100765 int so_error = 0;
766 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 struct timeval start_tv;
768 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100769#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 FD_ZERO(&rfds);
771 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100772 FD_ZERO(&wfds);
773 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100774
Bram Moolenaar562ca712016-03-09 21:50:05 +0100775 tv.tv_sec = waitnow / 1000;
776 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100777#ifndef WIN32
778 gettimeofday(&start_tv, NULL);
779#endif
780 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100781 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100782 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100783
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 if (ret < 0)
785 {
786 SOCK_ERRNO;
787 ch_errorn(channel,
788 "channel_open: Connect failed with errno %d", errno);
789 PERROR(_(e_cannot_connect));
790 sock_close(sd);
791 channel_free(channel);
792 return NULL;
793 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100794
Bram Moolenaare081e212016-02-28 22:33:46 +0100795#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100796 /* On Win32: select() is expected to work and wait for up to
797 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798 if (FD_ISSET(sd, &wfds))
799 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100800 elapsed_msec = waitnow;
801 if (waittime > 1 && elapsed_msec < waittime)
802 {
803 waittime -= elapsed_msec;
804 continue;
805 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100806#else
807 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 * After putting the socket in non-blocking mode, connect() will
809 * return EINPROGRESS, select() will not wait (as if writing is
810 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100811 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100814 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 {
816 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 if (ret < 0 || (so_error != 0
819 && so_error != EWOULDBLOCK
820 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100821# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100823# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824 ))
825 {
826 ch_errorn(channel,
827 "channel_open: Connect failed with errno %d",
828 so_error);
829 PERROR(_(e_cannot_connect));
830 sock_close(sd);
831 channel_free(channel);
832 return NULL;
833 }
834 }
835
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 if (FD_ISSET(sd, &wfds) && so_error == 0)
837 /* Did not detect an error, connection is established. */
838 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839
Bram Moolenaar045a2842016-03-08 22:33:07 +0100840 gettimeofday(&end_tv, NULL);
841 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
842 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100843#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100844 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845
846#ifndef WIN32
847 if (waittime > 1 && elapsed_msec < waittime)
848 {
849 /* The port isn't ready but we also didn't get an error.
850 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100851 * yet. Select() may return early, wait until the remaining
852 * "waitnow" and try again. */
853 waitnow -= elapsed_msec;
854 waittime -= elapsed_msec;
855 if (waitnow > 0)
856 {
857 mch_delay((long)waitnow, TRUE);
858 ui_breakcheck();
859 waittime -= waitnow;
860 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100861 if (!got_int)
862 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100863 if (waittime <= 0)
864 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100865 waittime = 1;
866 continue;
867 }
868 /* we were interrupted, behave as if timed out */
869 }
870#endif
871
872 /* We timed out. */
873 ch_error(channel, "Connection timed out");
874 sock_close(sd);
875 channel_free(channel);
876 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100877 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100878
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879 ch_log(channel, "Connection made");
880
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100881 if (waittime >= 0)
882 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100883#ifdef _WIN32
884 val = 0;
885 ioctlsocket(sd, FIONBIO, &val);
886#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100887 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100888#endif
889 }
890
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100891 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100892 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100893 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
894 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100895
896#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100897 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100898#endif
899
Bram Moolenaar77073442016-02-13 23:23:53 +0100900 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100901}
902
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100903/*
904 * Implements ch_open().
905 */
906 channel_T *
907channel_open_func(typval_T *argvars)
908{
909 char_u *address;
910 char_u *p;
911 char *rest;
912 int port;
913 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200914 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100915
916 address = get_tv_string(&argvars[0]);
917 if (argvars[1].v_type != VAR_UNKNOWN
918 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
919 {
920 EMSG(_(e_invarg));
921 return NULL;
922 }
923
924 /* parse address */
925 p = vim_strchr(address, ':');
926 if (p == NULL)
927 {
928 EMSG2(_(e_invarg2), address);
929 return NULL;
930 }
931 *p++ = NUL;
932 port = strtol((char *)p, &rest, 10);
933 if (*address == NUL || port <= 0 || *rest != NUL)
934 {
935 p[-1] = ':';
936 EMSG2(_(e_invarg2), address);
937 return NULL;
938 }
939
940 /* parse options */
941 clear_job_options(&opt);
942 opt.jo_mode = MODE_JSON;
943 opt.jo_timeout = 2000;
944 if (get_job_options(&argvars[1], &opt,
945 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200946 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100947 if (opt.jo_timeout < 0)
948 {
949 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200950 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100951 }
952
953 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
954 if (channel != NULL)
955 {
956 opt.jo_set = JO_ALL;
957 channel_set_options(channel, &opt);
958 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200959theend:
960 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100961 return channel;
962}
963
Bram Moolenaarde279892016-03-11 22:19:44 +0100964 static void
965may_close_part(sock_T *fd)
966{
967 if (*fd != INVALID_FD)
968 {
969 fd_close(*fd);
970 *fd = INVALID_FD;
971 }
972}
973
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100974 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100975channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100976{
Bram Moolenaarde279892016-03-11 22:19:44 +0100977 if (in != INVALID_FD)
978 {
979 may_close_part(&channel->CH_IN_FD);
980 channel->CH_IN_FD = in;
981 }
982 if (out != INVALID_FD)
983 {
984# if defined(FEAT_GUI)
985 channel_gui_unregister_one(channel, PART_OUT);
986# endif
987 may_close_part(&channel->CH_OUT_FD);
988 channel->CH_OUT_FD = out;
989# if defined(FEAT_GUI)
990 channel_gui_register_one(channel, PART_OUT);
991# endif
992 }
993 if (err != INVALID_FD)
994 {
995# if defined(FEAT_GUI)
996 channel_gui_unregister_one(channel, PART_ERR);
997# endif
998 may_close_part(&channel->CH_ERR_FD);
999 channel->CH_ERR_FD = err;
1000# if defined(FEAT_GUI)
1001 channel_gui_register_one(channel, PART_ERR);
1002# endif
1003 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001004}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001005
Bram Moolenaard6051b52016-02-28 15:49:03 +01001006/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001007 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001008 * This does not keep a refcount, when the job is freed ch_job is cleared.
1009 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001011channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001012{
Bram Moolenaar77073442016-02-13 23:23:53 +01001013 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001014
1015 channel_set_options(channel, options);
1016
1017 if (job->jv_in_buf != NULL)
1018 {
1019 chanpart_T *in_part = &channel->ch_part[PART_IN];
1020
1021 in_part->ch_buffer = job->jv_in_buf;
1022 ch_logs(channel, "reading from buffer '%s'",
1023 (char *)in_part->ch_buffer->b_ffname);
1024 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001025 {
1026 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1027 {
1028 /* Special mode: send last-but-one line when appending a line
1029 * to the buffer. */
1030 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001031 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001032 in_part->ch_buf_top =
1033 in_part->ch_buffer->b_ml.ml_line_count + 1;
1034 }
1035 else
1036 in_part->ch_buf_top = options->jo_in_top;
1037 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038 else
1039 in_part->ch_buf_top = 1;
1040 if (options->jo_set & JO_IN_BOT)
1041 in_part->ch_buf_bot = options->jo_in_bot;
1042 else
1043 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1044 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001045}
1046
1047/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001048 * Find a buffer matching "name" or create a new one.
1049 */
1050 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001051find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001052{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001053 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001054 buf_T *save_curbuf = curbuf;
1055
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001056 if (name != NULL && *name != NUL)
1057 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001058 if (buf == NULL)
1059 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001060 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001061 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001062 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001063 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001064#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001065 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1066 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001067#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001068 if (curbuf->b_ml.ml_mfp == NULL)
1069 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001070 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1071 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 changed_bytes(1, 0);
1073 curbuf = save_curbuf;
1074 }
1075
1076 return buf;
1077}
1078
1079/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001080 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001081 */
1082 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001083channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001084{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001085 int part;
1086 char_u **cbp;
1087 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001088
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001089 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001090 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001091 channel->ch_part[part].ch_mode = opt->jo_mode;
1092 if (opt->jo_set & JO_IN_MODE)
1093 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1094 if (opt->jo_set & JO_OUT_MODE)
1095 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1096 if (opt->jo_set & JO_ERR_MODE)
1097 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001098
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001099 if (opt->jo_set & JO_TIMEOUT)
1100 for (part = PART_SOCK; part <= PART_IN; ++part)
1101 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1102 if (opt->jo_set & JO_OUT_TIMEOUT)
1103 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1104 if (opt->jo_set & JO_ERR_TIMEOUT)
1105 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001106 if (opt->jo_set & JO_BLOCK_WRITE)
1107 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001108
1109 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001110 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001113 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001114 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001115 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1116 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001117 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001119 *pp = opt->jo_partial;
1120 if (*pp != NULL)
1121 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001122 }
1123 if (opt->jo_set & JO_OUT_CALLBACK)
1124 {
1125 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001126 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001127 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001128 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001129 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1130 *cbp = vim_strsave(opt->jo_out_cb);
1131 else
1132 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001133 *pp = opt->jo_out_partial;
1134 if (*pp != NULL)
1135 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001136 }
1137 if (opt->jo_set & JO_ERR_CALLBACK)
1138 {
1139 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001140 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001141 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001142 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1144 *cbp = vim_strsave(opt->jo_err_cb);
1145 else
1146 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001147 *pp = opt->jo_err_partial;
1148 if (*pp != NULL)
1149 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001150 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001151 if (opt->jo_set & JO_CLOSE_CALLBACK)
1152 {
1153 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001155 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001156 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001157 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1158 *cbp = vim_strsave(opt->jo_close_cb);
1159 else
1160 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001161 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001162 if (*pp != NULL)
1163 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001164 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001165
1166 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1167 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001168 /* writing output to a buffer. Default mode is NL. */
1169 if (!(opt->jo_set & JO_OUT_MODE))
1170 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001171 if (opt->jo_set & JO_OUT_BUF)
1172 channel->ch_part[PART_OUT].ch_buffer =
1173 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1174 else
1175 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001176 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1177 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001178 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1179 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001180
1181 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1182 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1183 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1184 {
1185 /* writing err to a buffer. Default mode is NL. */
1186 if (!(opt->jo_set & JO_ERR_MODE))
1187 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1188 if (opt->jo_io[PART_ERR] == JIO_OUT)
1189 channel->ch_part[PART_ERR].ch_buffer =
1190 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001191 else if (opt->jo_set & JO_ERR_BUF)
1192 channel->ch_part[PART_ERR].ch_buffer =
1193 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001194 else
1195 channel->ch_part[PART_ERR].ch_buffer =
1196 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1197 ch_logs(channel, "writing err to buffer '%s'",
1198 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1199 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001200
1201 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1202 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1203 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001204}
1205
1206/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001207 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001208 */
1209 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001210channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001211 channel_T *channel,
1212 int part,
1213 char_u *callback,
1214 partial_T *partial,
1215 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001216{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001217 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001218 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1219
1220 if (item != NULL)
1221 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001222 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001223 item->cq_partial = partial;
1224 if (partial != NULL)
1225 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001226 item->cq_seq_nr = id;
1227 item->cq_prev = head->cq_prev;
1228 head->cq_prev = item;
1229 item->cq_next = NULL;
1230 if (item->cq_prev == NULL)
1231 head->cq_next = item;
1232 else
1233 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001234 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001235}
1236
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001237 static void
1238write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1239{
1240 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001241 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001242 char_u *p;
1243
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001244 if ((p = alloc(len + 2)) == NULL)
1245 return;
1246 STRCPY(p, line);
1247 p[len] = NL;
1248 p[len + 1] = NUL;
1249 channel_send(channel, PART_IN, p, "write_buf_line()");
1250 vim_free(p);
1251}
1252
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001253/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001254 * Return TRUE if "channel" can be written to.
1255 * Returns FALSE if the input is closed or the write would block.
1256 */
1257 static int
1258can_write_buf_line(channel_T *channel)
1259{
1260 chanpart_T *in_part = &channel->ch_part[PART_IN];
1261
1262 if (in_part->ch_fd == INVALID_FD)
1263 return FALSE; /* pipe was closed */
1264
1265 /* for testing: block every other attempt to write */
1266 if (in_part->ch_block_write == 1)
1267 in_part->ch_block_write = -1;
1268 else if (in_part->ch_block_write == -1)
1269 in_part->ch_block_write = 1;
1270
1271 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1272#ifndef WIN32
1273 {
1274# if defined(HAVE_SELECT)
1275 struct timeval tval;
1276 fd_set wfds;
1277 int ret;
1278
1279 FD_ZERO(&wfds);
1280 FD_SET((int)in_part->ch_fd, &wfds);
1281 tval.tv_sec = 0;
1282 tval.tv_usec = 0;
1283 for (;;)
1284 {
1285 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1286# ifdef EINTR
1287 SOCK_ERRNO;
1288 if (ret == -1 && errno == EINTR)
1289 continue;
1290# endif
1291 if (ret <= 0 || in_part->ch_block_write == 1)
1292 {
1293 if (ret > 0)
1294 ch_log(channel, "FAKED Input not ready for writing");
1295 else
1296 ch_log(channel, "Input not ready for writing");
1297 return FALSE;
1298 }
1299 break;
1300 }
1301# else
1302 struct pollfd fds;
1303
1304 fds.fd = in_part->ch_fd;
1305 fds.events = POLLOUT;
1306 if (poll(&fds, 1, 0) <= 0)
1307 {
1308 ch_log(channel, "Input not ready for writing");
1309 return FALSE;
1310 }
1311 if (in_part->ch_block_write == 1)
1312 {
1313 ch_log(channel, "FAKED Input not ready for writing");
1314 return FALSE;
1315 }
1316# endif
1317 }
1318#endif
1319 return TRUE;
1320}
1321
1322/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001323 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001324 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001325 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001326channel_write_in(channel_T *channel)
1327{
1328 chanpart_T *in_part = &channel->ch_part[PART_IN];
1329 linenr_T lnum;
1330 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001331 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001332
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001333 if (buf == NULL || in_part->ch_buf_append)
1334 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001335 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1336 {
1337 /* buffer was wiped out or unloaded */
1338 in_part->ch_buffer = NULL;
1339 return;
1340 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001341
1342 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1343 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1344 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001345 if (!can_write_buf_line(channel))
1346 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001347 write_buf_line(buf, lnum, channel);
1348 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001349 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001350
1351 if (written == 1)
1352 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1353 else if (written > 1)
1354 ch_logn(channel, "written %d lines to channel", written);
1355
Bram Moolenaar014069a2016-03-03 22:51:40 +01001356 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001357 if (lnum > buf->b_ml.ml_line_count)
1358 {
1359 /* Writing is done, no longer need the buffer. */
1360 in_part->ch_buffer = NULL;
1361 ch_log(channel, "Finished writing all lines to channel");
1362 }
1363 else
1364 ch_logn(channel, "Still %d more lines to write",
1365 buf->b_ml.ml_line_count - lnum + 1);
1366}
1367
1368/*
1369 * Write any lines waiting to be written to a channel.
1370 */
1371 void
1372channel_write_any_lines()
1373{
1374 channel_T *channel;
1375
1376 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1377 {
1378 chanpart_T *in_part = &channel->ch_part[PART_IN];
1379
1380 if (in_part->ch_buffer != NULL)
1381 {
1382 if (in_part->ch_buf_append)
1383 channel_write_new_lines(in_part->ch_buffer);
1384 else
1385 channel_write_in(channel);
1386 }
1387 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001388}
1389
1390/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 * Write appended lines above the last one in "buf" to the channel.
1392 */
1393 void
1394channel_write_new_lines(buf_T *buf)
1395{
1396 channel_T *channel;
1397 int found_one = FALSE;
1398
1399 /* There could be more than one channel for the buffer, loop over all of
1400 * them. */
1401 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1402 {
1403 chanpart_T *in_part = &channel->ch_part[PART_IN];
1404 linenr_T lnum;
1405 int written = 0;
1406
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001408 {
1409 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001410 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 found_one = TRUE;
1412 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1413 ++lnum)
1414 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 if (!can_write_buf_line(channel))
1416 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 write_buf_line(buf, lnum, channel);
1418 ++written;
1419 }
1420
1421 if (written == 1)
1422 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1423 else if (written > 1)
1424 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001425 if (lnum < buf->b_ml.ml_line_count)
1426 ch_logn(channel, "Still %d more lines to write",
1427 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001428
1429 in_part->ch_buf_bot = lnum;
1430 }
1431 }
1432 if (!found_one)
1433 buf->b_write_to_channel = FALSE;
1434}
1435
1436/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001437 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001438 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001439 */
1440 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001441invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1442 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001443{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001444 typval_T rettv;
1445 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001446
Bram Moolenaar77073442016-02-13 23:23:53 +01001447 argv[0].v_type = VAR_CHANNEL;
1448 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001449
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001450 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001451 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001452 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001453 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001454}
1455
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001456/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001457 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001458 * The caller must free it.
1459 * Returns NULL if there is nothing.
1460 */
1461 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001462channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001463{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001464 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001465 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 char_u *p;
1467
Bram Moolenaar77073442016-02-13 23:23:53 +01001468 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001469 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001470 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001471 p = node->rq_buffer;
1472 head->rq_next = node->rq_next;
1473 if (node->rq_next == NULL)
1474 head->rq_prev = NULL;
1475 else
1476 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001477 vim_free(node);
1478 return p;
1479}
1480
1481/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001482 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001483 */
1484 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001485channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001486{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001487 readq_T *head = &channel->ch_part[part].ch_head;
1488 readq_T *node = head->rq_next;
1489 long_u len = 1;
1490 char_u *res;
1491 char_u *p;
1492
1493 /* If there is only one buffer just get that one. */
1494 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1495 return channel_get(channel, part);
1496
1497 /* Concatenate everything into one buffer. */
1498 for (node = head->rq_next; node != NULL; node = node->rq_next)
1499 len += (long_u)STRLEN(node->rq_buffer);
1500 res = lalloc(len, TRUE);
1501 if (res == NULL)
1502 return NULL;
1503 *res = NUL;
1504 for (node = head->rq_next; node != NULL; node = node->rq_next)
1505 STRCAT(res, node->rq_buffer);
1506
1507 /* Free all buffers */
1508 do
1509 {
1510 p = channel_get(channel, part);
1511 vim_free(p);
1512 } while (p != NULL);
1513
1514 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001515}
1516
1517/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001518 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001519 * Returns FAIL if that is not possible.
1520 */
1521 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001522channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001523{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001524 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001525 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001526 char_u *p;
1527
Bram Moolenaar77073442016-02-13 23:23:53 +01001528 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529 return FAIL;
1530
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1532 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533 if (p == NULL)
1534 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 STRCPY(p, node->rq_buffer);
1536 STRCAT(p, node->rq_next->rq_buffer);
1537 vim_free(node->rq_next->rq_buffer);
1538 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539
Bram Moolenaar77073442016-02-13 23:23:53 +01001540 /* dispose of the node and its buffer */
1541 head->rq_next = node->rq_next;
1542 head->rq_next->rq_prev = NULL;
1543 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001544 vim_free(node);
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001550 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001551 * Returns OK or FAIL.
1552 */
1553 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001554channel_save(channel_T *channel, int part, char_u *buf, int len,
1555 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556{
1557 readq_T *node;
1558 readq_T *head = &channel->ch_part[part].ch_head;
1559 char_u *p;
1560 int i;
1561
1562 node = (readq_T *)alloc(sizeof(readq_T));
1563 if (node == NULL)
1564 return FAIL; /* out of memory */
1565 node->rq_buffer = alloc(len + 1);
1566 if (node->rq_buffer == NULL)
1567 {
1568 vim_free(node);
1569 return FAIL; /* out of memory */
1570 }
1571
1572 if (channel->ch_part[part].ch_mode == MODE_NL)
1573 {
1574 /* Drop any CR before a NL. */
1575 p = node->rq_buffer;
1576 for (i = 0; i < len; ++i)
1577 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1578 *p++ = buf[i];
1579 *p = NUL;
1580 }
1581 else
1582 {
1583 mch_memmove(node->rq_buffer, buf, len);
1584 node->rq_buffer[len] = NUL;
1585 }
1586
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001587 if (prepend)
1588 {
1589 /* preend node to the head of the queue */
1590 node->rq_next = head->rq_next;
1591 node->rq_prev = NULL;
1592 if (head->rq_next == NULL)
1593 head->rq_prev = node;
1594 else
1595 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001597 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001598 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001599 {
1600 /* append node to the tail of the queue */
1601 node->rq_next = NULL;
1602 node->rq_prev = head->rq_prev;
1603 if (head->rq_prev == NULL)
1604 head->rq_next = node;
1605 else
1606 head->rq_prev->rq_next = node;
1607 head->rq_prev = node;
1608 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001610 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001612 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001613 fprintf(log_fd, "'");
1614 if (fwrite(buf, len, 1, log_fd) != 1)
1615 return FAIL;
1616 fprintf(log_fd, "'\n");
1617 }
1618 return OK;
1619}
1620
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001621 static int
1622channel_fill(js_read_T *reader)
1623{
1624 channel_T *channel = (channel_T *)reader->js_cookie;
1625 int part = reader->js_cookie_arg;
1626 char_u *next = channel_get(channel, part);
1627 int unused;
1628 int len;
1629 char_u *p;
1630
1631 if (next == NULL)
1632 return FALSE;
1633
1634 unused = reader->js_end - reader->js_buf - reader->js_used;
1635 if (unused > 0)
1636 {
1637 /* Prepend unused text. */
1638 len = (int)STRLEN(next);
1639 p = alloc(unused + len + 1);
1640 if (p == NULL)
1641 {
1642 vim_free(next);
1643 return FALSE;
1644 }
1645 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1646 mch_memmove(p + unused, next, len + 1);
1647 vim_free(next);
1648 next = p;
1649 }
1650
1651 vim_free(reader->js_buf);
1652 reader->js_buf = next;
1653 reader->js_used = 0;
1654 return TRUE;
1655}
1656
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001657/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001658 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001660 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001661 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001662 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001663channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001664{
1665 js_read_T reader;
1666 typval_T listtv;
1667 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001668 chanpart_T *chanpart = &channel->ch_part[part];
1669 jsonq_T *head = &chanpart->ch_json_head;
1670 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001671 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001673 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001674 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001678 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001679 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001680 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001681
1682 /* When a message is incomplete we wait for a short while for more to
1683 * arrive. After the delay drop the input, otherwise a truncated string
1684 * or list will make us hang. */
1685 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001686 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001687 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001688 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001689 /* Only accept the response when it is a list with at least two
1690 * items. */
1691 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001692 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001693 if (listtv.v_type != VAR_LIST)
1694 ch_error(channel, "Did not receive a list, discarding");
1695 else
1696 ch_errorn(channel, "Expected list with two items, got %d",
1697 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001699 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 else
1701 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001702 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1703 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001704 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705 else
1706 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001707 item->jq_value = alloc_tv();
1708 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001709 {
1710 vim_free(item);
1711 clear_tv(&listtv);
1712 }
1713 else
1714 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 *item->jq_value = listtv;
1716 item->jq_prev = head->jq_prev;
1717 head->jq_prev = item;
1718 item->jq_next = NULL;
1719 if (item->jq_prev == NULL)
1720 head->jq_next = item;
1721 else
1722 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001723 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001724 }
1725 }
1726 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001727
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 if (status == OK)
1729 chanpart->ch_waiting = FALSE;
1730 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001731 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001732 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001733 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001734 /* First time encountering incomplete message, set a deadline of
1735 * 100 msec. */
1736 ch_log(channel, "Incomplete message - wait for more");
1737 reader.js_used = 0;
1738 chanpart->ch_waiting = TRUE;
1739#ifdef WIN32
1740 chanpart->ch_deadline = GetTickCount() + 100L;
1741#else
1742 gettimeofday(&chanpart->ch_deadline, NULL);
1743 chanpart->ch_deadline.tv_usec += 100 * 1000;
1744 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1745 {
1746 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1747 ++chanpart->ch_deadline.tv_sec;
1748 }
1749#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001750 }
1751 else
1752 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001753 int timeout;
1754#ifdef WIN32
1755 timeout = GetTickCount() > chanpart->ch_deadline;
1756#else
1757 {
1758 struct timeval now_tv;
1759
1760 gettimeofday(&now_tv, NULL);
1761 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1762 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1763 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1764 }
1765#endif
1766 if (timeout)
1767 {
1768 status = FAIL;
1769 chanpart->ch_waiting = FALSE;
1770 }
1771 else
1772 {
1773 reader.js_used = 0;
1774 ch_log(channel, "still waiting on incomplete message");
1775 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001776 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001777 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001778
1779 if (status == FAIL)
1780 {
1781 ch_error(channel, "Decoding failed - discarding input");
1782 ret = FALSE;
1783 chanpart->ch_waiting = FALSE;
1784 }
1785 else if (reader.js_buf[reader.js_used] != NUL)
1786 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001788 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001789 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1790 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001791 ret = status == MAYBE ? FALSE: TRUE;
1792 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 else
1794 ret = FALSE;
1795
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001796 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001797 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798}
1799
1800/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001801 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001802 */
1803 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001804remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001805{
Bram Moolenaar77073442016-02-13 23:23:53 +01001806 if (node->cq_prev == NULL)
1807 head->cq_next = node->cq_next;
1808 else
1809 node->cq_prev->cq_next = node->cq_next;
1810 if (node->cq_next == NULL)
1811 head->cq_prev = node->cq_prev;
1812 else
1813 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001814}
1815
1816/*
1817 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001818 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001819 */
1820 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001821remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001822{
Bram Moolenaar77073442016-02-13 23:23:53 +01001823 if (node->jq_prev == NULL)
1824 head->jq_next = node->jq_next;
1825 else
1826 node->jq_prev->jq_next = node->jq_next;
1827 if (node->jq_next == NULL)
1828 head->jq_prev = node->jq_prev;
1829 else
1830 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001831 vim_free(node);
1832}
1833
1834/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001835 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001837 * When "id" is zero or negative jut get the first message. But not the one
1838 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001839 * Return OK when found and return the value in "rettv".
1840 * Return FAIL otherwise.
1841 */
1842 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001845 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847
Bram Moolenaar77073442016-02-13 23:23:53 +01001848 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001850 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001851 typval_T *tv = &l->lv_first->li_tv;
1852
1853 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001854 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001855 || tv->vval.v_number == 0
1856 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001858 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001859 if (tv->v_type == VAR_NUMBER)
1860 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001861 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 return OK;
1863 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001864 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001865 }
1866 return FAIL;
1867}
1868
Bram Moolenaarece61b02016-02-20 21:39:05 +01001869#define CH_JSON_MAX_ARGS 4
1870
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001871/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001873 * "argv[0]" is the command string.
1874 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001875 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001876 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001877channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001878{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001879 char_u *cmd = argv[0].vval.v_string;
1880 char_u *arg;
1881 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001886 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001887 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001888 return;
1889 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001890 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001891 if (arg == NULL)
1892 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001893
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001894 if (STRCMP(cmd, "ex") == 0)
1895 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001896 int save_called_emsg = called_emsg;
1897
1898 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001899 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001900 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001901 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001902 --emsg_silent;
1903 if (called_emsg)
1904 ch_logs(channel, "Ex command error: '%s'",
1905 (char *)get_vim_var_str(VV_ERRMSG));
1906 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001907 }
1908 else if (STRCMP(cmd, "normal") == 0)
1909 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001910 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001911
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001912 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001913 ea.arg = arg;
1914 ea.addr_count = 0;
1915 ea.forceit = TRUE; /* no mapping */
1916 ex_normal(&ea);
1917 }
1918 else if (STRCMP(cmd, "redraw") == 0)
1919 {
1920 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001921
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001922 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001923 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001924 ex_redraw(&ea);
1925 showruler(FALSE);
1926 setcursor();
1927 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001928#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001929 if (gui.in_use)
1930 {
1931 gui_update_cursor(FALSE, FALSE);
1932 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001933 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001934#endif
1935 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001937 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001938 int is_call = cmd[0] == 'c';
1939 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001940
Bram Moolenaarece61b02016-02-20 21:39:05 +01001941 if (argv[id_idx].v_type != VAR_UNKNOWN
1942 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001943 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001945 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001946 EMSG("E904: last argument for expr/call must be a number");
1947 }
1948 else if (is_call && argv[2].v_type != VAR_LIST)
1949 {
1950 ch_error(channel, "third argument for call must be a list");
1951 if (p_verbose > 2)
1952 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001953 }
1954 else
1955 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001956 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001959 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001960
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001961 /* Don't pollute the display with errors. */
1962 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001963 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001964 {
1965 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001966 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001967 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001968 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 {
1970 ch_logs(channel, "Calling '%s'", (char *)arg);
1971 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1972 tv = &res_tv;
1973 else
1974 tv = NULL;
1975 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001976
1977 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001978 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001979 int id = argv[id_idx].vval.v_number;
1980
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001982 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001983 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001984 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001985 /* If evaluation failed or the result can't be encoded
1986 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001987 vim_free(json);
1988 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001989 err_tv.v_type = VAR_STRING;
1990 err_tv.vval.v_string = (char_u *)"ERROR";
1991 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001992 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001993 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001994 if (json != NULL)
1995 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001996 channel_send(channel,
1997 part == PART_SOCK ? PART_SOCK : PART_IN,
1998 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001999 vim_free(json);
2000 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002001 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002002 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002003 if (tv == &res_tv)
2004 clear_tv(tv);
2005 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002006 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002008 }
2009 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002010 {
2011 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002012 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002013 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002014}
2015
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002016/*
2017 * Invoke the callback at "cbhead".
2018 * Does not redraw but sets channel_need_redraw.
2019 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002020 static void
2021invoke_one_time_callback(
2022 channel_T *channel,
2023 cbq_T *cbhead,
2024 cbq_T *item,
2025 typval_T *argv)
2026{
2027 ch_logs(channel, "Invoking one-time callback %s",
2028 (char *)item->cq_callback);
2029 /* Remove the item from the list first, if the callback
2030 * invokes ch_close() the list will be cleared. */
2031 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002032 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002033 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002034 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002035 vim_free(item);
2036}
2037
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002038 static void
2039append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2040{
2041 buf_T *save_curbuf = curbuf;
2042 linenr_T lnum = buffer->b_ml.ml_line_count;
2043 int save_write_to = buffer->b_write_to_channel;
2044
2045 /* If the buffer is also used as input insert above the last
2046 * line. Don't write these lines. */
2047 if (save_write_to)
2048 {
2049 --lnum;
2050 buffer->b_write_to_channel = FALSE;
2051 }
2052
2053 /* Append to the buffer */
2054 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2055
2056 curbuf = buffer;
2057 u_sync(TRUE);
2058 /* ignore undo failure, undo is not very useful here */
2059 ignored = u_save(lnum, lnum + 1);
2060
2061 ml_append(lnum, msg, 0, FALSE);
2062 appended_lines_mark(lnum, 1L);
2063 curbuf = save_curbuf;
2064
2065 if (buffer->b_nwindows > 0)
2066 {
2067 win_T *wp;
2068 win_T *save_curwin;
2069
2070 FOR_ALL_WINDOWS(wp)
2071 {
2072 if (wp->w_buffer == buffer
2073 && (save_write_to
2074 ? wp->w_cursor.lnum == lnum + 1
2075 : (wp->w_cursor.lnum == lnum
2076 && wp->w_cursor.col == 0)))
2077 {
2078 ++wp->w_cursor.lnum;
2079 save_curwin = curwin;
2080 curwin = wp;
2081 curbuf = curwin->w_buffer;
2082 scroll_cursor_bot(0, FALSE);
2083 curwin = save_curwin;
2084 curbuf = curwin->w_buffer;
2085 }
2086 }
2087 redraw_buf_later(buffer, VALID);
2088 channel_need_redraw = TRUE;
2089 }
2090
2091 if (save_write_to)
2092 {
2093 channel_T *ch;
2094
2095 /* Find channels reading from this buffer and adjust their
2096 * next-to-read line number. */
2097 buffer->b_write_to_channel = TRUE;
2098 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2099 {
2100 chanpart_T *in_part = &ch->ch_part[PART_IN];
2101
2102 if (in_part->ch_buffer == buffer)
2103 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2104 }
2105 }
2106}
2107
Bram Moolenaar437905c2016-04-26 19:01:05 +02002108 static void
2109drop_messages(channel_T *channel, int part)
2110{
2111 char_u *msg;
2112
2113 while ((msg = channel_get(channel, part)) != NULL)
2114 {
2115 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2116 vim_free(msg);
2117 }
2118}
2119
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002120/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002121 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002122 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002123 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002124 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002125 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002126may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002127{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 char_u *msg = NULL;
2129 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002131 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002132 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002133 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002134 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002135 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002136 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002137 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002139 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002140 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002141 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002142
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002143 /* Use a message-specific callback, part callback or channel callback */
2144 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2145 if (cbitem->cq_seq_nr == 0)
2146 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002147 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002148 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002149 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002150 partial = cbitem->cq_partial;
2151 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002152 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002153 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002154 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002155 partial = channel->ch_part[part].ch_partial;
2156 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002158 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002159 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002160 partial = channel->ch_partial;
2161 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002162
Bram Moolenaar187db502016-02-27 14:44:26 +01002163 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002164 if (buffer != NULL && !buf_valid(buffer))
2165 {
2166 /* buffer was wiped out */
2167 channel->ch_part[part].ch_buffer = NULL;
2168 buffer = NULL;
2169 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002170
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002171 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002172 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002173 listitem_T *item;
2174 int argc = 0;
2175
Bram Moolenaard7ece102016-02-02 23:23:02 +01002176 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002177 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002178 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002179 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002180 channel_parse_json(channel, part);
2181 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002182 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002183 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002184
Bram Moolenaarece61b02016-02-20 21:39:05 +01002185 for (item = listtv->vval.v_list->lv_first;
2186 item != NULL && argc < CH_JSON_MAX_ARGS;
2187 item = item->li_next)
2188 argv[argc++] = item->li_tv;
2189 while (argc < CH_JSON_MAX_ARGS)
2190 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002191
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 if (argv[0].v_type == VAR_STRING)
2193 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002194 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002195 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002196 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002197 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002198 }
2199
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002201 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002202 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002203 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002204 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002205 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002206 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002207 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002209 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002210 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002211 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002212 return FALSE;
2213 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002214 else
2215 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002216 /* If there is no callback or buffer drop the message. */
2217 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002218 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002219 /* If there is a close callback it may use ch_read() to get the
2220 * messages. */
2221 if (channel->ch_close_cb == NULL)
2222 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002223 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002224 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002225
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002226 if (ch_mode == MODE_NL)
2227 {
2228 char_u *nl;
2229 char_u *buf;
2230
2231 /* See if we have a message ending in NL in the first buffer. If
2232 * not try to concatenate the first and the second buffer. */
2233 while (TRUE)
2234 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002235 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002236 nl = vim_strchr(buf, NL);
2237 if (nl != NULL)
2238 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002239 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002240 return FALSE; /* incomplete message */
2241 }
2242 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002243 {
2244 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002245 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002246 *nl = NUL;
2247 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002248 else
2249 {
2250 /* Copy the message into allocated memory and remove it from
2251 * the buffer. */
2252 msg = vim_strnsave(buf, (int)(nl - buf));
2253 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2254 }
2255 }
2256 else
2257 /* For a raw channel we don't know where the message ends, just
2258 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002259 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002260
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002261 if (msg == NULL)
2262 return FALSE; /* out of memory (and avoids Coverity warning) */
2263
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002264 argv[1].v_type = VAR_STRING;
2265 argv[1].vval.v_string = msg;
2266 }
2267
Bram Moolenaara07fec92016-02-05 21:04:08 +01002268 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002269 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002270 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002271
2272 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002273 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002274 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002275 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002276 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002277 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002278 break;
2279 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002280 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002281 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002282 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002283 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002284 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002285 if (buffer != NULL)
2286 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002287 if (msg == NULL)
2288 /* JSON or JS mode: re-encode the message. */
2289 msg = json_encode(listtv, ch_mode);
2290 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002291 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002292 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002293
Bram Moolenaar187db502016-02-27 14:44:26 +01002294 if (callback != NULL)
2295 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002296 if (cbitem != NULL)
2297 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2298 else
2299 {
2300 /* invoke the channel callback */
2301 ch_logs(channel, "Invoking channel callback %s",
2302 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002303 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002304 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002305 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002306 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002307 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002308 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002309
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002310 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002311 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002312 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002313
2314 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002315}
2316
2317/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002318 * Return TRUE when channel "channel" is open for writing to.
2319 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002320 */
2321 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002322channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002323{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002325 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002326}
2327
2328/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002329 * Return TRUE when channel "channel" is open for reading or writing.
2330 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002331 */
2332 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002333channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002334{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002335 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002336 || channel->CH_IN_FD != INVALID_FD
2337 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002338 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002339}
2340
2341/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002342 * Return TRUE if "channel" has JSON or other typeahead.
2343 */
2344 static int
2345channel_has_readahead(channel_T *channel, int part)
2346{
2347 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2348
2349 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2350 {
2351 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2352 jsonq_T *item = head->jq_next;
2353
2354 return item != NULL;
2355 }
2356 return channel_peek(channel, part) != NULL;
2357}
2358
2359/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002360 * Return a string indicating the status of the channel.
2361 */
2362 char *
2363channel_status(channel_T *channel)
2364{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002365 int part;
2366 int has_readahead = FALSE;
2367
Bram Moolenaar77073442016-02-13 23:23:53 +01002368 if (channel == NULL)
2369 return "fail";
2370 if (channel_is_open(channel))
2371 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002372 for (part = PART_SOCK; part <= PART_ERR; ++part)
2373 if (channel_has_readahead(channel, part))
2374 {
2375 has_readahead = TRUE;
2376 break;
2377 }
2378
2379 if (has_readahead)
2380 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002381 return "closed";
2382}
2383
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002384 static void
2385channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2386{
2387 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002388 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002389 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002390 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002391
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002392 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002393 STRCAT(namebuf, "_");
2394 tail = STRLEN(namebuf);
2395
2396 STRCPY(namebuf + tail, "status");
2397 dict_add_nr_str(dict, namebuf, 0,
2398 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2399
2400 STRCPY(namebuf + tail, "mode");
2401 switch (chanpart->ch_mode)
2402 {
2403 case MODE_NL: s = "NL"; break;
2404 case MODE_RAW: s = "RAW"; break;
2405 case MODE_JSON: s = "JSON"; break;
2406 case MODE_JS: s = "JS"; break;
2407 }
2408 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2409
2410 STRCPY(namebuf + tail, "io");
2411 if (part == PART_SOCK)
2412 s = "socket";
2413 else switch (chanpart->ch_io)
2414 {
2415 case JIO_NULL: s = "null"; break;
2416 case JIO_PIPE: s = "pipe"; break;
2417 case JIO_FILE: s = "file"; break;
2418 case JIO_BUFFER: s = "buffer"; break;
2419 case JIO_OUT: s = "out"; break;
2420 }
2421 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2422
2423 STRCPY(namebuf + tail, "timeout");
2424 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2425}
2426
2427 void
2428channel_info(channel_T *channel, dict_T *dict)
2429{
2430 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2431 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2432
2433 if (channel->ch_hostname != NULL)
2434 {
2435 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2436 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2437 channel_part_info(channel, dict, "sock", PART_SOCK);
2438 }
2439 else
2440 {
2441 channel_part_info(channel, dict, "out", PART_OUT);
2442 channel_part_info(channel, dict, "err", PART_ERR);
2443 channel_part_info(channel, dict, "in", PART_IN);
2444 }
2445}
2446
Bram Moolenaar77073442016-02-13 23:23:53 +01002447/*
2448 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002449 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002450 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002451 */
2452 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002453channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002454{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002455 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002456
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002457#ifdef FEAT_GUI
2458 channel_gui_unregister(channel);
2459#endif
2460
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002462 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002463 sock_close(channel->CH_SOCK_FD);
2464 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002465 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002466 may_close_part(&channel->CH_IN_FD);
2467 may_close_part(&channel->CH_OUT_FD);
2468 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002469
Bram Moolenaar8b374212016-02-24 20:43:06 +01002470 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002471 {
2472 typval_T argv[1];
2473 typval_T rettv;
2474 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002475 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002476
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002477 /* Invoke callbacks before the close callback, since it's weird to
2478 * first invoke the close callback. Increment the refcount to avoid
2479 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002480 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002481 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002482 for (part = PART_SOCK; part <= PART_ERR; ++part)
2483 while (may_invoke_callback(channel, part))
2484 ;
2485
2486 /* Invoke the close callback, if still set. */
2487 if (channel->ch_close_cb != NULL)
2488 {
2489 ch_logs(channel, "Invoking close callback %s",
2490 (char *)channel->ch_close_cb);
2491 argv[0].v_type = VAR_CHANNEL;
2492 argv[0].vval.v_channel = channel;
2493 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002494 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2495 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002496 clear_tv(&rettv);
2497 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002498 --channel->ch_refcount;
2499
2500 /* the callback is only called once */
2501 vim_free(channel->ch_close_cb);
2502 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002503 partial_unref(channel->ch_close_partial);
2504 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002505
2506 /* any remaining messages are useless now */
2507 for (part = PART_SOCK; part <= PART_ERR; ++part)
2508 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002509 }
2510
2511 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002512}
2513
Bram Moolenaard04a0202016-01-26 23:30:18 +01002514/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002515 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002516 * Returns NULL if there is nothing.
2517 */
2518 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002519channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002520{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002521 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002522
Bram Moolenaar77073442016-02-13 23:23:53 +01002523 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002524 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002525 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002526}
2527
2528/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002529 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002530 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 static void
2532channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2535 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002536
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002537 while (channel_peek(channel, part) != NULL)
2538 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002539
2540 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002541 {
2542 cbq_T *node = cb_head->cq_next;
2543
2544 remove_cb_node(cb_head, node);
2545 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002546 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002547 vim_free(node);
2548 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002549
2550 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002551 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002552 free_tv(json_head->jq_next->jq_value);
2553 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002554 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002555
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002556 vim_free(channel->ch_part[part].ch_callback);
2557 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002558 partial_unref(channel->ch_part[part].ch_partial);
2559 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002560}
2561
2562/*
2563 * Clear all the read buffers on "channel".
2564 */
2565 void
2566channel_clear(channel_T *channel)
2567{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002568 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002569 vim_free(channel->ch_hostname);
2570 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002571 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002572 channel_clear_one(channel, PART_OUT);
2573 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002574 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002575 vim_free(channel->ch_callback);
2576 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002577 partial_unref(channel->ch_partial);
2578 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002579 vim_free(channel->ch_close_cb);
2580 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002581 partial_unref(channel->ch_close_partial);
2582 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002583}
2584
Bram Moolenaar77073442016-02-13 23:23:53 +01002585#if defined(EXITFREE) || defined(PROTO)
2586 void
2587channel_free_all(void)
2588{
2589 channel_T *channel;
2590
Bram Moolenaard6051b52016-02-28 15:49:03 +01002591 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002592 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2593 channel_clear(channel);
2594}
2595#endif
2596
2597
Bram Moolenaar715d2852016-04-30 17:06:31 +02002598/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002599#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002600
2601/* Buffer size for reading incoming messages. */
2602#define MAXMSGSIZE 4096
2603
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002604#if defined(HAVE_SELECT)
2605/*
2606 * Add write fds where we are waiting for writing to be possible.
2607 */
2608 static int
2609channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2610{
2611 int maxfd = maxfd_arg;
2612 channel_T *ch;
2613
2614 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2615 {
2616 chanpart_T *in_part = &ch->ch_part[PART_IN];
2617
2618 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2619 {
2620 FD_SET((int)in_part->ch_fd, wfds);
2621 if ((int)in_part->ch_fd >= maxfd)
2622 maxfd = (int)in_part->ch_fd + 1;
2623 }
2624 }
2625 return maxfd;
2626}
2627#else
2628/*
2629 * Add write fds where we are waiting for writing to be possible.
2630 */
2631 static int
2632channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2633{
2634 int nfd = nfd_in;
2635 channel_T *ch;
2636
2637 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2638 {
2639 chanpart_T *in_part = &ch->ch_part[PART_IN];
2640
2641 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2642 {
2643 in_part->ch_poll_idx = nfd;
2644 fds[nfd].fd = in_part->ch_fd;
2645 fds[nfd].events = POLLOUT;
2646 ++nfd;
2647 }
2648 else
2649 in_part->ch_poll_idx = -1;
2650 }
2651 return nfd;
2652}
2653#endif
2654
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002655typedef enum {
2656 CW_READY,
2657 CW_NOT_READY,
2658 CW_ERROR
2659} channel_wait_result;
2660
Bram Moolenaard04a0202016-01-26 23:30:18 +01002661/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002662 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002663 * Return CW_READY when there is something to read.
2664 * Return CW_NOT_READY when there is nothing to read.
2665 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002666 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002667 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002668channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002669{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002670 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002671 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002672
Bram Moolenaard8070362016-02-15 21:56:54 +01002673# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002675 {
2676 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002677 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002678 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002679 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002680
2681 /* reading from a pipe, not a socket */
2682 while (TRUE)
2683 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002684 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2685
2686 if (r && nread > 0)
2687 return CW_READY;
2688 if (r == 0)
2689 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002690
2691 /* perhaps write some buffer lines */
2692 channel_write_any_lines();
2693
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002694 sleep_time = deadline - GetTickCount();
2695 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002696 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002697 /* Wait for a little while. Very short at first, up to 10 msec
2698 * after looping a few times. */
2699 if (sleep_time > delay)
2700 sleep_time = delay;
2701 Sleep(sleep_time);
2702 delay = delay * 2;
2703 if (delay > 10)
2704 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002705 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002706 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002707 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002708#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002709 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002710#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002711 struct timeval tval;
2712 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002713 fd_set wfds;
2714 int ret;
2715 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002716
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002717 tval.tv_sec = timeout / 1000;
2718 tval.tv_usec = (timeout % 1000) * 1000;
2719 for (;;)
2720 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002721 FD_ZERO(&rfds);
2722 FD_SET((int)fd, &rfds);
2723
2724 /* Write lines to a pipe when a pipe can be written to. Need to
2725 * set this every time, some buffers may be done. */
2726 maxfd = (int)fd + 1;
2727 FD_ZERO(&wfds);
2728 maxfd = channel_fill_wfds(maxfd, &wfds);
2729
2730 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002731# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002732 SOCK_ERRNO;
2733 if (ret == -1 && errno == EINTR)
2734 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002735# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002736 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002737 {
2738 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002739 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002740 channel_write_any_lines();
2741 continue;
2742 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002743 break;
2744 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002745#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002746 for (;;)
2747 {
2748 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2749 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002750
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002751 fds[0].fd = fd;
2752 fds[0].events = POLLIN;
2753 nfd = channel_fill_poll_write(nfd, fds);
2754 if (poll(fds, nfd, timeout) > 0)
2755 {
2756 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002757 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002758 channel_write_any_lines();
2759 continue;
2760 }
2761 break;
2762 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002763#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002764 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002765 return CW_NOT_READY;
2766}
2767
2768 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002769channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002770{
2771 /* Do not call emsg(), most likely the other end just exited. */
2772 ch_errors(channel, "%s(): Cannot read from channel", func);
2773
2774 /* Queue a "DETACH" netbeans message in the command queue in order to
2775 * terminate the netbeans session later. Do not end the session here
2776 * directly as we may be running in the context of a call to
2777 * netbeans_parse_messages():
2778 * netbeans_parse_messages
2779 * -> autocmd triggered while processing the netbeans cmd
2780 * -> ui_breakcheck
2781 * -> gui event loop or select loop
2782 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002783 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002784 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002785 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002786 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002787 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2788
2789 /* When reading from stdout is not possible, assume the other side has
2790 * died. */
2791 channel_close(channel, TRUE);
2792 if (channel->ch_nb_close_cb != NULL)
2793 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002794}
2795
2796/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002797 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002798 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002799 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002800 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002801 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002802channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002803{
2804 static char_u *buf = NULL;
2805 int len = 0;
2806 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002807 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002808 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002809
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002810 fd = channel->ch_part[part].ch_fd;
2811 if (fd == INVALID_FD)
2812 {
2813 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002814 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002815 }
2816 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817
2818 /* Allocate a buffer to read into. */
2819 if (buf == NULL)
2820 {
2821 buf = alloc(MAXMSGSIZE);
2822 if (buf == NULL)
2823 return; /* out of memory! */
2824 }
2825
2826 /* Keep on reading for as long as there is something to read.
2827 * Use select() or poll() to avoid blocking on a message that is exactly
2828 * MAXMSGSIZE long. */
2829 for (;;)
2830 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002831 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002832 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002833 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002834 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002835 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002836 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002837 if (len <= 0)
2838 break; /* error or nothing more to read */
2839
2840 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002841 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002842 readlen += len;
2843 if (len < MAXMSGSIZE)
2844 break; /* did read everything that's available */
2845 }
2846
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002847 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002848 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002849 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002850
2851#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002852 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002853 if (CH_HAS_GUI && gtk_main_level() > 0)
2854 gtk_main_quit();
2855#endif
2856}
2857
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002858/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002859 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002860 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002861 * Returns what was read in allocated memory.
2862 * Returns NULL in case of error or timeout.
2863 */
2864 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002865channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002866{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002867 char_u *buf;
2868 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002869 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002870 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002871 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002872
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002873 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002874 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002875
2876 while (TRUE)
2877 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002878 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002879 if (buf != NULL && (mode == MODE_RAW
2880 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2881 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002882 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002883 continue;
2884
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002885 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002886 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002887 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002888 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002889 {
2890 ch_log(channel, "Timed out");
2891 return NULL;
2892 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002893 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002894 }
2895
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002896 if (mode == MODE_RAW)
2897 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002898 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002899 }
2900 else
2901 {
2902 nl = vim_strchr(buf, NL);
2903 if (nl[1] == NUL)
2904 {
2905 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002906 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002907 *nl = NUL;
2908 }
2909 else
2910 {
2911 /* Copy the message into allocated memory and remove it from the
2912 * buffer. */
2913 msg = vim_strnsave(buf, (int)(nl - buf));
2914 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2915 }
2916 }
2917 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002918 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002919 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002920}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002921
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002922/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002923 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002924 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002925 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002926 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002927 */
2928 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002929channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002930 channel_T *channel,
2931 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002932 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002933 int id,
2934 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002935{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002936 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002937 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002938 int timeout;
2939 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002940
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002941 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002942 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002943 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002944 for (;;)
2945 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002946 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002947
2948 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002949 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002950 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002951 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002952 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002953 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002954
Bram Moolenaard7ece102016-02-02 23:23:02 +01002955 if (!more)
2956 {
2957 /* Handle any other messages in the queue. If done some more
2958 * messages may have arrived. */
2959 if (channel_parse_messages())
2960 continue;
2961
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002962 /* Wait for up to the timeout. If there was an incomplete message
2963 * use the deadline for that. */
2964 timeout = timeout_arg;
2965 if (chanpart->ch_waiting)
2966 {
2967#ifdef WIN32
2968 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2969#else
2970 {
2971 struct timeval now_tv;
2972
2973 gettimeofday(&now_tv, NULL);
2974 timeout = (chanpart->ch_deadline.tv_sec
2975 - now_tv.tv_sec) * 1000
2976 + (chanpart->ch_deadline.tv_usec
2977 - now_tv.tv_usec) / 1000
2978 + 1;
2979 }
2980#endif
2981 if (timeout < 0)
2982 {
2983 /* Something went wrong, channel_parse_json() didn't
2984 * discard message. Cancel waiting. */
2985 chanpart->ch_waiting = FALSE;
2986 timeout = timeout_arg;
2987 }
2988 else if (timeout > timeout_arg)
2989 timeout = timeout_arg;
2990 }
2991 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002992 if (fd == INVALID_FD
2993 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002994 {
2995 if (timeout == timeout_arg)
2996 {
2997 if (fd != INVALID_FD)
2998 ch_log(channel, "Timed out");
2999 break;
3000 }
3001 }
3002 else
3003 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003004 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003005 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003006 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003007 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003008}
3009
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003010/*
3011 * Common for ch_read() and ch_readraw().
3012 */
3013 void
3014common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3015{
3016 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003017 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003018 jobopt_T opt;
3019 int mode;
3020 int timeout;
3021 int id = -1;
3022 typval_T *listtv = NULL;
3023
3024 /* return an empty string by default */
3025 rettv->v_type = VAR_STRING;
3026 rettv->vval.v_string = NULL;
3027
3028 clear_job_options(&opt);
3029 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3030 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003031 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003032
Bram Moolenaar437905c2016-04-26 19:01:05 +02003033 if (opt.jo_set & JO_PART)
3034 part = opt.jo_part;
3035 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003036 if (channel != NULL)
3037 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003038 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003039 part = channel_part_read(channel);
3040 mode = channel_get_mode(channel, part);
3041 timeout = channel_get_timeout(channel, part);
3042 if (opt.jo_set & JO_TIMEOUT)
3043 timeout = opt.jo_timeout;
3044
3045 if (raw || mode == MODE_RAW || mode == MODE_NL)
3046 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3047 else
3048 {
3049 if (opt.jo_set & JO_ID)
3050 id = opt.jo_id;
3051 channel_read_json_block(channel, part, timeout, id, &listtv);
3052 if (listtv != NULL)
3053 {
3054 *rettv = *listtv;
3055 vim_free(listtv);
3056 }
3057 else
3058 {
3059 rettv->v_type = VAR_SPECIAL;
3060 rettv->vval.v_number = VVAL_NONE;
3061 }
3062 }
3063 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003064
3065theend:
3066 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003067}
3068
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003069# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3070 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003071/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003072 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003073 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003074 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003075 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003076channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003077{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003078 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003079 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003080
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003081 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003082 for (channel = first_channel; channel != NULL;
3083 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003084 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003085 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003087 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003088 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003089 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003090 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003091 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003092 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003093}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003094# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003095
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003096# if defined(WIN32) || defined(PROTO)
3097/*
3098 * Check the channels for anything that is ready to be read.
3099 * The data is put in the read queue.
3100 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003101 void
3102channel_handle_events(void)
3103{
3104 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003105 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003106 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003107
3108 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3109 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003110 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003111 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003112 {
3113 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003114 if (fd != INVALID_FD)
3115 {
3116 int r = channel_wait(channel, fd, 0);
3117
3118 if (r == CW_READY)
3119 channel_read(channel, part, "channel_handle_events");
3120 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003121 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003122 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003123 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003124 }
3125}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003126# endif
3127
Bram Moolenaard04a0202016-01-26 23:30:18 +01003128/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003129 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003130 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003131 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003132 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003133 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003134channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003135{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003136 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003137 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003138 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003139
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003140 fd = channel->ch_part[part].ch_fd;
3141 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003142 {
3143 if (!channel->ch_error && fun != NULL)
3144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003145 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003146 EMSG2("E630: %s(): write while not connected", fun);
3147 }
3148 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003149 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003150 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003151
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003152 if (log_fd != NULL)
3153 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003154 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003155 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003156 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003157 fprintf(log_fd, "'\n");
3158 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003159 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003160 }
3161
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003162 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003163 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003164 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003165 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003166 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003167 {
3168 if (!channel->ch_error && fun != NULL)
3169 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003170 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003171 EMSG2("E631: %s(): write failed", fun);
3172 }
3173 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003174 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003175 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003176
3177 channel->ch_error = FALSE;
3178 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003179}
3180
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003181/*
3182 * Common for "ch_sendexpr()" and "ch_sendraw()".
3183 * Returns the channel if the caller should read the response.
3184 * Sets "part_read" to the the read fd.
3185 * Otherwise returns NULL.
3186 */
3187 channel_T *
3188send_common(
3189 typval_T *argvars,
3190 char_u *text,
3191 int id,
3192 int eval,
3193 jobopt_T *opt,
3194 char *fun,
3195 int *part_read)
3196{
3197 channel_T *channel;
3198 int part_send;
3199
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003200 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003201 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003202 if (channel == NULL)
3203 return NULL;
3204 part_send = channel_part_send(channel);
3205 *part_read = channel_part_read(channel);
3206
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003207 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3208 return NULL;
3209
3210 /* Set the callback. An empty callback means no callback and not reading
3211 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3212 * allowed. */
3213 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3214 {
3215 if (eval)
3216 {
3217 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3218 return NULL;
3219 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003220 channel_set_req_callback(channel, part_send,
3221 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003222 }
3223
3224 if (channel_send(channel, part_send, text, fun) == OK
3225 && opt->jo_callback == NULL)
3226 return channel;
3227 return NULL;
3228}
3229
3230/*
3231 * common for "ch_evalexpr()" and "ch_sendexpr()"
3232 */
3233 void
3234ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3235{
3236 char_u *text;
3237 typval_T *listtv;
3238 channel_T *channel;
3239 int id;
3240 ch_mode_T ch_mode;
3241 int part_send;
3242 int part_read;
3243 jobopt_T opt;
3244 int timeout;
3245
3246 /* return an empty string by default */
3247 rettv->v_type = VAR_STRING;
3248 rettv->vval.v_string = NULL;
3249
Bram Moolenaar437905c2016-04-26 19:01:05 +02003250 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003251 if (channel == NULL)
3252 return;
3253 part_send = channel_part_send(channel);
3254
3255 ch_mode = channel_get_mode(channel, part_send);
3256 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3257 {
3258 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3259 return;
3260 }
3261
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003262 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003263 text = json_encode_nr_expr(id, &argvars[1],
3264 ch_mode == MODE_JS ? JSON_JS : 0);
3265 if (text == NULL)
3266 return;
3267
3268 channel = send_common(argvars, text, id, eval, &opt,
3269 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3270 vim_free(text);
3271 if (channel != NULL && eval)
3272 {
3273 if (opt.jo_set & JO_TIMEOUT)
3274 timeout = opt.jo_timeout;
3275 else
3276 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003277 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3278 == OK)
3279 {
3280 list_T *list = listtv->vval.v_list;
3281
3282 /* Move the item from the list and then change the type to
3283 * avoid the value being freed. */
3284 *rettv = list->lv_last->li_tv;
3285 list->lv_last->li_tv.v_type = VAR_NUMBER;
3286 free_tv(listtv);
3287 }
3288 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003289 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003290}
3291
3292/*
3293 * common for "ch_evalraw()" and "ch_sendraw()"
3294 */
3295 void
3296ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3297{
3298 char_u buf[NUMBUFLEN];
3299 char_u *text;
3300 channel_T *channel;
3301 int part_read;
3302 jobopt_T opt;
3303 int timeout;
3304
3305 /* return an empty string by default */
3306 rettv->v_type = VAR_STRING;
3307 rettv->vval.v_string = NULL;
3308
3309 text = get_tv_string_buf(&argvars[1], buf);
3310 channel = send_common(argvars, text, 0, eval, &opt,
3311 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3312 if (channel != NULL && eval)
3313 {
3314 if (opt.jo_set & JO_TIMEOUT)
3315 timeout = opt.jo_timeout;
3316 else
3317 timeout = channel_get_timeout(channel, part_read);
3318 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3319 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003320 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003321}
3322
Bram Moolenaard04a0202016-01-26 23:30:18 +01003323# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003324/*
3325 * Add open channels to the poll struct.
3326 * Return the adjusted struct index.
3327 * The type of "fds" is hidden to avoid problems with the function proto.
3328 */
3329 int
3330channel_poll_setup(int nfd_in, void *fds_in)
3331{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003332 int nfd = nfd_in;
3333 channel_T *channel;
3334 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003335 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003336
Bram Moolenaar77073442016-02-13 23:23:53 +01003337 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003338 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003339 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003340 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003341 chanpart_T *ch_part = &channel->ch_part[part];
3342
3343 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003344 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003345 ch_part->ch_poll_idx = nfd;
3346 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003347 fds[nfd].events = POLLIN;
3348 nfd++;
3349 }
3350 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003351 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003352 }
3353 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003354
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003355 nfd = channel_fill_poll_write(nfd, fds);
3356
Bram Moolenaare0874f82016-01-24 20:36:41 +01003357 return nfd;
3358}
3359
3360/*
3361 * The type of "fds" is hidden to avoid problems with the function proto.
3362 */
3363 int
3364channel_poll_check(int ret_in, void *fds_in)
3365{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003366 int ret = ret_in;
3367 channel_T *channel;
3368 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003369 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003370 int idx;
3371 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003372
Bram Moolenaar77073442016-02-13 23:23:53 +01003373 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003374 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003375 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003376 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003377 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003378
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003379 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003380 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003381 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003382 --ret;
3383 }
3384 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003385
3386 in_part = &channel->ch_part[PART_IN];
3387 idx = in_part->ch_poll_idx;
3388 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3389 {
3390 if (in_part->ch_buf_append)
3391 {
3392 if (in_part->ch_buffer != NULL)
3393 channel_write_new_lines(in_part->ch_buffer);
3394 }
3395 else
3396 channel_write_in(channel);
3397 --ret;
3398 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003399 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003400
3401 return ret;
3402}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003403# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003404
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003405# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003406/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003407 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003408 */
3409 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003410channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003411{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003412 int maxfd = maxfd_in;
3413 channel_T *channel;
3414 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003415 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003416 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003417
Bram Moolenaar77073442016-02-13 23:23:53 +01003418 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003419 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003420 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003421 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003422 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003423
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003424 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003425 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003426 FD_SET((int)fd, rfds);
3427 if (maxfd < (int)fd)
3428 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003429 }
3430 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003431 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003432
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003433 maxfd = channel_fill_wfds(maxfd, wfds);
3434
Bram Moolenaare0874f82016-01-24 20:36:41 +01003435 return maxfd;
3436}
3437
3438/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003439 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003440 */
3441 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003442channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003443{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003444 int ret = ret_in;
3445 channel_T *channel;
3446 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003447 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003448 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003449 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003450
Bram Moolenaar77073442016-02-13 23:23:53 +01003451 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003452 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003453 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003454 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003455 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003456
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003457 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003458 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003459 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003460 --ret;
3461 }
3462 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003463
3464 in_part = &channel->ch_part[PART_IN];
3465 if (ret > 0 && in_part->ch_fd != INVALID_FD
3466 && FD_ISSET(in_part->ch_fd, wfds))
3467 {
3468 if (in_part->ch_buf_append)
3469 {
3470 if (in_part->ch_buffer != NULL)
3471 channel_write_new_lines(in_part->ch_buffer);
3472 }
3473 else
3474 channel_write_in(channel);
3475 --ret;
3476 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003477 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003478
3479 return ret;
3480}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003481# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003482
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003483/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003484 * Execute queued up commands.
3485 * Invoked from the main loop when it's safe to execute received commands.
3486 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003487 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003488 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003489channel_parse_messages(void)
3490{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003491 channel_T *channel = first_channel;
3492 int ret = FALSE;
3493 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003494 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003495
Bram Moolenaard0b65022016-03-06 21:50:33 +01003496 /* Only do this message when another message was given, otherwise we get
3497 * lots of them. */
3498 if (did_log_msg)
3499 {
3500 ch_log(NULL, "looking for messages on channels");
3501 did_log_msg = FALSE;
3502 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003503 while (channel != NULL)
3504 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003505 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003506 {
3507 /* channel is no longer useful, free it */
3508 channel_free(channel);
3509 channel = first_channel;
3510 part = PART_SOCK;
3511 continue;
3512 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003513 if (channel->ch_part[part].ch_fd != INVALID_FD
3514 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003515 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003516 /* Increase the refcount, in case the handler causes the channel
3517 * to be unreferenced or closed. */
3518 ++channel->ch_refcount;
3519 r = may_invoke_callback(channel, part);
3520 if (r == OK)
3521 ret = TRUE;
3522 if (channel_unref(channel) || r == OK)
3523 {
3524 /* channel was freed or something was done, start over */
3525 channel = first_channel;
3526 part = PART_SOCK;
3527 continue;
3528 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003529 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003530 if (part < PART_ERR)
3531 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003532 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003533 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003534 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003535 part = PART_SOCK;
3536 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003537 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003538
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003539 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003540 {
3541 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003542 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003543 }
3544
Bram Moolenaard7ece102016-02-02 23:23:02 +01003545 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003546}
3547
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003548/*
3549 * Mark references to lists used in channels.
3550 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003551 int
3552set_ref_in_channel(int copyID)
3553{
Bram Moolenaar77073442016-02-13 23:23:53 +01003554 int abort = FALSE;
3555 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003556 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003557
Bram Moolenaar77073442016-02-13 23:23:53 +01003558 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003559 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003560 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003561 tv.v_type = VAR_CHANNEL;
3562 tv.vval.v_channel = channel;
3563 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003564 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003565 return abort;
3566}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003567
3568/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003569 * Return the "part" to write to for "channel".
3570 */
3571 int
3572channel_part_send(channel_T *channel)
3573{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003574 if (channel->CH_SOCK_FD == INVALID_FD)
3575 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003576 return PART_SOCK;
3577}
3578
3579/*
3580 * Return the default "part" to read from for "channel".
3581 */
3582 int
3583channel_part_read(channel_T *channel)
3584{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003585 if (channel->CH_SOCK_FD == INVALID_FD)
3586 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 return PART_SOCK;
3588}
3589
3590/*
3591 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003592 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003593 */
3594 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003595channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003596{
Bram Moolenaar77073442016-02-13 23:23:53 +01003597 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003598 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003599 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003600}
3601
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003602/*
3603 * Return the timeout of "channel"/"part"
3604 */
3605 int
3606channel_get_timeout(channel_T *channel, int part)
3607{
3608 return channel->ch_part[part].ch_timeout;
3609}
3610
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003611 static int
3612handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3613{
3614 char_u *val = get_tv_string(item);
3615
3616 opt->jo_set |= jo;
3617 if (STRCMP(val, "nl") == 0)
3618 *modep = MODE_NL;
3619 else if (STRCMP(val, "raw") == 0)
3620 *modep = MODE_RAW;
3621 else if (STRCMP(val, "js") == 0)
3622 *modep = MODE_JS;
3623 else if (STRCMP(val, "json") == 0)
3624 *modep = MODE_JSON;
3625 else
3626 {
3627 EMSG2(_(e_invarg2), val);
3628 return FAIL;
3629 }
3630 return OK;
3631}
3632
3633 static int
3634handle_io(typval_T *item, int part, jobopt_T *opt)
3635{
3636 char_u *val = get_tv_string(item);
3637
3638 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3639 if (STRCMP(val, "null") == 0)
3640 opt->jo_io[part] = JIO_NULL;
3641 else if (STRCMP(val, "pipe") == 0)
3642 opt->jo_io[part] = JIO_PIPE;
3643 else if (STRCMP(val, "file") == 0)
3644 opt->jo_io[part] = JIO_FILE;
3645 else if (STRCMP(val, "buffer") == 0)
3646 opt->jo_io[part] = JIO_BUFFER;
3647 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3648 opt->jo_io[part] = JIO_OUT;
3649 else
3650 {
3651 EMSG2(_(e_invarg2), val);
3652 return FAIL;
3653 }
3654 return OK;
3655}
3656
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003657/*
3658 * Clear a jobopt_T before using it.
3659 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003660 void
3661clear_job_options(jobopt_T *opt)
3662{
3663 vim_memset(opt, 0, sizeof(jobopt_T));
3664}
3665
3666/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003667 * Free any members of a jobopt_T.
3668 */
3669 void
3670free_job_options(jobopt_T *opt)
3671{
3672 if (opt->jo_partial != NULL)
3673 partial_unref(opt->jo_partial);
3674 if (opt->jo_out_partial != NULL)
3675 partial_unref(opt->jo_out_partial);
3676 if (opt->jo_err_partial != NULL)
3677 partial_unref(opt->jo_err_partial);
3678 if (opt->jo_close_partial != NULL)
3679 partial_unref(opt->jo_close_partial);
3680}
3681
3682/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003683 * Get the PART_ number from the first character of an option name.
3684 */
3685 static int
3686part_from_char(int c)
3687{
3688 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3689}
3690
3691/*
3692 * Get the option entries from the dict in "tv", parse them and put the result
3693 * in "opt".
3694 * Only accept options in "supported".
3695 * If an option value is invalid return FAIL.
3696 */
3697 int
3698get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3699{
3700 typval_T *item;
3701 char_u *val;
3702 dict_T *dict;
3703 int todo;
3704 hashitem_T *hi;
3705 int part;
3706
3707 opt->jo_set = 0;
3708 if (tv->v_type == VAR_UNKNOWN)
3709 return OK;
3710 if (tv->v_type != VAR_DICT)
3711 {
3712 EMSG(_(e_invarg));
3713 return FAIL;
3714 }
3715 dict = tv->vval.v_dict;
3716 if (dict == NULL)
3717 return OK;
3718
3719 todo = (int)dict->dv_hashtab.ht_used;
3720 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3721 if (!HASHITEM_EMPTY(hi))
3722 {
3723 item = &dict_lookup(hi)->di_tv;
3724
3725 if (STRCMP(hi->hi_key, "mode") == 0)
3726 {
3727 if (!(supported & JO_MODE))
3728 break;
3729 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3730 return FAIL;
3731 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003732 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003733 {
3734 if (!(supported & JO_IN_MODE))
3735 break;
3736 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3737 == FAIL)
3738 return FAIL;
3739 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003740 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003741 {
3742 if (!(supported & JO_OUT_MODE))
3743 break;
3744 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3745 == FAIL)
3746 return FAIL;
3747 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003748 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003749 {
3750 if (!(supported & JO_ERR_MODE))
3751 break;
3752 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3753 == FAIL)
3754 return FAIL;
3755 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003756 else if (STRCMP(hi->hi_key, "in_io") == 0
3757 || STRCMP(hi->hi_key, "out_io") == 0
3758 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003759 {
3760 if (!(supported & JO_OUT_IO))
3761 break;
3762 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3763 return FAIL;
3764 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003765 else if (STRCMP(hi->hi_key, "in_name") == 0
3766 || STRCMP(hi->hi_key, "out_name") == 0
3767 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003768 {
3769 part = part_from_char(*hi->hi_key);
3770
3771 if (!(supported & JO_OUT_IO))
3772 break;
3773 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3774 opt->jo_io_name[part] =
3775 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3776 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003777 else if (STRCMP(hi->hi_key, "in_buf") == 0
3778 || STRCMP(hi->hi_key, "out_buf") == 0
3779 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003780 {
3781 part = part_from_char(*hi->hi_key);
3782
3783 if (!(supported & JO_OUT_IO))
3784 break;
3785 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3786 opt->jo_io_buf[part] = get_tv_number(item);
3787 if (opt->jo_io_buf[part] <= 0)
3788 {
3789 EMSG2(_(e_invarg2), get_tv_string(item));
3790 return FAIL;
3791 }
3792 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3793 {
3794 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3795 return FAIL;
3796 }
3797 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003798 else if (STRCMP(hi->hi_key, "in_top") == 0
3799 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003800 {
3801 linenr_T *lp;
3802
3803 if (!(supported & JO_OUT_IO))
3804 break;
3805 if (hi->hi_key[3] == 't')
3806 {
3807 lp = &opt->jo_in_top;
3808 opt->jo_set |= JO_IN_TOP;
3809 }
3810 else
3811 {
3812 lp = &opt->jo_in_bot;
3813 opt->jo_set |= JO_IN_BOT;
3814 }
3815 *lp = get_tv_number(item);
3816 if (*lp < 0)
3817 {
3818 EMSG2(_(e_invarg2), get_tv_string(item));
3819 return FAIL;
3820 }
3821 }
3822 else if (STRCMP(hi->hi_key, "channel") == 0)
3823 {
3824 if (!(supported & JO_OUT_IO))
3825 break;
3826 opt->jo_set |= JO_CHANNEL;
3827 if (item->v_type != VAR_CHANNEL)
3828 {
3829 EMSG2(_(e_invarg2), "channel");
3830 return FAIL;
3831 }
3832 opt->jo_channel = item->vval.v_channel;
3833 }
3834 else if (STRCMP(hi->hi_key, "callback") == 0)
3835 {
3836 if (!(supported & JO_CALLBACK))
3837 break;
3838 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003839 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003840 if (opt->jo_callback == NULL)
3841 {
3842 EMSG2(_(e_invarg2), "callback");
3843 return FAIL;
3844 }
3845 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003846 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003847 {
3848 if (!(supported & JO_OUT_CALLBACK))
3849 break;
3850 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003851 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852 if (opt->jo_out_cb == NULL)
3853 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003854 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003855 return FAIL;
3856 }
3857 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003858 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003859 {
3860 if (!(supported & JO_ERR_CALLBACK))
3861 break;
3862 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003863 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003864 if (opt->jo_err_cb == NULL)
3865 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003866 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003867 return FAIL;
3868 }
3869 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003870 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003871 {
3872 if (!(supported & JO_CLOSE_CALLBACK))
3873 break;
3874 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003875 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003876 if (opt->jo_close_cb == NULL)
3877 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003878 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003879 return FAIL;
3880 }
3881 }
3882 else if (STRCMP(hi->hi_key, "waittime") == 0)
3883 {
3884 if (!(supported & JO_WAITTIME))
3885 break;
3886 opt->jo_set |= JO_WAITTIME;
3887 opt->jo_waittime = get_tv_number(item);
3888 }
3889 else if (STRCMP(hi->hi_key, "timeout") == 0)
3890 {
3891 if (!(supported & JO_TIMEOUT))
3892 break;
3893 opt->jo_set |= JO_TIMEOUT;
3894 opt->jo_timeout = get_tv_number(item);
3895 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003896 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003897 {
3898 if (!(supported & JO_OUT_TIMEOUT))
3899 break;
3900 opt->jo_set |= JO_OUT_TIMEOUT;
3901 opt->jo_out_timeout = get_tv_number(item);
3902 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003903 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003904 {
3905 if (!(supported & JO_ERR_TIMEOUT))
3906 break;
3907 opt->jo_set |= JO_ERR_TIMEOUT;
3908 opt->jo_err_timeout = get_tv_number(item);
3909 }
3910 else if (STRCMP(hi->hi_key, "part") == 0)
3911 {
3912 if (!(supported & JO_PART))
3913 break;
3914 opt->jo_set |= JO_PART;
3915 val = get_tv_string(item);
3916 if (STRCMP(val, "err") == 0)
3917 opt->jo_part = PART_ERR;
3918 else
3919 {
3920 EMSG2(_(e_invarg2), val);
3921 return FAIL;
3922 }
3923 }
3924 else if (STRCMP(hi->hi_key, "id") == 0)
3925 {
3926 if (!(supported & JO_ID))
3927 break;
3928 opt->jo_set |= JO_ID;
3929 opt->jo_id = get_tv_number(item);
3930 }
3931 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3932 {
3933 if (!(supported & JO_STOPONEXIT))
3934 break;
3935 opt->jo_set |= JO_STOPONEXIT;
3936 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3937 opt->jo_soe_buf);
3938 if (opt->jo_stoponexit == NULL)
3939 {
3940 EMSG2(_(e_invarg2), "stoponexit");
3941 return FAIL;
3942 }
3943 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003944 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003945 {
3946 if (!(supported & JO_EXIT_CB))
3947 break;
3948 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003949 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3950 {
3951 opt->jo_exit_partial = item->vval.v_partial;
3952 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3953 }
3954 else
3955 opt->jo_exit_cb = get_tv_string_buf_chk(
3956 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003957 if (opt->jo_exit_cb == NULL)
3958 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003959 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 return FAIL;
3961 }
3962 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003963 else if (STRCMP(hi->hi_key, "block_write") == 0)
3964 {
3965 if (!(supported & JO_BLOCK_WRITE))
3966 break;
3967 opt->jo_set |= JO_BLOCK_WRITE;
3968 opt->jo_block_write = get_tv_number(item);
3969 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003970 else
3971 break;
3972 --todo;
3973 }
3974 if (todo > 0)
3975 {
3976 EMSG2(_(e_invarg2), hi->hi_key);
3977 return FAIL;
3978 }
3979
3980 return OK;
3981}
3982
3983/*
3984 * Get the channel from the argument.
3985 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02003986 * When "check_open" is TRUE check that the channel can be used.
3987 * When "reading" is TRUE "check_open" considers typeahead useful.
3988 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 */
3990 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02003991get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003992{
Bram Moolenaar437905c2016-04-26 19:01:05 +02003993 channel_T *channel = NULL;
3994 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003995
3996 if (tv->v_type == VAR_JOB)
3997 {
3998 if (tv->vval.v_job != NULL)
3999 channel = tv->vval.v_job->jv_channel;
4000 }
4001 else if (tv->v_type == VAR_CHANNEL)
4002 {
4003 channel = tv->vval.v_channel;
4004 }
4005 else
4006 {
4007 EMSG2(_(e_invarg2), get_tv_string(tv));
4008 return NULL;
4009 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004010 if (channel != NULL && reading)
4011 has_readahead = channel_has_readahead(channel,
4012 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004013
Bram Moolenaar437905c2016-04-26 19:01:05 +02004014 if (check_open && (channel == NULL || (!channel_is_open(channel)
4015 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004016 {
4017 EMSG(_("E906: not an open channel"));
4018 return NULL;
4019 }
4020 return channel;
4021}
4022
4023static job_T *first_job = NULL;
4024
4025 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004026job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027{
4028 ch_log(job->jv_channel, "Freeing job");
4029 if (job->jv_channel != NULL)
4030 {
4031 /* The link from the channel to the job doesn't count as a reference,
4032 * thus don't decrement the refcount of the job. The reference from
4033 * the job to the channel does count the refrence, decrement it and
4034 * NULL the reference. We don't set ch_job_killed, unreferencing the
4035 * job doesn't mean it stops running. */
4036 job->jv_channel->ch_job = NULL;
4037 channel_unref(job->jv_channel);
4038 }
4039 mch_clear_job(job);
4040
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004041 vim_free(job->jv_stoponexit);
4042 vim_free(job->jv_exit_cb);
4043 partial_unref(job->jv_exit_partial);
4044}
4045
4046 static void
4047job_free_job(job_T *job)
4048{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004049 if (job->jv_next != NULL)
4050 job->jv_next->jv_prev = job->jv_prev;
4051 if (job->jv_prev == NULL)
4052 first_job = job->jv_next;
4053 else
4054 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055 vim_free(job);
4056}
4057
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004058 static void
4059job_free(job_T *job)
4060{
4061 if (!in_free_unref_items)
4062 {
4063 job_free_contents(job);
4064 job_free_job(job);
4065 }
4066}
4067
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004068/*
4069 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004070 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4071 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004072 */
4073 static int
4074job_still_useful(job_T *job)
4075{
4076 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004077 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4078 || (job->jv_channel != NULL
4079 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004080}
4081
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004082/*
4083 * Mark references in jobs that are still useful.
4084 */
4085 int
4086set_ref_in_job(int copyID)
4087{
4088 int abort = FALSE;
4089 job_T *job;
4090 typval_T tv;
4091
4092 for (job = first_job; job != NULL; job = job->jv_next)
4093 if (job_still_useful(job))
4094 {
4095 tv.v_type = VAR_JOB;
4096 tv.vval.v_job = job;
4097 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4098 }
4099 return abort;
4100}
4101
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004102 void
4103job_unref(job_T *job)
4104{
4105 if (job != NULL && --job->jv_refcount <= 0)
4106 {
4107 /* Do not free the job when it has not ended yet and there is a
4108 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004109 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004110 {
4111 job_free(job);
4112 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004113 else if (job->jv_channel != NULL
4114 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115 {
4116 /* Do remove the link to the channel, otherwise it hangs
4117 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004118 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119 job->jv_channel->ch_job = NULL;
4120 channel_unref(job->jv_channel);
4121 job->jv_channel = NULL;
4122 }
4123 }
4124}
4125
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004126 int
4127free_unused_jobs_contents(int copyID, int mask)
4128{
4129 int did_free = FALSE;
4130 job_T *job;
4131
4132 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004133 if ((job->jv_copyID & mask) != (copyID & mask)
4134 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004135 {
4136 /* Free the channel and ordinary items it contains, but don't
4137 * recurse into Lists, Dictionaries etc. */
4138 job_free_contents(job);
4139 did_free = TRUE;
4140 }
4141 return did_free;
4142}
4143
4144 void
4145free_unused_jobs(int copyID, int mask)
4146{
4147 job_T *job;
4148 job_T *job_next;
4149
4150 for (job = first_job; job != NULL; job = job_next)
4151 {
4152 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004153 if ((job->jv_copyID & mask) != (copyID & mask)
4154 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004155 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004156 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004157 job_free_job(job);
4158 }
4159 }
4160}
4161
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004162/*
4163 * Allocate a job. Sets the refcount to one and sets options default.
4164 */
4165 static job_T *
4166job_alloc(void)
4167{
4168 job_T *job;
4169
4170 job = (job_T *)alloc_clear(sizeof(job_T));
4171 if (job != NULL)
4172 {
4173 job->jv_refcount = 1;
4174 job->jv_stoponexit = vim_strsave((char_u *)"term");
4175
4176 if (first_job != NULL)
4177 {
4178 first_job->jv_prev = job;
4179 job->jv_next = first_job;
4180 }
4181 first_job = job;
4182 }
4183 return job;
4184}
4185
4186 void
4187job_set_options(job_T *job, jobopt_T *opt)
4188{
4189 if (opt->jo_set & JO_STOPONEXIT)
4190 {
4191 vim_free(job->jv_stoponexit);
4192 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4193 job->jv_stoponexit = NULL;
4194 else
4195 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4196 }
4197 if (opt->jo_set & JO_EXIT_CB)
4198 {
4199 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004200 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004201 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004202 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004203 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004204 job->jv_exit_partial = NULL;
4205 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004206 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004207 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004208 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004209 job->jv_exit_partial = opt->jo_exit_partial;
4210 if (job->jv_exit_partial != NULL)
4211 ++job->jv_exit_partial->pt_refcount;
4212 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004213 }
4214}
4215
4216/*
4217 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4218 */
4219 void
4220job_stop_on_exit()
4221{
4222 job_T *job;
4223
4224 for (job = first_job; job != NULL; job = job->jv_next)
4225 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4226 mch_stop_job(job, job->jv_stoponexit);
4227}
4228
4229/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004230 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004231 */
4232 void
4233job_check_ended(void)
4234{
4235 static time_t last_check = 0;
4236 time_t now;
4237 job_T *job;
4238 job_T *next;
4239
4240 /* Only do this once in 10 seconds. */
4241 now = time(NULL);
4242 if (last_check + 10 < now)
4243 {
4244 last_check = now;
4245 for (job = first_job; job != NULL; job = next)
4246 {
4247 next = job->jv_next;
4248 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4249 job_status(job); /* may free "job" */
4250 }
4251 }
4252}
4253
4254/*
4255 * "job_start()" function
4256 */
4257 job_T *
4258job_start(typval_T *argvars)
4259{
4260 job_T *job;
4261 char_u *cmd = NULL;
4262#if defined(UNIX)
4263# define USE_ARGV
4264 char **argv = NULL;
4265 int argc = 0;
4266#else
4267 garray_T ga;
4268#endif
4269 jobopt_T opt;
4270 int part;
4271
4272 job = job_alloc();
4273 if (job == NULL)
4274 return NULL;
4275
4276 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004277#ifndef USE_ARGV
4278 ga_init2(&ga, (int)sizeof(char*), 20);
4279#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004280
4281 /* Default mode is NL. */
4282 clear_job_options(&opt);
4283 opt.jo_mode = MODE_NL;
4284 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004285 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4286 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004287 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004288
4289 /* Check that when io is "file" that there is a file name. */
4290 for (part = PART_OUT; part <= PART_IN; ++part)
4291 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4292 && opt.jo_io[part] == JIO_FILE
4293 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4294 || *opt.jo_io_name[part] == NUL))
4295 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004296 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004297 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004298 }
4299
4300 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4301 {
4302 buf_T *buf = NULL;
4303
4304 /* check that we can find the buffer before starting the job */
4305 if (opt.jo_set & JO_IN_BUF)
4306 {
4307 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4308 if (buf == NULL)
4309 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4310 }
4311 else if (!(opt.jo_set & JO_IN_NAME))
4312 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004313 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004314 }
4315 else
4316 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4317 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004318 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004319 if (buf->b_ml.ml_mfp == NULL)
4320 {
4321 char_u numbuf[NUMBUFLEN];
4322 char_u *s;
4323
4324 if (opt.jo_set & JO_IN_BUF)
4325 {
4326 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4327 s = numbuf;
4328 }
4329 else
4330 s = opt.jo_io_name[PART_IN];
4331 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004332 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004333 }
4334 job->jv_in_buf = buf;
4335 }
4336
4337 job_set_options(job, &opt);
4338
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004339 if (argvars[0].v_type == VAR_STRING)
4340 {
4341 /* Command is a string. */
4342 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004343 if (cmd == NULL || *cmd == NUL)
4344 {
4345 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004346 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004347 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004348#ifdef USE_ARGV
4349 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004350 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004351 argv[argc] = NULL;
4352#endif
4353 }
4354 else if (argvars[0].v_type != VAR_LIST
4355 || argvars[0].vval.v_list == NULL
4356 || argvars[0].vval.v_list->lv_len < 1)
4357 {
4358 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004359 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004360 }
4361 else
4362 {
4363 list_T *l = argvars[0].vval.v_list;
4364 listitem_T *li;
4365 char_u *s;
4366
4367#ifdef USE_ARGV
4368 /* Pass argv[] to mch_call_shell(). */
4369 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4370 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004371 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004372#endif
4373 for (li = l->lv_first; li != NULL; li = li->li_next)
4374 {
4375 s = get_tv_string_chk(&li->li_tv);
4376 if (s == NULL)
4377 goto theend;
4378#ifdef USE_ARGV
4379 argv[argc++] = (char *)s;
4380#else
4381 /* Only escape when needed, double quotes are not always allowed. */
4382 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4383 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004384# ifdef WIN32
4385 int old_ssl = p_ssl;
4386
4387 /* This is using CreateProcess, not cmd.exe. Always use
4388 * double quote and backslashes. */
4389 p_ssl = 0;
4390# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004391 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004392# ifdef WIN32
4393 p_ssl = old_ssl;
4394# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004395 if (s == NULL)
4396 goto theend;
4397 ga_concat(&ga, s);
4398 vim_free(s);
4399 }
4400 else
4401 ga_concat(&ga, s);
4402 if (li->li_next != NULL)
4403 ga_append(&ga, ' ');
4404#endif
4405 }
4406#ifdef USE_ARGV
4407 argv[argc] = NULL;
4408#else
4409 cmd = ga.ga_data;
4410#endif
4411 }
4412
4413#ifdef USE_ARGV
4414 if (ch_log_active())
4415 {
4416 garray_T ga;
4417 int i;
4418
4419 ga_init2(&ga, (int)sizeof(char), 200);
4420 for (i = 0; i < argc; ++i)
4421 {
4422 if (i > 0)
4423 ga_concat(&ga, (char_u *)" ");
4424 ga_concat(&ga, (char_u *)argv[i]);
4425 }
4426 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4427 ga_clear(&ga);
4428 }
4429 mch_start_job(argv, job, &opt);
4430#else
4431 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4432 mch_start_job((char *)cmd, job, &opt);
4433#endif
4434
4435 /* If the channel is reading from a buffer, write lines now. */
4436 if (job->jv_channel != NULL)
4437 channel_write_in(job->jv_channel);
4438
4439theend:
4440#ifdef USE_ARGV
4441 vim_free(argv);
4442#else
4443 vim_free(ga.ga_data);
4444#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004445 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004446 return job;
4447}
4448
4449/*
4450 * Get the status of "job" and invoke the exit callback when needed.
4451 * The returned string is not allocated.
4452 */
4453 char *
4454job_status(job_T *job)
4455{
4456 char *result;
4457
4458 if (job->jv_status == JOB_ENDED)
4459 /* No need to check, dead is dead. */
4460 result = "dead";
4461 else if (job->jv_status == JOB_FAILED)
4462 result = "fail";
4463 else
4464 {
4465 result = mch_job_status(job);
4466 if (job->jv_status == JOB_ENDED)
4467 ch_log(job->jv_channel, "Job ended");
4468 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4469 {
4470 typval_T argv[3];
4471 typval_T rettv;
4472 int dummy;
4473
4474 /* invoke the exit callback; make sure the refcount is > 0 */
4475 ++job->jv_refcount;
4476 argv[0].v_type = VAR_JOB;
4477 argv[0].vval.v_job = job;
4478 argv[1].v_type = VAR_NUMBER;
4479 argv[1].vval.v_number = job->jv_exitval;
4480 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004481 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4482 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004483 clear_tv(&rettv);
4484 --job->jv_refcount;
4485 }
4486 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4487 {
4488 /* The job was already unreferenced, now that it ended it can be
4489 * freed. Careful: caller must not use "job" after this! */
4490 job_free(job);
4491 }
4492 }
4493 return result;
4494}
4495
Bram Moolenaar8950a562016-03-12 15:22:55 +01004496/*
4497 * Implementation of job_info().
4498 */
4499 void
4500job_info(job_T *job, dict_T *dict)
4501{
4502 dictitem_T *item;
4503 varnumber_T nr;
4504
4505 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4506
4507 item = dictitem_alloc((char_u *)"channel");
4508 if (item == NULL)
4509 return;
4510 item->di_tv.v_lock = 0;
4511 item->di_tv.v_type = VAR_CHANNEL;
4512 item->di_tv.vval.v_channel = job->jv_channel;
4513 if (job->jv_channel != NULL)
4514 ++job->jv_channel->ch_refcount;
4515 if (dict_add(dict, item) == FAIL)
4516 dictitem_free(item);
4517
4518#ifdef UNIX
4519 nr = job->jv_pid;
4520#else
4521 nr = job->jv_proc_info.dwProcessId;
4522#endif
4523 dict_add_nr_str(dict, "process", nr, NULL);
4524
4525 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004526 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004527 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4528}
4529
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004530 int
4531job_stop(job_T *job, typval_T *argvars)
4532{
4533 char_u *arg;
4534
4535 if (argvars[1].v_type == VAR_UNKNOWN)
4536 arg = (char_u *)"";
4537 else
4538 {
4539 arg = get_tv_string_chk(&argvars[1]);
4540 if (arg == NULL)
4541 {
4542 EMSG(_(e_invarg));
4543 return 0;
4544 }
4545 }
4546 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4547 if (mch_stop_job(job, arg) == FAIL)
4548 return 0;
4549
4550 /* Assume that "hup" does not kill the job. */
4551 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4552 job->jv_channel->ch_job_killed = TRUE;
4553
4554 /* We don't try freeing the job, obviously the caller still has a
4555 * reference to it. */
4556 return 1;
4557}
4558
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004559#endif /* FEAT_JOB_CHANNEL */