Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1 | /* 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 | |
| 15 | #if defined(FEAT_CHANNEL) || defined(PROTO) |
| 16 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 17 | /* 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 Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 29 | # undef EWOULDBLOCK |
| 30 | # define EWOULDBLOCK WSAEWOULDBLOCK |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 31 | # undef EINPROGRESS |
| 32 | # define EINPROGRESS WSAEINPROGRESS |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 33 | # ifdef EINTR |
| 34 | # undef EINTR |
| 35 | # endif |
| 36 | # define EINTR WSAEINTR |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 37 | # 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 Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 40 | #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 Moolenaar | 0943a09 | 2016-02-16 13:11:17 +0100 | [diff] [blame] | 52 | # define fd_read(fd, buf, len) read(fd, buf, len) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 53 | # define fd_write(sd, buf, len) write(sd, buf, len) |
| 54 | # define fd_close(sd) close(sd) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 55 | #endif |
| 56 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 57 | /* Whether a redraw is needed for appending a line to a buffer. */ |
| 58 | static int channel_need_redraw = FALSE; |
| 59 | |
| 60 | |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 61 | #ifdef WIN32 |
| 62 | static int |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 63 | fd_read(sock_T fd, char *buf, size_t len) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 64 | { |
| 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 Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 74 | fd_write(sock_T fd, char *buf, size_t len) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 75 | { |
| 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 |
| 85 | fd_close(sock_T fd) |
| 86 | { |
| 87 | HANDLE h = (HANDLE)fd; |
| 88 | |
| 89 | CloseHandle(h); |
| 90 | } |
| 91 | #endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 92 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 93 | /* Log file opened with ch_logfile(). */ |
| 94 | static FILE *log_fd = NULL; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 95 | #ifdef FEAT_RELTIME |
| 96 | static proftime_T log_start; |
| 97 | #endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 98 | |
| 99 | void |
| 100 | ch_logfile(FILE *file) |
| 101 | { |
| 102 | if (log_fd != NULL) |
| 103 | fclose(log_fd); |
| 104 | log_fd = file; |
| 105 | if (log_fd != NULL) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 106 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 107 | fprintf(log_fd, "==== start log session ====\n"); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 108 | #ifdef FEAT_RELTIME |
| 109 | profile_start(&log_start); |
| 110 | #endif |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | int |
| 115 | ch_log_active() |
| 116 | { |
| 117 | return log_fd != NULL; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 121 | ch_log_lead(char *what, channel_T *ch) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 122 | { |
| 123 | if (log_fd != NULL) |
| 124 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 125 | #ifdef FEAT_RELTIME |
| 126 | proftime_T log_now; |
| 127 | |
| 128 | profile_start(&log_now); |
| 129 | profile_sub(&log_now, &log_start); |
| 130 | fprintf(log_fd, "%s ", profile_msg(&log_now)); |
| 131 | #endif |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 132 | if (ch != NULL) |
| 133 | fprintf(log_fd, "%son %d: ", what, ch->ch_id); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 134 | else |
| 135 | fprintf(log_fd, "%s: ", what); |
| 136 | } |
| 137 | } |
| 138 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 139 | void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 140 | ch_log(channel_T *ch, char *msg) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 141 | { |
| 142 | if (log_fd != NULL) |
| 143 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 144 | ch_log_lead("", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 145 | fputs(msg, log_fd); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 146 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 147 | fflush(log_fd); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 152 | ch_logn(channel_T *ch, char *msg, int nr) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 153 | { |
| 154 | if (log_fd != NULL) |
| 155 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 156 | ch_log_lead("", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 157 | fprintf(log_fd, msg, nr); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 158 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 159 | fflush(log_fd); |
| 160 | } |
| 161 | } |
| 162 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 163 | void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 164 | ch_logs(channel_T *ch, char *msg, char *name) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 165 | { |
| 166 | if (log_fd != NULL) |
| 167 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 168 | ch_log_lead("", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 169 | fprintf(log_fd, msg, name); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 170 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 171 | fflush(log_fd); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 176 | ch_logsn(channel_T *ch, char *msg, char *name, int nr) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 177 | { |
| 178 | if (log_fd != NULL) |
| 179 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 180 | ch_log_lead("", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 181 | fprintf(log_fd, msg, name, nr); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 182 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 183 | fflush(log_fd); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 188 | ch_error(channel_T *ch, char *msg) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 189 | { |
| 190 | if (log_fd != NULL) |
| 191 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 192 | ch_log_lead("ERR ", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 193 | fputs(msg, log_fd); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 194 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 195 | fflush(log_fd); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 200 | ch_errorn(channel_T *ch, char *msg, int nr) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 201 | { |
| 202 | if (log_fd != NULL) |
| 203 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 204 | ch_log_lead("ERR ", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 205 | fprintf(log_fd, msg, nr); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 206 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 207 | fflush(log_fd); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 212 | ch_errors(channel_T *ch, char *msg, char *arg) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 213 | { |
| 214 | if (log_fd != NULL) |
| 215 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 216 | ch_log_lead("ERR ", ch); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 217 | fprintf(log_fd, msg, arg); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 218 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 219 | fflush(log_fd); |
| 220 | } |
| 221 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 222 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 223 | #ifdef _WIN32 |
| 224 | # undef PERROR |
| 225 | # define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \ |
| 226 | (char_u *)msg, (char_u *)strerror_win32(errno)) |
| 227 | |
| 228 | static char * |
| 229 | strerror_win32(int eno) |
| 230 | { |
| 231 | static LPVOID msgbuf = NULL; |
| 232 | char_u *ptr; |
| 233 | |
| 234 | if (msgbuf) |
| 235 | LocalFree(msgbuf); |
| 236 | FormatMessage( |
| 237 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 238 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 239 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 240 | NULL, |
| 241 | eno, |
| 242 | MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), |
| 243 | (LPTSTR) &msgbuf, |
| 244 | 0, |
| 245 | NULL); |
| 246 | /* chomp \r or \n */ |
| 247 | for (ptr = (char_u *)msgbuf; *ptr; ptr++) |
| 248 | switch (*ptr) |
| 249 | { |
| 250 | case '\r': |
| 251 | STRMOVE(ptr, ptr + 1); |
| 252 | ptr--; |
| 253 | break; |
| 254 | case '\n': |
| 255 | if (*(ptr + 1) == '\0') |
| 256 | *ptr = '\0'; |
| 257 | else |
| 258 | *ptr = ' '; |
| 259 | break; |
| 260 | } |
| 261 | return msgbuf; |
| 262 | } |
| 263 | #endif |
| 264 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 265 | /* |
| 266 | * The list of all allocated channels. |
| 267 | */ |
| 268 | static channel_T *first_channel = NULL; |
| 269 | static int next_ch_id = 0; |
| 270 | |
| 271 | /* |
| 272 | * Allocate a new channel. The refcount is set to 1. |
| 273 | * The channel isn't actually used until it is opened. |
| 274 | * Returns NULL if out of memory. |
| 275 | */ |
| 276 | channel_T * |
| 277 | add_channel(void) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 278 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 279 | int part; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 280 | channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T)); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 281 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 282 | if (channel == NULL) |
| 283 | return NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 284 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 285 | channel->ch_id = next_ch_id++; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 286 | ch_log(channel, "Created channel"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 287 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 288 | #ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 289 | for (part = PART_SOCK; part <= PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 290 | #else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 291 | part = PART_SOCK; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 292 | #endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 293 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 294 | channel->ch_part[part].ch_fd = INVALID_FD; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 295 | #ifdef FEAT_GUI_X11 |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 296 | channel->ch_part[part].ch_inputHandler = (XtInputId)NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 297 | #endif |
| 298 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 299 | channel->ch_part[part].ch_inputHandler = 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 300 | #endif |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 301 | channel->ch_part[part].ch_timeout = 2000; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 302 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 303 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 304 | if (first_channel != NULL) |
| 305 | { |
| 306 | first_channel->ch_prev = channel; |
| 307 | channel->ch_next = first_channel; |
| 308 | } |
| 309 | first_channel = channel; |
| 310 | |
| 311 | channel->ch_refcount = 1; |
| 312 | return channel; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 313 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 314 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 315 | /* |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 316 | * Called when the refcount of a channel is zero. |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 317 | * Return TRUE if "channel" has a callback and the associated job wasn't |
| 318 | * killed. |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 319 | */ |
| 320 | static int |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 321 | channel_still_useful(channel_T *channel) |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 322 | { |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 323 | int has_sock_msg; |
| 324 | #ifdef CHANNEL_PIPES |
| 325 | int has_out_msg; |
| 326 | int has_err_msg; |
| 327 | #endif |
| 328 | |
| 329 | /* If the job was killed the channel is not expected to work anymore. */ |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 330 | if (channel->ch_job_killed && channel->ch_job == NULL) |
| 331 | return FALSE; |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 332 | |
| 333 | /* If there is a close callback it may still need to be invoked. */ |
| 334 | if (channel->ch_close_cb != NULL) |
| 335 | return TRUE; |
| 336 | |
| 337 | /* If there is no callback then nobody can get readahead. If the fd is |
| 338 | * closed and there is no readahead then the callback won't be called. */ |
| 339 | has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD |
| 340 | || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL |
| 341 | || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL; |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 342 | #ifdef CHANNEL_PIPES |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 343 | has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD |
| 344 | || channel->ch_part[PART_OUT].ch_head.rq_next != NULL |
| 345 | || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL; |
| 346 | has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD |
| 347 | || channel->ch_part[PART_ERR].ch_head.rq_next != NULL |
| 348 | || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL; |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 349 | #endif |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 350 | return (channel->ch_callback != NULL && (has_sock_msg |
| 351 | #ifdef CHANNEL_PIPES |
| 352 | || has_out_msg || has_err_msg |
| 353 | #endif |
| 354 | )) |
| 355 | #ifdef CHANNEL_PIPES |
| 356 | || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg) |
| 357 | || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg) |
| 358 | #endif |
| 359 | ; |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Close a channel and free all its resources if there is no further action |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 364 | * possible, there is no callback to be invoked or the associated job was |
| 365 | * killed. |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 366 | * Return TRUE if the channel was freed. |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 367 | */ |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 368 | int |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 369 | channel_may_free(channel_T *channel) |
| 370 | { |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 371 | if (!channel_still_useful(channel)) |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 372 | { |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 373 | channel_free(channel); |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 374 | return TRUE; |
| 375 | } |
| 376 | return FALSE; |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 380 | * Close a channel and free all its resources. |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 381 | */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 382 | void |
| 383 | channel_free(channel_T *channel) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 384 | { |
Bram Moolenaar | 8b37421 | 2016-02-24 20:43:06 +0100 | [diff] [blame] | 385 | channel_close(channel, TRUE); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 386 | channel_clear(channel); |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 387 | ch_log(channel, "Freeing channel"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 388 | if (channel->ch_next != NULL) |
| 389 | channel->ch_next->ch_prev = channel->ch_prev; |
| 390 | if (channel->ch_prev == NULL) |
| 391 | first_channel = channel->ch_next; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 392 | else |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 393 | channel->ch_prev->ch_next = channel->ch_next; |
| 394 | vim_free(channel); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 397 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 398 | |
| 399 | #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
| 400 | static void |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 401 | channel_read_fd(int fd) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 402 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 403 | channel_T *channel; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 404 | int part; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 405 | |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 406 | channel = channel_fd2channel(fd, &part); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 407 | if (channel == NULL) |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 408 | ch_errorn(NULL, "Channel for fd %d not found", fd); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 409 | else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 410 | channel_read(channel, part, "messageFromNetbeans"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 411 | } |
| 412 | #endif |
| 413 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 414 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 415 | * Read a command from netbeans. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 416 | * TODO: instead of channel ID use the FD. |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 417 | */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 418 | #ifdef FEAT_GUI_X11 |
| 419 | static void |
| 420 | messageFromNetbeans(XtPointer clientData, |
| 421 | int *unused1 UNUSED, |
| 422 | XtInputId *unused2 UNUSED) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 423 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 424 | channel_read_fd((int)(long)clientData); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 425 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 426 | #endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 427 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 428 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 429 | # if GTK_CHECK_VERSION(3,0,0) |
| 430 | static gboolean |
| 431 | messageFromNetbeans(GIOChannel *unused1 UNUSED, |
| 432 | GIOCondition unused2 UNUSED, |
| 433 | gpointer clientData) |
| 434 | { |
| 435 | channel_read_fd(GPOINTER_TO_INT(clientData)); |
| 436 | return TRUE; /* Return FALSE instead in case the event source is to |
| 437 | * be removed after this function returns. */ |
| 438 | } |
| 439 | # else |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 440 | static void |
| 441 | messageFromNetbeans(gpointer clientData, |
| 442 | gint unused1 UNUSED, |
| 443 | GdkInputCondition unused2 UNUSED) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 444 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 445 | channel_read_fd((int)(long)clientData); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 446 | } |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 447 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 448 | #endif |
| 449 | |
| 450 | static void |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 451 | channel_gui_register_one(channel_T *channel, int part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 452 | { |
| 453 | # ifdef FEAT_GUI_X11 |
| 454 | /* Tell notifier we are interested in being called |
| 455 | * when there is input on the editor connection socket. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 456 | if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL) |
| 457 | channel->ch_part[part].ch_inputHandler = XtAppAddInput( |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 458 | (XtAppContext)app_context, |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 459 | channel->ch_part[part].ch_fd, |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 460 | (XtPointer)(XtInputReadMask + XtInputExceptMask), |
| 461 | messageFromNetbeans, |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 462 | (XtPointer)(long)channel->ch_part[part].ch_fd); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 463 | # else |
| 464 | # ifdef FEAT_GUI_GTK |
| 465 | /* Tell gdk we are interested in being called when there |
| 466 | * is input on the editor connection socket. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 467 | if (channel->ch_part[part].ch_inputHandler == 0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 468 | # if GTK_CHECK_VERSION(3,0,0) |
| 469 | { |
| 470 | GIOChannel *chnnl = g_io_channel_unix_new( |
| 471 | (gint)channel->ch_part[part].ch_fd); |
| 472 | |
| 473 | channel->ch_part[part].ch_inputHandler = g_io_add_watch( |
| 474 | chnnl, |
| 475 | G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI, |
| 476 | messageFromNetbeans, |
| 477 | GINT_TO_POINTER(channel->ch_part[part].ch_fd)); |
| 478 | |
| 479 | g_io_channel_unref(chnnl); |
| 480 | } |
| 481 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 482 | channel->ch_part[part].ch_inputHandler = gdk_input_add( |
| 483 | (gint)channel->ch_part[part].ch_fd, |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 484 | (GdkInputCondition) |
| 485 | ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION), |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 486 | messageFromNetbeans, |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 487 | (gpointer)(long)channel->ch_part[part].ch_fd); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 488 | # endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 489 | # endif |
| 490 | # endif |
| 491 | } |
| 492 | |
| 493 | void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 494 | channel_gui_register(channel_T *channel) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 495 | { |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 496 | if (!CH_HAS_GUI) |
| 497 | return; |
| 498 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 499 | if (channel->CH_SOCK_FD != INVALID_FD) |
| 500 | channel_gui_register_one(channel, PART_SOCK); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 501 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 502 | if (channel->CH_OUT_FD != INVALID_FD) |
| 503 | channel_gui_register_one(channel, PART_OUT); |
| 504 | if (channel->CH_ERR_FD != INVALID_FD) |
| 505 | channel_gui_register_one(channel, PART_ERR); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 506 | # endif |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Register any of our file descriptors with the GUI event handling system. |
| 511 | * Called when the GUI has started. |
| 512 | */ |
| 513 | void |
| 514 | channel_gui_register_all(void) |
| 515 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 516 | channel_T *channel; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 517 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 518 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 519 | channel_gui_register(channel); |
| 520 | } |
| 521 | |
| 522 | static void |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 523 | channel_gui_unregister(channel_T *channel) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 524 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 525 | int part; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 526 | |
| 527 | #ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 528 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 529 | #else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 530 | part = PART_SOCK; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 531 | #endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 532 | { |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 533 | # ifdef FEAT_GUI_X11 |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 534 | if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 535 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 536 | XtRemoveInput(channel->ch_part[part].ch_inputHandler); |
| 537 | channel->ch_part[part].ch_inputHandler = (XtInputId)NULL; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 538 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 539 | # else |
| 540 | # ifdef FEAT_GUI_GTK |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 541 | if (channel->ch_part[part].ch_inputHandler != 0) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 542 | { |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 543 | # if GTK_CHECK_VERSION(3,0,0) |
| 544 | g_source_remove(channel->ch_part[part].ch_inputHandler); |
| 545 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 546 | gdk_input_remove(channel->ch_part[part].ch_inputHandler); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 547 | # endif |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 548 | channel->ch_part[part].ch_inputHandler = 0; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 549 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 550 | # endif |
| 551 | # endif |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 552 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | #endif |
| 556 | |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 557 | static char *e_cannot_connect = N_("E902: Cannot connect to port"); |
| 558 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 559 | /* |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 560 | * Open a socket channel to "hostname":"port". |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 561 | * "waittime" is the time in msec to wait for the connection. |
| 562 | * When negative wait forever. |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 563 | * Returns the channel for success. |
| 564 | * Returns NULL for failure. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 565 | */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 566 | channel_T * |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 567 | channel_open( |
| 568 | char *hostname, |
| 569 | int port_in, |
| 570 | int waittime, |
| 571 | void (*nb_close_cb)(void)) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 572 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 573 | int sd = -1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 574 | struct sockaddr_in server; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 575 | struct hostent *host; |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 576 | #ifdef WIN32 |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 577 | u_short port = port_in; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 578 | u_long val = 1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 579 | #else |
| 580 | int port = port_in; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 581 | struct timeval start_tv; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 582 | #endif |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 583 | channel_T *channel; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 584 | int ret; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 585 | |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 586 | #ifdef WIN32 |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 587 | channel_init_winsock(); |
| 588 | #endif |
| 589 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 590 | channel = add_channel(); |
| 591 | if (channel == NULL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 592 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 593 | ch_error(NULL, "Cannot allocate channel."); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 594 | return NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* Get the server internet address and put into addr structure */ |
| 598 | /* fill in the socket address structure and connect to server */ |
| 599 | vim_memset((char *)&server, 0, sizeof(server)); |
| 600 | server.sin_family = AF_INET; |
| 601 | server.sin_port = htons(port); |
| 602 | if ((host = gethostbyname(hostname)) == NULL) |
| 603 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 604 | ch_error(channel, "in gethostbyname() in channel_open()"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 605 | PERROR("E901: gethostbyname() in channel_open()"); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 606 | channel_free(channel); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 607 | return NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 608 | } |
| 609 | memcpy((char *)&server.sin_addr, host->h_addr, host->h_length); |
| 610 | |
Bram Moolenaar | 254e00d | 2016-02-19 23:23:12 +0100 | [diff] [blame] | 611 | /* On Mac and Solaris a zero timeout almost never works. At least wait |
| 612 | * one millisecond. Let's do it for all systems, because we don't know why |
| 613 | * this is needed. */ |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 614 | if (waittime == 0) |
| 615 | waittime = 1; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * For Unix we need to call connect() again after connect() failed. |
| 619 | * On Win32 one time is sufficient. |
| 620 | */ |
| 621 | while (TRUE) |
| 622 | { |
| 623 | if (sd >= 0) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 624 | sock_close(sd); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 625 | sd = socket(AF_INET, SOCK_STREAM, 0); |
| 626 | if (sd == -1) |
| 627 | { |
| 628 | ch_error(channel, "in socket() in channel_open()."); |
| 629 | PERROR("E898: socket() in channel_open()"); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 630 | channel_free(channel); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 631 | return NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 632 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 633 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 634 | if (waittime >= 0) |
| 635 | { |
| 636 | /* Make connect() non-blocking. */ |
| 637 | if ( |
| 638 | #ifdef _WIN32 |
| 639 | ioctlsocket(sd, FIONBIO, &val) < 0 |
| 640 | #else |
| 641 | fcntl(sd, F_SETFL, O_NONBLOCK) < 0 |
| 642 | #endif |
| 643 | ) |
| 644 | { |
| 645 | SOCK_ERRNO; |
| 646 | ch_errorn(channel, |
| 647 | "channel_open: Connect failed with errno %d", errno); |
| 648 | sock_close(sd); |
| 649 | channel_free(channel); |
| 650 | return NULL; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* Try connecting to the server. */ |
| 655 | ch_logsn(channel, "Connecting to %s port %d", hostname, port); |
| 656 | ret = connect(sd, (struct sockaddr *)&server, sizeof(server)); |
| 657 | |
| 658 | SOCK_ERRNO; |
| 659 | if (ret < 0) |
| 660 | { |
| 661 | if (errno != EWOULDBLOCK |
| 662 | && errno != ECONNREFUSED |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 663 | #ifdef EINPROGRESS |
| 664 | && errno != EINPROGRESS |
| 665 | #endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 666 | ) |
| 667 | { |
| 668 | ch_errorn(channel, |
| 669 | "channel_open: Connect failed with errno %d", errno); |
| 670 | PERROR(_(e_cannot_connect)); |
| 671 | sock_close(sd); |
| 672 | channel_free(channel); |
| 673 | return NULL; |
| 674 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 675 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 676 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 677 | /* If we don't block and connect() failed then try using select() to |
| 678 | * wait for the connection to be made. */ |
| 679 | if (waittime >= 0 && ret < 0) |
| 680 | { |
| 681 | struct timeval tv; |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 682 | fd_set rfds; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 683 | fd_set wfds; |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 684 | #ifndef WIN32 |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 685 | int so_error = 0; |
| 686 | socklen_t so_error_len = sizeof(so_error); |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 687 | #endif |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 688 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 689 | FD_ZERO(&rfds); |
| 690 | FD_SET(sd, &rfds); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 691 | FD_ZERO(&wfds); |
| 692 | FD_SET(sd, &wfds); |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 693 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 694 | tv.tv_sec = waittime / 1000; |
| 695 | tv.tv_usec = (waittime % 1000) * 1000; |
| 696 | #ifndef WIN32 |
| 697 | gettimeofday(&start_tv, NULL); |
| 698 | #endif |
| 699 | ch_logn(channel, |
| 700 | "Waiting for connection (waittime %d msec)...", waittime); |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 701 | ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv); |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 702 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 703 | if (ret < 0) |
| 704 | { |
| 705 | SOCK_ERRNO; |
| 706 | ch_errorn(channel, |
| 707 | "channel_open: Connect failed with errno %d", errno); |
| 708 | PERROR(_(e_cannot_connect)); |
| 709 | sock_close(sd); |
| 710 | channel_free(channel); |
| 711 | return NULL; |
| 712 | } |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 713 | |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 714 | #ifdef WIN32 |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 715 | /* On Win32: select() is expected to work and wait for up to the |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 716 | * waittime for the socket to be open. */ |
| 717 | if (!FD_ISSET(sd, &wfds) || ret == 0) |
| 718 | #else |
| 719 | /* On Linux-like systems: See socket(7) for the behavior |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 720 | * After putting the socket in non-blocking mode, connect() will |
| 721 | * return EINPROGRESS, select() will not wait (as if writing is |
| 722 | * possible), need to use getsockopt() to check if the socket is |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 723 | * actually connect. |
| 724 | * We detect an failure to connect when both read and write fds |
| 725 | * are set. Use getsockopt() to find out what kind of failure. */ |
| 726 | if (FD_ISSET(sd, &rfds) && FD_ISSET(sd, &wfds)) |
| 727 | { |
| 728 | ret = getsockopt(sd, |
| 729 | SOL_SOCKET, SO_ERROR, &so_error, &so_error_len); |
| 730 | if (ret < 0 || (so_error != 0 |
| 731 | && so_error != EWOULDBLOCK |
| 732 | && so_error != ECONNREFUSED |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 733 | # ifdef EINPROGRESS |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 734 | && so_error != EINPROGRESS |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 735 | # endif |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 736 | )) |
| 737 | { |
| 738 | ch_errorn(channel, |
| 739 | "channel_open: Connect failed with errno %d", |
| 740 | so_error); |
| 741 | PERROR(_(e_cannot_connect)); |
| 742 | sock_close(sd); |
| 743 | channel_free(channel); |
| 744 | return NULL; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (!FD_ISSET(sd, &wfds) || so_error != 0) |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 749 | #endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 750 | { |
| 751 | #ifndef WIN32 |
| 752 | struct timeval end_tv; |
| 753 | long elapsed_msec; |
| 754 | |
| 755 | gettimeofday(&end_tv, NULL); |
| 756 | elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000 |
| 757 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; |
| 758 | if (waittime > 1 && elapsed_msec < waittime) |
| 759 | { |
| 760 | /* The port isn't ready but we also didn't get an error. |
| 761 | * This happens when the server didn't open the socket |
| 762 | * yet. Wait a bit and try again. */ |
| 763 | mch_delay(waittime < 50 ? (long)waittime : 50L, TRUE); |
| 764 | ui_breakcheck(); |
| 765 | if (!got_int) |
| 766 | { |
| 767 | /* reduce the waittime by the elapsed time and the 50 |
| 768 | * msec delay (or a bit more) */ |
| 769 | waittime -= elapsed_msec; |
| 770 | if (waittime > 50) |
| 771 | waittime -= 50; |
| 772 | else |
| 773 | waittime = 1; |
| 774 | continue; |
| 775 | } |
| 776 | /* we were interrupted, behave as if timed out */ |
| 777 | } |
| 778 | #endif |
| 779 | /* We timed out. */ |
| 780 | ch_error(channel, "Connection timed out"); |
| 781 | sock_close(sd); |
| 782 | channel_free(channel); |
| 783 | return NULL; |
| 784 | } |
| 785 | |
| 786 | ch_log(channel, "Connection made"); |
| 787 | break; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 788 | } |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 789 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 790 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 791 | if (waittime >= 0) |
| 792 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 793 | #ifdef _WIN32 |
| 794 | val = 0; |
| 795 | ioctlsocket(sd, FIONBIO, &val); |
| 796 | #else |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 797 | (void)fcntl(sd, F_SETFL, 0); |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 798 | #endif |
| 799 | } |
| 800 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 801 | channel->CH_SOCK_FD = (sock_T)sd; |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 802 | channel->ch_nb_close_cb = nb_close_cb; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 803 | |
| 804 | #ifdef FEAT_GUI |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 805 | channel_gui_register(channel); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 806 | #endif |
| 807 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 808 | return channel; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | #if defined(CHANNEL_PIPES) || defined(PROTO) |
| 812 | void |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 813 | channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 814 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 815 | channel->CH_IN_FD = in; |
| 816 | channel->CH_OUT_FD = out; |
| 817 | channel->CH_ERR_FD = err; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 818 | } |
| 819 | #endif |
| 820 | |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 821 | /* |
| 822 | * Sets the job the channel is associated with. |
| 823 | * This does not keep a refcount, when the job is freed ch_job is cleared. |
| 824 | */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 825 | void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 826 | channel_set_job(channel_T *channel, job_T *job) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 827 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 828 | channel->ch_job = job; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /* |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 832 | * Find a buffer matching "name" or create a new one. |
| 833 | */ |
| 834 | static buf_T * |
| 835 | find_buffer(char_u *name) |
| 836 | { |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 837 | buf_T *buf = NULL; |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 838 | buf_T *save_curbuf = curbuf; |
| 839 | |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 840 | if (name != NULL && *name != NUL) |
| 841 | buf = buflist_findname(name); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 842 | if (buf == NULL) |
| 843 | { |
Bram Moolenaar | e26643e | 2016-02-27 21:53:02 +0100 | [diff] [blame] | 844 | buf = buflist_new(name == NULL || *name == NUL ? NULL : name, |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 845 | NULL, (linenr_T)0, BLN_LISTED); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 846 | buf_copy_options(buf, BCO_ENTER); |
| 847 | #ifdef FEAT_QUICKFIX |
| 848 | clear_string_option(&buf->b_p_bt); |
| 849 | buf->b_p_bt = vim_strsave((char_u *)"nofile"); |
| 850 | clear_string_option(&buf->b_p_bh); |
| 851 | buf->b_p_bh = vim_strsave((char_u *)"hide"); |
| 852 | #endif |
| 853 | curbuf = buf; |
| 854 | ml_open(curbuf); |
| 855 | ml_replace(1, (char_u *)"Reading from channel output...", TRUE); |
| 856 | changed_bytes(1, 0); |
| 857 | curbuf = save_curbuf; |
| 858 | } |
| 859 | |
| 860 | return buf; |
| 861 | } |
| 862 | |
| 863 | /* |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 864 | * Set various properties from an "opt" argument. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 865 | */ |
| 866 | void |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 867 | channel_set_options(channel_T *channel, jobopt_T *opt) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 868 | { |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 869 | int part; |
| 870 | char_u **cbp; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 871 | |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 872 | if (opt->jo_set & JO_MODE) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 873 | for (part = PART_SOCK; part <= PART_IN; ++part) |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 874 | channel->ch_part[part].ch_mode = opt->jo_mode; |
| 875 | if (opt->jo_set & JO_IN_MODE) |
| 876 | channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode; |
| 877 | if (opt->jo_set & JO_OUT_MODE) |
| 878 | channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode; |
| 879 | if (opt->jo_set & JO_ERR_MODE) |
| 880 | channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode; |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 881 | |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 882 | if (opt->jo_set & JO_TIMEOUT) |
| 883 | for (part = PART_SOCK; part <= PART_IN; ++part) |
| 884 | channel->ch_part[part].ch_timeout = opt->jo_timeout; |
| 885 | if (opt->jo_set & JO_OUT_TIMEOUT) |
| 886 | channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout; |
| 887 | if (opt->jo_set & JO_ERR_TIMEOUT) |
| 888 | channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout; |
| 889 | |
| 890 | if (opt->jo_set & JO_CALLBACK) |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 891 | { |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 892 | cbp = &channel->ch_callback; |
| 893 | vim_free(*cbp); |
| 894 | if (opt->jo_callback != NULL && *opt->jo_callback != NUL) |
| 895 | *cbp = vim_strsave(opt->jo_callback); |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 896 | else |
Bram Moolenaar | b6b5252 | 2016-02-20 23:30:07 +0100 | [diff] [blame] | 897 | *cbp = NULL; |
| 898 | } |
| 899 | if (opt->jo_set & JO_OUT_CALLBACK) |
| 900 | { |
| 901 | cbp = &channel->ch_part[PART_OUT].ch_callback; |
| 902 | vim_free(*cbp); |
| 903 | if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL) |
| 904 | *cbp = vim_strsave(opt->jo_out_cb); |
| 905 | else |
| 906 | *cbp = NULL; |
| 907 | } |
| 908 | if (opt->jo_set & JO_ERR_CALLBACK) |
| 909 | { |
| 910 | cbp = &channel->ch_part[PART_ERR].ch_callback; |
| 911 | vim_free(*cbp); |
| 912 | if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL) |
| 913 | *cbp = vim_strsave(opt->jo_err_cb); |
| 914 | else |
| 915 | *cbp = NULL; |
Bram Moolenaar | 0ba75a9 | 2016-02-19 23:21:26 +0100 | [diff] [blame] | 916 | } |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 917 | if (opt->jo_set & JO_CLOSE_CALLBACK) |
| 918 | { |
| 919 | cbp = &channel->ch_close_cb; |
| 920 | vim_free(*cbp); |
| 921 | if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL) |
| 922 | *cbp = vim_strsave(opt->jo_close_cb); |
| 923 | else |
| 924 | *cbp = NULL; |
| 925 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 926 | |
| 927 | if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER) |
| 928 | { |
| 929 | /* writing output to a buffer. Force mode to NL. */ |
| 930 | channel->ch_part[PART_OUT].ch_mode = MODE_NL; |
| 931 | channel->ch_part[PART_OUT].ch_buffer = |
| 932 | find_buffer(opt->jo_io_name[PART_OUT]); |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 933 | ch_logs(channel, "writing to buffer '%s'", |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 934 | (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname); |
| 935 | } |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 939 | * Set the callback for "channel"/"part" for the response with "id". |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 940 | */ |
| 941 | void |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 942 | channel_set_req_callback( |
| 943 | channel_T *channel, |
| 944 | int part, |
| 945 | char_u *callback, |
| 946 | int id) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 947 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 948 | cbq_T *head = &channel->ch_part[part].ch_cb_head; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 949 | cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T)); |
| 950 | |
| 951 | if (item != NULL) |
| 952 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 953 | item->cq_callback = vim_strsave(callback); |
| 954 | item->cq_seq_nr = id; |
| 955 | item->cq_prev = head->cq_prev; |
| 956 | head->cq_prev = item; |
| 957 | item->cq_next = NULL; |
| 958 | if (item->cq_prev == NULL) |
| 959 | head->cq_next = item; |
| 960 | else |
| 961 | item->cq_prev->cq_next = item; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 962 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 966 | * Invoke the "callback" on channel "channel". |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 967 | */ |
| 968 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 969 | invoke_callback(channel_T *channel, char_u *callback, typval_T *argv) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 970 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 971 | typval_T rettv; |
| 972 | int dummy; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 973 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 974 | argv[0].v_type = VAR_CHANNEL; |
| 975 | argv[0].vval.v_channel = channel; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 976 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 977 | call_func(callback, (int)STRLEN(callback), |
| 978 | &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL); |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 979 | clear_tv(&rettv); |
| 980 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 981 | /* If an echo command was used the cursor needs to be put back where |
Bram Moolenaar | 18b5d6d | 2016-02-28 19:30:24 +0100 | [diff] [blame] | 982 | * it belongs. If highlighting was changed a redraw is needed. */ |
| 983 | update_screen(0); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 984 | setcursor(); |
| 985 | cursor_on(); |
| 986 | out_flush(); |
Bram Moolenaar | 18b5d6d | 2016-02-28 19:30:24 +0100 | [diff] [blame] | 987 | #ifdef FEAT_GUI |
| 988 | gui_update_cursor(TRUE, FALSE); |
| 989 | gui_mch_flush(); |
| 990 | #endif |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 993 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 994 | * Return the first buffer from channel "channel"/"part" and remove it. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 995 | * The caller must free it. |
| 996 | * Returns NULL if there is nothing. |
| 997 | */ |
| 998 | char_u * |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 999 | channel_get(channel_T *channel, int part) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1000 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1001 | readq_T *head = &channel->ch_part[part].ch_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1002 | readq_T *node = head->rq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1003 | char_u *p; |
| 1004 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1005 | if (node == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1006 | return NULL; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1007 | /* dispose of the node but keep the buffer */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1008 | p = node->rq_buffer; |
| 1009 | head->rq_next = node->rq_next; |
| 1010 | if (node->rq_next == NULL) |
| 1011 | head->rq_prev = NULL; |
| 1012 | else |
| 1013 | node->rq_next->rq_prev = NULL; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1014 | vim_free(node); |
| 1015 | return p; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1019 | * Returns the whole buffer contents concatenated for "channel"/"part". |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1020 | */ |
| 1021 | static char_u * |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1022 | channel_get_all(channel_T *channel, int part) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1023 | { |
| 1024 | /* Concatenate everything into one buffer. |
| 1025 | * TODO: avoid multiple allocations. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1026 | while (channel_collapse(channel, part) == OK) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1027 | ; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1028 | return channel_get(channel, part); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1032 | * Collapses the first and second buffer for "channel"/"part". |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1033 | * Returns FAIL if that is not possible. |
| 1034 | */ |
| 1035 | int |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1036 | channel_collapse(channel_T *channel, int part) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1037 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1038 | readq_T *head = &channel->ch_part[part].ch_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1039 | readq_T *node = head->rq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1040 | char_u *p; |
| 1041 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1042 | if (node == NULL || node->rq_next == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1043 | return FAIL; |
| 1044 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1045 | p = alloc((unsigned)(STRLEN(node->rq_buffer) |
| 1046 | + STRLEN(node->rq_next->rq_buffer) + 1)); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1047 | if (p == NULL) |
| 1048 | return FAIL; /* out of memory */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1049 | STRCPY(p, node->rq_buffer); |
| 1050 | STRCAT(p, node->rq_next->rq_buffer); |
| 1051 | vim_free(node->rq_next->rq_buffer); |
| 1052 | node->rq_next->rq_buffer = p; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1053 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1054 | /* dispose of the node and its buffer */ |
| 1055 | head->rq_next = node->rq_next; |
| 1056 | head->rq_next->rq_prev = NULL; |
| 1057 | vim_free(node->rq_buffer); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1058 | vim_free(node); |
| 1059 | return OK; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1063 | * Store "buf[len]" on "channel"/"part". |
| 1064 | * Returns OK or FAIL. |
| 1065 | */ |
| 1066 | static int |
| 1067 | channel_save(channel_T *channel, int part, char_u *buf, int len) |
| 1068 | { |
| 1069 | readq_T *node; |
| 1070 | readq_T *head = &channel->ch_part[part].ch_head; |
| 1071 | char_u *p; |
| 1072 | int i; |
| 1073 | |
| 1074 | node = (readq_T *)alloc(sizeof(readq_T)); |
| 1075 | if (node == NULL) |
| 1076 | return FAIL; /* out of memory */ |
| 1077 | node->rq_buffer = alloc(len + 1); |
| 1078 | if (node->rq_buffer == NULL) |
| 1079 | { |
| 1080 | vim_free(node); |
| 1081 | return FAIL; /* out of memory */ |
| 1082 | } |
| 1083 | |
| 1084 | if (channel->ch_part[part].ch_mode == MODE_NL) |
| 1085 | { |
| 1086 | /* Drop any CR before a NL. */ |
| 1087 | p = node->rq_buffer; |
| 1088 | for (i = 0; i < len; ++i) |
| 1089 | if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL) |
| 1090 | *p++ = buf[i]; |
| 1091 | *p = NUL; |
| 1092 | } |
| 1093 | else |
| 1094 | { |
| 1095 | mch_memmove(node->rq_buffer, buf, len); |
| 1096 | node->rq_buffer[len] = NUL; |
| 1097 | } |
| 1098 | |
| 1099 | /* append node to the tail of the queue */ |
| 1100 | node->rq_next = NULL; |
| 1101 | node->rq_prev = head->rq_prev; |
| 1102 | if (head->rq_prev == NULL) |
| 1103 | head->rq_next = node; |
| 1104 | else |
| 1105 | head->rq_prev->rq_next = node; |
| 1106 | head->rq_prev = node; |
| 1107 | |
| 1108 | if (log_fd != NULL) |
| 1109 | { |
| 1110 | ch_log_lead("RECV ", channel); |
| 1111 | fprintf(log_fd, "'"); |
| 1112 | if (fwrite(buf, len, 1, log_fd) != 1) |
| 1113 | return FAIL; |
| 1114 | fprintf(log_fd, "'\n"); |
| 1115 | } |
| 1116 | return OK; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Use the read buffer of "channel"/"part" and parse a JSON messages that is |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1121 | * complete. The messages are added to the queue. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1122 | * Return TRUE if there is more to read. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1123 | */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1124 | static int |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1125 | channel_parse_json(channel_T *channel, int part) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1126 | { |
| 1127 | js_read_T reader; |
| 1128 | typval_T listtv; |
| 1129 | jsonq_T *item; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1130 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1131 | int ret; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1132 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1133 | if (channel_peek(channel, part) == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1134 | return FALSE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1135 | |
| 1136 | /* TODO: make reader work properly */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1137 | /* reader.js_buf = channel_peek(channel, part); */ |
| 1138 | reader.js_buf = channel_get_all(channel, part); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1139 | reader.js_used = 0; |
Bram Moolenaar | 56ead34 | 2016-02-02 18:20:08 +0100 | [diff] [blame] | 1140 | reader.js_fill = NULL; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1141 | /* reader.js_fill = channel_fill; */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1142 | reader.js_cookie = channel; |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 1143 | ret = json_decode(&reader, &listtv, |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1144 | channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1145 | if (ret == OK) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1146 | { |
Bram Moolenaar | 6076fe1 | 2016-02-05 22:49:56 +0100 | [diff] [blame] | 1147 | /* Only accept the response when it is a list with at least two |
| 1148 | * items. */ |
| 1149 | if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1150 | { |
| 1151 | /* TODO: give error */ |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1152 | clear_tv(&listtv); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1153 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1154 | else |
| 1155 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1156 | item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T)); |
| 1157 | if (item == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1158 | clear_tv(&listtv); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1159 | else |
| 1160 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1161 | item->jq_value = alloc_tv(); |
| 1162 | if (item->jq_value == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1163 | { |
| 1164 | vim_free(item); |
| 1165 | clear_tv(&listtv); |
| 1166 | } |
| 1167 | else |
| 1168 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1169 | *item->jq_value = listtv; |
| 1170 | item->jq_prev = head->jq_prev; |
| 1171 | head->jq_prev = item; |
| 1172 | item->jq_next = NULL; |
| 1173 | if (item->jq_prev == NULL) |
| 1174 | head->jq_next = item; |
| 1175 | else |
| 1176 | item->jq_prev->jq_next = item; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1177 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | } |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1181 | |
| 1182 | /* Put the unread part back into the channel. |
| 1183 | * TODO: insert in front */ |
| 1184 | if (reader.js_buf[reader.js_used] != NUL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1185 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1186 | channel_save(channel, part, reader.js_buf + reader.js_used, |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1187 | (int)(reader.js_end - reader.js_buf) - reader.js_used); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1188 | ret = TRUE; |
| 1189 | } |
| 1190 | else |
| 1191 | ret = FALSE; |
| 1192 | |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1193 | vim_free(reader.js_buf); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1194 | return ret; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1198 | * Remove "node" from the queue that it is in. Does not free it. |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1199 | */ |
| 1200 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1201 | remove_cb_node(cbq_T *head, cbq_T *node) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1202 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1203 | if (node->cq_prev == NULL) |
| 1204 | head->cq_next = node->cq_next; |
| 1205 | else |
| 1206 | node->cq_prev->cq_next = node->cq_next; |
| 1207 | if (node->cq_next == NULL) |
| 1208 | head->cq_prev = node->cq_prev; |
| 1209 | else |
| 1210 | node->cq_next->cq_prev = node->cq_prev; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * Remove "node" from the queue that it is in and free it. |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1215 | * Caller should have freed or used node->jq_value. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1216 | */ |
| 1217 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1218 | remove_json_node(jsonq_T *head, jsonq_T *node) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1219 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1220 | if (node->jq_prev == NULL) |
| 1221 | head->jq_next = node->jq_next; |
| 1222 | else |
| 1223 | node->jq_prev->jq_next = node->jq_next; |
| 1224 | if (node->jq_next == NULL) |
| 1225 | head->jq_prev = node->jq_prev; |
| 1226 | else |
| 1227 | node->jq_next->jq_prev = node->jq_prev; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1228 | vim_free(node); |
| 1229 | } |
| 1230 | |
| 1231 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1232 | * Get a message from the JSON queue for channel "channel". |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1233 | * When "id" is positive it must match the first number in the list. |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1234 | * When "id" is zero or negative jut get the first message. But not the one |
| 1235 | * with id ch_block_id. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1236 | * Return OK when found and return the value in "rettv". |
| 1237 | * Return FAIL otherwise. |
| 1238 | */ |
| 1239 | static int |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1240 | channel_get_json(channel_T *channel, int part, int id, typval_T **rettv) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1241 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1242 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1243 | jsonq_T *item = head->jq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1244 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1245 | while (item != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1246 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1247 | list_T *l = item->jq_value->vval.v_list; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1248 | typval_T *tv = &l->lv_first->li_tv; |
| 1249 | |
| 1250 | if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id) |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1251 | || (id <= 0 && (tv->v_type != VAR_NUMBER |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1252 | || tv->vval.v_number == 0 |
| 1253 | || tv->vval.v_number != channel->ch_part[part].ch_block_id))) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1254 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1255 | *rettv = item->jq_value; |
| 1256 | remove_json_node(head, item); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1257 | return OK; |
| 1258 | } |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1259 | item = item->jq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1260 | } |
| 1261 | return FAIL; |
| 1262 | } |
| 1263 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1264 | #define CH_JSON_MAX_ARGS 4 |
| 1265 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1266 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1267 | * Execute a command received over "channel"/"part" |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1268 | * "argv[0]" is the command string. |
| 1269 | * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing. |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1270 | */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1271 | static void |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1272 | channel_exe_cmd(channel_T *channel, int part, typval_T *argv) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1273 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1274 | char_u *cmd = argv[0].vval.v_string; |
| 1275 | char_u *arg; |
| 1276 | int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1277 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1278 | if (argv[1].v_type != VAR_STRING) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1279 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1280 | ch_error(channel, "received command with non-string argument"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1281 | if (p_verbose > 2) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1282 | EMSG("E903: received command with non-string argument"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1283 | return; |
| 1284 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1285 | arg = argv[1].vval.v_string; |
Bram Moolenaar | 14ad611 | 2016-02-01 21:47:13 +0100 | [diff] [blame] | 1286 | if (arg == NULL) |
| 1287 | arg = (char_u *)""; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1288 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1289 | if (STRCMP(cmd, "ex") == 0) |
| 1290 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1291 | do_cmdline_cmd(arg); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1292 | } |
| 1293 | else if (STRCMP(cmd, "normal") == 0) |
| 1294 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1295 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1296 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1297 | ea.arg = arg; |
| 1298 | ea.addr_count = 0; |
| 1299 | ea.forceit = TRUE; /* no mapping */ |
| 1300 | ex_normal(&ea); |
| 1301 | } |
| 1302 | else if (STRCMP(cmd, "redraw") == 0) |
| 1303 | { |
| 1304 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1305 | |
Bram Moolenaar | 14ad611 | 2016-02-01 21:47:13 +0100 | [diff] [blame] | 1306 | ea.forceit = *arg != NUL; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1307 | ex_redraw(&ea); |
| 1308 | showruler(FALSE); |
| 1309 | setcursor(); |
| 1310 | out_flush(); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1311 | #ifdef FEAT_GUI |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1312 | if (gui.in_use) |
| 1313 | { |
| 1314 | gui_update_cursor(FALSE, FALSE); |
| 1315 | gui_mch_flush(); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1316 | } |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1317 | #endif |
| 1318 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1319 | else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1320 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1321 | int is_call = cmd[0] == 'c'; |
| 1322 | int id_idx = is_call ? 3 : 2; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1323 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1324 | if (argv[id_idx].v_type != VAR_UNKNOWN |
| 1325 | && argv[id_idx].v_type != VAR_NUMBER) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1326 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1327 | ch_error(channel, "last argument for expr/call must be a number"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1328 | if (p_verbose > 2) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1329 | EMSG("E904: last argument for expr/call must be a number"); |
| 1330 | } |
| 1331 | else if (is_call && argv[2].v_type != VAR_LIST) |
| 1332 | { |
| 1333 | ch_error(channel, "third argument for call must be a list"); |
| 1334 | if (p_verbose > 2) |
| 1335 | EMSG("E904: third argument for call must be a list"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1336 | } |
| 1337 | else |
| 1338 | { |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 1339 | typval_T *tv; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1340 | typval_T res_tv; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1341 | typval_T err_tv; |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1342 | char_u *json = NULL; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1343 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 1344 | /* Don't pollute the display with errors. */ |
| 1345 | ++emsg_skip; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1346 | if (!is_call) |
| 1347 | tv = eval_expr(arg, NULL); |
| 1348 | else if (func_call(arg, &argv[2], NULL, &res_tv) == OK) |
| 1349 | tv = &res_tv; |
| 1350 | else |
| 1351 | tv = NULL; |
| 1352 | |
| 1353 | if (argv[id_idx].v_type == VAR_NUMBER) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1354 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1355 | int id = argv[id_idx].vval.v_number; |
| 1356 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1357 | if (tv != NULL) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1358 | json = json_encode_nr_expr(id, tv, options); |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1359 | if (tv == NULL || (json != NULL && *json == NUL)) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1360 | { |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1361 | /* If evaluation failed or the result can't be encoded |
| 1362 | * then return the string "ERROR". */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1363 | vim_free(json); |
| 1364 | free_tv(tv); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1365 | err_tv.v_type = VAR_STRING; |
| 1366 | err_tv.vval.v_string = (char_u *)"ERROR"; |
| 1367 | tv = &err_tv; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1368 | json = json_encode_nr_expr(id, tv, options); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1369 | } |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1370 | if (json != NULL) |
| 1371 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1372 | channel_send(channel, |
| 1373 | part == PART_SOCK ? PART_SOCK : PART_IN, |
| 1374 | json, (char *)cmd); |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1375 | vim_free(json); |
| 1376 | } |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1377 | } |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 1378 | --emsg_skip; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1379 | if (tv == &res_tv) |
| 1380 | clear_tv(tv); |
| 1381 | else if (tv != &err_tv) |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 1382 | free_tv(tv); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1383 | } |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1384 | } |
| 1385 | else if (p_verbose > 2) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1386 | { |
| 1387 | ch_errors(channel, "Receved unknown command: %s", (char *)cmd); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1388 | EMSG2("E905: received unknown command: %s", cmd); |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1389 | } |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1393 | * Invoke a callback for "channel"/"part" if needed. |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1394 | * Return TRUE when a message was handled, there might be another one. |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1395 | */ |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1396 | static int |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1397 | may_invoke_callback(channel_T *channel, int part) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1398 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1399 | char_u *msg = NULL; |
| 1400 | typval_T *listtv = NULL; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1401 | typval_T argv[CH_JSON_MAX_ARGS]; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1402 | int seq_nr = -1; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1403 | ch_mode_T ch_mode = channel->ch_part[part].ch_mode; |
| 1404 | char_u *callback = NULL; |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1405 | buf_T *buffer = NULL; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1406 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1407 | if (channel->ch_nb_close_cb != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1408 | /* this channel is handled elsewhere (netbeans) */ |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1409 | return FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1410 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1411 | if (channel->ch_part[part].ch_callback != NULL) |
| 1412 | callback = channel->ch_part[part].ch_callback; |
| 1413 | else |
| 1414 | callback = channel->ch_callback; |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1415 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1416 | buffer = channel->ch_part[part].ch_buffer; |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1417 | if (buffer != NULL && !buf_valid(buffer)) |
| 1418 | { |
| 1419 | /* buffer was wiped out */ |
| 1420 | channel->ch_part[part].ch_buffer = NULL; |
| 1421 | buffer = NULL; |
| 1422 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1423 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1424 | if (ch_mode == MODE_JSON || ch_mode == MODE_JS) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1425 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1426 | listitem_T *item; |
| 1427 | int argc = 0; |
| 1428 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1429 | /* Get any json message in the queue. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1430 | if (channel_get_json(channel, part, -1, &listtv) == FAIL) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1431 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1432 | /* Parse readahead, return when there is still no message. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1433 | channel_parse_json(channel, part); |
| 1434 | if (channel_get_json(channel, part, -1, &listtv) == FAIL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1435 | return FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1436 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1437 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1438 | for (item = listtv->vval.v_list->lv_first; |
| 1439 | item != NULL && argc < CH_JSON_MAX_ARGS; |
| 1440 | item = item->li_next) |
| 1441 | argv[argc++] = item->li_tv; |
| 1442 | while (argc < CH_JSON_MAX_ARGS) |
| 1443 | argv[argc++].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1444 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1445 | if (argv[0].v_type == VAR_STRING) |
| 1446 | { |
| 1447 | char_u *cmd = argv[0].vval.v_string; |
| 1448 | |
| 1449 | /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1450 | ch_logs(channel, "Executing %s command", (char *)cmd); |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1451 | channel_exe_cmd(channel, part, argv); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1452 | free_tv(listtv); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1453 | return TRUE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1454 | } |
| 1455 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1456 | if (argv[0].v_type != VAR_NUMBER) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1457 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1458 | ch_error(channel, |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1459 | "Dropping message with invalid sequence number type"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1460 | free_tv(listtv); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1461 | return FALSE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1462 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 1463 | seq_nr = argv[0].vval.v_number; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1464 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1465 | else if (channel_peek(channel, part) == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1466 | { |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1467 | /* nothing to read on RAW or NL channel */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1468 | return FALSE; |
| 1469 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1470 | else |
| 1471 | { |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1472 | /* If there is no callback or buffer drop the message. */ |
| 1473 | if (callback == NULL && buffer == NULL) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1474 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1475 | while ((msg = channel_get(channel, part)) != NULL) |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1476 | { |
| 1477 | ch_logs(channel, "Dropping message '%s'", (char *)msg); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1478 | vim_free(msg); |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1479 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1480 | return FALSE; |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1481 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1482 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1483 | if (ch_mode == MODE_NL) |
| 1484 | { |
| 1485 | char_u *nl; |
| 1486 | char_u *buf; |
| 1487 | |
| 1488 | /* See if we have a message ending in NL in the first buffer. If |
| 1489 | * not try to concatenate the first and the second buffer. */ |
| 1490 | while (TRUE) |
| 1491 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1492 | buf = channel_peek(channel, part); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1493 | nl = vim_strchr(buf, NL); |
| 1494 | if (nl != NULL) |
| 1495 | break; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1496 | if (channel_collapse(channel, part) == FAIL) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1497 | return FALSE; /* incomplete message */ |
| 1498 | } |
| 1499 | if (nl[1] == NUL) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1500 | { |
| 1501 | /* get the whole buffer, drop the NL */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1502 | msg = channel_get(channel, part); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1503 | *nl = NUL; |
| 1504 | } |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1505 | else |
| 1506 | { |
| 1507 | /* Copy the message into allocated memory and remove it from |
| 1508 | * the buffer. */ |
| 1509 | msg = vim_strnsave(buf, (int)(nl - buf)); |
| 1510 | mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1); |
| 1511 | } |
| 1512 | } |
| 1513 | else |
| 1514 | /* For a raw channel we don't know where the message ends, just |
| 1515 | * get everything we have. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1516 | msg = channel_get_all(channel, part); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1517 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1518 | argv[1].v_type = VAR_STRING; |
| 1519 | argv[1].vval.v_string = msg; |
| 1520 | } |
| 1521 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1522 | if (seq_nr > 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1523 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1524 | cbq_T *head = &channel->ch_part[part].ch_cb_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1525 | cbq_T *item = head->cq_next; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1526 | int done = FALSE; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* invoke the one-time callback with the matching nr */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1529 | while (item != NULL) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1530 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1531 | if (item->cq_seq_nr == seq_nr) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1532 | { |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 1533 | ch_logs(channel, "Invoking one-time callback %s", |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1534 | (char *)item->cq_callback); |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1535 | /* Remove the item from the list first, if the callback |
| 1536 | * invokes ch_close() the list will be cleared. */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1537 | remove_cb_node(head, item); |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1538 | invoke_callback(channel, item->cq_callback, argv); |
| 1539 | vim_free(item->cq_callback); |
| 1540 | vim_free(item); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1541 | done = TRUE; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1542 | break; |
| 1543 | } |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1544 | item = item->cq_next; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1545 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1546 | if (!done) |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1547 | ch_logn(channel, "Dropping message %d without callback", seq_nr); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1548 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1549 | else if (callback != NULL || buffer != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1550 | { |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1551 | if (buffer != NULL) |
| 1552 | { |
| 1553 | buf_T *save_curbuf = curbuf; |
| 1554 | linenr_T lnum = buffer->b_ml.ml_line_count; |
| 1555 | |
| 1556 | /* Append to the buffer */ |
| 1557 | ch_logn(channel, "appending line %d to buffer", (int)lnum + 1); |
| 1558 | |
| 1559 | curbuf = buffer; |
| 1560 | u_sync(TRUE); |
| 1561 | u_save(lnum, lnum + 1); |
| 1562 | |
| 1563 | ml_append(lnum, msg, 0, FALSE); |
| 1564 | appended_lines_mark(lnum, 1L); |
| 1565 | curbuf = save_curbuf; |
| 1566 | |
| 1567 | if (buffer->b_nwindows > 0) |
| 1568 | { |
| 1569 | win_T *wp; |
| 1570 | win_T *save_curwin; |
| 1571 | |
| 1572 | FOR_ALL_WINDOWS(wp) |
| 1573 | { |
| 1574 | if (wp->w_buffer == buffer |
| 1575 | && wp->w_cursor.lnum == lnum |
| 1576 | && wp->w_cursor.col == 0) |
| 1577 | { |
| 1578 | ++wp->w_cursor.lnum; |
| 1579 | save_curwin = curwin; |
| 1580 | curwin = wp; |
| 1581 | curbuf = curwin->w_buffer; |
| 1582 | scroll_cursor_bot(0, FALSE); |
| 1583 | curwin = save_curwin; |
| 1584 | curbuf = curwin->w_buffer; |
| 1585 | } |
| 1586 | } |
| 1587 | redraw_buf_later(buffer, VALID); |
| 1588 | channel_need_redraw = TRUE; |
| 1589 | } |
| 1590 | } |
| 1591 | if (callback != NULL) |
| 1592 | { |
| 1593 | /* invoke the channel callback */ |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 1594 | ch_logs(channel, "Invoking channel callback %s", (char *)callback); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1595 | invoke_callback(channel, callback, argv); |
| 1596 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1597 | } |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1598 | else if (msg != NULL) |
| 1599 | ch_logs(channel, "Dropping message '%s'", (char *)msg); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1600 | else |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1601 | ch_log(channel, "Dropping message"); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1602 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1603 | if (listtv != NULL) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1604 | free_tv(listtv); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1605 | vim_free(msg); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1606 | |
| 1607 | return TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1611 | * Return TRUE when channel "channel" is open for writing to. |
| 1612 | * Also returns FALSE or invalid "channel". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1613 | */ |
| 1614 | int |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1615 | channel_can_write_to(channel_T *channel) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1616 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1617 | return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1618 | #ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1619 | || channel->CH_IN_FD != INVALID_FD |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1620 | #endif |
| 1621 | ); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1625 | * Return TRUE when channel "channel" is open for reading or writing. |
| 1626 | * Also returns FALSE for invalid "channel". |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1627 | */ |
| 1628 | int |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1629 | channel_is_open(channel_T *channel) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1630 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1631 | return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1632 | #ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1633 | || channel->CH_IN_FD != INVALID_FD |
| 1634 | || channel->CH_OUT_FD != INVALID_FD |
| 1635 | || channel->CH_ERR_FD != INVALID_FD |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1636 | #endif |
| 1637 | ); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1641 | * Return a string indicating the status of the channel. |
| 1642 | */ |
| 1643 | char * |
| 1644 | channel_status(channel_T *channel) |
| 1645 | { |
| 1646 | if (channel == NULL) |
| 1647 | return "fail"; |
| 1648 | if (channel_is_open(channel)) |
| 1649 | return "open"; |
| 1650 | return "closed"; |
| 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * Close channel "channel". |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 1655 | * Trigger the close callback if "invoke_close_cb" is TRUE. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1656 | * Does not clear the buffers. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1657 | */ |
| 1658 | void |
Bram Moolenaar | 8b37421 | 2016-02-24 20:43:06 +0100 | [diff] [blame] | 1659 | channel_close(channel_T *channel, int invoke_close_cb) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1660 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1661 | ch_log(channel, "Closing channel"); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1662 | |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 1663 | #ifdef FEAT_GUI |
| 1664 | channel_gui_unregister(channel); |
| 1665 | #endif |
| 1666 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1667 | if (channel->CH_SOCK_FD != INVALID_FD) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1668 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1669 | sock_close(channel->CH_SOCK_FD); |
| 1670 | channel->CH_SOCK_FD = INVALID_FD; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1671 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1672 | #if defined(CHANNEL_PIPES) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1673 | if (channel->CH_IN_FD != INVALID_FD) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1674 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1675 | fd_close(channel->CH_IN_FD); |
| 1676 | channel->CH_IN_FD = INVALID_FD; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1677 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1678 | if (channel->CH_OUT_FD != INVALID_FD) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1679 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1680 | fd_close(channel->CH_OUT_FD); |
| 1681 | channel->CH_OUT_FD = INVALID_FD; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1682 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1683 | if (channel->CH_ERR_FD != INVALID_FD) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1684 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1685 | fd_close(channel->CH_ERR_FD); |
| 1686 | channel->CH_ERR_FD = INVALID_FD; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1687 | } |
| 1688 | #endif |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 1689 | |
Bram Moolenaar | 8b37421 | 2016-02-24 20:43:06 +0100 | [diff] [blame] | 1690 | if (invoke_close_cb && channel->ch_close_cb != NULL) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1691 | { |
| 1692 | typval_T argv[1]; |
| 1693 | typval_T rettv; |
| 1694 | int dummy; |
| 1695 | |
| 1696 | /* invoke the close callback; increment the refcount to avoid it |
| 1697 | * being freed halfway */ |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1698 | ch_logs(channel, "Invoking close callback %s", |
| 1699 | (char *)channel->ch_close_cb); |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1700 | argv[0].v_type = VAR_CHANNEL; |
| 1701 | argv[0].vval.v_channel = channel; |
| 1702 | ++channel->ch_refcount; |
| 1703 | call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb), |
| 1704 | &rettv, 1, argv, 0L, 0L, &dummy, TRUE, NULL); |
| 1705 | clear_tv(&rettv); |
| 1706 | --channel->ch_refcount; |
| 1707 | |
| 1708 | /* the callback is only called once */ |
| 1709 | vim_free(channel->ch_close_cb); |
| 1710 | channel->ch_close_cb = NULL; |
| 1711 | } |
| 1712 | |
| 1713 | channel->ch_nb_close_cb = NULL; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1716 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1717 | * Return the first buffer from "channel"/"part" without removing it. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1718 | * Returns NULL if there is nothing. |
| 1719 | */ |
| 1720 | char_u * |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1721 | channel_peek(channel_T *channel, int part) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1722 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1723 | readq_T *head = &channel->ch_part[part].ch_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1724 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1725 | if (head->rq_next == NULL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1726 | return NULL; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1727 | return head->rq_next->rq_buffer; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1731 | * Clear the read buffer on "channel"/"part". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1732 | */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1733 | static void |
| 1734 | channel_clear_one(channel_T *channel, int part) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1735 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1736 | jsonq_T *json_head = &channel->ch_part[part].ch_json_head; |
| 1737 | cbq_T *cb_head = &channel->ch_part[part].ch_cb_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1738 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1739 | while (channel_peek(channel, part) != NULL) |
| 1740 | vim_free(channel_get(channel, part)); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1741 | |
| 1742 | while (cb_head->cq_next != NULL) |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 1743 | { |
| 1744 | cbq_T *node = cb_head->cq_next; |
| 1745 | |
| 1746 | remove_cb_node(cb_head, node); |
| 1747 | vim_free(node->cq_callback); |
| 1748 | vim_free(node); |
| 1749 | } |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1750 | |
| 1751 | while (json_head->jq_next != NULL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1752 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1753 | free_tv(json_head->jq_next->jq_value); |
| 1754 | remove_json_node(json_head, json_head->jq_next); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1755 | } |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1756 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1757 | vim_free(channel->ch_part[part].ch_callback); |
| 1758 | channel->ch_part[part].ch_callback = NULL; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * Clear all the read buffers on "channel". |
| 1763 | */ |
| 1764 | void |
| 1765 | channel_clear(channel_T *channel) |
| 1766 | { |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1767 | ch_log(channel, "Clearing channel"); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1768 | channel_clear_one(channel, PART_SOCK); |
| 1769 | #ifdef CHANNEL_PIPES |
| 1770 | channel_clear_one(channel, PART_OUT); |
| 1771 | channel_clear_one(channel, PART_ERR); |
| 1772 | #endif |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 1773 | vim_free(channel->ch_callback); |
| 1774 | channel->ch_callback = NULL; |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1775 | vim_free(channel->ch_close_cb); |
| 1776 | channel->ch_close_cb = NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1779 | #if defined(EXITFREE) || defined(PROTO) |
| 1780 | void |
| 1781 | channel_free_all(void) |
| 1782 | { |
| 1783 | channel_T *channel; |
| 1784 | |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1785 | ch_log(NULL, "channel_free_all()"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1786 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
| 1787 | channel_clear(channel); |
| 1788 | } |
| 1789 | #endif |
| 1790 | |
| 1791 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1792 | /* Sent when the channel is found closed when reading. */ |
Bram Moolenaar | eed284a | 2016-02-22 23:13:33 +0100 | [diff] [blame] | 1793 | #define DETACH_MSG_RAW "DETACH\n" |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* Buffer size for reading incoming messages. */ |
| 1796 | #define MAXMSGSIZE 4096 |
| 1797 | |
| 1798 | /* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1799 | * Check for reading from "fd" with "timeout" msec. |
| 1800 | * Return FAIL when there is nothing to read. |
| 1801 | */ |
| 1802 | static int |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1803 | channel_wait(channel_T *channel, sock_T fd, int timeout) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1804 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1805 | if (timeout > 0) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1806 | ch_logn(channel, "Waiting for up to %d msec", timeout); |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1807 | |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1808 | # ifdef WIN32 |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1809 | if (fd != channel->CH_SOCK_FD) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1810 | { |
| 1811 | DWORD nread; |
| 1812 | int diff; |
| 1813 | DWORD deadline = GetTickCount() + timeout; |
| 1814 | |
| 1815 | /* reading from a pipe, not a socket */ |
| 1816 | while (TRUE) |
| 1817 | { |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 1818 | if (PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL) |
| 1819 | && nread > 0) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1820 | return OK; |
| 1821 | diff = deadline - GetTickCount(); |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 1822 | if (diff <= 0) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1823 | break; |
| 1824 | /* Wait for 5 msec. |
| 1825 | * TODO: increase the sleep time when looping more often */ |
| 1826 | Sleep(5); |
| 1827 | } |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1828 | } |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1829 | else |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1830 | #endif |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1831 | { |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 1832 | #if defined(HAVE_SELECT) |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1833 | struct timeval tval; |
| 1834 | fd_set rfds; |
| 1835 | int ret; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1836 | |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1837 | FD_ZERO(&rfds); |
| 1838 | FD_SET((int)fd, &rfds); |
| 1839 | tval.tv_sec = timeout / 1000; |
| 1840 | tval.tv_usec = (timeout % 1000) * 1000; |
| 1841 | for (;;) |
| 1842 | { |
| 1843 | ret = select((int)fd + 1, &rfds, NULL, NULL, &tval); |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 1844 | # ifdef EINTR |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1845 | SOCK_ERRNO; |
| 1846 | if (ret == -1 && errno == EINTR) |
| 1847 | continue; |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 1848 | # endif |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1849 | if (ret > 0) |
| 1850 | return OK; |
| 1851 | break; |
| 1852 | } |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 1853 | #else |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1854 | struct pollfd fds; |
| 1855 | |
| 1856 | fds.fd = fd; |
| 1857 | fds.events = POLLIN; |
| 1858 | if (poll(&fds, 1, timeout) > 0) |
| 1859 | return OK; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1860 | #endif |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1861 | } |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 1862 | return FAIL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Return a unique ID to be used in a message. |
| 1867 | */ |
| 1868 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 1869 | channel_get_id(void) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1870 | { |
| 1871 | static int next_id = 1; |
| 1872 | |
| 1873 | return next_id++; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1877 | * Read from channel "channel" for as long as there is something to read. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1878 | * "part" is PART_SOCK, PART_OUT or PART_ERR. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1879 | * The data is put in the read queue. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1880 | */ |
| 1881 | void |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1882 | channel_read(channel_T *channel, int part, char *func) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1883 | { |
| 1884 | static char_u *buf = NULL; |
| 1885 | int len = 0; |
| 1886 | int readlen = 0; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1887 | sock_T fd; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1888 | int use_socket = FALSE; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1889 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1890 | fd = channel->ch_part[part].ch_fd; |
| 1891 | if (fd == INVALID_FD) |
| 1892 | { |
| 1893 | ch_error(channel, "channel_read() called while socket is closed"); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1894 | return; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1895 | } |
| 1896 | use_socket = fd == channel->CH_SOCK_FD; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1897 | |
| 1898 | /* Allocate a buffer to read into. */ |
| 1899 | if (buf == NULL) |
| 1900 | { |
| 1901 | buf = alloc(MAXMSGSIZE); |
| 1902 | if (buf == NULL) |
| 1903 | return; /* out of memory! */ |
| 1904 | } |
| 1905 | |
| 1906 | /* Keep on reading for as long as there is something to read. |
| 1907 | * Use select() or poll() to avoid blocking on a message that is exactly |
| 1908 | * MAXMSGSIZE long. */ |
| 1909 | for (;;) |
| 1910 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1911 | if (channel_wait(channel, fd, 0) == FAIL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1912 | break; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1913 | if (use_socket) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1914 | len = sock_read(fd, (char *)buf, MAXMSGSIZE); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1915 | else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1916 | len = fd_read(fd, (char *)buf, MAXMSGSIZE); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1917 | if (len <= 0) |
| 1918 | break; /* error or nothing more to read */ |
| 1919 | |
| 1920 | /* Store the read message in the queue. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1921 | channel_save(channel, part, buf, len); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1922 | readlen += len; |
| 1923 | if (len < MAXMSGSIZE) |
| 1924 | break; /* did read everything that's available */ |
| 1925 | } |
| 1926 | |
Bram Moolenaar | 4cafa6d | 2016-02-26 11:52:39 +0100 | [diff] [blame] | 1927 | /* Reading a disconnection (readlen == 0), or an error. */ |
Bram Moolenaar | bd73ae1 | 2016-02-22 22:19:22 +0100 | [diff] [blame] | 1928 | if (readlen <= 0) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1929 | { |
Bram Moolenaar | 4cafa6d | 2016-02-26 11:52:39 +0100 | [diff] [blame] | 1930 | /* Do not give an error message, most likely the other end just |
| 1931 | * exited. */ |
| 1932 | ch_errors(channel, "%s(): Cannot read from channel", func); |
| 1933 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1934 | /* Queue a "DETACH" netbeans message in the command queue in order to |
| 1935 | * terminate the netbeans session later. Do not end the session here |
| 1936 | * directly as we may be running in the context of a call to |
| 1937 | * netbeans_parse_messages(): |
| 1938 | * netbeans_parse_messages |
| 1939 | * -> autocmd triggered while processing the netbeans cmd |
| 1940 | * -> ui_breakcheck |
| 1941 | * -> gui event loop or select loop |
| 1942 | * -> channel_read() |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 1943 | * Don't send "DETACH" for a JS or JSON channel. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1944 | */ |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 1945 | if (channel->ch_part[part].ch_mode == MODE_RAW |
| 1946 | || channel->ch_part[part].ch_mode == MODE_NL) |
| 1947 | channel_save(channel, part, (char_u *)DETACH_MSG_RAW, |
| 1948 | (int)STRLEN(DETACH_MSG_RAW)); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1949 | |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 1950 | /* TODO: When reading from stdout is not possible, should we try to |
| 1951 | * keep stdin and stderr open? Probably not, assume the other side |
| 1952 | * has died. */ |
Bram Moolenaar | 8b37421 | 2016-02-24 20:43:06 +0100 | [diff] [blame] | 1953 | channel_close(channel, TRUE); |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1954 | if (channel->ch_nb_close_cb != NULL) |
| 1955 | (*channel->ch_nb_close_cb)(); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | #if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1959 | /* signal the main loop that there is something to read */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1960 | if (CH_HAS_GUI && gtk_main_level() > 0) |
| 1961 | gtk_main_quit(); |
| 1962 | #endif |
| 1963 | } |
| 1964 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1965 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1966 | * Read from RAW or NL "channel"/"part". Blocks until there is something to |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1967 | * read or the timeout expires. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1968 | * Returns what was read in allocated memory. |
| 1969 | * Returns NULL in case of error or timeout. |
| 1970 | */ |
| 1971 | char_u * |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 1972 | channel_read_block(channel_T *channel, int part, int timeout) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1973 | { |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1974 | char_u *buf; |
| 1975 | char_u *msg; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1976 | ch_mode_T mode = channel->ch_part[part].ch_mode; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1977 | sock_T fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1978 | char_u *nl; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1979 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 1980 | ch_logsn(channel, "Blocking %s read, timeout: %d msec", |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1981 | mode == MODE_RAW ? "RAW" : "NL", timeout); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1982 | |
| 1983 | while (TRUE) |
| 1984 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1985 | buf = channel_peek(channel, part); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1986 | if (buf != NULL && (mode == MODE_RAW |
| 1987 | || (mode == MODE_NL && vim_strchr(buf, NL) != NULL))) |
| 1988 | break; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1989 | if (buf != NULL && channel_collapse(channel, part) == OK) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1990 | continue; |
| 1991 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 1992 | /* Wait for up to the channel timeout. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1993 | if (fd == INVALID_FD |
| 1994 | || channel_wait(channel, fd, timeout) == FAIL) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1995 | return NULL; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1996 | channel_read(channel, part, "channel_read_block"); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 1999 | if (mode == MODE_RAW) |
| 2000 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2001 | msg = channel_get_all(channel, part); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2002 | } |
| 2003 | else |
| 2004 | { |
| 2005 | nl = vim_strchr(buf, NL); |
| 2006 | if (nl[1] == NUL) |
| 2007 | { |
| 2008 | /* get the whole buffer */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2009 | msg = channel_get(channel, part); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2010 | *nl = NUL; |
| 2011 | } |
| 2012 | else |
| 2013 | { |
| 2014 | /* Copy the message into allocated memory and remove it from the |
| 2015 | * buffer. */ |
| 2016 | msg = vim_strnsave(buf, (int)(nl - buf)); |
| 2017 | mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1); |
| 2018 | } |
| 2019 | } |
| 2020 | if (log_fd != NULL) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 2021 | ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg)); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2022 | return msg; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2023 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2024 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2025 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2026 | * Read one JSON message with ID "id" from "channel"/"part" and store the |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2027 | * result in "rettv". |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 2028 | * When "id" is -1 accept any message; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 2029 | * Blocks until the message is received or the timeout is reached. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2030 | */ |
| 2031 | int |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 2032 | channel_read_json_block( |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 2033 | channel_T *channel, |
| 2034 | int part, |
| 2035 | int timeout, |
| 2036 | int id, |
| 2037 | typval_T **rettv) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2038 | { |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 2039 | int more; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 2040 | sock_T fd; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2041 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 2042 | ch_log(channel, "Reading JSON"); |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 2043 | if (id != -1) |
| 2044 | channel->ch_part[part].ch_block_id = id; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2045 | for (;;) |
| 2046 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2047 | more = channel_parse_json(channel, part); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2048 | |
| 2049 | /* search for messsage "id" */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2050 | if (channel_get_json(channel, part, id, rettv) == OK) |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 2051 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2052 | channel->ch_part[part].ch_block_id = 0; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2053 | return OK; |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 2054 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2055 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2056 | if (!more) |
| 2057 | { |
| 2058 | /* Handle any other messages in the queue. If done some more |
| 2059 | * messages may have arrived. */ |
| 2060 | if (channel_parse_messages()) |
| 2061 | continue; |
| 2062 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 2063 | /* Wait for up to the timeout. */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2064 | fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 2065 | if (fd == INVALID_FD || channel_wait(channel, fd, timeout) == FAIL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2066 | break; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2067 | channel_read(channel, part, "channel_read_json_block"); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2068 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2069 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2070 | channel->ch_part[part].ch_block_id = 0; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2071 | return FAIL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2072 | } |
| 2073 | |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 2074 | # if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \ |
| 2075 | || defined(PROTO) |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 2076 | /* |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 2077 | * Lookup the channel from the socket. Set "partp" to the fd index. |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2078 | * Returns NULL when the socket isn't found. |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 2079 | */ |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2080 | channel_T * |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 2081 | channel_fd2channel(sock_T fd, int *partp) |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 2082 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2083 | channel_T *channel; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2084 | int part; |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 2085 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2086 | if (fd != INVALID_FD) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2087 | for (channel = first_channel; channel != NULL; |
| 2088 | channel = channel->ch_next) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2089 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2090 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2091 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2092 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2093 | part = PART_SOCK; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2094 | # endif |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2095 | if (channel->ch_part[part].ch_fd == fd) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2096 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 2097 | *partp = part; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2098 | return channel; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2099 | } |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2100 | } |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2101 | return NULL; |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 2102 | } |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 2103 | # endif |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2104 | |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 2105 | # if defined(WIN32) || defined(PROTO) |
| 2106 | /* |
| 2107 | * Check the channels for anything that is ready to be read. |
| 2108 | * The data is put in the read queue. |
| 2109 | */ |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2110 | void |
| 2111 | channel_handle_events(void) |
| 2112 | { |
| 2113 | channel_T *channel; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2114 | int part; |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 2115 | sock_T fd; |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2116 | |
| 2117 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
| 2118 | { |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 2119 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2120 | /* check the socket and pipes */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2121 | for (part = PART_SOCK; part <= PART_ERR; ++part) |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 2122 | # else |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2123 | /* only check the socket */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2124 | part = PART_SOCK; |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2125 | # endif |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 2126 | { |
| 2127 | fd = channel->ch_part[part].ch_fd; |
| 2128 | if (fd != INVALID_FD && channel_wait(channel, fd, 0) == OK) |
| 2129 | channel_read(channel, part, "channel_handle_events"); |
| 2130 | } |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2131 | } |
| 2132 | } |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 2133 | # endif |
| 2134 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2135 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2136 | * Write "buf" (NUL terminated string) to "channel"/"part". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2137 | * When "fun" is not NULL an error message might be given. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2138 | * Return FAIL or OK. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2139 | */ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2140 | int |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2141 | channel_send(channel_T *channel, int part, char_u *buf, char *fun) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2142 | { |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2143 | int len = (int)STRLEN(buf); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2144 | int res; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2145 | sock_T fd; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2146 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2147 | fd = channel->ch_part[part].ch_fd; |
| 2148 | if (fd == INVALID_FD) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2149 | { |
| 2150 | if (!channel->ch_error && fun != NULL) |
| 2151 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 2152 | ch_errors(channel, "%s(): write while not connected", fun); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2153 | EMSG2("E630: %s(): write while not connected", fun); |
| 2154 | } |
| 2155 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2156 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2157 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2158 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2159 | if (log_fd != NULL) |
| 2160 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2161 | ch_log_lead("SEND ", channel); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2162 | fprintf(log_fd, "'"); |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2163 | ignored = (int)fwrite(buf, len, 1, log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2164 | fprintf(log_fd, "'\n"); |
| 2165 | fflush(log_fd); |
| 2166 | } |
| 2167 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2168 | if (part == PART_SOCK) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2169 | res = sock_write(fd, (char *)buf, len); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2170 | else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 2171 | res = fd_write(fd, (char *)buf, len); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2172 | if (res != len) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2173 | { |
| 2174 | if (!channel->ch_error && fun != NULL) |
| 2175 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 2176 | ch_errors(channel, "%s(): write failed", fun); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2177 | EMSG2("E631: %s(): write failed", fun); |
| 2178 | } |
| 2179 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2180 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2181 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2182 | |
| 2183 | channel->ch_error = FALSE; |
| 2184 | return OK; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | # if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2188 | /* |
| 2189 | * Add open channels to the poll struct. |
| 2190 | * Return the adjusted struct index. |
| 2191 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 2192 | */ |
| 2193 | int |
| 2194 | channel_poll_setup(int nfd_in, void *fds_in) |
| 2195 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2196 | int nfd = nfd_in; |
| 2197 | channel_T *channel; |
| 2198 | struct pollfd *fds = fds_in; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2199 | int part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2200 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2201 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2202 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2203 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2204 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2205 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2206 | part = PART_SOCK; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2207 | # endif |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2208 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2209 | if (channel->ch_part[part].ch_fd != INVALID_FD) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2210 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2211 | channel->ch_part[part].ch_poll_idx = nfd; |
| 2212 | fds[nfd].fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2213 | fds[nfd].events = POLLIN; |
| 2214 | nfd++; |
| 2215 | } |
| 2216 | else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2217 | channel->ch_part[part].ch_poll_idx = -1; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2218 | } |
| 2219 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2220 | |
| 2221 | return nfd; |
| 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 2226 | */ |
| 2227 | int |
| 2228 | channel_poll_check(int ret_in, void *fds_in) |
| 2229 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2230 | int ret = ret_in; |
| 2231 | channel_T *channel; |
| 2232 | struct pollfd *fds = fds_in; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2233 | int part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2234 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2235 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2236 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2237 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2238 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2239 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2240 | part = PART_SOCK; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2241 | # endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2242 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2243 | int idx = channel->ch_part[part].ch_poll_idx; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2244 | |
| 2245 | if (ret > 0 && idx != -1 && fds[idx].revents & POLLIN) |
| 2246 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2247 | channel_read(channel, part, "channel_poll_check"); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2248 | --ret; |
| 2249 | } |
| 2250 | } |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2251 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2252 | |
| 2253 | return ret; |
| 2254 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2255 | # endif /* UNIX && !HAVE_SELECT */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2256 | |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2257 | # if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2258 | /* |
| 2259 | * The type of "rfds" is hidden to avoid problems with the function proto. |
| 2260 | */ |
| 2261 | int |
| 2262 | channel_select_setup(int maxfd_in, void *rfds_in) |
| 2263 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2264 | int maxfd = maxfd_in; |
| 2265 | channel_T *channel; |
| 2266 | fd_set *rfds = rfds_in; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2267 | int part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2268 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2269 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2270 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2271 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2272 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2273 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2274 | part = PART_SOCK; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2275 | # endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2276 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2277 | sock_T fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2278 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2279 | if (fd != INVALID_FD) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2280 | { |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 2281 | FD_SET((int)fd, rfds); |
| 2282 | if (maxfd < (int)fd) |
| 2283 | maxfd = (int)fd; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2284 | } |
| 2285 | } |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2286 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2287 | |
| 2288 | return maxfd; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * The type of "rfds" is hidden to avoid problems with the function proto. |
| 2293 | */ |
| 2294 | int |
| 2295 | channel_select_check(int ret_in, void *rfds_in) |
| 2296 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2297 | int ret = ret_in; |
| 2298 | channel_T *channel; |
| 2299 | fd_set *rfds = rfds_in; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2300 | int part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2301 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2302 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2303 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2304 | # ifdef CHANNEL_PIPES |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2305 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2306 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2307 | part = PART_SOCK; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2308 | # endif |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2309 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2310 | sock_T fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2311 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2312 | if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds)) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2313 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2314 | channel_read(channel, part, "channel_select_check"); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 2315 | --ret; |
| 2316 | } |
| 2317 | } |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 2318 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2319 | |
| 2320 | return ret; |
| 2321 | } |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 2322 | # endif /* !WIN32 && HAVE_SELECT */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2323 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2324 | /* |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2325 | * Return TRUE if "channel" has JSON or other typeahead. |
| 2326 | */ |
| 2327 | static int |
| 2328 | channel_has_readahead(channel_T *channel, int part) |
| 2329 | { |
| 2330 | ch_mode_T ch_mode = channel->ch_part[part].ch_mode; |
| 2331 | |
| 2332 | if (ch_mode == MODE_JSON || ch_mode == MODE_JS) |
| 2333 | { |
| 2334 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
| 2335 | jsonq_T *item = head->jq_next; |
| 2336 | |
| 2337 | return item != NULL; |
| 2338 | } |
| 2339 | return channel_peek(channel, part) != NULL; |
| 2340 | } |
| 2341 | |
| 2342 | /* |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2343 | * Execute queued up commands. |
| 2344 | * Invoked from the main loop when it's safe to execute received commands. |
| 2345 | * Return TRUE when something was done. |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2346 | */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2347 | int |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2348 | channel_parse_messages(void) |
| 2349 | { |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 2350 | channel_T *channel = first_channel; |
| 2351 | int ret = FALSE; |
| 2352 | int r; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2353 | int part = PART_SOCK; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2354 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2355 | ch_log(NULL, "looking for messages on channels"); |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 2356 | while (channel != NULL) |
| 2357 | { |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 2358 | if (channel->ch_refcount == 0 && !channel_still_useful(channel)) |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 2359 | { |
| 2360 | /* channel is no longer useful, free it */ |
| 2361 | channel_free(channel); |
| 2362 | channel = first_channel; |
| 2363 | part = PART_SOCK; |
| 2364 | continue; |
| 2365 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2366 | if (channel->ch_part[part].ch_fd != INVALID_FD |
| 2367 | || channel_has_readahead(channel, part)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2368 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2369 | /* Increase the refcount, in case the handler causes the channel |
| 2370 | * to be unreferenced or closed. */ |
| 2371 | ++channel->ch_refcount; |
| 2372 | r = may_invoke_callback(channel, part); |
| 2373 | if (r == OK) |
| 2374 | ret = TRUE; |
| 2375 | if (channel_unref(channel) || r == OK) |
| 2376 | { |
| 2377 | /* channel was freed or something was done, start over */ |
| 2378 | channel = first_channel; |
| 2379 | part = PART_SOCK; |
| 2380 | continue; |
| 2381 | } |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2382 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2383 | #ifdef CHANNEL_PIPES |
| 2384 | if (part < PART_ERR) |
| 2385 | ++part; |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 2386 | else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2387 | #endif |
| 2388 | { |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 2389 | channel = channel->ch_next; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2390 | part = PART_SOCK; |
| 2391 | } |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 2392 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2393 | |
| 2394 | if (channel_need_redraw && must_redraw) |
| 2395 | { |
| 2396 | channel_need_redraw = FALSE; |
| 2397 | update_screen(0); |
| 2398 | setcursor(); |
| 2399 | cursor_on(); |
| 2400 | out_flush(); |
| 2401 | } |
| 2402 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2403 | return ret; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2404 | } |
| 2405 | |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 2406 | /* |
| 2407 | * Mark references to lists used in channels. |
| 2408 | */ |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2409 | int |
| 2410 | set_ref_in_channel(int copyID) |
| 2411 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2412 | int abort = FALSE; |
| 2413 | channel_T *channel; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2414 | int part; |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2415 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2416 | for (channel = first_channel; channel != NULL; channel = channel->ch_next) |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2417 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2418 | #ifdef CHANNEL_PIPES |
| 2419 | for (part = PART_SOCK; part < PART_IN; ++part) |
| 2420 | #else |
| 2421 | part = PART_SOCK; |
| 2422 | #endif |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2423 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2424 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
| 2425 | jsonq_T *item = head->jq_next; |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2426 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2427 | while (item != NULL) |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2428 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2429 | list_T *l = item->jq_value->vval.v_list; |
| 2430 | |
| 2431 | if (l->lv_copyID != copyID) |
| 2432 | { |
| 2433 | l->lv_copyID = copyID; |
| 2434 | abort = abort || set_ref_in_list(l, copyID, NULL); |
| 2435 | } |
| 2436 | item = item->jq_next; |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2437 | } |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | return abort; |
| 2441 | } |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 2442 | |
| 2443 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2444 | * Return the "part" to write to for "channel". |
| 2445 | */ |
| 2446 | int |
| 2447 | channel_part_send(channel_T *channel) |
| 2448 | { |
| 2449 | #ifdef CHANNEL_PIPES |
| 2450 | if (channel->CH_SOCK_FD == INVALID_FD) |
| 2451 | return PART_IN; |
| 2452 | #endif |
| 2453 | return PART_SOCK; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * Return the default "part" to read from for "channel". |
| 2458 | */ |
| 2459 | int |
| 2460 | channel_part_read(channel_T *channel) |
| 2461 | { |
| 2462 | #ifdef CHANNEL_PIPES |
| 2463 | if (channel->CH_SOCK_FD == INVALID_FD) |
| 2464 | return PART_OUT; |
| 2465 | #endif |
| 2466 | return PART_SOCK; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * Return the mode of "channel"/"part" |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2471 | * If "channel" is invalid returns MODE_JSON. |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 2472 | */ |
| 2473 | ch_mode_T |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2474 | channel_get_mode(channel_T *channel, int part) |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 2475 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2476 | if (channel == NULL) |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 2477 | return MODE_JSON; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2478 | return channel->ch_part[part].ch_mode; |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 2479 | } |
| 2480 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 2481 | /* |
| 2482 | * Return the timeout of "channel"/"part" |
| 2483 | */ |
| 2484 | int |
| 2485 | channel_get_timeout(channel_T *channel, int part) |
| 2486 | { |
| 2487 | return channel->ch_part[part].ch_timeout; |
| 2488 | } |
| 2489 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 2490 | #endif /* FEAT_CHANNEL */ |