blob: 10f1bc8d185834536e11b77489556ec73e3b9496 [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);
1006#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001007 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1008 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001009#endif
1010 curbuf = buf;
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 Moolenaarb6b52522016-02-20 23:30:07 +01001028 int part;
1029 char_u **cbp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001030
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001031 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001032 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001033 channel->ch_part[part].ch_mode = opt->jo_mode;
1034 if (opt->jo_set & JO_IN_MODE)
1035 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1036 if (opt->jo_set & JO_OUT_MODE)
1037 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1038 if (opt->jo_set & JO_ERR_MODE)
1039 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001040
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001041 if (opt->jo_set & JO_TIMEOUT)
1042 for (part = PART_SOCK; part <= PART_IN; ++part)
1043 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1044 if (opt->jo_set & JO_OUT_TIMEOUT)
1045 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1046 if (opt->jo_set & JO_ERR_TIMEOUT)
1047 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1048
1049 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001050 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001051 cbp = &channel->ch_callback;
1052 vim_free(*cbp);
1053 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1054 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001055 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001056 *cbp = NULL;
1057 }
1058 if (opt->jo_set & JO_OUT_CALLBACK)
1059 {
1060 cbp = &channel->ch_part[PART_OUT].ch_callback;
1061 vim_free(*cbp);
1062 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1063 *cbp = vim_strsave(opt->jo_out_cb);
1064 else
1065 *cbp = NULL;
1066 }
1067 if (opt->jo_set & JO_ERR_CALLBACK)
1068 {
1069 cbp = &channel->ch_part[PART_ERR].ch_callback;
1070 vim_free(*cbp);
1071 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1072 *cbp = vim_strsave(opt->jo_err_cb);
1073 else
1074 *cbp = NULL;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001075 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001076 if (opt->jo_set & JO_CLOSE_CALLBACK)
1077 {
1078 cbp = &channel->ch_close_cb;
1079 vim_free(*cbp);
1080 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1081 *cbp = vim_strsave(opt->jo_close_cb);
1082 else
1083 *cbp = NULL;
1084 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001085
1086 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1087 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001088 /* writing output to a buffer. Default mode is NL. */
1089 if (!(opt->jo_set & JO_OUT_MODE))
1090 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001091 if (opt->jo_set & JO_OUT_BUF)
1092 channel->ch_part[PART_OUT].ch_buffer =
1093 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1094 else
1095 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001096 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1097 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001098 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1099 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001100
1101 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1102 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1103 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1104 {
1105 /* writing err to a buffer. Default mode is NL. */
1106 if (!(opt->jo_set & JO_ERR_MODE))
1107 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1108 if (opt->jo_io[PART_ERR] == JIO_OUT)
1109 channel->ch_part[PART_ERR].ch_buffer =
1110 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001111 else if (opt->jo_set & JO_ERR_BUF)
1112 channel->ch_part[PART_ERR].ch_buffer =
1113 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001114 else
1115 channel->ch_part[PART_ERR].ch_buffer =
1116 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1117 ch_logs(channel, "writing err to buffer '%s'",
1118 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1119 }
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001120}
1121
1122/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001123 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001124 */
1125 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001126channel_set_req_callback(
1127 channel_T *channel,
1128 int part,
1129 char_u *callback,
1130 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001131{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001132 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001133 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1134
1135 if (item != NULL)
1136 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001137 item->cq_callback = vim_strsave(callback);
1138 item->cq_seq_nr = id;
1139 item->cq_prev = head->cq_prev;
1140 head->cq_prev = item;
1141 item->cq_next = NULL;
1142 if (item->cq_prev == NULL)
1143 head->cq_next = item;
1144 else
1145 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001146 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001147}
1148
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001149 static void
1150write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1151{
1152 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001153 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001154 char_u *p;
1155
1156 /* TODO: check if channel can be written to, do not block on write */
1157 if ((p = alloc(len + 2)) == NULL)
1158 return;
1159 STRCPY(p, line);
1160 p[len] = NL;
1161 p[len + 1] = NUL;
1162 channel_send(channel, PART_IN, p, "write_buf_line()");
1163 vim_free(p);
1164}
1165
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001166/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001167 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001168 */
1169 void
1170channel_write_in(channel_T *channel)
1171{
1172 chanpart_T *in_part = &channel->ch_part[PART_IN];
1173 linenr_T lnum;
1174 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001175 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001176
1177 if (buf == NULL)
1178 return;
1179 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1180 {
1181 /* buffer was wiped out or unloaded */
1182 in_part->ch_buffer = NULL;
1183 return;
1184 }
1185 if (in_part->ch_fd == INVALID_FD)
1186 /* pipe was closed */
1187 return;
1188
1189 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1190 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1191 {
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001192 write_buf_line(buf, lnum, channel);
1193 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001194 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001195
1196 if (written == 1)
1197 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1198 else if (written > 1)
1199 ch_logn(channel, "written %d lines to channel", written);
1200
Bram Moolenaar014069a2016-03-03 22:51:40 +01001201 in_part->ch_buf_top = lnum;
1202}
1203
1204/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001205 * Write appended lines above the last one in "buf" to the channel.
1206 */
1207 void
1208channel_write_new_lines(buf_T *buf)
1209{
1210 channel_T *channel;
1211 int found_one = FALSE;
1212
1213 /* There could be more than one channel for the buffer, loop over all of
1214 * them. */
1215 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1216 {
1217 chanpart_T *in_part = &channel->ch_part[PART_IN];
1218 linenr_T lnum;
1219 int written = 0;
1220
1221 if (in_part->ch_buffer == buf)
1222 {
1223 if (in_part->ch_fd == INVALID_FD)
1224 /* pipe was closed */
1225 continue;
1226 found_one = TRUE;
1227 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1228 ++lnum)
1229 {
1230 write_buf_line(buf, lnum, channel);
1231 ++written;
1232 }
1233
1234 if (written == 1)
1235 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1236 else if (written > 1)
1237 ch_logn(channel, "written %d lines to channel", written);
1238
1239 in_part->ch_buf_bot = lnum;
1240 }
1241 }
1242 if (!found_one)
1243 buf->b_write_to_channel = FALSE;
1244}
1245
1246/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001247 * Invoke the "callback" on channel "channel".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001248 */
1249 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001250invoke_callback(channel_T *channel, char_u *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001251{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001252 typval_T rettv;
1253 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001254
Bram Moolenaar77073442016-02-13 23:23:53 +01001255 argv[0].v_type = VAR_CHANNEL;
1256 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001257
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001258 call_func(callback, (int)STRLEN(callback),
1259 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001260 clear_tv(&rettv);
1261
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001262 /* If an echo command was used the cursor needs to be put back where
Bram Moolenaar18b5d6d2016-02-28 19:30:24 +01001263 * it belongs. If highlighting was changed a redraw is needed. */
1264 update_screen(0);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001265 setcursor();
1266 cursor_on();
1267 out_flush();
Bram Moolenaar18b5d6d2016-02-28 19:30:24 +01001268#ifdef FEAT_GUI
Bram Moolenaara96909c2016-03-05 22:19:41 +01001269 if (gui.in_use)
1270 {
1271 gui_update_cursor(TRUE, FALSE);
1272 gui_mch_flush();
1273 }
Bram Moolenaar18b5d6d2016-02-28 19:30:24 +01001274#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001275}
1276
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001277/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001278 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001279 * The caller must free it.
1280 * Returns NULL if there is nothing.
1281 */
1282 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001283channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001284{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001285 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001286 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001287 char_u *p;
1288
Bram Moolenaar77073442016-02-13 23:23:53 +01001289 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001290 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001291 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001292 p = node->rq_buffer;
1293 head->rq_next = node->rq_next;
1294 if (node->rq_next == NULL)
1295 head->rq_prev = NULL;
1296 else
1297 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001298 vim_free(node);
1299 return p;
1300}
1301
1302/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001303 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001304 */
1305 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001306channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001307{
1308 /* Concatenate everything into one buffer.
1309 * TODO: avoid multiple allocations. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001310 while (channel_collapse(channel, part) == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001311 ;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001312 return channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001313}
1314
1315/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001316 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001317 * Returns FAIL if that is not possible.
1318 */
1319 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001320channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001321{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001322 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001323 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001324 char_u *p;
1325
Bram Moolenaar77073442016-02-13 23:23:53 +01001326 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001327 return FAIL;
1328
Bram Moolenaar77073442016-02-13 23:23:53 +01001329 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1330 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001331 if (p == NULL)
1332 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001333 STRCPY(p, node->rq_buffer);
1334 STRCAT(p, node->rq_next->rq_buffer);
1335 vim_free(node->rq_next->rq_buffer);
1336 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001337
Bram Moolenaar77073442016-02-13 23:23:53 +01001338 /* dispose of the node and its buffer */
1339 head->rq_next = node->rq_next;
1340 head->rq_next->rq_prev = NULL;
1341 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001342 vim_free(node);
1343 return OK;
1344}
1345
1346/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001347 * Store "buf[len]" on "channel"/"part".
1348 * Returns OK or FAIL.
1349 */
1350 static int
1351channel_save(channel_T *channel, int part, char_u *buf, int len)
1352{
1353 readq_T *node;
1354 readq_T *head = &channel->ch_part[part].ch_head;
1355 char_u *p;
1356 int i;
1357
1358 node = (readq_T *)alloc(sizeof(readq_T));
1359 if (node == NULL)
1360 return FAIL; /* out of memory */
1361 node->rq_buffer = alloc(len + 1);
1362 if (node->rq_buffer == NULL)
1363 {
1364 vim_free(node);
1365 return FAIL; /* out of memory */
1366 }
1367
1368 if (channel->ch_part[part].ch_mode == MODE_NL)
1369 {
1370 /* Drop any CR before a NL. */
1371 p = node->rq_buffer;
1372 for (i = 0; i < len; ++i)
1373 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1374 *p++ = buf[i];
1375 *p = NUL;
1376 }
1377 else
1378 {
1379 mch_memmove(node->rq_buffer, buf, len);
1380 node->rq_buffer[len] = NUL;
1381 }
1382
1383 /* append node to the tail of the queue */
1384 node->rq_next = NULL;
1385 node->rq_prev = head->rq_prev;
1386 if (head->rq_prev == NULL)
1387 head->rq_next = node;
1388 else
1389 head->rq_prev->rq_next = node;
1390 head->rq_prev = node;
1391
1392 if (log_fd != NULL)
1393 {
1394 ch_log_lead("RECV ", channel);
1395 fprintf(log_fd, "'");
1396 if (fwrite(buf, len, 1, log_fd) != 1)
1397 return FAIL;
1398 fprintf(log_fd, "'\n");
1399 }
1400 return OK;
1401}
1402
1403/*
1404 * Use the read buffer of "channel"/"part" and parse a JSON messages that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001405 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001406 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001407 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001408 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001409channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001410{
1411 js_read_T reader;
1412 typval_T listtv;
1413 jsonq_T *item;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001414 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001415 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001416
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001417 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001418 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001419
1420 /* TODO: make reader work properly */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001421 /* reader.js_buf = channel_peek(channel, part); */
1422 reader.js_buf = channel_get_all(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001423 reader.js_used = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001424 reader.js_fill = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001425 /* reader.js_fill = channel_fill; */
Bram Moolenaar77073442016-02-13 23:23:53 +01001426 reader.js_cookie = channel;
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001427 ret = json_decode(&reader, &listtv,
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001428 channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001429 if (ret == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001430 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001431 /* Only accept the response when it is a list with at least two
1432 * items. */
1433 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001434 {
1435 /* TODO: give error */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001436 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001437 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001438 else
1439 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001440 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1441 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001442 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001443 else
1444 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001445 item->jq_value = alloc_tv();
1446 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001447 {
1448 vim_free(item);
1449 clear_tv(&listtv);
1450 }
1451 else
1452 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001453 *item->jq_value = listtv;
1454 item->jq_prev = head->jq_prev;
1455 head->jq_prev = item;
1456 item->jq_next = NULL;
1457 if (item->jq_prev == NULL)
1458 head->jq_next = item;
1459 else
1460 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001461 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001462 }
1463 }
1464 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001465
1466 /* Put the unread part back into the channel.
1467 * TODO: insert in front */
1468 if (reader.js_buf[reader.js_used] != NUL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001469 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001470 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001471 (int)(reader.js_end - reader.js_buf) - reader.js_used);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001472 ret = TRUE;
1473 }
1474 else
1475 ret = FALSE;
1476
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001477 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001478 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001479}
1480
1481/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001482 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001483 */
1484 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001485remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001486{
Bram Moolenaar77073442016-02-13 23:23:53 +01001487 if (node->cq_prev == NULL)
1488 head->cq_next = node->cq_next;
1489 else
1490 node->cq_prev->cq_next = node->cq_next;
1491 if (node->cq_next == NULL)
1492 head->cq_prev = node->cq_prev;
1493 else
1494 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001495}
1496
1497/*
1498 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001499 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001500 */
1501 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001502remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001503{
Bram Moolenaar77073442016-02-13 23:23:53 +01001504 if (node->jq_prev == NULL)
1505 head->jq_next = node->jq_next;
1506 else
1507 node->jq_prev->jq_next = node->jq_next;
1508 if (node->jq_next == NULL)
1509 head->jq_prev = node->jq_prev;
1510 else
1511 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001512 vim_free(node);
1513}
1514
1515/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001516 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001517 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001518 * When "id" is zero or negative jut get the first message. But not the one
1519 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001520 * Return OK when found and return the value in "rettv".
1521 * Return FAIL otherwise.
1522 */
1523 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001524channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001525{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001526 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001527 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001528
Bram Moolenaar77073442016-02-13 23:23:53 +01001529 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001530 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001532 typval_T *tv = &l->lv_first->li_tv;
1533
1534 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001535 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001536 || tv->vval.v_number == 0
1537 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001538 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001539 *rettv = item->jq_value;
1540 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001541 return OK;
1542 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001543 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001544 }
1545 return FAIL;
1546}
1547
Bram Moolenaarece61b02016-02-20 21:39:05 +01001548#define CH_JSON_MAX_ARGS 4
1549
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001550/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001551 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001552 * "argv[0]" is the command string.
1553 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001554 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001555 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001556channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001557{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001558 char_u *cmd = argv[0].vval.v_string;
1559 char_u *arg;
1560 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001561
Bram Moolenaarece61b02016-02-20 21:39:05 +01001562 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001563 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001564 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001565 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001566 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001567 return;
1568 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001569 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001570 if (arg == NULL)
1571 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001572
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001573 if (STRCMP(cmd, "ex") == 0)
1574 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001575 do_cmdline_cmd(arg);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001576 }
1577 else if (STRCMP(cmd, "normal") == 0)
1578 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001579 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001580
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001581 ea.arg = arg;
1582 ea.addr_count = 0;
1583 ea.forceit = TRUE; /* no mapping */
1584 ex_normal(&ea);
1585 }
1586 else if (STRCMP(cmd, "redraw") == 0)
1587 {
1588 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001589
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001590 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001591 ex_redraw(&ea);
1592 showruler(FALSE);
1593 setcursor();
1594 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001595#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001596 if (gui.in_use)
1597 {
1598 gui_update_cursor(FALSE, FALSE);
1599 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001600 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001601#endif
1602 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001603 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001604 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001605 int is_call = cmd[0] == 'c';
1606 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001607
Bram Moolenaarece61b02016-02-20 21:39:05 +01001608 if (argv[id_idx].v_type != VAR_UNKNOWN
1609 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001610 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001611 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001612 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001613 EMSG("E904: last argument for expr/call must be a number");
1614 }
1615 else if (is_call && argv[2].v_type != VAR_LIST)
1616 {
1617 ch_error(channel, "third argument for call must be a list");
1618 if (p_verbose > 2)
1619 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001620 }
1621 else
1622 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001623 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001624 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001625 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001626 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001627
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001628 /* Don't pollute the display with errors. */
1629 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001630 if (!is_call)
1631 tv = eval_expr(arg, NULL);
1632 else if (func_call(arg, &argv[2], NULL, &res_tv) == OK)
1633 tv = &res_tv;
1634 else
1635 tv = NULL;
1636
1637 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001638 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001639 int id = argv[id_idx].vval.v_number;
1640
Bram Moolenaar55fab432016-02-07 16:53:13 +01001641 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001642 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001643 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001644 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001645 /* If evaluation failed or the result can't be encoded
1646 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001647 vim_free(json);
1648 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001649 err_tv.v_type = VAR_STRING;
1650 err_tv.vval.v_string = (char_u *)"ERROR";
1651 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001652 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001653 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001654 if (json != NULL)
1655 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001656 channel_send(channel,
1657 part == PART_SOCK ? PART_SOCK : PART_IN,
1658 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001659 vim_free(json);
1660 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001661 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001662 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001663 if (tv == &res_tv)
1664 clear_tv(tv);
1665 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001666 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001667 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001668 }
1669 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001670 {
1671 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001672 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001673 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001674}
1675
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001676 static void
1677invoke_one_time_callback(
1678 channel_T *channel,
1679 cbq_T *cbhead,
1680 cbq_T *item,
1681 typval_T *argv)
1682{
1683 ch_logs(channel, "Invoking one-time callback %s",
1684 (char *)item->cq_callback);
1685 /* Remove the item from the list first, if the callback
1686 * invokes ch_close() the list will be cleared. */
1687 remove_cb_node(cbhead, item);
1688 invoke_callback(channel, item->cq_callback, argv);
1689 vim_free(item->cq_callback);
1690 vim_free(item);
1691}
1692
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001693 static void
1694append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
1695{
1696 buf_T *save_curbuf = curbuf;
1697 linenr_T lnum = buffer->b_ml.ml_line_count;
1698 int save_write_to = buffer->b_write_to_channel;
1699
1700 /* If the buffer is also used as input insert above the last
1701 * line. Don't write these lines. */
1702 if (save_write_to)
1703 {
1704 --lnum;
1705 buffer->b_write_to_channel = FALSE;
1706 }
1707
1708 /* Append to the buffer */
1709 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
1710
1711 curbuf = buffer;
1712 u_sync(TRUE);
1713 /* ignore undo failure, undo is not very useful here */
1714 ignored = u_save(lnum, lnum + 1);
1715
1716 ml_append(lnum, msg, 0, FALSE);
1717 appended_lines_mark(lnum, 1L);
1718 curbuf = save_curbuf;
1719
1720 if (buffer->b_nwindows > 0)
1721 {
1722 win_T *wp;
1723 win_T *save_curwin;
1724
1725 FOR_ALL_WINDOWS(wp)
1726 {
1727 if (wp->w_buffer == buffer
1728 && (save_write_to
1729 ? wp->w_cursor.lnum == lnum + 1
1730 : (wp->w_cursor.lnum == lnum
1731 && wp->w_cursor.col == 0)))
1732 {
1733 ++wp->w_cursor.lnum;
1734 save_curwin = curwin;
1735 curwin = wp;
1736 curbuf = curwin->w_buffer;
1737 scroll_cursor_bot(0, FALSE);
1738 curwin = save_curwin;
1739 curbuf = curwin->w_buffer;
1740 }
1741 }
1742 redraw_buf_later(buffer, VALID);
1743 channel_need_redraw = TRUE;
1744 }
1745
1746 if (save_write_to)
1747 {
1748 channel_T *ch;
1749
1750 /* Find channels reading from this buffer and adjust their
1751 * next-to-read line number. */
1752 buffer->b_write_to_channel = TRUE;
1753 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
1754 {
1755 chanpart_T *in_part = &ch->ch_part[PART_IN];
1756
1757 if (in_part->ch_buffer == buffer)
1758 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
1759 }
1760 }
1761}
1762
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001763/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001764 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001765 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001766 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001767 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001768may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001769{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001770 char_u *msg = NULL;
1771 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001772 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001773 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001774 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001775 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001776 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001777 char_u *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001778 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001779
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001780 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001781 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001782 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001783
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001784 /* Use a message-specific callback, part callback or channel callback */
1785 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
1786 if (cbitem->cq_seq_nr == 0)
1787 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001788 if (cbitem != NULL)
1789 callback = cbitem->cq_callback;
1790 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791 callback = channel->ch_part[part].ch_callback;
1792 else
1793 callback = channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001794
Bram Moolenaar187db502016-02-27 14:44:26 +01001795 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001796 if (buffer != NULL && !buf_valid(buffer))
1797 {
1798 /* buffer was wiped out */
1799 channel->ch_part[part].ch_buffer = NULL;
1800 buffer = NULL;
1801 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001803 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001804 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001805 listitem_T *item;
1806 int argc = 0;
1807
Bram Moolenaard7ece102016-02-02 23:23:02 +01001808 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001809 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001810 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001811 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001812 channel_parse_json(channel, part);
1813 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001814 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001815 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001816
Bram Moolenaarece61b02016-02-20 21:39:05 +01001817 for (item = listtv->vval.v_list->lv_first;
1818 item != NULL && argc < CH_JSON_MAX_ARGS;
1819 item = item->li_next)
1820 argv[argc++] = item->li_tv;
1821 while (argc < CH_JSON_MAX_ARGS)
1822 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001823
Bram Moolenaarece61b02016-02-20 21:39:05 +01001824 if (argv[0].v_type == VAR_STRING)
1825 {
1826 char_u *cmd = argv[0].vval.v_string;
1827
1828 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaar77073442016-02-13 23:23:53 +01001829 ch_logs(channel, "Executing %s command", (char *)cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001830 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01001831 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001832 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001833 }
1834
Bram Moolenaarece61b02016-02-20 21:39:05 +01001835 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001837 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001838 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01001839 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001840 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001841 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001842 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001843 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001844 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001845 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001846 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 return FALSE;
1848 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001849 else
1850 {
Bram Moolenaar187db502016-02-27 14:44:26 +01001851 /* If there is no callback or buffer drop the message. */
1852 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001853 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001854 while ((msg = channel_get(channel, part)) != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +01001855 {
1856 ch_logs(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001857 vim_free(msg);
Bram Moolenaard6051b52016-02-28 15:49:03 +01001858 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001859 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001860 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001861
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001862 if (ch_mode == MODE_NL)
1863 {
1864 char_u *nl;
1865 char_u *buf;
1866
1867 /* See if we have a message ending in NL in the first buffer. If
1868 * not try to concatenate the first and the second buffer. */
1869 while (TRUE)
1870 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001871 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001872 nl = vim_strchr(buf, NL);
1873 if (nl != NULL)
1874 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001875 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001876 return FALSE; /* incomplete message */
1877 }
1878 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01001879 {
1880 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001881 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01001882 *nl = NUL;
1883 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001884 else
1885 {
1886 /* Copy the message into allocated memory and remove it from
1887 * the buffer. */
1888 msg = vim_strnsave(buf, (int)(nl - buf));
1889 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
1890 }
1891 }
1892 else
1893 /* For a raw channel we don't know where the message ends, just
1894 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001895 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01001896
Bram Moolenaarbf73b912016-03-02 21:16:59 +01001897 if (msg == NULL)
1898 return FALSE; /* out of memory (and avoids Coverity warning) */
1899
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001900 argv[1].v_type = VAR_STRING;
1901 argv[1].vval.v_string = msg;
1902 }
1903
Bram Moolenaara07fec92016-02-05 21:04:08 +01001904 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001905 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001906 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001907
1908 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01001909 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001910 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001911 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001912 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001913 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001914 break;
1915 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001916 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01001917 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001918 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001919 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920 {
Bram Moolenaar187db502016-02-27 14:44:26 +01001921 if (buffer != NULL)
1922 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001923 if (msg == NULL)
1924 /* JSON or JS mode: re-encode the message. */
1925 msg = json_encode(listtv, ch_mode);
1926 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001927 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01001928 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001929
Bram Moolenaar187db502016-02-27 14:44:26 +01001930 if (callback != NULL)
1931 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01001932 if (cbitem != NULL)
1933 invoke_one_time_callback(channel, cbhead, cbitem, argv);
1934 else
1935 {
1936 /* invoke the channel callback */
1937 ch_logs(channel, "Invoking channel callback %s",
1938 (char *)callback);
1939 invoke_callback(channel, callback, argv);
1940 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001941 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001942 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001943 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001944 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001945
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001946 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01001947 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001948 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001949
1950 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001951}
1952
1953/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001954 * Return TRUE when channel "channel" is open for writing to.
1955 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01001956 */
1957 int
Bram Moolenaar77073442016-02-13 23:23:53 +01001958channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001959{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001960 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001961 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01001962}
1963
1964/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001965 * Return TRUE when channel "channel" is open for reading or writing.
1966 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001967 */
1968 int
Bram Moolenaar77073442016-02-13 23:23:53 +01001969channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001970{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001971 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001972 || channel->CH_IN_FD != INVALID_FD
1973 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001974 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001975}
1976
1977/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001978 * Return a string indicating the status of the channel.
1979 */
1980 char *
1981channel_status(channel_T *channel)
1982{
1983 if (channel == NULL)
1984 return "fail";
1985 if (channel_is_open(channel))
1986 return "open";
1987 return "closed";
1988}
1989
1990/*
1991 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01001992 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01001993 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01001994 */
1995 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01001996channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01001997{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01001998 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01001999
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002000#ifdef FEAT_GUI
2001 channel_gui_unregister(channel);
2002#endif
2003
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002004 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002005 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002006 sock_close(channel->CH_SOCK_FD);
2007 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002008 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002009 may_close_part(&channel->CH_IN_FD);
2010 may_close_part(&channel->CH_OUT_FD);
2011 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002012
Bram Moolenaar8b374212016-02-24 20:43:06 +01002013 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002014 {
2015 typval_T argv[1];
2016 typval_T rettv;
2017 int dummy;
2018
2019 /* invoke the close callback; increment the refcount to avoid it
2020 * being freed halfway */
Bram Moolenaard6051b52016-02-28 15:49:03 +01002021 ch_logs(channel, "Invoking close callback %s",
2022 (char *)channel->ch_close_cb);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002023 argv[0].v_type = VAR_CHANNEL;
2024 argv[0].vval.v_channel = channel;
2025 ++channel->ch_refcount;
2026 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
2027 &rettv, 1, argv, 0L, 0L, &dummy, TRUE, NULL);
2028 clear_tv(&rettv);
2029 --channel->ch_refcount;
2030
2031 /* the callback is only called once */
2032 vim_free(channel->ch_close_cb);
2033 channel->ch_close_cb = NULL;
2034 }
2035
2036 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002037}
2038
Bram Moolenaard04a0202016-01-26 23:30:18 +01002039/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002040 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002041 * Returns NULL if there is nothing.
2042 */
2043 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002044channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002045{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002046 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002047
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002049 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002051}
2052
2053/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002054 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002055 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002056 static void
2057channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002058{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002059 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2060 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002061
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002062 while (channel_peek(channel, part) != NULL)
2063 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002064
2065 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002066 {
2067 cbq_T *node = cb_head->cq_next;
2068
2069 remove_cb_node(cb_head, node);
2070 vim_free(node->cq_callback);
2071 vim_free(node);
2072 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002073
2074 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002075 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002076 free_tv(json_head->jq_next->jq_value);
2077 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002078 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002079
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002080 vim_free(channel->ch_part[part].ch_callback);
2081 channel->ch_part[part].ch_callback = NULL;
2082}
2083
2084/*
2085 * Clear all the read buffers on "channel".
2086 */
2087 void
2088channel_clear(channel_T *channel)
2089{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002090 ch_log(channel, "Clearing channel");
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002091 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002092 channel_clear_one(channel, PART_OUT);
2093 channel_clear_one(channel, PART_ERR);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002094 vim_free(channel->ch_callback);
2095 channel->ch_callback = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002096 vim_free(channel->ch_close_cb);
2097 channel->ch_close_cb = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002098}
2099
Bram Moolenaar77073442016-02-13 23:23:53 +01002100#if defined(EXITFREE) || defined(PROTO)
2101 void
2102channel_free_all(void)
2103{
2104 channel_T *channel;
2105
Bram Moolenaard6051b52016-02-28 15:49:03 +01002106 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002107 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2108 channel_clear(channel);
2109}
2110#endif
2111
2112
Bram Moolenaard04a0202016-01-26 23:30:18 +01002113/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002114#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002115
2116/* Buffer size for reading incoming messages. */
2117#define MAXMSGSIZE 4096
2118
2119/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002120 * Check for reading from "fd" with "timeout" msec.
2121 * Return FAIL when there is nothing to read.
2122 */
2123 static int
Bram Moolenaard8070362016-02-15 21:56:54 +01002124channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002125{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002126 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002127 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002128
Bram Moolenaard8070362016-02-15 21:56:54 +01002129# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002130 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002131 {
2132 DWORD nread;
2133 int diff;
2134 DWORD deadline = GetTickCount() + timeout;
2135
2136 /* reading from a pipe, not a socket */
2137 while (TRUE)
2138 {
Bram Moolenaare74e8e72016-02-16 22:01:30 +01002139 if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL)
2140 && nread > 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002141 return OK;
2142 diff = deadline - GetTickCount();
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002143 if (diff <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002144 break;
2145 /* Wait for 5 msec.
2146 * TODO: increase the sleep time when looping more often */
2147 Sleep(5);
2148 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002149 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002150 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002151#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002152 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002153#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002154 struct timeval tval;
2155 fd_set rfds;
2156 int ret;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002157
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002158 FD_ZERO(&rfds);
2159 FD_SET((int)fd, &rfds);
2160 tval.tv_sec = timeout / 1000;
2161 tval.tv_usec = (timeout % 1000) * 1000;
2162 for (;;)
2163 {
2164 ret = select((int)fd + 1, &rfds, NULL, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002165# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002166 SOCK_ERRNO;
2167 if (ret == -1 && errno == EINTR)
2168 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002169# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002170 if (ret > 0)
2171 return OK;
2172 break;
2173 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002174#else
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002175 struct pollfd fds;
2176
2177 fds.fd = fd;
2178 fds.events = POLLIN;
2179 if (poll(&fds, 1, timeout) > 0)
2180 return OK;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002181#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002182 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002183 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002184}
2185
2186/*
2187 * Return a unique ID to be used in a message.
2188 */
2189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190channel_get_id(void)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002191{
2192 static int next_id = 1;
2193
2194 return next_id++;
2195}
2196
2197/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002198 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002199 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002200 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002201 */
2202 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002203channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002204{
2205 static char_u *buf = NULL;
2206 int len = 0;
2207 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002208 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002209 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002210
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002211 fd = channel->ch_part[part].ch_fd;
2212 if (fd == INVALID_FD)
2213 {
2214 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002215 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002216 }
2217 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002218
2219 /* Allocate a buffer to read into. */
2220 if (buf == NULL)
2221 {
2222 buf = alloc(MAXMSGSIZE);
2223 if (buf == NULL)
2224 return; /* out of memory! */
2225 }
2226
2227 /* Keep on reading for as long as there is something to read.
2228 * Use select() or poll() to avoid blocking on a message that is exactly
2229 * MAXMSGSIZE long. */
2230 for (;;)
2231 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002232 if (channel_wait(channel, fd, 0) == FAIL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002233 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002234 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002235 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002236 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002237 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002238 if (len <= 0)
2239 break; /* error or nothing more to read */
2240
2241 /* Store the read message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002242 channel_save(channel, part, buf, len);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002243 readlen += len;
2244 if (len < MAXMSGSIZE)
2245 break; /* did read everything that's available */
2246 }
2247
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002248 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002249 if (readlen <= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002250 {
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002251 /* Do not give an error message, most likely the other end just
2252 * exited. */
2253 ch_errors(channel, "%s(): Cannot read from channel", func);
2254
Bram Moolenaard04a0202016-01-26 23:30:18 +01002255 /* Queue a "DETACH" netbeans message in the command queue in order to
2256 * terminate the netbeans session later. Do not end the session here
2257 * directly as we may be running in the context of a call to
2258 * netbeans_parse_messages():
2259 * netbeans_parse_messages
2260 * -> autocmd triggered while processing the netbeans cmd
2261 * -> ui_breakcheck
2262 * -> gui event loop or select loop
2263 * -> channel_read()
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002264 * Don't send "DETACH" for a JS or JSON channel.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002265 */
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +01002266 if (channel->ch_part[part].ch_mode == MODE_RAW
2267 || channel->ch_part[part].ch_mode == MODE_NL)
2268 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
2269 (int)STRLEN(DETACH_MSG_RAW));
Bram Moolenaard04a0202016-01-26 23:30:18 +01002270
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002271 /* TODO: When reading from stdout is not possible, should we try to
2272 * keep stdin and stderr open? Probably not, assume the other side
2273 * has died. */
Bram Moolenaar8b374212016-02-24 20:43:06 +01002274 channel_close(channel, TRUE);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002275 if (channel->ch_nb_close_cb != NULL)
2276 (*channel->ch_nb_close_cb)();
Bram Moolenaard04a0202016-01-26 23:30:18 +01002277 }
2278
2279#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002280 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002281 if (CH_HAS_GUI && gtk_main_level() > 0)
2282 gtk_main_quit();
2283#endif
2284}
2285
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002286/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002287 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002288 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002289 * Returns what was read in allocated memory.
2290 * Returns NULL in case of error or timeout.
2291 */
2292 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002293channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002294{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002295 char_u *buf;
2296 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002297 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002298 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002299 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002300
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002301 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002302 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002303
2304 while (TRUE)
2305 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002306 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002307 if (buf != NULL && (mode == MODE_RAW
2308 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2309 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002310 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002311 continue;
2312
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002313 /* Wait for up to the channel timeout. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002314 if (fd == INVALID_FD
2315 || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002316 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002317 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002318 }
2319
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002320 if (mode == MODE_RAW)
2321 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002323 }
2324 else
2325 {
2326 nl = vim_strchr(buf, NL);
2327 if (nl[1] == NUL)
2328 {
2329 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002330 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002331 *nl = NUL;
2332 }
2333 else
2334 {
2335 /* Copy the message into allocated memory and remove it from the
2336 * buffer. */
2337 msg = vim_strnsave(buf, (int)(nl - buf));
2338 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2339 }
2340 }
2341 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002342 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002343 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002344}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002345
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002346/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002347 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002348 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002349 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002350 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002351 */
2352 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002353channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002354 channel_T *channel,
2355 int part,
2356 int timeout,
2357 int id,
2358 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002359{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002360 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002361 sock_T fd;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002362
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002363 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002364 if (id != -1)
2365 channel->ch_part[part].ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002366 for (;;)
2367 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002368 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002369
2370 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002371 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002372 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002373 channel->ch_part[part].ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002374 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002375 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002376
Bram Moolenaard7ece102016-02-02 23:23:02 +01002377 if (!more)
2378 {
2379 /* Handle any other messages in the queue. If done some more
2380 * messages may have arrived. */
2381 if (channel_parse_messages())
2382 continue;
2383
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002384 /* Wait for up to the timeout. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002385 fd = channel->ch_part[part].ch_fd;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002386 if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002387 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002388 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01002389 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002390 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002391 channel->ch_part[part].ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002392 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002393}
2394
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002395/*
2396 * Common for ch_read() and ch_readraw().
2397 */
2398 void
2399common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
2400{
2401 channel_T *channel;
2402 int part;
2403 jobopt_T opt;
2404 int mode;
2405 int timeout;
2406 int id = -1;
2407 typval_T *listtv = NULL;
2408
2409 /* return an empty string by default */
2410 rettv->v_type = VAR_STRING;
2411 rettv->vval.v_string = NULL;
2412
2413 clear_job_options(&opt);
2414 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
2415 == FAIL)
2416 return;
2417
2418 channel = get_channel_arg(&argvars[0], TRUE);
2419 if (channel != NULL)
2420 {
2421 if (opt.jo_set & JO_PART)
2422 part = opt.jo_part;
2423 else
2424 part = channel_part_read(channel);
2425 mode = channel_get_mode(channel, part);
2426 timeout = channel_get_timeout(channel, part);
2427 if (opt.jo_set & JO_TIMEOUT)
2428 timeout = opt.jo_timeout;
2429
2430 if (raw || mode == MODE_RAW || mode == MODE_NL)
2431 rettv->vval.v_string = channel_read_block(channel, part, timeout);
2432 else
2433 {
2434 if (opt.jo_set & JO_ID)
2435 id = opt.jo_id;
2436 channel_read_json_block(channel, part, timeout, id, &listtv);
2437 if (listtv != NULL)
2438 {
2439 *rettv = *listtv;
2440 vim_free(listtv);
2441 }
2442 else
2443 {
2444 rettv->v_type = VAR_SPECIAL;
2445 rettv->vval.v_number = VVAL_NONE;
2446 }
2447 }
2448 }
2449}
2450
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002451# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
2452 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002453/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002454 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01002455 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002456 */
Bram Moolenaar77073442016-02-13 23:23:53 +01002457 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002458channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002459{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002460 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002462
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002463 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01002464 for (channel = first_channel; channel != NULL;
2465 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002466 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002467 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002468 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002469 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01002470 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002471 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002472 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002473 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002474 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002475}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002476# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002477
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002478# if defined(WIN32) || defined(PROTO)
2479/*
2480 * Check the channels for anything that is ready to be read.
2481 * The data is put in the read queue.
2482 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002483 void
2484channel_handle_events(void)
2485{
2486 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002487 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002488 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002489
2490 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2491 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002492 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002493 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01002494 {
2495 fd = channel->ch_part[part].ch_fd;
2496 if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK)
2497 channel_read(channel, part, "channel_handle_events");
2498 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002499 }
2500}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01002501# endif
2502
Bram Moolenaard04a0202016-01-26 23:30:18 +01002503/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002504 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002505 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002506 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002507 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002508 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002509channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002510{
Bram Moolenaard04a0202016-01-26 23:30:18 +01002511 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002512 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002513 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002514
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002515 fd = channel->ch_part[part].ch_fd;
2516 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002517 {
2518 if (!channel->ch_error && fun != NULL)
2519 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002520 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521 EMSG2("E630: %s(): write while not connected", fun);
2522 }
2523 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002524 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002525 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002526
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002527 if (log_fd != NULL)
2528 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002529 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002530 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002531 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002532 fprintf(log_fd, "'\n");
2533 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01002534 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002535 }
2536
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002537 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002538 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002539 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002540 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002541 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002542 {
2543 if (!channel->ch_error && fun != NULL)
2544 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002545 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002546 EMSG2("E631: %s(): write failed", fun);
2547 }
2548 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002549 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002550 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002551
2552 channel->ch_error = FALSE;
2553 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002554}
2555
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002556/*
2557 * Common for "ch_sendexpr()" and "ch_sendraw()".
2558 * Returns the channel if the caller should read the response.
2559 * Sets "part_read" to the the read fd.
2560 * Otherwise returns NULL.
2561 */
2562 channel_T *
2563send_common(
2564 typval_T *argvars,
2565 char_u *text,
2566 int id,
2567 int eval,
2568 jobopt_T *opt,
2569 char *fun,
2570 int *part_read)
2571{
2572 channel_T *channel;
2573 int part_send;
2574
2575 channel = get_channel_arg(&argvars[0], TRUE);
2576 if (channel == NULL)
2577 return NULL;
2578 part_send = channel_part_send(channel);
2579 *part_read = channel_part_read(channel);
2580
2581 clear_job_options(opt);
2582 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
2583 return NULL;
2584
2585 /* Set the callback. An empty callback means no callback and not reading
2586 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
2587 * allowed. */
2588 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
2589 {
2590 if (eval)
2591 {
2592 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
2593 return NULL;
2594 }
2595 channel_set_req_callback(channel, part_send, opt->jo_callback, id);
2596 }
2597
2598 if (channel_send(channel, part_send, text, fun) == OK
2599 && opt->jo_callback == NULL)
2600 return channel;
2601 return NULL;
2602}
2603
2604/*
2605 * common for "ch_evalexpr()" and "ch_sendexpr()"
2606 */
2607 void
2608ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
2609{
2610 char_u *text;
2611 typval_T *listtv;
2612 channel_T *channel;
2613 int id;
2614 ch_mode_T ch_mode;
2615 int part_send;
2616 int part_read;
2617 jobopt_T opt;
2618 int timeout;
2619
2620 /* return an empty string by default */
2621 rettv->v_type = VAR_STRING;
2622 rettv->vval.v_string = NULL;
2623
2624 channel = get_channel_arg(&argvars[0], TRUE);
2625 if (channel == NULL)
2626 return;
2627 part_send = channel_part_send(channel);
2628
2629 ch_mode = channel_get_mode(channel, part_send);
2630 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
2631 {
2632 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
2633 return;
2634 }
2635
2636 id = channel_get_id();
2637 text = json_encode_nr_expr(id, &argvars[1],
2638 ch_mode == MODE_JS ? JSON_JS : 0);
2639 if (text == NULL)
2640 return;
2641
2642 channel = send_common(argvars, text, id, eval, &opt,
2643 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
2644 vim_free(text);
2645 if (channel != NULL && eval)
2646 {
2647 if (opt.jo_set & JO_TIMEOUT)
2648 timeout = opt.jo_timeout;
2649 else
2650 timeout = channel_get_timeout(channel, part_read);
2651 timeout = channel_get_timeout(channel, part_read);
2652 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
2653 == OK)
2654 {
2655 list_T *list = listtv->vval.v_list;
2656
2657 /* Move the item from the list and then change the type to
2658 * avoid the value being freed. */
2659 *rettv = list->lv_last->li_tv;
2660 list->lv_last->li_tv.v_type = VAR_NUMBER;
2661 free_tv(listtv);
2662 }
2663 }
2664}
2665
2666/*
2667 * common for "ch_evalraw()" and "ch_sendraw()"
2668 */
2669 void
2670ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
2671{
2672 char_u buf[NUMBUFLEN];
2673 char_u *text;
2674 channel_T *channel;
2675 int part_read;
2676 jobopt_T opt;
2677 int timeout;
2678
2679 /* return an empty string by default */
2680 rettv->v_type = VAR_STRING;
2681 rettv->vval.v_string = NULL;
2682
2683 text = get_tv_string_buf(&argvars[1], buf);
2684 channel = send_common(argvars, text, 0, eval, &opt,
2685 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
2686 if (channel != NULL && eval)
2687 {
2688 if (opt.jo_set & JO_TIMEOUT)
2689 timeout = opt.jo_timeout;
2690 else
2691 timeout = channel_get_timeout(channel, part_read);
2692 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
2693 }
2694}
2695
Bram Moolenaard04a0202016-01-26 23:30:18 +01002696# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002697/*
2698 * Add open channels to the poll struct.
2699 * Return the adjusted struct index.
2700 * The type of "fds" is hidden to avoid problems with the function proto.
2701 */
2702 int
2703channel_poll_setup(int nfd_in, void *fds_in)
2704{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002705 int nfd = nfd_in;
2706 channel_T *channel;
2707 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002708 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002709
Bram Moolenaar77073442016-02-13 23:23:53 +01002710 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002711 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002712 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002713 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002714 if (channel->ch_part[part].ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002715 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002716 channel->ch_part[part].ch_poll_idx = nfd;
2717 fds[nfd].fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002718 fds[nfd].events = POLLIN;
2719 nfd++;
2720 }
2721 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002722 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002723 }
2724 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002725
2726 return nfd;
2727}
2728
2729/*
2730 * The type of "fds" is hidden to avoid problems with the function proto.
2731 */
2732 int
2733channel_poll_check(int ret_in, void *fds_in)
2734{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002735 int ret = ret_in;
2736 channel_T *channel;
2737 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002738 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002739
Bram Moolenaar77073442016-02-13 23:23:53 +01002740 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002741 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002742 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002743 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002744 int idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002745
2746 if (ret > 0 && idx != -1 && fds[idx].revents & POLLIN)
2747 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002748 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002749 --ret;
2750 }
2751 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002752 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002753
2754 return ret;
2755}
Bram Moolenaard04a0202016-01-26 23:30:18 +01002756# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002757
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002758# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01002759/*
2760 * The type of "rfds" is hidden to avoid problems with the function proto.
2761 */
2762 int
2763channel_select_setup(int maxfd_in, void *rfds_in)
2764{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002765 int maxfd = maxfd_in;
2766 channel_T *channel;
2767 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002768 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002769
Bram Moolenaar77073442016-02-13 23:23:53 +01002770 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002771 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002772 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002773 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002774 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002775
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002776 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002777 {
Bram Moolenaard8070362016-02-15 21:56:54 +01002778 FD_SET((int)fd, rfds);
2779 if (maxfd < (int)fd)
2780 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002781 }
2782 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002783 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002784
2785 return maxfd;
2786}
2787
2788/*
2789 * The type of "rfds" is hidden to avoid problems with the function proto.
2790 */
2791 int
2792channel_select_check(int ret_in, void *rfds_in)
2793{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002794 int ret = ret_in;
2795 channel_T *channel;
2796 fd_set *rfds = rfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002797 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002798
Bram Moolenaar77073442016-02-13 23:23:53 +01002799 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002800 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002801 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002802 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002803 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002804
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002805 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002806 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002807 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01002808 --ret;
2809 }
2810 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002811 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01002812
2813 return ret;
2814}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002815# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01002816
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002817/*
Bram Moolenaar187db502016-02-27 14:44:26 +01002818 * Return TRUE if "channel" has JSON or other typeahead.
2819 */
2820 static int
2821channel_has_readahead(channel_T *channel, int part)
2822{
2823 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2824
2825 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2826 {
2827 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2828 jsonq_T *item = head->jq_next;
2829
2830 return item != NULL;
2831 }
2832 return channel_peek(channel, part) != NULL;
2833}
2834
2835/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01002836 * Execute queued up commands.
2837 * Invoked from the main loop when it's safe to execute received commands.
2838 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002839 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002840 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002841channel_parse_messages(void)
2842{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002843 channel_T *channel = first_channel;
2844 int ret = FALSE;
2845 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002846 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002847
Bram Moolenaard0b65022016-03-06 21:50:33 +01002848 /* Only do this message when another message was given, otherwise we get
2849 * lots of them. */
2850 if (did_log_msg)
2851 {
2852 ch_log(NULL, "looking for messages on channels");
2853 did_log_msg = FALSE;
2854 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002855 while (channel != NULL)
2856 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01002857 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002858 {
2859 /* channel is no longer useful, free it */
2860 channel_free(channel);
2861 channel = first_channel;
2862 part = PART_SOCK;
2863 continue;
2864 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002865 if (channel->ch_part[part].ch_fd != INVALID_FD
2866 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01002867 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002868 /* Increase the refcount, in case the handler causes the channel
2869 * to be unreferenced or closed. */
2870 ++channel->ch_refcount;
2871 r = may_invoke_callback(channel, part);
2872 if (r == OK)
2873 ret = TRUE;
2874 if (channel_unref(channel) || r == OK)
2875 {
2876 /* channel was freed or something was done, start over */
2877 channel = first_channel;
2878 part = PART_SOCK;
2879 continue;
2880 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002881 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002882 if (part < PART_ERR)
2883 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002884 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002885 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002886 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002887 part = PART_SOCK;
2888 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002889 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002890
2891 if (channel_need_redraw && must_redraw)
2892 {
2893 channel_need_redraw = FALSE;
2894 update_screen(0);
2895 setcursor();
2896 cursor_on();
2897 out_flush();
2898 }
2899
Bram Moolenaard7ece102016-02-02 23:23:02 +01002900 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002901}
2902
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01002903/*
2904 * Mark references to lists used in channels.
2905 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002906 int
2907set_ref_in_channel(int copyID)
2908{
Bram Moolenaar77073442016-02-13 23:23:53 +01002909 int abort = FALSE;
2910 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002911 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002912
Bram Moolenaar77073442016-02-13 23:23:53 +01002913 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002914 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002915 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002916 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002917 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2918 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002919
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002920 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002921 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002922 list_T *l = item->jq_value->vval.v_list;
2923
2924 if (l->lv_copyID != copyID)
2925 {
2926 l->lv_copyID = copyID;
2927 abort = abort || set_ref_in_list(l, copyID, NULL);
2928 }
2929 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002930 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01002931 }
2932 }
2933 return abort;
2934}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01002935
2936/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002937 * Return the "part" to write to for "channel".
2938 */
2939 int
2940channel_part_send(channel_T *channel)
2941{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002942 if (channel->CH_SOCK_FD == INVALID_FD)
2943 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002944 return PART_SOCK;
2945}
2946
2947/*
2948 * Return the default "part" to read from for "channel".
2949 */
2950 int
2951channel_part_read(channel_T *channel)
2952{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002953 if (channel->CH_SOCK_FD == INVALID_FD)
2954 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002955 return PART_SOCK;
2956}
2957
2958/*
2959 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01002960 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01002961 */
2962 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002963channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01002964{
Bram Moolenaar77073442016-02-13 23:23:53 +01002965 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01002966 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002967 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01002968}
2969
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002970/*
2971 * Return the timeout of "channel"/"part"
2972 */
2973 int
2974channel_get_timeout(channel_T *channel, int part)
2975{
2976 return channel->ch_part[part].ch_timeout;
2977}
2978
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01002979/*
2980 * Get a callback from "arg". It can be a Funcref or a function name.
2981 * When "arg" is zero return an empty string.
2982 * Return NULL for an invalid argument.
2983 */
2984 static char_u *
2985get_callback(typval_T *arg)
2986{
2987 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
2988 return arg->vval.v_string;
2989 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
2990 return (char_u *)"";
2991 EMSG(_("E999: Invalid callback argument"));
2992 return NULL;
2993}
2994
2995 static int
2996handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
2997{
2998 char_u *val = get_tv_string(item);
2999
3000 opt->jo_set |= jo;
3001 if (STRCMP(val, "nl") == 0)
3002 *modep = MODE_NL;
3003 else if (STRCMP(val, "raw") == 0)
3004 *modep = MODE_RAW;
3005 else if (STRCMP(val, "js") == 0)
3006 *modep = MODE_JS;
3007 else if (STRCMP(val, "json") == 0)
3008 *modep = MODE_JSON;
3009 else
3010 {
3011 EMSG2(_(e_invarg2), val);
3012 return FAIL;
3013 }
3014 return OK;
3015}
3016
3017 static int
3018handle_io(typval_T *item, int part, jobopt_T *opt)
3019{
3020 char_u *val = get_tv_string(item);
3021
3022 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3023 if (STRCMP(val, "null") == 0)
3024 opt->jo_io[part] = JIO_NULL;
3025 else if (STRCMP(val, "pipe") == 0)
3026 opt->jo_io[part] = JIO_PIPE;
3027 else if (STRCMP(val, "file") == 0)
3028 opt->jo_io[part] = JIO_FILE;
3029 else if (STRCMP(val, "buffer") == 0)
3030 opt->jo_io[part] = JIO_BUFFER;
3031 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3032 opt->jo_io[part] = JIO_OUT;
3033 else
3034 {
3035 EMSG2(_(e_invarg2), val);
3036 return FAIL;
3037 }
3038 return OK;
3039}
3040
3041 void
3042clear_job_options(jobopt_T *opt)
3043{
3044 vim_memset(opt, 0, sizeof(jobopt_T));
3045}
3046
3047/*
3048 * Get the PART_ number from the first character of an option name.
3049 */
3050 static int
3051part_from_char(int c)
3052{
3053 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3054}
3055
3056/*
3057 * Get the option entries from the dict in "tv", parse them and put the result
3058 * in "opt".
3059 * Only accept options in "supported".
3060 * If an option value is invalid return FAIL.
3061 */
3062 int
3063get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3064{
3065 typval_T *item;
3066 char_u *val;
3067 dict_T *dict;
3068 int todo;
3069 hashitem_T *hi;
3070 int part;
3071
3072 opt->jo_set = 0;
3073 if (tv->v_type == VAR_UNKNOWN)
3074 return OK;
3075 if (tv->v_type != VAR_DICT)
3076 {
3077 EMSG(_(e_invarg));
3078 return FAIL;
3079 }
3080 dict = tv->vval.v_dict;
3081 if (dict == NULL)
3082 return OK;
3083
3084 todo = (int)dict->dv_hashtab.ht_used;
3085 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3086 if (!HASHITEM_EMPTY(hi))
3087 {
3088 item = &dict_lookup(hi)->di_tv;
3089
3090 if (STRCMP(hi->hi_key, "mode") == 0)
3091 {
3092 if (!(supported & JO_MODE))
3093 break;
3094 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3095 return FAIL;
3096 }
3097 else if (STRCMP(hi->hi_key, "in-mode") == 0)
3098 {
3099 if (!(supported & JO_IN_MODE))
3100 break;
3101 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3102 == FAIL)
3103 return FAIL;
3104 }
3105 else if (STRCMP(hi->hi_key, "out-mode") == 0)
3106 {
3107 if (!(supported & JO_OUT_MODE))
3108 break;
3109 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3110 == FAIL)
3111 return FAIL;
3112 }
3113 else if (STRCMP(hi->hi_key, "err-mode") == 0)
3114 {
3115 if (!(supported & JO_ERR_MODE))
3116 break;
3117 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3118 == FAIL)
3119 return FAIL;
3120 }
3121 else if (STRCMP(hi->hi_key, "in-io") == 0
3122 || STRCMP(hi->hi_key, "out-io") == 0
3123 || STRCMP(hi->hi_key, "err-io") == 0)
3124 {
3125 if (!(supported & JO_OUT_IO))
3126 break;
3127 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3128 return FAIL;
3129 }
3130 else if (STRCMP(hi->hi_key, "in-name") == 0
3131 || STRCMP(hi->hi_key, "out-name") == 0
3132 || STRCMP(hi->hi_key, "err-name") == 0)
3133 {
3134 part = part_from_char(*hi->hi_key);
3135
3136 if (!(supported & JO_OUT_IO))
3137 break;
3138 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3139 opt->jo_io_name[part] =
3140 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3141 }
3142 else if (STRCMP(hi->hi_key, "in-buf") == 0
3143 || STRCMP(hi->hi_key, "out-buf") == 0
3144 || STRCMP(hi->hi_key, "err-buf") == 0)
3145 {
3146 part = part_from_char(*hi->hi_key);
3147
3148 if (!(supported & JO_OUT_IO))
3149 break;
3150 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3151 opt->jo_io_buf[part] = get_tv_number(item);
3152 if (opt->jo_io_buf[part] <= 0)
3153 {
3154 EMSG2(_(e_invarg2), get_tv_string(item));
3155 return FAIL;
3156 }
3157 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3158 {
3159 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3160 return FAIL;
3161 }
3162 }
3163 else if (STRCMP(hi->hi_key, "in-top") == 0
3164 || STRCMP(hi->hi_key, "in-bot") == 0)
3165 {
3166 linenr_T *lp;
3167
3168 if (!(supported & JO_OUT_IO))
3169 break;
3170 if (hi->hi_key[3] == 't')
3171 {
3172 lp = &opt->jo_in_top;
3173 opt->jo_set |= JO_IN_TOP;
3174 }
3175 else
3176 {
3177 lp = &opt->jo_in_bot;
3178 opt->jo_set |= JO_IN_BOT;
3179 }
3180 *lp = get_tv_number(item);
3181 if (*lp < 0)
3182 {
3183 EMSG2(_(e_invarg2), get_tv_string(item));
3184 return FAIL;
3185 }
3186 }
3187 else if (STRCMP(hi->hi_key, "channel") == 0)
3188 {
3189 if (!(supported & JO_OUT_IO))
3190 break;
3191 opt->jo_set |= JO_CHANNEL;
3192 if (item->v_type != VAR_CHANNEL)
3193 {
3194 EMSG2(_(e_invarg2), "channel");
3195 return FAIL;
3196 }
3197 opt->jo_channel = item->vval.v_channel;
3198 }
3199 else if (STRCMP(hi->hi_key, "callback") == 0)
3200 {
3201 if (!(supported & JO_CALLBACK))
3202 break;
3203 opt->jo_set |= JO_CALLBACK;
3204 opt->jo_callback = get_callback(item);
3205 if (opt->jo_callback == NULL)
3206 {
3207 EMSG2(_(e_invarg2), "callback");
3208 return FAIL;
3209 }
3210 }
3211 else if (STRCMP(hi->hi_key, "out-cb") == 0)
3212 {
3213 if (!(supported & JO_OUT_CALLBACK))
3214 break;
3215 opt->jo_set |= JO_OUT_CALLBACK;
3216 opt->jo_out_cb = get_callback(item);
3217 if (opt->jo_out_cb == NULL)
3218 {
3219 EMSG2(_(e_invarg2), "out-cb");
3220 return FAIL;
3221 }
3222 }
3223 else if (STRCMP(hi->hi_key, "err-cb") == 0)
3224 {
3225 if (!(supported & JO_ERR_CALLBACK))
3226 break;
3227 opt->jo_set |= JO_ERR_CALLBACK;
3228 opt->jo_err_cb = get_callback(item);
3229 if (opt->jo_err_cb == NULL)
3230 {
3231 EMSG2(_(e_invarg2), "err-cb");
3232 return FAIL;
3233 }
3234 }
3235 else if (STRCMP(hi->hi_key, "close-cb") == 0)
3236 {
3237 if (!(supported & JO_CLOSE_CALLBACK))
3238 break;
3239 opt->jo_set |= JO_CLOSE_CALLBACK;
3240 opt->jo_close_cb = get_callback(item);
3241 if (opt->jo_close_cb == NULL)
3242 {
3243 EMSG2(_(e_invarg2), "close-cb");
3244 return FAIL;
3245 }
3246 }
3247 else if (STRCMP(hi->hi_key, "waittime") == 0)
3248 {
3249 if (!(supported & JO_WAITTIME))
3250 break;
3251 opt->jo_set |= JO_WAITTIME;
3252 opt->jo_waittime = get_tv_number(item);
3253 }
3254 else if (STRCMP(hi->hi_key, "timeout") == 0)
3255 {
3256 if (!(supported & JO_TIMEOUT))
3257 break;
3258 opt->jo_set |= JO_TIMEOUT;
3259 opt->jo_timeout = get_tv_number(item);
3260 }
3261 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
3262 {
3263 if (!(supported & JO_OUT_TIMEOUT))
3264 break;
3265 opt->jo_set |= JO_OUT_TIMEOUT;
3266 opt->jo_out_timeout = get_tv_number(item);
3267 }
3268 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
3269 {
3270 if (!(supported & JO_ERR_TIMEOUT))
3271 break;
3272 opt->jo_set |= JO_ERR_TIMEOUT;
3273 opt->jo_err_timeout = get_tv_number(item);
3274 }
3275 else if (STRCMP(hi->hi_key, "part") == 0)
3276 {
3277 if (!(supported & JO_PART))
3278 break;
3279 opt->jo_set |= JO_PART;
3280 val = get_tv_string(item);
3281 if (STRCMP(val, "err") == 0)
3282 opt->jo_part = PART_ERR;
3283 else
3284 {
3285 EMSG2(_(e_invarg2), val);
3286 return FAIL;
3287 }
3288 }
3289 else if (STRCMP(hi->hi_key, "id") == 0)
3290 {
3291 if (!(supported & JO_ID))
3292 break;
3293 opt->jo_set |= JO_ID;
3294 opt->jo_id = get_tv_number(item);
3295 }
3296 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3297 {
3298 if (!(supported & JO_STOPONEXIT))
3299 break;
3300 opt->jo_set |= JO_STOPONEXIT;
3301 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3302 opt->jo_soe_buf);
3303 if (opt->jo_stoponexit == NULL)
3304 {
3305 EMSG2(_(e_invarg2), "stoponexit");
3306 return FAIL;
3307 }
3308 }
3309 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
3310 {
3311 if (!(supported & JO_EXIT_CB))
3312 break;
3313 opt->jo_set |= JO_EXIT_CB;
3314 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
3315 if (opt->jo_exit_cb == NULL)
3316 {
3317 EMSG2(_(e_invarg2), "exit-cb");
3318 return FAIL;
3319 }
3320 }
3321 else
3322 break;
3323 --todo;
3324 }
3325 if (todo > 0)
3326 {
3327 EMSG2(_(e_invarg2), hi->hi_key);
3328 return FAIL;
3329 }
3330
3331 return OK;
3332}
3333
3334/*
3335 * Get the channel from the argument.
3336 * Returns NULL if the handle is invalid.
3337 */
3338 channel_T *
3339get_channel_arg(typval_T *tv, int check_open)
3340{
3341 channel_T *channel = NULL;
3342
3343 if (tv->v_type == VAR_JOB)
3344 {
3345 if (tv->vval.v_job != NULL)
3346 channel = tv->vval.v_job->jv_channel;
3347 }
3348 else if (tv->v_type == VAR_CHANNEL)
3349 {
3350 channel = tv->vval.v_channel;
3351 }
3352 else
3353 {
3354 EMSG2(_(e_invarg2), get_tv_string(tv));
3355 return NULL;
3356 }
3357
3358 if (check_open && (channel == NULL || !channel_is_open(channel)))
3359 {
3360 EMSG(_("E906: not an open channel"));
3361 return NULL;
3362 }
3363 return channel;
3364}
3365
3366static job_T *first_job = NULL;
3367
3368 static void
3369job_free(job_T *job)
3370{
3371 ch_log(job->jv_channel, "Freeing job");
3372 if (job->jv_channel != NULL)
3373 {
3374 /* The link from the channel to the job doesn't count as a reference,
3375 * thus don't decrement the refcount of the job. The reference from
3376 * the job to the channel does count the refrence, decrement it and
3377 * NULL the reference. We don't set ch_job_killed, unreferencing the
3378 * job doesn't mean it stops running. */
3379 job->jv_channel->ch_job = NULL;
3380 channel_unref(job->jv_channel);
3381 }
3382 mch_clear_job(job);
3383
3384 if (job->jv_next != NULL)
3385 job->jv_next->jv_prev = job->jv_prev;
3386 if (job->jv_prev == NULL)
3387 first_job = job->jv_next;
3388 else
3389 job->jv_prev->jv_next = job->jv_next;
3390
3391 vim_free(job->jv_stoponexit);
3392 vim_free(job->jv_exit_cb);
3393 vim_free(job);
3394}
3395
3396 void
3397job_unref(job_T *job)
3398{
3399 if (job != NULL && --job->jv_refcount <= 0)
3400 {
3401 /* Do not free the job when it has not ended yet and there is a
3402 * "stoponexit" flag or an exit callback. */
3403 if (job->jv_status != JOB_STARTED
3404 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
3405 {
3406 job_free(job);
3407 }
3408 else if (job->jv_channel != NULL)
3409 {
3410 /* Do remove the link to the channel, otherwise it hangs
3411 * around until Vim exits. See job_free() for refcount. */
3412 job->jv_channel->ch_job = NULL;
3413 channel_unref(job->jv_channel);
3414 job->jv_channel = NULL;
3415 }
3416 }
3417}
3418
3419/*
3420 * Allocate a job. Sets the refcount to one and sets options default.
3421 */
3422 static job_T *
3423job_alloc(void)
3424{
3425 job_T *job;
3426
3427 job = (job_T *)alloc_clear(sizeof(job_T));
3428 if (job != NULL)
3429 {
3430 job->jv_refcount = 1;
3431 job->jv_stoponexit = vim_strsave((char_u *)"term");
3432
3433 if (first_job != NULL)
3434 {
3435 first_job->jv_prev = job;
3436 job->jv_next = first_job;
3437 }
3438 first_job = job;
3439 }
3440 return job;
3441}
3442
3443 void
3444job_set_options(job_T *job, jobopt_T *opt)
3445{
3446 if (opt->jo_set & JO_STOPONEXIT)
3447 {
3448 vim_free(job->jv_stoponexit);
3449 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
3450 job->jv_stoponexit = NULL;
3451 else
3452 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
3453 }
3454 if (opt->jo_set & JO_EXIT_CB)
3455 {
3456 vim_free(job->jv_exit_cb);
3457 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
3458 job->jv_exit_cb = NULL;
3459 else
3460 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
3461 }
3462}
3463
3464/*
3465 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
3466 */
3467 void
3468job_stop_on_exit()
3469{
3470 job_T *job;
3471
3472 for (job = first_job; job != NULL; job = job->jv_next)
3473 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
3474 mch_stop_job(job, job->jv_stoponexit);
3475}
3476
3477/*
3478 * Called once in a while: check if any jobs with an "exit-cb" have ended.
3479 */
3480 void
3481job_check_ended(void)
3482{
3483 static time_t last_check = 0;
3484 time_t now;
3485 job_T *job;
3486 job_T *next;
3487
3488 /* Only do this once in 10 seconds. */
3489 now = time(NULL);
3490 if (last_check + 10 < now)
3491 {
3492 last_check = now;
3493 for (job = first_job; job != NULL; job = next)
3494 {
3495 next = job->jv_next;
3496 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
3497 job_status(job); /* may free "job" */
3498 }
3499 }
3500}
3501
3502/*
3503 * "job_start()" function
3504 */
3505 job_T *
3506job_start(typval_T *argvars)
3507{
3508 job_T *job;
3509 char_u *cmd = NULL;
3510#if defined(UNIX)
3511# define USE_ARGV
3512 char **argv = NULL;
3513 int argc = 0;
3514#else
3515 garray_T ga;
3516#endif
3517 jobopt_T opt;
3518 int part;
3519
3520 job = job_alloc();
3521 if (job == NULL)
3522 return NULL;
3523
3524 job->jv_status = JOB_FAILED;
3525
3526 /* Default mode is NL. */
3527 clear_job_options(&opt);
3528 opt.jo_mode = MODE_NL;
3529 if (get_job_options(&argvars[1], &opt,
3530 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
3531 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
3532 return job;
3533
3534 /* Check that when io is "file" that there is a file name. */
3535 for (part = PART_OUT; part <= PART_IN; ++part)
3536 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
3537 && opt.jo_io[part] == JIO_FILE
3538 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
3539 || *opt.jo_io_name[part] == NUL))
3540 {
3541 EMSG(_("E920: -io file requires -name to be set"));
3542 return job;
3543 }
3544
3545 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
3546 {
3547 buf_T *buf = NULL;
3548
3549 /* check that we can find the buffer before starting the job */
3550 if (opt.jo_set & JO_IN_BUF)
3551 {
3552 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
3553 if (buf == NULL)
3554 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
3555 }
3556 else if (!(opt.jo_set & JO_IN_NAME))
3557 {
3558 EMSG(_("E915: in-io buffer requires in-buf or in-name to be set"));
3559 }
3560 else
3561 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
3562 if (buf == NULL)
3563 return job;
3564 if (buf->b_ml.ml_mfp == NULL)
3565 {
3566 char_u numbuf[NUMBUFLEN];
3567 char_u *s;
3568
3569 if (opt.jo_set & JO_IN_BUF)
3570 {
3571 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
3572 s = numbuf;
3573 }
3574 else
3575 s = opt.jo_io_name[PART_IN];
3576 EMSG2(_("E918: buffer must be loaded: %s"), s);
3577 return job;
3578 }
3579 job->jv_in_buf = buf;
3580 }
3581
3582 job_set_options(job, &opt);
3583
3584#ifndef USE_ARGV
3585 ga_init2(&ga, (int)sizeof(char*), 20);
3586#endif
3587
3588 if (argvars[0].v_type == VAR_STRING)
3589 {
3590 /* Command is a string. */
3591 cmd = argvars[0].vval.v_string;
3592#ifdef USE_ARGV
3593 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
3594 return job;
3595 argv[argc] = NULL;
3596#endif
3597 }
3598 else if (argvars[0].v_type != VAR_LIST
3599 || argvars[0].vval.v_list == NULL
3600 || argvars[0].vval.v_list->lv_len < 1)
3601 {
3602 EMSG(_(e_invarg));
3603 return job;
3604 }
3605 else
3606 {
3607 list_T *l = argvars[0].vval.v_list;
3608 listitem_T *li;
3609 char_u *s;
3610
3611#ifdef USE_ARGV
3612 /* Pass argv[] to mch_call_shell(). */
3613 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
3614 if (argv == NULL)
3615 return job;
3616#endif
3617 for (li = l->lv_first; li != NULL; li = li->li_next)
3618 {
3619 s = get_tv_string_chk(&li->li_tv);
3620 if (s == NULL)
3621 goto theend;
3622#ifdef USE_ARGV
3623 argv[argc++] = (char *)s;
3624#else
3625 /* Only escape when needed, double quotes are not always allowed. */
3626 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
3627 {
3628 s = vim_strsave_shellescape(s, FALSE, TRUE);
3629 if (s == NULL)
3630 goto theend;
3631 ga_concat(&ga, s);
3632 vim_free(s);
3633 }
3634 else
3635 ga_concat(&ga, s);
3636 if (li->li_next != NULL)
3637 ga_append(&ga, ' ');
3638#endif
3639 }
3640#ifdef USE_ARGV
3641 argv[argc] = NULL;
3642#else
3643 cmd = ga.ga_data;
3644#endif
3645 }
3646
3647#ifdef USE_ARGV
3648 if (ch_log_active())
3649 {
3650 garray_T ga;
3651 int i;
3652
3653 ga_init2(&ga, (int)sizeof(char), 200);
3654 for (i = 0; i < argc; ++i)
3655 {
3656 if (i > 0)
3657 ga_concat(&ga, (char_u *)" ");
3658 ga_concat(&ga, (char_u *)argv[i]);
3659 }
3660 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
3661 ga_clear(&ga);
3662 }
3663 mch_start_job(argv, job, &opt);
3664#else
3665 ch_logs(NULL, "Starting job: %s", (char *)cmd);
3666 mch_start_job((char *)cmd, job, &opt);
3667#endif
3668
3669 /* If the channel is reading from a buffer, write lines now. */
3670 if (job->jv_channel != NULL)
3671 channel_write_in(job->jv_channel);
3672
3673theend:
3674#ifdef USE_ARGV
3675 vim_free(argv);
3676#else
3677 vim_free(ga.ga_data);
3678#endif
3679 return job;
3680}
3681
3682/*
3683 * Get the status of "job" and invoke the exit callback when needed.
3684 * The returned string is not allocated.
3685 */
3686 char *
3687job_status(job_T *job)
3688{
3689 char *result;
3690
3691 if (job->jv_status == JOB_ENDED)
3692 /* No need to check, dead is dead. */
3693 result = "dead";
3694 else if (job->jv_status == JOB_FAILED)
3695 result = "fail";
3696 else
3697 {
3698 result = mch_job_status(job);
3699 if (job->jv_status == JOB_ENDED)
3700 ch_log(job->jv_channel, "Job ended");
3701 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
3702 {
3703 typval_T argv[3];
3704 typval_T rettv;
3705 int dummy;
3706
3707 /* invoke the exit callback; make sure the refcount is > 0 */
3708 ++job->jv_refcount;
3709 argv[0].v_type = VAR_JOB;
3710 argv[0].vval.v_job = job;
3711 argv[1].v_type = VAR_NUMBER;
3712 argv[1].vval.v_number = job->jv_exitval;
3713 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
3714 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
3715 clear_tv(&rettv);
3716 --job->jv_refcount;
3717 }
3718 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
3719 {
3720 /* The job was already unreferenced, now that it ended it can be
3721 * freed. Careful: caller must not use "job" after this! */
3722 job_free(job);
3723 }
3724 }
3725 return result;
3726}
3727
3728 int
3729job_stop(job_T *job, typval_T *argvars)
3730{
3731 char_u *arg;
3732
3733 if (argvars[1].v_type == VAR_UNKNOWN)
3734 arg = (char_u *)"";
3735 else
3736 {
3737 arg = get_tv_string_chk(&argvars[1]);
3738 if (arg == NULL)
3739 {
3740 EMSG(_(e_invarg));
3741 return 0;
3742 }
3743 }
3744 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
3745 if (mch_stop_job(job, arg) == FAIL)
3746 return 0;
3747
3748 /* Assume that "hup" does not kill the job. */
3749 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
3750 job->jv_channel->ch_job_killed = TRUE;
3751
3752 /* We don't try freeing the job, obviously the caller still has a
3753 * reference to it. */
3754 return 1;
3755}
3756
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003757#endif /* FEAT_JOB_CHANNEL */