blob: 7fe68d5b4538b36912580e4c2f50abc6e99507c4 [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 Moolenaar187db502016-02-27 14:44:26 +010057/* Whether a redraw is needed for appending a line to a buffer. */
58static int channel_need_redraw = FALSE;
59
60
Bram Moolenaard8070362016-02-15 21:56:54 +010061#ifdef WIN32
62 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010063fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010064{
65 HANDLE h = (HANDLE)fd;
66 DWORD nread;
67
68 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
69 return -1;
70 return (int)nread;
71}
72
73 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010074fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010075{
76 HANDLE h = (HANDLE)fd;
77 DWORD nwrite;
78
79 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
80 return -1;
81 return (int)nwrite;
82}
83
84 static void
85fd_close(sock_T fd)
86{
87 HANDLE h = (HANDLE)fd;
88
89 CloseHandle(h);
90}
91#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010092
Bram Moolenaar6463ca22016-02-13 17:04:46 +010093/* Log file opened with ch_logfile(). */
94static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010095#ifdef FEAT_RELTIME
96static proftime_T log_start;
97#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010098
99 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100100ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100101{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102 FILE *file = NULL;
103
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100104 if (log_fd != NULL)
105 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100106
107 if (*fname != NUL)
108 {
109 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
110 if (file == NULL)
111 {
112 EMSG2(_(e_notopen), fname);
113 return;
114 }
115 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100116 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100117
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100119 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121#ifdef FEAT_RELTIME
122 profile_start(&log_start);
123#endif
124 }
125}
126
127 int
128ch_log_active()
129{
130 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100131}
132
133 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100134ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135{
136 if (log_fd != NULL)
137 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100138#ifdef FEAT_RELTIME
139 proftime_T log_now;
140
141 profile_start(&log_now);
142 profile_sub(&log_now, &log_start);
143 fprintf(log_fd, "%s ", profile_msg(&log_now));
144#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100145 if (ch != NULL)
146 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100147 else
148 fprintf(log_fd, "%s: ", what);
149 }
150}
151
Bram Moolenaard0b65022016-03-06 21:50:33 +0100152static int did_log_msg = TRUE;
153
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100154 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100155ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100156{
157 if (log_fd != NULL)
158 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100159 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100163 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 }
165}
166
167 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100168ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169{
170 if (log_fd != NULL)
171 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100172 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100174 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100176 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 }
178}
179
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100180 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100181ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182{
183 if (log_fd != NULL)
184 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100189 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 }
191}
192
193 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100194ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195{
196 if (log_fd != NULL)
197 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100198 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100199 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100200 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100202 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 }
204}
205
206 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100207ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208{
209 if (log_fd != NULL)
210 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100211 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100212 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100213 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100215 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 }
217}
218
219 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100220ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221{
222 if (log_fd != NULL)
223 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100224 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100225 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100226 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100228 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 }
230}
231
232 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100233ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234{
235 if (log_fd != NULL)
236 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100237 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100238 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100239 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100241 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 }
243}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100244
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100245#ifdef _WIN32
246# undef PERROR
247# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
248 (char_u *)msg, (char_u *)strerror_win32(errno))
249
250 static char *
251strerror_win32(int eno)
252{
253 static LPVOID msgbuf = NULL;
254 char_u *ptr;
255
256 if (msgbuf)
257 LocalFree(msgbuf);
258 FormatMessage(
259 FORMAT_MESSAGE_ALLOCATE_BUFFER |
260 FORMAT_MESSAGE_FROM_SYSTEM |
261 FORMAT_MESSAGE_IGNORE_INSERTS,
262 NULL,
263 eno,
264 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
265 (LPTSTR) &msgbuf,
266 0,
267 NULL);
268 /* chomp \r or \n */
269 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
270 switch (*ptr)
271 {
272 case '\r':
273 STRMOVE(ptr, ptr + 1);
274 ptr--;
275 break;
276 case '\n':
277 if (*(ptr + 1) == '\0')
278 *ptr = '\0';
279 else
280 *ptr = ' ';
281 break;
282 }
283 return msgbuf;
284}
285#endif
286
Bram Moolenaar77073442016-02-13 23:23:53 +0100287/*
288 * The list of all allocated channels.
289 */
290static channel_T *first_channel = NULL;
291static int next_ch_id = 0;
292
293/*
294 * Allocate a new channel. The refcount is set to 1.
295 * The channel isn't actually used until it is opened.
296 * Returns NULL if out of memory.
297 */
298 channel_T *
299add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100300{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100301 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100302 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100303
Bram Moolenaar77073442016-02-13 23:23:53 +0100304 if (channel == NULL)
305 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100306
Bram Moolenaar77073442016-02-13 23:23:53 +0100307 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100308 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100309
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100310 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100311 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#endif
316#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100318#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100320 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100321
Bram Moolenaar77073442016-02-13 23:23:53 +0100322 if (first_channel != NULL)
323 {
324 first_channel->ch_prev = channel;
325 channel->ch_next = first_channel;
326 }
327 first_channel = channel;
328
329 channel->ch_refcount = 1;
330 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100331}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100332
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100334 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100335 * Return TRUE if "channel" has a callback and the associated job wasn't
336 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100337 */
338 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100339channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100340{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100341 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 int has_out_msg;
343 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344
345 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346 if (channel->ch_job_killed && channel->ch_job == NULL)
347 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348
349 /* If there is a close callback it may still need to be invoked. */
350 if (channel->ch_close_cb != NULL)
351 return TRUE;
352
353 /* If there is no callback then nobody can get readahead. If the fd is
354 * closed and there is no readahead then the callback won't be called. */
355 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
356 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
357 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
359 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
360 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
361 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
362 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
363 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100365 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100368}
369
370/*
371 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100372 * possible, there is no callback to be invoked or the associated job was
373 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100374 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100375 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100376 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377channel_may_free(channel_T *channel)
378{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100379 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100380 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100382 return TRUE;
383 }
384 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100385}
386
387/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100388 * Decrement the reference count on "channel" and maybe free it when it goes
389 * down to zero. Don't free it if there is a pending action.
390 * Returns TRUE when the channel is no longer referenced.
391 */
392 int
393channel_unref(channel_T *channel)
394{
395 if (channel != NULL && --channel->ch_refcount <= 0)
396 return channel_may_free(channel);
397 return FALSE;
398}
399
400/*
Bram Moolenaar77073442016-02-13 23:23:53 +0100401 * Close a channel and free all its resources.
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100402 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100403 void
404channel_free(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100405{
Bram Moolenaar8b374212016-02-24 20:43:06 +0100406 channel_close(channel, TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +0100407 channel_clear(channel);
Bram Moolenaard6051b52016-02-28 15:49:03 +0100408 ch_log(channel, "Freeing channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100409 if (channel->ch_next != NULL)
410 channel->ch_next->ch_prev = channel->ch_prev;
411 if (channel->ch_prev == NULL)
412 first_channel = channel->ch_next;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100413 else
Bram Moolenaar77073442016-02-13 23:23:53 +0100414 channel->ch_prev->ch_next = channel->ch_next;
415 vim_free(channel);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100416}
417
Bram Moolenaard04a0202016-01-26 23:30:18 +0100418#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100419
420#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
421 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100422channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100423{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100424 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100425 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100426
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100427 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100428 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100429 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100430 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100431 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100432}
433#endif
434
Bram Moolenaare0874f82016-01-24 20:36:41 +0100435/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100436 * Read a command from netbeans.
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100437 * TODO: instead of channel ID use the FD.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100438 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100439#ifdef FEAT_GUI_X11
440 static void
441messageFromNetbeans(XtPointer clientData,
442 int *unused1 UNUSED,
443 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100444{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100445 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100446}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100447#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100448
Bram Moolenaard04a0202016-01-26 23:30:18 +0100449#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100450# if GTK_CHECK_VERSION(3,0,0)
451 static gboolean
452messageFromNetbeans(GIOChannel *unused1 UNUSED,
453 GIOCondition unused2 UNUSED,
454 gpointer clientData)
455{
456 channel_read_fd(GPOINTER_TO_INT(clientData));
457 return TRUE; /* Return FALSE instead in case the event source is to
458 * be removed after this function returns. */
459}
460# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100461 static void
462messageFromNetbeans(gpointer clientData,
463 gint unused1 UNUSED,
464 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100466 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467}
Bram Moolenaar98921892016-02-23 17:14:37 +0100468# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100469#endif
470
471 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100472channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100473{
Bram Moolenaarde279892016-03-11 22:19:44 +0100474 if (!CH_HAS_GUI)
475 return;
476
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100477# ifdef FEAT_GUI_X11
478 /* Tell notifier we are interested in being called
479 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100480 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
481 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100482 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100483 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100484 (XtPointer)(XtInputReadMask + XtInputExceptMask),
485 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100486 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100487# else
488# ifdef FEAT_GUI_GTK
489 /* Tell gdk we are interested in being called when there
490 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100491 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100492# if GTK_CHECK_VERSION(3,0,0)
493 {
494 GIOChannel *chnnl = g_io_channel_unix_new(
495 (gint)channel->ch_part[part].ch_fd);
496
497 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
498 chnnl,
499 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
500 messageFromNetbeans,
501 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
502
503 g_io_channel_unref(chnnl);
504 }
505# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100506 channel->ch_part[part].ch_inputHandler = gdk_input_add(
507 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100508 (GdkInputCondition)
509 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100510 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100512# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100513# endif
514# endif
515}
516
Bram Moolenaarde279892016-03-11 22:19:44 +0100517 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100518channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100520 if (channel->CH_SOCK_FD != INVALID_FD)
521 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100522 if (channel->CH_OUT_FD != INVALID_FD)
523 channel_gui_register_one(channel, PART_OUT);
524 if (channel->CH_ERR_FD != INVALID_FD)
525 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100526}
527
528/*
529 * Register any of our file descriptors with the GUI event handling system.
530 * Called when the GUI has started.
531 */
532 void
533channel_gui_register_all(void)
534{
Bram Moolenaar77073442016-02-13 23:23:53 +0100535 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100536
Bram Moolenaar77073442016-02-13 23:23:53 +0100537 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100538 channel_gui_register(channel);
539}
540
541 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100542channel_gui_unregister_one(channel_T *channel, int part)
543{
544# ifdef FEAT_GUI_X11
545 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
546 {
547 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
548 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
549 }
550# else
551# ifdef FEAT_GUI_GTK
552 if (channel->ch_part[part].ch_inputHandler != 0)
553 {
554# if GTK_CHECK_VERSION(3,0,0)
555 g_source_remove(channel->ch_part[part].ch_inputHandler);
556# else
557 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
558# endif
559 channel->ch_part[part].ch_inputHandler = 0;
560 }
561# endif
562# endif
563}
564
565 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100566channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100567{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100569
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100571 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100572}
573
574#endif
575
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100576static char *e_cannot_connect = N_("E902: Cannot connect to port");
577
Bram Moolenaard04a0202016-01-26 23:30:18 +0100578/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100579 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100580 * "waittime" is the time in msec to wait for the connection.
581 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100582 * Returns the channel for success.
583 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100584 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100585 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100586channel_open(
587 char *hostname,
588 int port_in,
589 int waittime,
590 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100591{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100592 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100593 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100594 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100595#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100596 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100597 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100598#else
599 int port = port_in;
600#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100601 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100602 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100603
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100604#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100605 channel_init_winsock();
606#endif
607
Bram Moolenaar77073442016-02-13 23:23:53 +0100608 channel = add_channel();
609 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100610 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100611 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100612 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100613 }
614
615 /* Get the server internet address and put into addr structure */
616 /* fill in the socket address structure and connect to server */
617 vim_memset((char *)&server, 0, sizeof(server));
618 server.sin_family = AF_INET;
619 server.sin_port = htons(port);
620 if ((host = gethostbyname(hostname)) == NULL)
621 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100622 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100623 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100624 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100625 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100626 }
627 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
628
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100629 /* On Mac and Solaris a zero timeout almost never works. At least wait
630 * one millisecond. Let's do it for all systems, because we don't know why
631 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100632 if (waittime == 0)
633 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100634
635 /*
636 * For Unix we need to call connect() again after connect() failed.
637 * On Win32 one time is sufficient.
638 */
639 while (TRUE)
640 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100641 long elapsed_msec = 0;
642 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100643
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100645 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100646 sd = socket(AF_INET, SOCK_STREAM, 0);
647 if (sd == -1)
648 {
649 ch_error(channel, "in socket() in channel_open().");
650 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100651 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100652 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100654
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100655 if (waittime >= 0)
656 {
657 /* Make connect() non-blocking. */
658 if (
659#ifdef _WIN32
660 ioctlsocket(sd, FIONBIO, &val) < 0
661#else
662 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
663#endif
664 )
665 {
666 SOCK_ERRNO;
667 ch_errorn(channel,
668 "channel_open: Connect failed with errno %d", errno);
669 sock_close(sd);
670 channel_free(channel);
671 return NULL;
672 }
673 }
674
675 /* Try connecting to the server. */
676 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
677 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
678
Bram Moolenaar045a2842016-03-08 22:33:07 +0100679 if (ret == 0)
680 /* The connection could be established. */
681 break;
682
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100683 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100684 if (waittime < 0 || (errno != EWOULDBLOCK
685 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100686#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100687 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100688#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100689 ))
690 {
691 ch_errorn(channel,
692 "channel_open: Connect failed with errno %d", errno);
693 PERROR(_(e_cannot_connect));
694 sock_close(sd);
695 channel_free(channel);
696 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100699 /* Limit the waittime to 50 msec. If it doesn't work within this
700 * time we close the socket and try creating it again. */
701 waitnow = waittime > 50 ? 50 : waittime;
702
Bram Moolenaar045a2842016-03-08 22:33:07 +0100703 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100704 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100705#ifndef WIN32
706 if (errno != ECONNREFUSED)
707#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 {
709 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100710 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100711 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100712#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100713 int so_error = 0;
714 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100715 struct timeval start_tv;
716 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100717#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100718 FD_ZERO(&rfds);
719 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 FD_ZERO(&wfds);
721 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100722
Bram Moolenaar562ca712016-03-09 21:50:05 +0100723 tv.tv_sec = waitnow / 1000;
724 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725#ifndef WIN32
726 gettimeofday(&start_tv, NULL);
727#endif
728 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100729 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100730 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100731
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100732 if (ret < 0)
733 {
734 SOCK_ERRNO;
735 ch_errorn(channel,
736 "channel_open: Connect failed with errno %d", errno);
737 PERROR(_(e_cannot_connect));
738 sock_close(sd);
739 channel_free(channel);
740 return NULL;
741 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100742
Bram Moolenaare081e212016-02-28 22:33:46 +0100743#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100744 /* On Win32: select() is expected to work and wait for up to
745 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100746 if (FD_ISSET(sd, &wfds))
747 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100748 elapsed_msec = waitnow;
749 if (waittime > 1 && elapsed_msec < waittime)
750 {
751 waittime -= elapsed_msec;
752 continue;
753 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100754#else
755 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 * After putting the socket in non-blocking mode, connect() will
757 * return EINPROGRESS, select() will not wait (as if writing is
758 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100759 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100760 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100761 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100762 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100763 {
764 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100766 if (ret < 0 || (so_error != 0
767 && so_error != EWOULDBLOCK
768 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100769# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100770 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100771# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100772 ))
773 {
774 ch_errorn(channel,
775 "channel_open: Connect failed with errno %d",
776 so_error);
777 PERROR(_(e_cannot_connect));
778 sock_close(sd);
779 channel_free(channel);
780 return NULL;
781 }
782 }
783
Bram Moolenaar045a2842016-03-08 22:33:07 +0100784 if (FD_ISSET(sd, &wfds) && so_error == 0)
785 /* Did not detect an error, connection is established. */
786 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787
Bram Moolenaar045a2842016-03-08 22:33:07 +0100788 gettimeofday(&end_tv, NULL);
789 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
790 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100791#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100792 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100793
794#ifndef WIN32
795 if (waittime > 1 && elapsed_msec < waittime)
796 {
797 /* The port isn't ready but we also didn't get an error.
798 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 * yet. Select() may return early, wait until the remaining
800 * "waitnow" and try again. */
801 waitnow -= elapsed_msec;
802 waittime -= elapsed_msec;
803 if (waitnow > 0)
804 {
805 mch_delay((long)waitnow, TRUE);
806 ui_breakcheck();
807 waittime -= waitnow;
808 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100809 if (!got_int)
810 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100811 if (waittime <= 0)
812 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100813 waittime = 1;
814 continue;
815 }
816 /* we were interrupted, behave as if timed out */
817 }
818#endif
819
820 /* We timed out. */
821 ch_error(channel, "Connection timed out");
822 sock_close(sd);
823 channel_free(channel);
824 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100825 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100826
Bram Moolenaar045a2842016-03-08 22:33:07 +0100827 ch_log(channel, "Connection made");
828
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100829 if (waittime >= 0)
830 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100831#ifdef _WIN32
832 val = 0;
833 ioctlsocket(sd, FIONBIO, &val);
834#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100835 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100836#endif
837 }
838
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100839 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100840 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100841
842#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100843 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100844#endif
845
Bram Moolenaar77073442016-02-13 23:23:53 +0100846 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100847}
848
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100849/*
850 * Implements ch_open().
851 */
852 channel_T *
853channel_open_func(typval_T *argvars)
854{
855 char_u *address;
856 char_u *p;
857 char *rest;
858 int port;
859 jobopt_T opt;
860 channel_T *channel;
861
862 address = get_tv_string(&argvars[0]);
863 if (argvars[1].v_type != VAR_UNKNOWN
864 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
865 {
866 EMSG(_(e_invarg));
867 return NULL;
868 }
869
870 /* parse address */
871 p = vim_strchr(address, ':');
872 if (p == NULL)
873 {
874 EMSG2(_(e_invarg2), address);
875 return NULL;
876 }
877 *p++ = NUL;
878 port = strtol((char *)p, &rest, 10);
879 if (*address == NUL || port <= 0 || *rest != NUL)
880 {
881 p[-1] = ':';
882 EMSG2(_(e_invarg2), address);
883 return NULL;
884 }
885
886 /* parse options */
887 clear_job_options(&opt);
888 opt.jo_mode = MODE_JSON;
889 opt.jo_timeout = 2000;
890 if (get_job_options(&argvars[1], &opt,
891 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
892 return NULL;
893 if (opt.jo_timeout < 0)
894 {
895 EMSG(_(e_invarg));
896 return NULL;
897 }
898
899 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
900 if (channel != NULL)
901 {
902 opt.jo_set = JO_ALL;
903 channel_set_options(channel, &opt);
904 }
905 return channel;
906}
907
Bram Moolenaarde279892016-03-11 22:19:44 +0100908 static void
909may_close_part(sock_T *fd)
910{
911 if (*fd != INVALID_FD)
912 {
913 fd_close(*fd);
914 *fd = INVALID_FD;
915 }
916}
917
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100918 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100919channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100920{
Bram Moolenaarde279892016-03-11 22:19:44 +0100921 if (in != INVALID_FD)
922 {
923 may_close_part(&channel->CH_IN_FD);
924 channel->CH_IN_FD = in;
925 }
926 if (out != INVALID_FD)
927 {
928# if defined(FEAT_GUI)
929 channel_gui_unregister_one(channel, PART_OUT);
930# endif
931 may_close_part(&channel->CH_OUT_FD);
932 channel->CH_OUT_FD = out;
933# if defined(FEAT_GUI)
934 channel_gui_register_one(channel, PART_OUT);
935# endif
936 }
937 if (err != INVALID_FD)
938 {
939# if defined(FEAT_GUI)
940 channel_gui_unregister_one(channel, PART_ERR);
941# endif
942 may_close_part(&channel->CH_ERR_FD);
943 channel->CH_ERR_FD = err;
944# if defined(FEAT_GUI)
945 channel_gui_register_one(channel, PART_ERR);
946# endif
947 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100948}
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100949
Bram Moolenaard6051b52016-02-28 15:49:03 +0100950/*
Bram Moolenaar014069a2016-03-03 22:51:40 +0100951 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +0100952 * This does not keep a refcount, when the job is freed ch_job is cleared.
953 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100954 void
Bram Moolenaar014069a2016-03-03 22:51:40 +0100955channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100956{
Bram Moolenaar77073442016-02-13 23:23:53 +0100957 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +0100958
959 channel_set_options(channel, options);
960
961 if (job->jv_in_buf != NULL)
962 {
963 chanpart_T *in_part = &channel->ch_part[PART_IN];
964
965 in_part->ch_buffer = job->jv_in_buf;
966 ch_logs(channel, "reading from buffer '%s'",
967 (char *)in_part->ch_buffer->b_ffname);
968 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +0100969 {
970 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
971 {
972 /* Special mode: send last-but-one line when appending a line
973 * to the buffer. */
974 in_part->ch_buffer->b_write_to_channel = TRUE;
975 in_part->ch_buf_top =
976 in_part->ch_buffer->b_ml.ml_line_count + 1;
977 }
978 else
979 in_part->ch_buf_top = options->jo_in_top;
980 }
Bram Moolenaar014069a2016-03-03 22:51:40 +0100981 else
982 in_part->ch_buf_top = 1;
983 if (options->jo_set & JO_IN_BOT)
984 in_part->ch_buf_bot = options->jo_in_bot;
985 else
986 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
987 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100988}
989
990/*
Bram Moolenaar187db502016-02-27 14:44:26 +0100991 * Find a buffer matching "name" or create a new one.
992 */
993 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +0100994find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +0100995{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100996 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +0100997 buf_T *save_curbuf = curbuf;
998
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100999 if (name != NULL && *name != NUL)
1000 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001001 if (buf == NULL)
1002 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001003 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001004 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001005 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001006 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001007#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001008 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1009 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001010#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001011 if (curbuf->b_ml.ml_mfp == NULL)
1012 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001013 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1014 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001015 changed_bytes(1, 0);
1016 curbuf = save_curbuf;
1017 }
1018
1019 return buf;
1020}
1021
1022/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001023 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001024 */
1025 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001026channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001027{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001028 int part;
1029 char_u **cbp;
1030 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001031
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001032 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001033 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001034 channel->ch_part[part].ch_mode = opt->jo_mode;
1035 if (opt->jo_set & JO_IN_MODE)
1036 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1037 if (opt->jo_set & JO_OUT_MODE)
1038 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1039 if (opt->jo_set & JO_ERR_MODE)
1040 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001041
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001042 if (opt->jo_set & JO_TIMEOUT)
1043 for (part = PART_SOCK; part <= PART_IN; ++part)
1044 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1045 if (opt->jo_set & JO_OUT_TIMEOUT)
1046 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1047 if (opt->jo_set & JO_ERR_TIMEOUT)
1048 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1049
1050 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001051 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001052 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001053 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001054 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001055 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001056 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1057 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001058 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001059 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001060 *pp = opt->jo_partial;
1061 if (*pp != NULL)
1062 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001063 }
1064 if (opt->jo_set & JO_OUT_CALLBACK)
1065 {
1066 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001067 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001068 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001069 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001070 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1071 *cbp = vim_strsave(opt->jo_out_cb);
1072 else
1073 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001074 *pp = opt->jo_out_partial;
1075 if (*pp != NULL)
1076 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001077 }
1078 if (opt->jo_set & JO_ERR_CALLBACK)
1079 {
1080 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001081 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001082 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001083 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001084 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1085 *cbp = vim_strsave(opt->jo_err_cb);
1086 else
1087 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001088 *pp = opt->jo_err_partial;
1089 if (*pp != NULL)
1090 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001091 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001092 if (opt->jo_set & JO_CLOSE_CALLBACK)
1093 {
1094 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001095 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001096 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001097 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001098 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1099 *cbp = vim_strsave(opt->jo_close_cb);
1100 else
1101 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001102 *pp = opt->jo_err_partial;
1103 if (*pp != NULL)
1104 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001105 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001106
1107 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1108 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001109 /* writing output to a buffer. Default mode is NL. */
1110 if (!(opt->jo_set & JO_OUT_MODE))
1111 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001112 if (opt->jo_set & JO_OUT_BUF)
1113 channel->ch_part[PART_OUT].ch_buffer =
1114 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1115 else
1116 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001117 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1118 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001119 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1120 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001121
1122 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1123 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1124 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1125 {
1126 /* writing err to a buffer. Default mode is NL. */
1127 if (!(opt->jo_set & JO_ERR_MODE))
1128 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1129 if (opt->jo_io[PART_ERR] == JIO_OUT)
1130 channel->ch_part[PART_ERR].ch_buffer =
1131 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001132 else if (opt->jo_set & JO_ERR_BUF)
1133 channel->ch_part[PART_ERR].ch_buffer =
1134 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001135 else
1136 channel->ch_part[PART_ERR].ch_buffer =
1137 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1138 ch_logs(channel, "writing err to buffer '%s'",
1139 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1140 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001141}
1142
1143/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001144 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001145 */
1146 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001147channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001148 channel_T *channel,
1149 int part,
1150 char_u *callback,
1151 partial_T *partial,
1152 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001153{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001154 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001155 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1156
1157 if (item != NULL)
1158 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001159 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 item->cq_partial = partial;
1161 if (partial != NULL)
1162 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001163 item->cq_seq_nr = id;
1164 item->cq_prev = head->cq_prev;
1165 head->cq_prev = item;
1166 item->cq_next = NULL;
1167 if (item->cq_prev == NULL)
1168 head->cq_next = item;
1169 else
1170 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001171 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001172}
1173
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001174 static void
1175write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1176{
1177 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001178 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001179 char_u *p;
1180
1181 /* TODO: check if channel can be written to, do not block on write */
1182 if ((p = alloc(len + 2)) == NULL)
1183 return;
1184 STRCPY(p, line);
1185 p[len] = NL;
1186 p[len + 1] = NUL;
1187 channel_send(channel, PART_IN, p, "write_buf_line()");
1188 vim_free(p);
1189}
1190
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001191/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001192 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001193 */
1194 void
1195channel_write_in(channel_T *channel)
1196{
1197 chanpart_T *in_part = &channel->ch_part[PART_IN];
1198 linenr_T lnum;
1199 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001200 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001201
1202 if (buf == NULL)
1203 return;
1204 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1205 {
1206 /* buffer was wiped out or unloaded */
1207 in_part->ch_buffer = NULL;
1208 return;
1209 }
1210 if (in_part->ch_fd == INVALID_FD)
1211 /* pipe was closed */
1212 return;
1213
1214 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1215 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1216 {
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001217 write_buf_line(buf, lnum, channel);
1218 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001219 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001220
1221 if (written == 1)
1222 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1223 else if (written > 1)
1224 ch_logn(channel, "written %d lines to channel", written);
1225
Bram Moolenaar014069a2016-03-03 22:51:40 +01001226 in_part->ch_buf_top = lnum;
1227}
1228
1229/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001230 * Write appended lines above the last one in "buf" to the channel.
1231 */
1232 void
1233channel_write_new_lines(buf_T *buf)
1234{
1235 channel_T *channel;
1236 int found_one = FALSE;
1237
1238 /* There could be more than one channel for the buffer, loop over all of
1239 * them. */
1240 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1241 {
1242 chanpart_T *in_part = &channel->ch_part[PART_IN];
1243 linenr_T lnum;
1244 int written = 0;
1245
1246 if (in_part->ch_buffer == buf)
1247 {
1248 if (in_part->ch_fd == INVALID_FD)
1249 /* pipe was closed */
1250 continue;
1251 found_one = TRUE;
1252 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1253 ++lnum)
1254 {
1255 write_buf_line(buf, lnum, channel);
1256 ++written;
1257 }
1258
1259 if (written == 1)
1260 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1261 else if (written > 1)
1262 ch_logn(channel, "written %d lines to channel", written);
1263
1264 in_part->ch_buf_bot = lnum;
1265 }
1266 }
1267 if (!found_one)
1268 buf->b_write_to_channel = FALSE;
1269}
1270
1271/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001272 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001273 */
1274 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001275invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1276 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001278 typval_T rettv;
1279 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001280
Bram Moolenaar77073442016-02-13 23:23:53 +01001281 argv[0].v_type = VAR_CHANNEL;
1282 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001283
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001284 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001285 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001286 clear_tv(&rettv);
1287
Bram Moolenaara3dc5e92016-03-15 23:19:14 +01001288 redraw_after_callback();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001289}
1290
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001291/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001292 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001293 * The caller must free it.
1294 * Returns NULL if there is nothing.
1295 */
1296 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001297channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001298{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001300 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001301 char_u *p;
1302
Bram Moolenaar77073442016-02-13 23:23:53 +01001303 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001304 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001305 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001306 p = node->rq_buffer;
1307 head->rq_next = node->rq_next;
1308 if (node->rq_next == NULL)
1309 head->rq_prev = NULL;
1310 else
1311 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001312 vim_free(node);
1313 return p;
1314}
1315
1316/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001317 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001318 */
1319 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001320channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001321{
1322 /* Concatenate everything into one buffer.
1323 * TODO: avoid multiple allocations. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001324 while (channel_collapse(channel, part) == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001325 ;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001326 return channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001327}
1328
1329/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001330 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001331 * Returns FAIL if that is not possible.
1332 */
1333 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001334channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001335{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001336 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001337 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001338 char_u *p;
1339
Bram Moolenaar77073442016-02-13 23:23:53 +01001340 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001341 return FAIL;
1342
Bram Moolenaar77073442016-02-13 23:23:53 +01001343 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1344 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001345 if (p == NULL)
1346 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001347 STRCPY(p, node->rq_buffer);
1348 STRCAT(p, node->rq_next->rq_buffer);
1349 vim_free(node->rq_next->rq_buffer);
1350 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001351
Bram Moolenaar77073442016-02-13 23:23:53 +01001352 /* dispose of the node and its buffer */
1353 head->rq_next = node->rq_next;
1354 head->rq_next->rq_prev = NULL;
1355 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001356 vim_free(node);
1357 return OK;
1358}
1359
1360/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001361 * Store "buf[len]" on "channel"/"part".
1362 * Returns OK or FAIL.
1363 */
1364 static int
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001365channel_save(channel_T *channel, int part, char_u *buf, int len, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001366{
1367 readq_T *node;
1368 readq_T *head = &channel->ch_part[part].ch_head;
1369 char_u *p;
1370 int i;
1371
1372 node = (readq_T *)alloc(sizeof(readq_T));
1373 if (node == NULL)
1374 return FAIL; /* out of memory */
1375 node->rq_buffer = alloc(len + 1);
1376 if (node->rq_buffer == NULL)
1377 {
1378 vim_free(node);
1379 return FAIL; /* out of memory */
1380 }
1381
1382 if (channel->ch_part[part].ch_mode == MODE_NL)
1383 {
1384 /* Drop any CR before a NL. */
1385 p = node->rq_buffer;
1386 for (i = 0; i < len; ++i)
1387 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1388 *p++ = buf[i];
1389 *p = NUL;
1390 }
1391 else
1392 {
1393 mch_memmove(node->rq_buffer, buf, len);
1394 node->rq_buffer[len] = NUL;
1395 }
1396
1397 /* append node to the tail of the queue */
1398 node->rq_next = NULL;
1399 node->rq_prev = head->rq_prev;
1400 if (head->rq_prev == NULL)
1401 head->rq_next = node;
1402 else
1403 head->rq_prev->rq_next = node;
1404 head->rq_prev = node;
1405
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001406 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001407 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001408 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001409 fprintf(log_fd, "'");
1410 if (fwrite(buf, len, 1, log_fd) != 1)
1411 return FAIL;
1412 fprintf(log_fd, "'\n");
1413 }
1414 return OK;
1415}
1416
1417/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001418 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001419 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001420 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001421 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001422 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001423channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001424{
1425 js_read_T reader;
1426 typval_T listtv;
1427 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001428 chanpart_T *chanpart = &channel->ch_part[part];
1429 jsonq_T *head = &chanpart->ch_json_head;
1430 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001431 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001432
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001433 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001434 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001435
1436 /* TODO: make reader work properly */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001437 /* reader.js_buf = channel_peek(channel, part); */
1438 reader.js_buf = channel_get_all(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001439 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001440 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001441 /* reader.js_fill = channel_fill; */
Bram Moolenaar77073442016-02-13 23:23:53 +01001442 reader.js_cookie = channel;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001443
1444 /* When a message is incomplete we wait for a short while for more to
1445 * arrive. After the delay drop the input, otherwise a truncated string
1446 * or list will make us hang. */
1447 status = json_decode(&reader, &listtv,
1448 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
1449 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001450 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001451 /* Only accept the response when it is a list with at least two
1452 * items. */
1453 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001454 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001455 if (listtv.v_type != VAR_LIST)
1456 ch_error(channel, "Did not receive a list, discarding");
1457 else
1458 ch_errorn(channel, "Expected list with two items, got %d",
1459 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001460 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001461 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001462 else
1463 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001464 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1465 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001467 else
1468 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001469 item->jq_value = alloc_tv();
1470 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001471 {
1472 vim_free(item);
1473 clear_tv(&listtv);
1474 }
1475 else
1476 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001477 *item->jq_value = listtv;
1478 item->jq_prev = head->jq_prev;
1479 head->jq_prev = item;
1480 item->jq_next = NULL;
1481 if (item->jq_prev == NULL)
1482 head->jq_next = item;
1483 else
1484 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001485 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001486 }
1487 }
1488 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001489
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001490 if (status == OK)
1491 chanpart->ch_waiting = FALSE;
1492 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001493 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001494 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001495 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001496 /* First time encountering incomplete message, set a deadline of
1497 * 100 msec. */
1498 ch_log(channel, "Incomplete message - wait for more");
1499 reader.js_used = 0;
1500 chanpart->ch_waiting = TRUE;
1501#ifdef WIN32
1502 chanpart->ch_deadline = GetTickCount() + 100L;
1503#else
1504 gettimeofday(&chanpart->ch_deadline, NULL);
1505 chanpart->ch_deadline.tv_usec += 100 * 1000;
1506 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1507 {
1508 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1509 ++chanpart->ch_deadline.tv_sec;
1510 }
1511#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001512 }
1513 else
1514 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001515 int timeout;
1516#ifdef WIN32
1517 timeout = GetTickCount() > chanpart->ch_deadline;
1518#else
1519 {
1520 struct timeval now_tv;
1521
1522 gettimeofday(&now_tv, NULL);
1523 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1524 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1525 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1526 }
1527#endif
1528 if (timeout)
1529 {
1530 status = FAIL;
1531 chanpart->ch_waiting = FALSE;
1532 }
1533 else
1534 {
1535 reader.js_used = 0;
1536 ch_log(channel, "still waiting on incomplete message");
1537 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001538 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001539 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001540
1541 if (status == FAIL)
1542 {
1543 ch_error(channel, "Decoding failed - discarding input");
1544 ret = FALSE;
1545 chanpart->ch_waiting = FALSE;
1546 }
1547 else if (reader.js_buf[reader.js_used] != NUL)
1548 {
1549 /* Put the unread part back into the channel.
1550 * TODO: insert in front */
1551 channel_save(channel, part, reader.js_buf + reader.js_used,
1552 (int)(reader.js_end - reader.js_buf) - reader.js_used, NULL);
1553 ret = status == MAYBE ? FALSE: TRUE;
1554 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001555 else
1556 ret = FALSE;
1557
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001558 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001559 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001560}
1561
1562/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001563 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001564 */
1565 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001566remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001567{
Bram Moolenaar77073442016-02-13 23:23:53 +01001568 if (node->cq_prev == NULL)
1569 head->cq_next = node->cq_next;
1570 else
1571 node->cq_prev->cq_next = node->cq_next;
1572 if (node->cq_next == NULL)
1573 head->cq_prev = node->cq_prev;
1574 else
1575 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001576}
1577
1578/*
1579 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001580 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001581 */
1582 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001583remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001584{
Bram Moolenaar77073442016-02-13 23:23:53 +01001585 if (node->jq_prev == NULL)
1586 head->jq_next = node->jq_next;
1587 else
1588 node->jq_prev->jq_next = node->jq_next;
1589 if (node->jq_next == NULL)
1590 head->jq_prev = node->jq_prev;
1591 else
1592 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001593 vim_free(node);
1594}
1595
1596/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001597 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001598 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001599 * When "id" is zero or negative jut get the first message. But not the one
1600 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001601 * Return OK when found and return the value in "rettv".
1602 * Return FAIL otherwise.
1603 */
1604 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001605channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001606{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001608 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001612 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001613 typval_T *tv = &l->lv_first->li_tv;
1614
1615 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001616 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001617 || tv->vval.v_number == 0
1618 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001620 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001621 if (tv->v_type == VAR_NUMBER)
1622 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001623 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624 return OK;
1625 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001627 }
1628 return FAIL;
1629}
1630
Bram Moolenaarece61b02016-02-20 21:39:05 +01001631#define CH_JSON_MAX_ARGS 4
1632
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001633/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001634 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001635 * "argv[0]" is the command string.
1636 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001637 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001638 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001639channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001640{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001641 char_u *cmd = argv[0].vval.v_string;
1642 char_u *arg;
1643 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001644
Bram Moolenaarece61b02016-02-20 21:39:05 +01001645 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001646 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001647 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001648 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001649 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001650 return;
1651 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001652 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001653 if (arg == NULL)
1654 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001655
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001656 if (STRCMP(cmd, "ex") == 0)
1657 {
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001658 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001659 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001660 }
1661 else if (STRCMP(cmd, "normal") == 0)
1662 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001663 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001664
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001665 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001666 ea.arg = arg;
1667 ea.addr_count = 0;
1668 ea.forceit = TRUE; /* no mapping */
1669 ex_normal(&ea);
1670 }
1671 else if (STRCMP(cmd, "redraw") == 0)
1672 {
1673 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001674
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001675 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001676 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001677 ex_redraw(&ea);
1678 showruler(FALSE);
1679 setcursor();
1680 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001681#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001682 if (gui.in_use)
1683 {
1684 gui_update_cursor(FALSE, FALSE);
1685 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001686 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001687#endif
1688 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001689 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001690 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001691 int is_call = cmd[0] == 'c';
1692 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001693
Bram Moolenaarece61b02016-02-20 21:39:05 +01001694 if (argv[id_idx].v_type != VAR_UNKNOWN
1695 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001696 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001697 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001698 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001699 EMSG("E904: last argument for expr/call must be a number");
1700 }
1701 else if (is_call && argv[2].v_type != VAR_LIST)
1702 {
1703 ch_error(channel, "third argument for call must be a list");
1704 if (p_verbose > 2)
1705 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001706 }
1707 else
1708 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001709 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001710 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001711 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001712 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001713
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001714 /* Don't pollute the display with errors. */
1715 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001716 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001717 {
1718 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001719 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001720 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001721 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001722 {
1723 ch_logs(channel, "Calling '%s'", (char *)arg);
1724 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1725 tv = &res_tv;
1726 else
1727 tv = NULL;
1728 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001729
1730 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001731 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001732 int id = argv[id_idx].vval.v_number;
1733
Bram Moolenaar55fab432016-02-07 16:53:13 +01001734 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001735 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001736 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001737 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001738 /* If evaluation failed or the result can't be encoded
1739 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001740 vim_free(json);
1741 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001742 err_tv.v_type = VAR_STRING;
1743 err_tv.vval.v_string = (char_u *)"ERROR";
1744 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001745 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001746 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001747 if (json != NULL)
1748 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001749 channel_send(channel,
1750 part == PART_SOCK ? PART_SOCK : PART_IN,
1751 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001752 vim_free(json);
1753 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001754 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001755 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001756 if (tv == &res_tv)
1757 clear_tv(tv);
1758 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001759 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001760 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001761 }
1762 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001763 {
1764 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001765 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001766 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001767}
1768
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001769 static void
1770invoke_one_time_callback(
1771 channel_T *channel,
1772 cbq_T *cbhead,
1773 cbq_T *item,
1774 typval_T *argv)
1775{
1776 ch_logs(channel, "Invoking one-time callback %s",
1777 (char *)item->cq_callback);
1778 /* Remove the item from the list first, if the callback
1779 * invokes ch_close() the list will be cleared. */
1780 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001781 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001782 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001783 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001784 vim_free(item);
1785}
1786
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001787 static void
1788append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
1789{
1790 buf_T *save_curbuf = curbuf;
1791 linenr_T lnum = buffer->b_ml.ml_line_count;
1792 int save_write_to = buffer->b_write_to_channel;
1793
1794 /* If the buffer is also used as input insert above the last
1795 * line. Don't write these lines. */
1796 if (save_write_to)
1797 {
1798 --lnum;
1799 buffer->b_write_to_channel = FALSE;
1800 }
1801
1802 /* Append to the buffer */
1803 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
1804
1805 curbuf = buffer;
1806 u_sync(TRUE);
1807 /* ignore undo failure, undo is not very useful here */
1808 ignored = u_save(lnum, lnum + 1);
1809
1810 ml_append(lnum, msg, 0, FALSE);
1811 appended_lines_mark(lnum, 1L);
1812 curbuf = save_curbuf;
1813
1814 if (buffer->b_nwindows > 0)
1815 {
1816 win_T *wp;
1817 win_T *save_curwin;
1818
1819 FOR_ALL_WINDOWS(wp)
1820 {
1821 if (wp->w_buffer == buffer
1822 && (save_write_to
1823 ? wp->w_cursor.lnum == lnum + 1
1824 : (wp->w_cursor.lnum == lnum
1825 && wp->w_cursor.col == 0)))
1826 {
1827 ++wp->w_cursor.lnum;
1828 save_curwin = curwin;
1829 curwin = wp;
1830 curbuf = curwin->w_buffer;
1831 scroll_cursor_bot(0, FALSE);
1832 curwin = save_curwin;
1833 curbuf = curwin->w_buffer;
1834 }
1835 }
1836 redraw_buf_later(buffer, VALID);
1837 channel_need_redraw = TRUE;
1838 }
1839
1840 if (save_write_to)
1841 {
1842 channel_T *ch;
1843
1844 /* Find channels reading from this buffer and adjust their
1845 * next-to-read line number. */
1846 buffer->b_write_to_channel = TRUE;
1847 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
1848 {
1849 chanpart_T *in_part = &ch->ch_part[PART_IN];
1850
1851 if (in_part->ch_buffer == buffer)
1852 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
1853 }
1854 }
1855}
1856
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001857/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001859 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001860 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001861 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001862may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001863{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001864 char_u *msg = NULL;
1865 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001866 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001867 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001868 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001869 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001870 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001871 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001872 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001873 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001874
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001875 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001877 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001878
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001879 /* Use a message-specific callback, part callback or channel callback */
1880 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
1881 if (cbitem->cq_seq_nr == 0)
1882 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001883 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001884 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001885 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001886 partial = cbitem->cq_partial;
1887 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001888 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001889 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001891 partial = channel->ch_part[part].ch_partial;
1892 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001893 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001894 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001895 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001896 partial = channel->ch_partial;
1897 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001898
Bram Moolenaar187db502016-02-27 14:44:26 +01001899 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001900 if (buffer != NULL && !buf_valid(buffer))
1901 {
1902 /* buffer was wiped out */
1903 channel->ch_part[part].ch_buffer = NULL;
1904 buffer = NULL;
1905 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001906
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001907 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001908 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001909 listitem_T *item;
1910 int argc = 0;
1911
Bram Moolenaard7ece102016-02-02 23:23:02 +01001912 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001913 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001914 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001915 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001916 channel_parse_json(channel, part);
1917 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001918 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001919 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920
Bram Moolenaarece61b02016-02-20 21:39:05 +01001921 for (item = listtv->vval.v_list->lv_first;
1922 item != NULL && argc < CH_JSON_MAX_ARGS;
1923 item = item->li_next)
1924 argv[argc++] = item->li_tv;
1925 while (argc < CH_JSON_MAX_ARGS)
1926 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001927
Bram Moolenaarece61b02016-02-20 21:39:05 +01001928 if (argv[0].v_type == VAR_STRING)
1929 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001930 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01001931 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01001932 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001933 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001934 }
1935
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001937 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001938 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001939 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01001940 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001941 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001942 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001944 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001945 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001946 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001947 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001948 return FALSE;
1949 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001950 else
1951 {
Bram Moolenaar187db502016-02-27 14:44:26 +01001952 /* If there is no callback or buffer drop the message. */
1953 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001954 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001955 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01001956 {
1957 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001958 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01001959 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001960 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001961 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001962
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001963 if (ch_mode == MODE_NL)
1964 {
1965 char_u *nl;
1966 char_u *buf;
1967
1968 /* See if we have a message ending in NL in the first buffer. If
1969 * not try to concatenate the first and the second buffer. */
1970 while (TRUE)
1971 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001972 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001973 nl = vim_strchr(buf, NL);
1974 if (nl != NULL)
1975 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001976 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001977 return FALSE; /* incomplete message */
1978 }
1979 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01001980 {
1981 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001982 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01001983 *nl = NUL;
1984 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001985 else
1986 {
1987 /* Copy the message into allocated memory and remove it from
1988 * the buffer. */
1989 msg = vim_strnsave(buf, (int)(nl - buf));
1990 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
1991 }
1992 }
1993 else
1994 /* For a raw channel we don't know where the message ends, just
1995 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001996 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001997
Bram Moolenaarbf73b912016-03-02 21:16:59 +01001998 if (msg == NULL)
1999 return FALSE; /* out of memory (and avoids Coverity warning) */
2000
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002001 argv[1].v_type = VAR_STRING;
2002 argv[1].vval.v_string = msg;
2003 }
2004
Bram Moolenaara07fec92016-02-05 21:04:08 +01002005 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002006 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002007 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002008
2009 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002010 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002011 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002012 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002013 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002014 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002015 break;
2016 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002017 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002018 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002019 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002020 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002022 if (buffer != NULL)
2023 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002024 if (msg == NULL)
2025 /* JSON or JS mode: re-encode the message. */
2026 msg = json_encode(listtv, ch_mode);
2027 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002028 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002029 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002030
Bram Moolenaar187db502016-02-27 14:44:26 +01002031 if (callback != NULL)
2032 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002033 if (cbitem != NULL)
2034 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2035 else
2036 {
2037 /* invoke the channel callback */
2038 ch_logs(channel, "Invoking channel callback %s",
2039 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002040 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002041 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002042 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002043 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002044 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002045 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002046
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002049 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002050
2051 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002052}
2053
2054/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002055 * Return TRUE when channel "channel" is open for writing to.
2056 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002057 */
2058 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002059channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002060{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002061 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002062 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002063}
2064
2065/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002066 * Return TRUE when channel "channel" is open for reading or writing.
2067 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002068 */
2069 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002070channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002071{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002072 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002073 || channel->CH_IN_FD != INVALID_FD
2074 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002075 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002076}
2077
2078/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002079 * Return a string indicating the status of the channel.
2080 */
2081 char *
2082channel_status(channel_T *channel)
2083{
2084 if (channel == NULL)
2085 return "fail";
2086 if (channel_is_open(channel))
2087 return "open";
2088 return "closed";
2089}
2090
2091/*
2092 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002093 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002094 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002095 */
2096 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002097channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002098{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002099 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002100
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002101#ifdef FEAT_GUI
2102 channel_gui_unregister(channel);
2103#endif
2104
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002105 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002106 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002107 sock_close(channel->CH_SOCK_FD);
2108 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002109 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002110 may_close_part(&channel->CH_IN_FD);
2111 may_close_part(&channel->CH_OUT_FD);
2112 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002113
Bram Moolenaar8b374212016-02-24 20:43:06 +01002114 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002115 {
2116 typval_T argv[1];
2117 typval_T rettv;
2118 int dummy;
2119
2120 /* invoke the close callback; increment the refcount to avoid it
2121 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002122 ch_logs(channel, "Invoking close callback %s",
2123 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002124 argv[0].v_type = VAR_CHANNEL;
2125 argv[0].vval.v_channel = channel;
2126 ++channel->ch_refcount;
2127 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002128 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2129 channel->ch_close_partial, NULL);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002130 clear_tv(&rettv);
2131 --channel->ch_refcount;
2132
2133 /* the callback is only called once */
2134 vim_free(channel->ch_close_cb);
2135 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002136 partial_unref(channel->ch_close_partial);
2137 channel->ch_close_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002138 }
2139
2140 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002141}
2142
Bram Moolenaard04a0202016-01-26 23:30:18 +01002143/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002144 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002145 * Returns NULL if there is nothing.
2146 */
2147 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002148channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002149{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002150 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002151
Bram Moolenaar77073442016-02-13 23:23:53 +01002152 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002153 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002154 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002155}
2156
2157/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002158 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002159 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002160 static void
2161channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002162{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002163 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2164 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002165
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002166 while (channel_peek(channel, part) != NULL)
2167 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002168
2169 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002170 {
2171 cbq_T *node = cb_head->cq_next;
2172
2173 remove_cb_node(cb_head, node);
2174 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002175 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002176 vim_free(node);
2177 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002178
2179 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002180 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002181 free_tv(json_head->jq_next->jq_value);
2182 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002183 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002184
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002185 vim_free(channel->ch_part[part].ch_callback);
2186 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002187 partial_unref(channel->ch_part[part].ch_partial);
2188 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002189}
2190
2191/*
2192 * Clear all the read buffers on "channel".
2193 */
2194 void
2195channel_clear(channel_T *channel)
2196{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002197 ch_log(channel, "Clearing channel");
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002198 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002199 channel_clear_one(channel, PART_OUT);
2200 channel_clear_one(channel, PART_ERR);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002201 vim_free(channel->ch_callback);
2202 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002203 partial_unref(channel->ch_partial);
2204 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002205 vim_free(channel->ch_close_cb);
2206 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002207 partial_unref(channel->ch_close_partial);
2208 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002209}
2210
Bram Moolenaar77073442016-02-13 23:23:53 +01002211#if defined(EXITFREE) || defined(PROTO)
2212 void
2213channel_free_all(void)
2214{
2215 channel_T *channel;
2216
Bram Moolenaard6051b52016-02-28 15:49:03 +01002217 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002218 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2219 channel_clear(channel);
2220}
2221#endif
2222
2223
Bram Moolenaard04a0202016-01-26 23:30:18 +01002224/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002225#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002226
2227/* Buffer size for reading incoming messages. */
2228#define MAXMSGSIZE 4096
2229
2230/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002231 * Check for reading from "fd" with "timeout" msec.
2232 * Return FAIL when there is nothing to read.
2233 */
2234 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002235channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002236{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002237 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002238 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002239
Bram Moolenaard8070362016-02-15 21:56:54 +01002240# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002241 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002242 {
2243 DWORD nread;
2244 int diff;
2245 DWORD deadline = GetTickCount() + timeout;
2246
2247 /* reading from a pipe, not a socket */
2248 while (TRUE)
2249 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002250 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2251 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002252 return OK;
2253 diff = deadline - GetTickCount();
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002254 if (diff <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002255 break;
2256 /* Wait for 5 msec.
2257 * TODO: increase the sleep time when looping more often */
2258 Sleep(5);
2259 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002260 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002261 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002262#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002263 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002264#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002265 struct timeval tval;
2266 fd_set rfds;
2267 int ret;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002268
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002269 FD_ZERO(&rfds);
2270 FD_SET((int)fd, &rfds);
2271 tval.tv_sec = timeout / 1000;
2272 tval.tv_usec = (timeout % 1000) * 1000;
2273 for (;;)
2274 {
2275 ret = select((int)fd + 1, &rfds, NULL, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002276# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002277 SOCK_ERRNO;
2278 if (ret == -1 && errno == EINTR)
2279 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002280# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002281 if (ret > 0)
2282 return OK;
2283 break;
2284 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002285#else
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002286 struct pollfd fds;
2287
2288 fds.fd = fd;
2289 fds.events = POLLIN;
2290 if (poll(&fds, 1, timeout) > 0)
2291 return OK;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002292#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002293 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002294 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002295}
2296
2297/*
2298 * Return a unique ID to be used in a message.
2299 */
2300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002302{
2303 static int next_id = 1;
2304
2305 return next_id++;
2306}
2307
2308/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002309 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002310 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002311 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002312 */
2313 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002314channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002315{
2316 static char_u *buf = NULL;
2317 int len = 0;
2318 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002319 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002320 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002321
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 fd = channel->ch_part[part].ch_fd;
2323 if (fd == INVALID_FD)
2324 {
2325 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002326 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002327 }
2328 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002329
2330 /* Allocate a buffer to read into. */
2331 if (buf == NULL)
2332 {
2333 buf = alloc(MAXMSGSIZE);
2334 if (buf == NULL)
2335 return; /* out of memory! */
2336 }
2337
2338 /* Keep on reading for as long as there is something to read.
2339 * Use select() or poll() to avoid blocking on a message that is exactly
2340 * MAXMSGSIZE long. */
2341 for (;;)
2342 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002343 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002344 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002345 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002346 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002347 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002348 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002349 if (len <= 0)
2350 break; /* error or nothing more to read */
2351
2352 /* Store the read message in the queue. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002353 channel_save(channel, part, buf, len, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002354 readlen += len;
2355 if (len < MAXMSGSIZE)
2356 break; /* did read everything that's available */
2357 }
2358
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002359 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002360 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002361 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002362 /* Do not give an error message, most likely the other end just
2363 * exited. */
2364 ch_errors(channel, "%s(): Cannot read from channel", func);
2365
Bram Moolenaard04a0202016-01-26 23:30:18 +01002366 /* Queue a "DETACH" netbeans message in the command queue in order to
2367 * terminate the netbeans session later. Do not end the session here
2368 * directly as we may be running in the context of a call to
2369 * netbeans_parse_messages():
2370 * netbeans_parse_messages
2371 * -> autocmd triggered while processing the netbeans cmd
2372 * -> ui_breakcheck
2373 * -> gui event loop or select loop
2374 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002375 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002376 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002377 if (channel->ch_part[part].ch_mode == MODE_RAW
2378 || channel->ch_part[part].ch_mode == MODE_NL)
2379 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002380 (int)STRLEN(DETACH_MSG_RAW), "PUT ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002381
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002382 /* TODO: When reading from stdout is not possible, should we try to
2383 * keep stdin and stderr open? Probably not, assume the other side
2384 * has died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002385 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002386 if (channel->ch_nb_close_cb != NULL)
2387 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002388 }
2389
2390#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002391 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002392 if (CH_HAS_GUI && gtk_main_level() > 0)
2393 gtk_main_quit();
2394#endif
2395}
2396
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002397/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002398 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002399 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002400 * Returns what was read in allocated memory.
2401 * Returns NULL in case of error or timeout.
2402 */
2403 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002404channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002405{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002406 char_u *buf;
2407 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002408 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002409 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002410 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002411
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002412 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002413 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002414
2415 while (TRUE)
2416 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002417 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002418 if (buf != NULL && (mode == MODE_RAW
2419 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2420 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002421 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002422 continue;
2423
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002424 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002425 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002426 return NULL;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002427 if (channel_wait(channel, fd, timeout) == FAIL)
2428 {
2429 ch_log(channel, "Timed out");
2430 return NULL;
2431 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002432 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002433 }
2434
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002435 if (mode == MODE_RAW)
2436 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002437 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002438 }
2439 else
2440 {
2441 nl = vim_strchr(buf, NL);
2442 if (nl[1] == NUL)
2443 {
2444 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002445 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002446 *nl = NUL;
2447 }
2448 else
2449 {
2450 /* Copy the message into allocated memory and remove it from the
2451 * buffer. */
2452 msg = vim_strnsave(buf, (int)(nl - buf));
2453 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2454 }
2455 }
2456 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002457 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002458 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002459}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002460
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002461/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002462 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002463 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002464 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002465 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002466 */
2467 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002468channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002469 channel_T *channel,
2470 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002471 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002472 int id,
2473 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002474{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002475 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002476 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002477 int timeout;
2478 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002479
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002480 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002481 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002482 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002483 for (;;)
2484 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002485 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002486
2487 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002488 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002489 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002490 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002491 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002492 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002493
Bram Moolenaard7ece102016-02-02 23:23:02 +01002494 if (!more)
2495 {
2496 /* Handle any other messages in the queue. If done some more
2497 * messages may have arrived. */
2498 if (channel_parse_messages())
2499 continue;
2500
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002501 /* Wait for up to the timeout. If there was an incomplete message
2502 * use the deadline for that. */
2503 timeout = timeout_arg;
2504 if (chanpart->ch_waiting)
2505 {
2506#ifdef WIN32
2507 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2508#else
2509 {
2510 struct timeval now_tv;
2511
2512 gettimeofday(&now_tv, NULL);
2513 timeout = (chanpart->ch_deadline.tv_sec
2514 - now_tv.tv_sec) * 1000
2515 + (chanpart->ch_deadline.tv_usec
2516 - now_tv.tv_usec) / 1000
2517 + 1;
2518 }
2519#endif
2520 if (timeout < 0)
2521 {
2522 /* Something went wrong, channel_parse_json() didn't
2523 * discard message. Cancel waiting. */
2524 chanpart->ch_waiting = FALSE;
2525 timeout = timeout_arg;
2526 }
2527 else if (timeout > timeout_arg)
2528 timeout = timeout_arg;
2529 }
2530 fd = chanpart->ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002531 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002532 {
2533 if (timeout == timeout_arg)
2534 {
2535 if (fd != INVALID_FD)
2536 ch_log(channel, "Timed out");
2537 break;
2538 }
2539 }
2540 else
2541 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002542 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002543 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002544 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002545 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002546}
2547
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002548/*
2549 * Common for ch_read() and ch_readraw().
2550 */
2551 void
2552common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2553{
2554 channel_T *channel;
2555 int part;
2556 jobopt_T opt;
2557 int mode;
2558 int timeout;
2559 int id = -1;
2560 typval_T *listtv = NULL;
2561
2562 /* return an empty string by default */
2563 rettv->v_type = VAR_STRING;
2564 rettv->vval.v_string = NULL;
2565
2566 clear_job_options(&opt);
2567 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2568 == FAIL)
2569 return;
2570
2571 channel = get_channel_arg(&argvars[0], TRUE);
2572 if (channel != NULL)
2573 {
2574 if (opt.jo_set & JO_PART)
2575 part = opt.jo_part;
2576 else
2577 part = channel_part_read(channel);
2578 mode = channel_get_mode(channel, part);
2579 timeout = channel_get_timeout(channel, part);
2580 if (opt.jo_set & JO_TIMEOUT)
2581 timeout = opt.jo_timeout;
2582
2583 if (raw || mode == MODE_RAW || mode == MODE_NL)
2584 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2585 else
2586 {
2587 if (opt.jo_set & JO_ID)
2588 id = opt.jo_id;
2589 channel_read_json_block(channel, part, timeout, id, &listtv);
2590 if (listtv != NULL)
2591 {
2592 *rettv = *listtv;
2593 vim_free(listtv);
2594 }
2595 else
2596 {
2597 rettv->v_type = VAR_SPECIAL;
2598 rettv->vval.v_number = VVAL_NONE;
2599 }
2600 }
2601 }
2602}
2603
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002604# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2605 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002606/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002607 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002608 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002609 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002610 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002611channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002612{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002613 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002614 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002615
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002616 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01002617 for (channel = first_channel; channel != NULL;
2618 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002619 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002620 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002621 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002622 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002623 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002624 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002625 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002626 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002627 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002628}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002629# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002630
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002631# if defined(WIN32) || defined(PROTO)
2632/*
2633 * Check the channels for anything that is ready to be read.
2634 * The data is put in the read queue.
2635 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002636 void
2637channel_handle_events(void)
2638{
2639 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002640 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002641 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002642
2643 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2644 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002645 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002646 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002647 {
2648 fd = channel->ch_part[part].ch_fd;
2649 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
2650 channel_read(channel, part, "channel_handle_events");
2651 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002652 }
2653}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002654# endif
2655
Bram Moolenaard04a0202016-01-26 23:30:18 +01002656/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002657 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002658 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002659 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002661 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002662channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002663{
Bram Moolenaard04a0202016-01-26 23:30:18 +01002664 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002665 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002666 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002667
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002668 fd = channel->ch_part[part].ch_fd;
2669 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002670 {
2671 if (!channel->ch_error && fun != NULL)
2672 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002673 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002674 EMSG2("E630: %s(): write while not connected", fun);
2675 }
2676 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002677 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002678 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002679
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002680 if (log_fd != NULL)
2681 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002682 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002683 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002684 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002685 fprintf(log_fd, "'\n");
2686 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01002687 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002688 }
2689
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002690 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002691 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002692 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002693 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002694 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002695 {
2696 if (!channel->ch_error && fun != NULL)
2697 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002698 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002699 EMSG2("E631: %s(): write failed", fun);
2700 }
2701 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002702 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002703 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002704
2705 channel->ch_error = FALSE;
2706 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002707}
2708
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002709/*
2710 * Common for "ch_sendexpr()" and "ch_sendraw()".
2711 * Returns the channel if the caller should read the response.
2712 * Sets "part_read" to the the read fd.
2713 * Otherwise returns NULL.
2714 */
2715 channel_T *
2716send_common(
2717 typval_T *argvars,
2718 char_u *text,
2719 int id,
2720 int eval,
2721 jobopt_T *opt,
2722 char *fun,
2723 int *part_read)
2724{
2725 channel_T *channel;
2726 int part_send;
2727
2728 channel = get_channel_arg(&argvars[0], TRUE);
2729 if (channel == NULL)
2730 return NULL;
2731 part_send = channel_part_send(channel);
2732 *part_read = channel_part_read(channel);
2733
2734 clear_job_options(opt);
2735 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
2736 return NULL;
2737
2738 /* Set the callback. An empty callback means no callback and not reading
2739 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
2740 * allowed. */
2741 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
2742 {
2743 if (eval)
2744 {
2745 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
2746 return NULL;
2747 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002748 channel_set_req_callback(channel, part_send,
2749 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002750 }
2751
2752 if (channel_send(channel, part_send, text, fun) == OK
2753 && opt->jo_callback == NULL)
2754 return channel;
2755 return NULL;
2756}
2757
2758/*
2759 * common for "ch_evalexpr()" and "ch_sendexpr()"
2760 */
2761 void
2762ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
2763{
2764 char_u *text;
2765 typval_T *listtv;
2766 channel_T *channel;
2767 int id;
2768 ch_mode_T ch_mode;
2769 int part_send;
2770 int part_read;
2771 jobopt_T opt;
2772 int timeout;
2773
2774 /* return an empty string by default */
2775 rettv->v_type = VAR_STRING;
2776 rettv->vval.v_string = NULL;
2777
2778 channel = get_channel_arg(&argvars[0], TRUE);
2779 if (channel == NULL)
2780 return;
2781 part_send = channel_part_send(channel);
2782
2783 ch_mode = channel_get_mode(channel, part_send);
2784 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
2785 {
2786 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
2787 return;
2788 }
2789
2790 id = channel_get_id();
2791 text = json_encode_nr_expr(id, &argvars[1],
2792 ch_mode == MODE_JS ? JSON_JS : 0);
2793 if (text == NULL)
2794 return;
2795
2796 channel = send_common(argvars, text, id, eval, &opt,
2797 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
2798 vim_free(text);
2799 if (channel != NULL && eval)
2800 {
2801 if (opt.jo_set & JO_TIMEOUT)
2802 timeout = opt.jo_timeout;
2803 else
2804 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002805 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
2806 == OK)
2807 {
2808 list_T *list = listtv->vval.v_list;
2809
2810 /* Move the item from the list and then change the type to
2811 * avoid the value being freed. */
2812 *rettv = list->lv_last->li_tv;
2813 list->lv_last->li_tv.v_type = VAR_NUMBER;
2814 free_tv(listtv);
2815 }
2816 }
2817}
2818
2819/*
2820 * common for "ch_evalraw()" and "ch_sendraw()"
2821 */
2822 void
2823ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
2824{
2825 char_u buf[NUMBUFLEN];
2826 char_u *text;
2827 channel_T *channel;
2828 int part_read;
2829 jobopt_T opt;
2830 int timeout;
2831
2832 /* return an empty string by default */
2833 rettv->v_type = VAR_STRING;
2834 rettv->vval.v_string = NULL;
2835
2836 text = get_tv_string_buf(&argvars[1], buf);
2837 channel = send_common(argvars, text, 0, eval, &opt,
2838 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
2839 if (channel != NULL && eval)
2840 {
2841 if (opt.jo_set & JO_TIMEOUT)
2842 timeout = opt.jo_timeout;
2843 else
2844 timeout = channel_get_timeout(channel, part_read);
2845 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
2846 }
2847}
2848
Bram Moolenaard04a0202016-01-26 23:30:18 +01002849# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002850/*
2851 * Add open channels to the poll struct.
2852 * Return the adjusted struct index.
2853 * The type of "fds" is hidden to avoid problems with the function proto.
2854 */
2855 int
2856channel_poll_setup(int nfd_in, void *fds_in)
2857{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002858 int nfd = nfd_in;
2859 channel_T *channel;
2860 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002861 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002862
Bram Moolenaar77073442016-02-13 23:23:53 +01002863 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002864 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002865 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002866 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002867 if (channel->ch_part[part].ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002868 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002869 channel->ch_part[part].ch_poll_idx = nfd;
2870 fds[nfd].fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002871 fds[nfd].events = POLLIN;
2872 nfd++;
2873 }
2874 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002875 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002876 }
2877 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002878
2879 return nfd;
2880}
2881
2882/*
2883 * The type of "fds" is hidden to avoid problems with the function proto.
2884 */
2885 int
2886channel_poll_check(int ret_in, void *fds_in)
2887{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002888 int ret = ret_in;
2889 channel_T *channel;
2890 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002891 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002892
Bram Moolenaar77073442016-02-13 23:23:53 +01002893 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002894 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002895 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002896 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002897 int idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002898
2899 if (ret > 0 && idx != -1 && fds[idx].revents & POLLIN)
2900 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002901 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002902 --ret;
2903 }
2904 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002905 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002906
2907 return ret;
2908}
Bram Moolenaard04a0202016-01-26 23:30:18 +01002909# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002910
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002911# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002912/*
2913 * The type of "rfds" is hidden to avoid problems with the function proto.
2914 */
2915 int
2916channel_select_setup(int maxfd_in, void *rfds_in)
2917{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002918 int maxfd = maxfd_in;
2919 channel_T *channel;
2920 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002921 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002922
Bram Moolenaar77073442016-02-13 23:23:53 +01002923 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002924 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002925 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002926 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002927 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002928
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002929 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002930 {
Bram Moolenaard8070362016-02-15 21:56:54 +01002931 FD_SET((int)fd, rfds);
2932 if (maxfd < (int)fd)
2933 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002934 }
2935 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002936 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002937
2938 return maxfd;
2939}
2940
2941/*
2942 * The type of "rfds" is hidden to avoid problems with the function proto.
2943 */
2944 int
2945channel_select_check(int ret_in, void *rfds_in)
2946{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002947 int ret = ret_in;
2948 channel_T *channel;
2949 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002950 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002951
Bram Moolenaar77073442016-02-13 23:23:53 +01002952 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002953 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002954 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002955 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002957
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002958 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002959 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002960 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002961 --ret;
2962 }
2963 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002964 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002965
2966 return ret;
2967}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002968# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002969
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002970/*
Bram Moolenaar187db502016-02-27 14:44:26 +01002971 * Return TRUE if "channel" has JSON or other typeahead.
2972 */
2973 static int
2974channel_has_readahead(channel_T *channel, int part)
2975{
2976 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2977
2978 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2979 {
2980 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2981 jsonq_T *item = head->jq_next;
2982
2983 return item != NULL;
2984 }
2985 return channel_peek(channel, part) != NULL;
2986}
2987
2988/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01002989 * Execute queued up commands.
2990 * Invoked from the main loop when it's safe to execute received commands.
2991 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002992 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002993 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002994channel_parse_messages(void)
2995{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002996 channel_T *channel = first_channel;
2997 int ret = FALSE;
2998 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002999 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003000
Bram Moolenaard0b65022016-03-06 21:50:33 +01003001 /* Only do this message when another message was given, otherwise we get
3002 * lots of them. */
3003 if (did_log_msg)
3004 {
3005 ch_log(NULL, "looking for messages on channels");
3006 did_log_msg = FALSE;
3007 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003008 while (channel != NULL)
3009 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003010 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003011 {
3012 /* channel is no longer useful, free it */
3013 channel_free(channel);
3014 channel = first_channel;
3015 part = PART_SOCK;
3016 continue;
3017 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003018 if (channel->ch_part[part].ch_fd != INVALID_FD
3019 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003020 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003021 /* Increase the refcount, in case the handler causes the channel
3022 * to be unreferenced or closed. */
3023 ++channel->ch_refcount;
3024 r = may_invoke_callback(channel, part);
3025 if (r == OK)
3026 ret = TRUE;
3027 if (channel_unref(channel) || r == OK)
3028 {
3029 /* channel was freed or something was done, start over */
3030 channel = first_channel;
3031 part = PART_SOCK;
3032 continue;
3033 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003034 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003035 if (part < PART_ERR)
3036 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003037 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003038 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003039 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003040 part = PART_SOCK;
3041 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003042 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003043
3044 if (channel_need_redraw && must_redraw)
3045 {
3046 channel_need_redraw = FALSE;
3047 update_screen(0);
3048 setcursor();
3049 cursor_on();
3050 out_flush();
3051 }
3052
Bram Moolenaard7ece102016-02-02 23:23:02 +01003053 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003054}
3055
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003056/*
3057 * Mark references to lists used in channels.
3058 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003059 int
3060set_ref_in_channel(int copyID)
3061{
Bram Moolenaar77073442016-02-13 23:23:53 +01003062 int abort = FALSE;
3063 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003064 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003065
Bram Moolenaar77073442016-02-13 23:23:53 +01003066 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003067 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003068 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003069 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003070 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3071 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003072
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003073 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003074 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003075 list_T *l = item->jq_value->vval.v_list;
3076
3077 if (l->lv_copyID != copyID)
3078 {
3079 l->lv_copyID = copyID;
3080 abort = abort || set_ref_in_list(l, copyID, NULL);
3081 }
3082 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003083 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003084 }
3085 }
3086 return abort;
3087}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003088
3089/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003090 * Return the "part" to write to for "channel".
3091 */
3092 int
3093channel_part_send(channel_T *channel)
3094{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003095 if (channel->CH_SOCK_FD == INVALID_FD)
3096 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003097 return PART_SOCK;
3098}
3099
3100/*
3101 * Return the default "part" to read from for "channel".
3102 */
3103 int
3104channel_part_read(channel_T *channel)
3105{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003106 if (channel->CH_SOCK_FD == INVALID_FD)
3107 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003108 return PART_SOCK;
3109}
3110
3111/*
3112 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003113 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003114 */
3115 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003116channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003117{
Bram Moolenaar77073442016-02-13 23:23:53 +01003118 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003119 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003120 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003121}
3122
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003123/*
3124 * Return the timeout of "channel"/"part"
3125 */
3126 int
3127channel_get_timeout(channel_T *channel, int part)
3128{
3129 return channel->ch_part[part].ch_timeout;
3130}
3131
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003132 static int
3133handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3134{
3135 char_u *val = get_tv_string(item);
3136
3137 opt->jo_set |= jo;
3138 if (STRCMP(val, "nl") == 0)
3139 *modep = MODE_NL;
3140 else if (STRCMP(val, "raw") == 0)
3141 *modep = MODE_RAW;
3142 else if (STRCMP(val, "js") == 0)
3143 *modep = MODE_JS;
3144 else if (STRCMP(val, "json") == 0)
3145 *modep = MODE_JSON;
3146 else
3147 {
3148 EMSG2(_(e_invarg2), val);
3149 return FAIL;
3150 }
3151 return OK;
3152}
3153
3154 static int
3155handle_io(typval_T *item, int part, jobopt_T *opt)
3156{
3157 char_u *val = get_tv_string(item);
3158
3159 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3160 if (STRCMP(val, "null") == 0)
3161 opt->jo_io[part] = JIO_NULL;
3162 else if (STRCMP(val, "pipe") == 0)
3163 opt->jo_io[part] = JIO_PIPE;
3164 else if (STRCMP(val, "file") == 0)
3165 opt->jo_io[part] = JIO_FILE;
3166 else if (STRCMP(val, "buffer") == 0)
3167 opt->jo_io[part] = JIO_BUFFER;
3168 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3169 opt->jo_io[part] = JIO_OUT;
3170 else
3171 {
3172 EMSG2(_(e_invarg2), val);
3173 return FAIL;
3174 }
3175 return OK;
3176}
3177
3178 void
3179clear_job_options(jobopt_T *opt)
3180{
3181 vim_memset(opt, 0, sizeof(jobopt_T));
3182}
3183
3184/*
3185 * Get the PART_ number from the first character of an option name.
3186 */
3187 static int
3188part_from_char(int c)
3189{
3190 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3191}
3192
3193/*
3194 * Get the option entries from the dict in "tv", parse them and put the result
3195 * in "opt".
3196 * Only accept options in "supported".
3197 * If an option value is invalid return FAIL.
3198 */
3199 int
3200get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3201{
3202 typval_T *item;
3203 char_u *val;
3204 dict_T *dict;
3205 int todo;
3206 hashitem_T *hi;
3207 int part;
3208
3209 opt->jo_set = 0;
3210 if (tv->v_type == VAR_UNKNOWN)
3211 return OK;
3212 if (tv->v_type != VAR_DICT)
3213 {
3214 EMSG(_(e_invarg));
3215 return FAIL;
3216 }
3217 dict = tv->vval.v_dict;
3218 if (dict == NULL)
3219 return OK;
3220
3221 todo = (int)dict->dv_hashtab.ht_used;
3222 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3223 if (!HASHITEM_EMPTY(hi))
3224 {
3225 item = &dict_lookup(hi)->di_tv;
3226
3227 if (STRCMP(hi->hi_key, "mode") == 0)
3228 {
3229 if (!(supported & JO_MODE))
3230 break;
3231 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3232 return FAIL;
3233 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003234 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003235 {
3236 if (!(supported & JO_IN_MODE))
3237 break;
3238 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3239 == FAIL)
3240 return FAIL;
3241 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003242 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003243 {
3244 if (!(supported & JO_OUT_MODE))
3245 break;
3246 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3247 == FAIL)
3248 return FAIL;
3249 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003250 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003251 {
3252 if (!(supported & JO_ERR_MODE))
3253 break;
3254 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3255 == FAIL)
3256 return FAIL;
3257 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003258 else if (STRCMP(hi->hi_key, "in_io") == 0
3259 || STRCMP(hi->hi_key, "out_io") == 0
3260 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003261 {
3262 if (!(supported & JO_OUT_IO))
3263 break;
3264 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3265 return FAIL;
3266 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003267 else if (STRCMP(hi->hi_key, "in_name") == 0
3268 || STRCMP(hi->hi_key, "out_name") == 0
3269 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003270 {
3271 part = part_from_char(*hi->hi_key);
3272
3273 if (!(supported & JO_OUT_IO))
3274 break;
3275 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3276 opt->jo_io_name[part] =
3277 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3278 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003279 else if (STRCMP(hi->hi_key, "in_buf") == 0
3280 || STRCMP(hi->hi_key, "out_buf") == 0
3281 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003282 {
3283 part = part_from_char(*hi->hi_key);
3284
3285 if (!(supported & JO_OUT_IO))
3286 break;
3287 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3288 opt->jo_io_buf[part] = get_tv_number(item);
3289 if (opt->jo_io_buf[part] <= 0)
3290 {
3291 EMSG2(_(e_invarg2), get_tv_string(item));
3292 return FAIL;
3293 }
3294 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3295 {
3296 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3297 return FAIL;
3298 }
3299 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003300 else if (STRCMP(hi->hi_key, "in_top") == 0
3301 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003302 {
3303 linenr_T *lp;
3304
3305 if (!(supported & JO_OUT_IO))
3306 break;
3307 if (hi->hi_key[3] == 't')
3308 {
3309 lp = &opt->jo_in_top;
3310 opt->jo_set |= JO_IN_TOP;
3311 }
3312 else
3313 {
3314 lp = &opt->jo_in_bot;
3315 opt->jo_set |= JO_IN_BOT;
3316 }
3317 *lp = get_tv_number(item);
3318 if (*lp < 0)
3319 {
3320 EMSG2(_(e_invarg2), get_tv_string(item));
3321 return FAIL;
3322 }
3323 }
3324 else if (STRCMP(hi->hi_key, "channel") == 0)
3325 {
3326 if (!(supported & JO_OUT_IO))
3327 break;
3328 opt->jo_set |= JO_CHANNEL;
3329 if (item->v_type != VAR_CHANNEL)
3330 {
3331 EMSG2(_(e_invarg2), "channel");
3332 return FAIL;
3333 }
3334 opt->jo_channel = item->vval.v_channel;
3335 }
3336 else if (STRCMP(hi->hi_key, "callback") == 0)
3337 {
3338 if (!(supported & JO_CALLBACK))
3339 break;
3340 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003341 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003342 if (opt->jo_callback == NULL)
3343 {
3344 EMSG2(_(e_invarg2), "callback");
3345 return FAIL;
3346 }
3347 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003348 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003349 {
3350 if (!(supported & JO_OUT_CALLBACK))
3351 break;
3352 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003353 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003354 if (opt->jo_out_cb == NULL)
3355 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003356 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003357 return FAIL;
3358 }
3359 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003360 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003361 {
3362 if (!(supported & JO_ERR_CALLBACK))
3363 break;
3364 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003365 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003366 if (opt->jo_err_cb == NULL)
3367 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003368 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003369 return FAIL;
3370 }
3371 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003372 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003373 {
3374 if (!(supported & JO_CLOSE_CALLBACK))
3375 break;
3376 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003377 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003378 if (opt->jo_close_cb == NULL)
3379 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003380 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003381 return FAIL;
3382 }
3383 }
3384 else if (STRCMP(hi->hi_key, "waittime") == 0)
3385 {
3386 if (!(supported & JO_WAITTIME))
3387 break;
3388 opt->jo_set |= JO_WAITTIME;
3389 opt->jo_waittime = get_tv_number(item);
3390 }
3391 else if (STRCMP(hi->hi_key, "timeout") == 0)
3392 {
3393 if (!(supported & JO_TIMEOUT))
3394 break;
3395 opt->jo_set |= JO_TIMEOUT;
3396 opt->jo_timeout = get_tv_number(item);
3397 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003398 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003399 {
3400 if (!(supported & JO_OUT_TIMEOUT))
3401 break;
3402 opt->jo_set |= JO_OUT_TIMEOUT;
3403 opt->jo_out_timeout = get_tv_number(item);
3404 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003405 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003406 {
3407 if (!(supported & JO_ERR_TIMEOUT))
3408 break;
3409 opt->jo_set |= JO_ERR_TIMEOUT;
3410 opt->jo_err_timeout = get_tv_number(item);
3411 }
3412 else if (STRCMP(hi->hi_key, "part") == 0)
3413 {
3414 if (!(supported & JO_PART))
3415 break;
3416 opt->jo_set |= JO_PART;
3417 val = get_tv_string(item);
3418 if (STRCMP(val, "err") == 0)
3419 opt->jo_part = PART_ERR;
3420 else
3421 {
3422 EMSG2(_(e_invarg2), val);
3423 return FAIL;
3424 }
3425 }
3426 else if (STRCMP(hi->hi_key, "id") == 0)
3427 {
3428 if (!(supported & JO_ID))
3429 break;
3430 opt->jo_set |= JO_ID;
3431 opt->jo_id = get_tv_number(item);
3432 }
3433 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3434 {
3435 if (!(supported & JO_STOPONEXIT))
3436 break;
3437 opt->jo_set |= JO_STOPONEXIT;
3438 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3439 opt->jo_soe_buf);
3440 if (opt->jo_stoponexit == NULL)
3441 {
3442 EMSG2(_(e_invarg2), "stoponexit");
3443 return FAIL;
3444 }
3445 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003446 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003447 {
3448 if (!(supported & JO_EXIT_CB))
3449 break;
3450 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003451 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3452 {
3453 opt->jo_exit_partial = item->vval.v_partial;
3454 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3455 }
3456 else
3457 opt->jo_exit_cb = get_tv_string_buf_chk(
3458 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003459 if (opt->jo_exit_cb == NULL)
3460 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003461 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003462 return FAIL;
3463 }
3464 }
3465 else
3466 break;
3467 --todo;
3468 }
3469 if (todo > 0)
3470 {
3471 EMSG2(_(e_invarg2), hi->hi_key);
3472 return FAIL;
3473 }
3474
3475 return OK;
3476}
3477
3478/*
3479 * Get the channel from the argument.
3480 * Returns NULL if the handle is invalid.
3481 */
3482 channel_T *
3483get_channel_arg(typval_T *tv, int check_open)
3484{
3485 channel_T *channel = NULL;
3486
3487 if (tv->v_type == VAR_JOB)
3488 {
3489 if (tv->vval.v_job != NULL)
3490 channel = tv->vval.v_job->jv_channel;
3491 }
3492 else if (tv->v_type == VAR_CHANNEL)
3493 {
3494 channel = tv->vval.v_channel;
3495 }
3496 else
3497 {
3498 EMSG2(_(e_invarg2), get_tv_string(tv));
3499 return NULL;
3500 }
3501
3502 if (check_open && (channel == NULL || !channel_is_open(channel)))
3503 {
3504 EMSG(_("E906: not an open channel"));
3505 return NULL;
3506 }
3507 return channel;
3508}
3509
3510static job_T *first_job = NULL;
3511
3512 static void
3513job_free(job_T *job)
3514{
3515 ch_log(job->jv_channel, "Freeing job");
3516 if (job->jv_channel != NULL)
3517 {
3518 /* The link from the channel to the job doesn't count as a reference,
3519 * thus don't decrement the refcount of the job. The reference from
3520 * the job to the channel does count the refrence, decrement it and
3521 * NULL the reference. We don't set ch_job_killed, unreferencing the
3522 * job doesn't mean it stops running. */
3523 job->jv_channel->ch_job = NULL;
3524 channel_unref(job->jv_channel);
3525 }
3526 mch_clear_job(job);
3527
3528 if (job->jv_next != NULL)
3529 job->jv_next->jv_prev = job->jv_prev;
3530 if (job->jv_prev == NULL)
3531 first_job = job->jv_next;
3532 else
3533 job->jv_prev->jv_next = job->jv_next;
3534
3535 vim_free(job->jv_stoponexit);
3536 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003537 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003538 vim_free(job);
3539}
3540
3541 void
3542job_unref(job_T *job)
3543{
3544 if (job != NULL && --job->jv_refcount <= 0)
3545 {
3546 /* Do not free the job when it has not ended yet and there is a
3547 * "stoponexit" flag or an exit callback. */
3548 if (job->jv_status != JOB_STARTED
3549 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
3550 {
3551 job_free(job);
3552 }
3553 else if (job->jv_channel != NULL)
3554 {
3555 /* Do remove the link to the channel, otherwise it hangs
3556 * around until Vim exits. See job_free() for refcount. */
3557 job->jv_channel->ch_job = NULL;
3558 channel_unref(job->jv_channel);
3559 job->jv_channel = NULL;
3560 }
3561 }
3562}
3563
3564/*
3565 * Allocate a job. Sets the refcount to one and sets options default.
3566 */
3567 static job_T *
3568job_alloc(void)
3569{
3570 job_T *job;
3571
3572 job = (job_T *)alloc_clear(sizeof(job_T));
3573 if (job != NULL)
3574 {
3575 job->jv_refcount = 1;
3576 job->jv_stoponexit = vim_strsave((char_u *)"term");
3577
3578 if (first_job != NULL)
3579 {
3580 first_job->jv_prev = job;
3581 job->jv_next = first_job;
3582 }
3583 first_job = job;
3584 }
3585 return job;
3586}
3587
3588 void
3589job_set_options(job_T *job, jobopt_T *opt)
3590{
3591 if (opt->jo_set & JO_STOPONEXIT)
3592 {
3593 vim_free(job->jv_stoponexit);
3594 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
3595 job->jv_stoponexit = NULL;
3596 else
3597 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
3598 }
3599 if (opt->jo_set & JO_EXIT_CB)
3600 {
3601 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003602 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003603 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003604 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003605 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003606 job->jv_exit_partial = NULL;
3607 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003608 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003609 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003610 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003611 job->jv_exit_partial = opt->jo_exit_partial;
3612 if (job->jv_exit_partial != NULL)
3613 ++job->jv_exit_partial->pt_refcount;
3614 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003615 }
3616}
3617
3618/*
3619 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
3620 */
3621 void
3622job_stop_on_exit()
3623{
3624 job_T *job;
3625
3626 for (job = first_job; job != NULL; job = job->jv_next)
3627 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
3628 mch_stop_job(job, job->jv_stoponexit);
3629}
3630
3631/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003632 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003633 */
3634 void
3635job_check_ended(void)
3636{
3637 static time_t last_check = 0;
3638 time_t now;
3639 job_T *job;
3640 job_T *next;
3641
3642 /* Only do this once in 10 seconds. */
3643 now = time(NULL);
3644 if (last_check + 10 < now)
3645 {
3646 last_check = now;
3647 for (job = first_job; job != NULL; job = next)
3648 {
3649 next = job->jv_next;
3650 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
3651 job_status(job); /* may free "job" */
3652 }
3653 }
3654}
3655
3656/*
3657 * "job_start()" function
3658 */
3659 job_T *
3660job_start(typval_T *argvars)
3661{
3662 job_T *job;
3663 char_u *cmd = NULL;
3664#if defined(UNIX)
3665# define USE_ARGV
3666 char **argv = NULL;
3667 int argc = 0;
3668#else
3669 garray_T ga;
3670#endif
3671 jobopt_T opt;
3672 int part;
3673
3674 job = job_alloc();
3675 if (job == NULL)
3676 return NULL;
3677
3678 job->jv_status = JOB_FAILED;
3679
3680 /* Default mode is NL. */
3681 clear_job_options(&opt);
3682 opt.jo_mode = MODE_NL;
3683 if (get_job_options(&argvars[1], &opt,
3684 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
3685 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
3686 return job;
3687
3688 /* Check that when io is "file" that there is a file name. */
3689 for (part = PART_OUT; part <= PART_IN; ++part)
3690 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
3691 && opt.jo_io[part] == JIO_FILE
3692 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
3693 || *opt.jo_io_name[part] == NUL))
3694 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003695 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003696 return job;
3697 }
3698
3699 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
3700 {
3701 buf_T *buf = NULL;
3702
3703 /* check that we can find the buffer before starting the job */
3704 if (opt.jo_set & JO_IN_BUF)
3705 {
3706 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
3707 if (buf == NULL)
3708 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
3709 }
3710 else if (!(opt.jo_set & JO_IN_NAME))
3711 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003712 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003713 }
3714 else
3715 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
3716 if (buf == NULL)
3717 return job;
3718 if (buf->b_ml.ml_mfp == NULL)
3719 {
3720 char_u numbuf[NUMBUFLEN];
3721 char_u *s;
3722
3723 if (opt.jo_set & JO_IN_BUF)
3724 {
3725 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
3726 s = numbuf;
3727 }
3728 else
3729 s = opt.jo_io_name[PART_IN];
3730 EMSG2(_("E918: buffer must be loaded: %s"), s);
3731 return job;
3732 }
3733 job->jv_in_buf = buf;
3734 }
3735
3736 job_set_options(job, &opt);
3737
3738#ifndef USE_ARGV
3739 ga_init2(&ga, (int)sizeof(char*), 20);
3740#endif
3741
3742 if (argvars[0].v_type == VAR_STRING)
3743 {
3744 /* Command is a string. */
3745 cmd = argvars[0].vval.v_string;
3746#ifdef USE_ARGV
3747 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
3748 return job;
3749 argv[argc] = NULL;
3750#endif
3751 }
3752 else if (argvars[0].v_type != VAR_LIST
3753 || argvars[0].vval.v_list == NULL
3754 || argvars[0].vval.v_list->lv_len < 1)
3755 {
3756 EMSG(_(e_invarg));
3757 return job;
3758 }
3759 else
3760 {
3761 list_T *l = argvars[0].vval.v_list;
3762 listitem_T *li;
3763 char_u *s;
3764
3765#ifdef USE_ARGV
3766 /* Pass argv[] to mch_call_shell(). */
3767 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
3768 if (argv == NULL)
3769 return job;
3770#endif
3771 for (li = l->lv_first; li != NULL; li = li->li_next)
3772 {
3773 s = get_tv_string_chk(&li->li_tv);
3774 if (s == NULL)
3775 goto theend;
3776#ifdef USE_ARGV
3777 argv[argc++] = (char *)s;
3778#else
3779 /* Only escape when needed, double quotes are not always allowed. */
3780 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
3781 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01003782# ifdef WIN32
3783 int old_ssl = p_ssl;
3784
3785 /* This is using CreateProcess, not cmd.exe. Always use
3786 * double quote and backslashes. */
3787 p_ssl = 0;
3788# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003789 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01003790# ifdef WIN32
3791 p_ssl = old_ssl;
3792# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003793 if (s == NULL)
3794 goto theend;
3795 ga_concat(&ga, s);
3796 vim_free(s);
3797 }
3798 else
3799 ga_concat(&ga, s);
3800 if (li->li_next != NULL)
3801 ga_append(&ga, ' ');
3802#endif
3803 }
3804#ifdef USE_ARGV
3805 argv[argc] = NULL;
3806#else
3807 cmd = ga.ga_data;
3808#endif
3809 }
3810
3811#ifdef USE_ARGV
3812 if (ch_log_active())
3813 {
3814 garray_T ga;
3815 int i;
3816
3817 ga_init2(&ga, (int)sizeof(char), 200);
3818 for (i = 0; i < argc; ++i)
3819 {
3820 if (i > 0)
3821 ga_concat(&ga, (char_u *)" ");
3822 ga_concat(&ga, (char_u *)argv[i]);
3823 }
3824 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
3825 ga_clear(&ga);
3826 }
3827 mch_start_job(argv, job, &opt);
3828#else
3829 ch_logs(NULL, "Starting job: %s", (char *)cmd);
3830 mch_start_job((char *)cmd, job, &opt);
3831#endif
3832
3833 /* If the channel is reading from a buffer, write lines now. */
3834 if (job->jv_channel != NULL)
3835 channel_write_in(job->jv_channel);
3836
3837theend:
3838#ifdef USE_ARGV
3839 vim_free(argv);
3840#else
3841 vim_free(ga.ga_data);
3842#endif
3843 return job;
3844}
3845
3846/*
3847 * Get the status of "job" and invoke the exit callback when needed.
3848 * The returned string is not allocated.
3849 */
3850 char *
3851job_status(job_T *job)
3852{
3853 char *result;
3854
3855 if (job->jv_status == JOB_ENDED)
3856 /* No need to check, dead is dead. */
3857 result = "dead";
3858 else if (job->jv_status == JOB_FAILED)
3859 result = "fail";
3860 else
3861 {
3862 result = mch_job_status(job);
3863 if (job->jv_status == JOB_ENDED)
3864 ch_log(job->jv_channel, "Job ended");
3865 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
3866 {
3867 typval_T argv[3];
3868 typval_T rettv;
3869 int dummy;
3870
3871 /* invoke the exit callback; make sure the refcount is > 0 */
3872 ++job->jv_refcount;
3873 argv[0].v_type = VAR_JOB;
3874 argv[0].vval.v_job = job;
3875 argv[1].v_type = VAR_NUMBER;
3876 argv[1].vval.v_number = job->jv_exitval;
3877 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003878 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
3879 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003880 clear_tv(&rettv);
3881 --job->jv_refcount;
3882 }
3883 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
3884 {
3885 /* The job was already unreferenced, now that it ended it can be
3886 * freed. Careful: caller must not use "job" after this! */
3887 job_free(job);
3888 }
3889 }
3890 return result;
3891}
3892
Bram Moolenaar8950a562016-03-12 15:22:55 +01003893/*
3894 * Implementation of job_info().
3895 */
3896 void
3897job_info(job_T *job, dict_T *dict)
3898{
3899 dictitem_T *item;
3900 varnumber_T nr;
3901
3902 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
3903
3904 item = dictitem_alloc((char_u *)"channel");
3905 if (item == NULL)
3906 return;
3907 item->di_tv.v_lock = 0;
3908 item->di_tv.v_type = VAR_CHANNEL;
3909 item->di_tv.vval.v_channel = job->jv_channel;
3910 if (job->jv_channel != NULL)
3911 ++job->jv_channel->ch_refcount;
3912 if (dict_add(dict, item) == FAIL)
3913 dictitem_free(item);
3914
3915#ifdef UNIX
3916 nr = job->jv_pid;
3917#else
3918 nr = job->jv_proc_info.dwProcessId;
3919#endif
3920 dict_add_nr_str(dict, "process", nr, NULL);
3921
3922 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003923 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01003924 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
3925}
3926
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003927 int
3928job_stop(job_T *job, typval_T *argvars)
3929{
3930 char_u *arg;
3931
3932 if (argvars[1].v_type == VAR_UNKNOWN)
3933 arg = (char_u *)"";
3934 else
3935 {
3936 arg = get_tv_string_chk(&argvars[1]);
3937 if (arg == NULL)
3938 {
3939 EMSG(_(e_invarg));
3940 return 0;
3941 }
3942 }
3943 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
3944 if (mch_stop_job(job, arg) == FAIL)
3945 return 0;
3946
3947 /* Assume that "hup" does not kill the job. */
3948 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
3949 job->jv_channel->ch_job_killed = TRUE;
3950
3951 /* We don't try freeing the job, obviously the caller still has a
3952 * reference to it. */
3953 return 1;
3954}
3955
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003956#endif /* FEAT_JOB_CHANNEL */