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 | /* |
| 18 | * Change the zero to 1 to enable debugging. |
| 19 | * This will write a file "channel_debug.log". |
| 20 | */ |
| 21 | #if 0 |
| 22 | # define CHERROR(fmt, arg) cherror(fmt, arg) |
| 23 | # define CHLOG(idx, send, buf) chlog(idx, send, buf) |
| 24 | # define CHFILE "channel_debug.log" |
| 25 | |
| 26 | static void cherror(char *fmt, char *arg); |
| 27 | static void chlog(int send, char_u *buf); |
| 28 | #else |
| 29 | # define CHERROR(fmt, arg) |
| 30 | # define CHLOG(idx, send, buf) |
| 31 | #endif |
| 32 | |
| 33 | /* TRUE when netbeans is running with a GUI. */ |
| 34 | #ifdef FEAT_GUI |
| 35 | # define CH_HAS_GUI (gui.in_use || gui.starting) |
| 36 | #endif |
| 37 | |
| 38 | /* Note: when making changes here also adjust configure.in. */ |
| 39 | #ifdef WIN32 |
| 40 | /* WinSock API is separated from C API, thus we can't use read(), write(), |
| 41 | * errno... */ |
| 42 | # define SOCK_ERRNO errno = WSAGetLastError() |
| 43 | # undef ECONNREFUSED |
| 44 | # define ECONNREFUSED WSAECONNREFUSED |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 45 | # undef EWOULDBLOCK |
| 46 | # define EWOULDBLOCK WSAEWOULDBLOCK |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 47 | # ifdef EINTR |
| 48 | # undef EINTR |
| 49 | # endif |
| 50 | # define EINTR WSAEINTR |
| 51 | # define sock_write(sd, buf, len) send(sd, buf, len, 0) |
| 52 | # define sock_read(sd, buf, len) recv(sd, buf, len, 0) |
| 53 | # define sock_close(sd) closesocket(sd) |
| 54 | # define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */ |
| 55 | #else |
| 56 | # include <netdb.h> |
| 57 | # include <netinet/in.h> |
| 58 | |
| 59 | # include <sys/socket.h> |
| 60 | # ifdef HAVE_LIBGEN_H |
| 61 | # include <libgen.h> |
| 62 | # endif |
| 63 | # define SOCK_ERRNO |
| 64 | # define sock_write(sd, buf, len) write(sd, buf, len) |
| 65 | # define sock_read(sd, buf, len) read(sd, buf, len) |
| 66 | # define sock_close(sd) close(sd) |
| 67 | #endif |
| 68 | |
| 69 | #ifdef FEAT_GUI_W32 |
| 70 | extern HWND s_hwnd; /* Gvim's Window handle */ |
| 71 | #endif |
| 72 | |
| 73 | struct readqueue |
| 74 | { |
| 75 | char_u *buffer; |
| 76 | struct readqueue *next; |
| 77 | struct readqueue *prev; |
| 78 | }; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 79 | typedef struct readqueue readq_T; |
| 80 | |
| 81 | struct jsonqueue |
| 82 | { |
| 83 | typval_T *value; |
| 84 | struct jsonqueue *next; |
| 85 | struct jsonqueue *prev; |
| 86 | }; |
| 87 | typedef struct jsonqueue jsonq_T; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 88 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 89 | struct cbqueue |
| 90 | { |
| 91 | char_u *callback; |
| 92 | int seq_nr; |
| 93 | struct cbqueue *next; |
| 94 | struct cbqueue *prev; |
| 95 | }; |
| 96 | typedef struct cbqueue cbq_T; |
| 97 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 98 | typedef struct { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 99 | sock_T ch_fd; /* the socket, -1 for a closed channel */ |
| 100 | int ch_idx; /* used by channel_poll_setup() */ |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 101 | readq_T ch_head; /* dummy node, header for circular queue */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 102 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 103 | int ch_error; /* When TRUE an error was reported. Avoids giving |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 104 | * pages full of error messages when the other side |
| 105 | * has exited, only mention the first error until the |
| 106 | * connection works again. */ |
| 107 | #ifdef FEAT_GUI_X11 |
| 108 | XtInputId ch_inputHandler; /* Cookie for input */ |
| 109 | #endif |
| 110 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 111 | gint ch_inputHandler; /* Cookie for input */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 112 | #endif |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 113 | #ifdef WIN32 |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 114 | int ch_inputHandler; /* simply ret.value of WSAAsyncSelect() */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 115 | #endif |
| 116 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 117 | void (*ch_close_cb)(void); /* callback for when channel is closed */ |
| 118 | |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 119 | int ch_block_id; /* ID that channel_read_json_block() is |
| 120 | waiting for */ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 121 | char_u *ch_callback; /* function to call when a msg is not handled */ |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 122 | cbq_T ch_cb_head; /* dummy node for pre-request callbacks */ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 123 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 124 | ch_mode_T ch_mode; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 125 | jsonq_T ch_json_head; /* dummy node, header for circular queue */ |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 126 | |
| 127 | int ch_timeout; /* request timeout in msec */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 128 | } channel_T; |
| 129 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 130 | /* |
| 131 | * Information about all channels. |
| 132 | * There can be gaps for closed channels, they will be reused later. |
| 133 | */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 134 | static channel_T *channels = NULL; |
| 135 | static int channel_count = 0; |
| 136 | |
| 137 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 138 | * TODO: open debug file when desired. |
| 139 | */ |
| 140 | FILE *debugfd = NULL; |
| 141 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 142 | #ifdef _WIN32 |
| 143 | # undef PERROR |
| 144 | # define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \ |
| 145 | (char_u *)msg, (char_u *)strerror_win32(errno)) |
| 146 | |
| 147 | static char * |
| 148 | strerror_win32(int eno) |
| 149 | { |
| 150 | static LPVOID msgbuf = NULL; |
| 151 | char_u *ptr; |
| 152 | |
| 153 | if (msgbuf) |
| 154 | LocalFree(msgbuf); |
| 155 | FormatMessage( |
| 156 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 157 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 158 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 159 | NULL, |
| 160 | eno, |
| 161 | MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), |
| 162 | (LPTSTR) &msgbuf, |
| 163 | 0, |
| 164 | NULL); |
| 165 | /* chomp \r or \n */ |
| 166 | for (ptr = (char_u *)msgbuf; *ptr; ptr++) |
| 167 | switch (*ptr) |
| 168 | { |
| 169 | case '\r': |
| 170 | STRMOVE(ptr, ptr + 1); |
| 171 | ptr--; |
| 172 | break; |
| 173 | case '\n': |
| 174 | if (*(ptr + 1) == '\0') |
| 175 | *ptr = '\0'; |
| 176 | else |
| 177 | *ptr = ' '; |
| 178 | break; |
| 179 | } |
| 180 | return msgbuf; |
| 181 | } |
| 182 | #endif |
| 183 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 184 | /* |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 185 | * Add a new channel slot, return the index. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 186 | * The channel isn't actually used into ch_fd is set >= 0; |
| 187 | * Returns -1 if all channels are in use. |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 188 | */ |
| 189 | static int |
| 190 | add_channel(void) |
| 191 | { |
| 192 | int idx; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 193 | channel_T *ch; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 194 | |
| 195 | if (channels != NULL) |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 196 | { |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 197 | for (idx = 0; idx < channel_count; ++idx) |
| 198 | if (channels[idx].ch_fd < 0) |
| 199 | /* re-use a closed channel slot */ |
| 200 | return idx; |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame] | 201 | if (channel_count == MAX_OPEN_CHANNELS) |
| 202 | return -1; |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | channels = (channel_T *)alloc((int)sizeof(channel_T) |
| 207 | * MAX_OPEN_CHANNELS); |
| 208 | if (channels == NULL) |
| 209 | return -1; |
| 210 | } |
| 211 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 212 | ch = &channels[channel_count]; |
| 213 | (void)vim_memset(ch, 0, sizeof(channel_T)); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 214 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 215 | ch->ch_fd = (sock_T)-1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 216 | #ifdef FEAT_GUI_X11 |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 217 | ch->ch_inputHandler = (XtInputId)NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 218 | #endif |
| 219 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 220 | ch->ch_inputHandler = 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 221 | #endif |
| 222 | #ifdef FEAT_GUI_W32 |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 223 | ch->ch_inputHandler = -1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 224 | #endif |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 225 | /* initialize circular queues */ |
| 226 | ch->ch_head.next = &ch->ch_head; |
| 227 | ch->ch_head.prev = &ch->ch_head; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 228 | ch->ch_cb_head.next = &ch->ch_cb_head; |
| 229 | ch->ch_cb_head.prev = &ch->ch_cb_head; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 230 | ch->ch_json_head.next = &ch->ch_json_head; |
| 231 | ch->ch_json_head.prev = &ch->ch_json_head; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 232 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 233 | ch->ch_timeout = 2000; |
| 234 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 235 | return channel_count++; |
| 236 | } |
| 237 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 238 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 239 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 240 | * Read a command from netbeans. |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 241 | */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 242 | #ifdef FEAT_GUI_X11 |
| 243 | static void |
| 244 | messageFromNetbeans(XtPointer clientData, |
| 245 | int *unused1 UNUSED, |
| 246 | XtInputId *unused2 UNUSED) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 247 | { |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 248 | channel_read((int)(long)clientData); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 249 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 250 | #endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 251 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 252 | #ifdef FEAT_GUI_GTK |
| 253 | static void |
| 254 | messageFromNetbeans(gpointer clientData, |
| 255 | gint unused1 UNUSED, |
| 256 | GdkInputCondition unused2 UNUSED) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 257 | { |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 258 | channel_read((int)(long)clientData); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 259 | } |
| 260 | #endif |
| 261 | |
| 262 | static void |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 263 | channel_gui_register(int idx) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 264 | { |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 265 | channel_T *channel = &channels[idx]; |
| 266 | |
| 267 | if (!CH_HAS_GUI) |
| 268 | return; |
| 269 | |
| 270 | # ifdef FEAT_GUI_X11 |
| 271 | /* tell notifier we are interested in being called |
| 272 | * when there is input on the editor connection socket |
| 273 | */ |
| 274 | if (channel->ch_inputHandler == (XtInputId)NULL) |
| 275 | channel->ch_inputHandler = |
| 276 | XtAppAddInput((XtAppContext)app_context, channel->ch_fd, |
| 277 | (XtPointer)(XtInputReadMask + XtInputExceptMask), |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 278 | messageFromNetbeans, (XtPointer)(long)idx); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 279 | # else |
| 280 | # ifdef FEAT_GUI_GTK |
| 281 | /* |
| 282 | * Tell gdk we are interested in being called when there |
| 283 | * is input on the editor connection socket |
| 284 | */ |
| 285 | if (channel->ch_inputHandler == 0) |
| 286 | channel->ch_inputHandler = |
| 287 | gdk_input_add((gint)channel->ch_fd, (GdkInputCondition) |
| 288 | ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION), |
| 289 | messageFromNetbeans, (gpointer)(long)idx); |
| 290 | # else |
| 291 | # ifdef FEAT_GUI_W32 |
| 292 | /* |
| 293 | * Tell Windows we are interested in receiving message when there |
| 294 | * is input on the editor connection socket. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 295 | */ |
| 296 | if (channel->ch_inputHandler == -1) |
| 297 | channel->ch_inputHandler = |
| 298 | WSAAsyncSelect(channel->ch_fd, s_hwnd, WM_NETBEANS, FD_READ); |
| 299 | # endif |
| 300 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 301 | # endif |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Register any of our file descriptors with the GUI event handling system. |
| 306 | * Called when the GUI has started. |
| 307 | */ |
| 308 | void |
| 309 | channel_gui_register_all(void) |
| 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for (i = 0; i < channel_count; ++i) |
| 314 | if (channels[i].ch_fd >= 0) |
| 315 | channel_gui_register(i); |
| 316 | } |
| 317 | |
| 318 | static void |
| 319 | channel_gui_unregister(int idx) |
| 320 | { |
| 321 | channel_T *channel = &channels[idx]; |
| 322 | |
| 323 | # ifdef FEAT_GUI_X11 |
| 324 | if (channel->ch_inputHandler != (XtInputId)NULL) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 325 | { |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 326 | XtRemoveInput(channel->ch_inputHandler); |
| 327 | channel->ch_inputHandler = (XtInputId)NULL; |
| 328 | } |
| 329 | # else |
| 330 | # ifdef FEAT_GUI_GTK |
| 331 | if (channel->ch_inputHandler != 0) |
| 332 | { |
| 333 | gdk_input_remove(channel->ch_inputHandler); |
| 334 | channel->ch_inputHandler = 0; |
| 335 | } |
| 336 | # else |
| 337 | # ifdef FEAT_GUI_W32 |
| 338 | if (channel->ch_inputHandler == 0) |
| 339 | { |
Bram Moolenaar | 54e09e7 | 2016-01-26 23:49:31 +0100 | [diff] [blame] | 340 | WSAAsyncSelect(channel->ch_fd, s_hwnd, 0, 0); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 341 | channel->ch_inputHandler = -1; |
| 342 | } |
| 343 | # endif |
| 344 | # endif |
| 345 | # endif |
| 346 | } |
| 347 | |
| 348 | #endif |
| 349 | |
| 350 | /* |
| 351 | * Open a channel to "hostname":"port". |
| 352 | * Returns the channel number for success. |
| 353 | * Returns a negative number for failure. |
| 354 | */ |
| 355 | int |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 356 | channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 357 | { |
| 358 | int sd; |
| 359 | struct sockaddr_in server; |
| 360 | struct hostent * host; |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 361 | #ifdef WIN32 |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 362 | u_short port = port_in; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 363 | u_long val = 1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 364 | #else |
| 365 | int port = port_in; |
| 366 | #endif |
| 367 | int idx; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 368 | int ret; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 369 | |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 370 | #ifdef WIN32 |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 371 | channel_init_winsock(); |
| 372 | #endif |
| 373 | |
| 374 | idx = add_channel(); |
| 375 | if (idx < 0) |
| 376 | { |
| 377 | CHERROR("All channels are in use\n", ""); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 378 | EMSG(_("E897: All channels are in use")); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1) |
| 383 | { |
| 384 | CHERROR("error in socket() in channel_open()\n", ""); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 385 | PERROR("E898: socket() in channel_open()"); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | /* Get the server internet address and put into addr structure */ |
| 390 | /* fill in the socket address structure and connect to server */ |
| 391 | vim_memset((char *)&server, 0, sizeof(server)); |
| 392 | server.sin_family = AF_INET; |
| 393 | server.sin_port = htons(port); |
| 394 | if ((host = gethostbyname(hostname)) == NULL) |
| 395 | { |
| 396 | CHERROR("error in gethostbyname() in channel_open()\n", ""); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 397 | PERROR("E901: gethostbyname() in channel_open()"); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 398 | sock_close(sd); |
| 399 | return -1; |
| 400 | } |
| 401 | memcpy((char *)&server.sin_addr, host->h_addr, host->h_length); |
| 402 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 403 | if (waittime >= 0) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 404 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 405 | /* Make connect non-blocking. */ |
| 406 | if ( |
| 407 | #ifdef _WIN32 |
| 408 | ioctlsocket(sd, FIONBIO, &val) < 0 |
| 409 | #else |
| 410 | fcntl(sd, F_SETFL, O_NONBLOCK) < 0 |
| 411 | #endif |
| 412 | ) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 413 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 414 | SOCK_ERRNO; |
| 415 | CHERROR("channel_open: Connect failed with errno %d\n", errno); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 416 | sock_close(sd); |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 417 | return -1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 418 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Try connecting to the server. */ |
| 422 | ret = connect(sd, (struct sockaddr *)&server, sizeof(server)); |
| 423 | SOCK_ERRNO; |
| 424 | if (ret < 0) |
| 425 | { |
| 426 | if (errno != EWOULDBLOCK && errno != EINPROGRESS) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 427 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 428 | CHERROR("channel_open: Connect failed with errno %d\n", errno); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 429 | CHERROR("Cannot connect to port\n", ""); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 430 | PERROR(_("E902: Cannot connect to port")); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 431 | sock_close(sd); |
| 432 | return -1; |
| 433 | } |
| 434 | } |
| 435 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 436 | if (waittime >= 0 && ret < 0) |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 437 | { |
| 438 | struct timeval tv; |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 439 | fd_set wfds; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 440 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 441 | FD_ZERO(&wfds); |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 442 | FD_SET(sd, &wfds); |
Bram Moolenaar | 26dfc41 | 2016-02-06 18:18:54 +0100 | [diff] [blame] | 443 | tv.tv_sec = waittime / 1000; |
| 444 | tv.tv_usec = (waittime % 1000) * 1000; |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 445 | ret = select((int)sd + 1, NULL, &wfds, NULL, &tv); |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 446 | if (ret < 0) |
| 447 | { |
| 448 | SOCK_ERRNO; |
| 449 | CHERROR("channel_open: Connect failed with errno %d\n", errno); |
| 450 | CHERROR("Cannot connect to port\n", ""); |
| 451 | PERROR(_("E902: Cannot connect to port")); |
| 452 | sock_close(sd); |
| 453 | return -1; |
| 454 | } |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 455 | if (!FD_ISSET(sd, &wfds)) |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 456 | { |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 457 | /* don't give an error, we just timed out. */ |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 458 | sock_close(sd); |
| 459 | return -1; |
| 460 | } |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 461 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 462 | |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 463 | if (waittime >= 0) |
| 464 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 465 | #ifdef _WIN32 |
| 466 | val = 0; |
| 467 | ioctlsocket(sd, FIONBIO, &val); |
| 468 | #else |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 469 | (void)fcntl(sd, F_SETFL, 0); |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 470 | #endif |
| 471 | } |
| 472 | |
Bram Moolenaar | 0fa98e7 | 2016-02-07 22:21:19 +0100 | [diff] [blame] | 473 | /* Only retry for netbeans. TODO: can we use a waittime instead? */ |
| 474 | if (errno == ECONNREFUSED && close_cb != NULL) |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 475 | { |
| 476 | sock_close(sd); |
| 477 | if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1) |
| 478 | { |
| 479 | SOCK_ERRNO; |
| 480 | CHERROR("socket() retry in channel_open()\n", ""); |
| 481 | PERROR("E900: socket() retry in channel_open()"); |
| 482 | return -1; |
| 483 | } |
| 484 | if (connect(sd, (struct sockaddr *)&server, sizeof(server))) |
| 485 | { |
| 486 | int retries = 36; |
| 487 | int success = FALSE; |
| 488 | |
| 489 | SOCK_ERRNO; |
| 490 | while (retries-- && ((errno == ECONNREFUSED) |
| 491 | || (errno == EINTR))) |
| 492 | { |
| 493 | CHERROR("retrying...\n", ""); |
| 494 | mch_delay(3000L, TRUE); |
| 495 | ui_breakcheck(); |
| 496 | if (got_int) |
| 497 | { |
| 498 | errno = EINTR; |
| 499 | break; |
| 500 | } |
| 501 | if (connect(sd, (struct sockaddr *)&server, |
| 502 | sizeof(server)) == 0) |
| 503 | { |
| 504 | success = TRUE; |
| 505 | break; |
| 506 | } |
| 507 | SOCK_ERRNO; |
| 508 | } |
| 509 | if (!success) |
| 510 | { |
| 511 | /* Get here when the server can't be found. */ |
| 512 | CHERROR("Cannot connect to port after retry\n", ""); |
| 513 | PERROR(_("E899: Cannot connect to port after retry2")); |
| 514 | sock_close(sd); |
| 515 | return -1; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 520 | channels[idx].ch_fd = sd; |
| 521 | channels[idx].ch_close_cb = close_cb; |
| 522 | |
| 523 | #ifdef FEAT_GUI |
| 524 | channel_gui_register(idx); |
| 525 | #endif |
| 526 | |
| 527 | return idx; |
| 528 | } |
| 529 | |
| 530 | /* |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 531 | * Set the json mode of channel "idx" to "ch_mode". |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 532 | */ |
| 533 | void |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 534 | channel_set_json_mode(int idx, ch_mode_T ch_mode) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 535 | { |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 536 | channels[idx].ch_mode = ch_mode; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 540 | * Set the read timeout of channel "idx". |
| 541 | */ |
| 542 | void |
| 543 | channel_set_timeout(int idx, int timeout) |
| 544 | { |
| 545 | channels[idx].ch_timeout = timeout; |
| 546 | } |
| 547 | |
| 548 | /* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 549 | * Set the callback for channel "idx". |
| 550 | */ |
| 551 | void |
| 552 | channel_set_callback(int idx, char_u *callback) |
| 553 | { |
| 554 | vim_free(channels[idx].ch_callback); |
| 555 | channels[idx].ch_callback = vim_strsave(callback); |
| 556 | } |
| 557 | |
| 558 | /* |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 559 | * Set the callback for channel "idx" for the response with "id". |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 560 | */ |
| 561 | void |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 562 | channel_set_req_callback(int idx, char_u *callback, int id) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 563 | { |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 564 | cbq_T *cbhead = &channels[idx].ch_cb_head; |
| 565 | cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T)); |
| 566 | |
| 567 | if (item != NULL) |
| 568 | { |
| 569 | item->callback = vim_strsave(callback); |
| 570 | item->seq_nr = id; |
| 571 | item->prev = cbhead->prev; |
| 572 | cbhead->prev = item; |
| 573 | item->next = cbhead; |
| 574 | item->prev->next = item; |
| 575 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 579 | * Invoke the "callback" on channel "idx". |
| 580 | */ |
| 581 | static void |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 582 | invoke_callback(int idx, char_u *callback, typval_T *argv) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 583 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 584 | typval_T rettv; |
| 585 | int dummy; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 586 | |
| 587 | argv[0].v_type = VAR_NUMBER; |
| 588 | argv[0].vval.v_number = idx; |
| 589 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 590 | call_func(callback, (int)STRLEN(callback), |
| 591 | &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL); |
| 592 | /* If an echo command was used the cursor needs to be put back where |
| 593 | * it belongs. */ |
| 594 | setcursor(); |
| 595 | cursor_on(); |
| 596 | out_flush(); |
| 597 | } |
| 598 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 599 | /* |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 600 | * Return the first buffer from the channel and remove it. |
| 601 | * The caller must free it. |
| 602 | * Returns NULL if there is nothing. |
| 603 | */ |
| 604 | char_u * |
| 605 | channel_get(int idx) |
| 606 | { |
| 607 | readq_T *head = &channels[idx].ch_head; |
| 608 | readq_T *node; |
| 609 | char_u *p; |
| 610 | |
| 611 | if (head->next == head || head->next == NULL) |
| 612 | return NULL; |
| 613 | node = head->next; |
| 614 | /* dispose of the node but keep the buffer */ |
| 615 | p = node->buffer; |
| 616 | head->next = node->next; |
| 617 | node->next->prev = node->prev; |
| 618 | vim_free(node); |
| 619 | return p; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Returns the whole buffer contents concatenated. |
| 624 | */ |
| 625 | static char_u * |
| 626 | channel_get_all(int idx) |
| 627 | { |
| 628 | /* Concatenate everything into one buffer. |
| 629 | * TODO: avoid multiple allocations. */ |
| 630 | while (channel_collapse(idx) == OK) |
| 631 | ; |
| 632 | return channel_get(idx); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Collapses the first and second buffer in the channel "idx". |
| 637 | * Returns FAIL if that is not possible. |
| 638 | */ |
| 639 | int |
| 640 | channel_collapse(int idx) |
| 641 | { |
| 642 | readq_T *head = &channels[idx].ch_head; |
| 643 | readq_T *node = head->next; |
| 644 | char_u *p; |
| 645 | |
| 646 | if (node == head || node == NULL || node->next == head) |
| 647 | return FAIL; |
| 648 | |
| 649 | p = alloc((unsigned)(STRLEN(node->buffer) |
| 650 | + STRLEN(node->next->buffer) + 1)); |
| 651 | if (p == NULL) |
| 652 | return FAIL; /* out of memory */ |
| 653 | STRCPY(p, node->buffer); |
| 654 | STRCAT(p, node->next->buffer); |
| 655 | vim_free(node->next->buffer); |
| 656 | node->next->buffer = p; |
| 657 | |
| 658 | /* dispose of the node and buffer */ |
| 659 | head->next = node->next; |
| 660 | node->next->prev = node->prev; |
| 661 | vim_free(node->buffer); |
| 662 | vim_free(node); |
| 663 | return OK; |
| 664 | } |
| 665 | |
| 666 | /* |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 667 | * Use the read buffer of channel "ch_idx" and parse a JSON messages that is |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 668 | * complete. The messages are added to the queue. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 669 | * Return TRUE if there is more to read. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 670 | */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 671 | static int |
| 672 | channel_parse_json(int ch_idx) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 673 | { |
| 674 | js_read_T reader; |
| 675 | typval_T listtv; |
| 676 | jsonq_T *item; |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 677 | channel_T *channel = &channels[ch_idx]; |
| 678 | jsonq_T *head = &channel->ch_json_head; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 679 | int ret; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 680 | |
| 681 | if (channel_peek(ch_idx) == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 682 | return FALSE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 683 | |
| 684 | /* TODO: make reader work properly */ |
| 685 | /* reader.js_buf = channel_peek(ch_idx); */ |
| 686 | reader.js_buf = channel_get_all(ch_idx); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 687 | reader.js_used = 0; |
Bram Moolenaar | 56ead34 | 2016-02-02 18:20:08 +0100 | [diff] [blame] | 688 | reader.js_fill = NULL; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 689 | /* reader.js_fill = channel_fill; */ |
| 690 | reader.js_cookie = &ch_idx; |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 691 | ret = json_decode(&reader, &listtv, |
| 692 | channel->ch_mode == MODE_JS ? JSON_JS : 0); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 693 | if (ret == OK) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 694 | { |
Bram Moolenaar | 6076fe1 | 2016-02-05 22:49:56 +0100 | [diff] [blame] | 695 | /* Only accept the response when it is a list with at least two |
| 696 | * items. */ |
| 697 | 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] | 698 | { |
| 699 | /* TODO: give error */ |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 700 | clear_tv(&listtv); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 701 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 702 | else |
| 703 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 704 | item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T)); |
| 705 | if (item == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 706 | clear_tv(&listtv); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 707 | else |
| 708 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 709 | item->value = alloc_tv(); |
| 710 | if (item->value == NULL) |
| 711 | { |
| 712 | vim_free(item); |
| 713 | clear_tv(&listtv); |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | *item->value = listtv; |
| 718 | item->prev = head->prev; |
| 719 | head->prev = item; |
| 720 | item->next = head; |
| 721 | item->prev->next = item; |
| 722 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 726 | |
| 727 | /* Put the unread part back into the channel. |
| 728 | * TODO: insert in front */ |
| 729 | if (reader.js_buf[reader.js_used] != NUL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 730 | { |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 731 | channel_save(ch_idx, reader.js_buf + reader.js_used, |
| 732 | (int)(reader.js_end - reader.js_buf) - reader.js_used); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 733 | ret = TRUE; |
| 734 | } |
| 735 | else |
| 736 | ret = FALSE; |
| 737 | |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 738 | vim_free(reader.js_buf); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 739 | return ret; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Remove "node" from the queue that it is in and free it. |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 744 | * Also frees the contained callback name. |
| 745 | */ |
| 746 | static void |
| 747 | remove_cb_node(cbq_T *node) |
| 748 | { |
| 749 | node->prev->next = node->next; |
| 750 | node->next->prev = node->prev; |
| 751 | vim_free(node->callback); |
| 752 | vim_free(node); |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Remove "node" from the queue that it is in and free it. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 757 | * Caller should have freed or used node->value. |
| 758 | */ |
| 759 | static void |
| 760 | remove_json_node(jsonq_T *node) |
| 761 | { |
| 762 | node->prev->next = node->next; |
| 763 | node->next->prev = node->prev; |
| 764 | vim_free(node); |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Get a message from the JSON queue for channel "ch_idx". |
| 769 | * 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] | 770 | * When "id" is zero or negative jut get the first message. But not the one |
| 771 | * with id ch_block_id. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 772 | * Return OK when found and return the value in "rettv". |
| 773 | * Return FAIL otherwise. |
| 774 | */ |
| 775 | static int |
| 776 | channel_get_json(int ch_idx, int id, typval_T **rettv) |
| 777 | { |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 778 | channel_T *channel = &channels[ch_idx]; |
| 779 | jsonq_T *head = &channel->ch_json_head; |
| 780 | jsonq_T *item = head->next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 781 | |
| 782 | while (item != head) |
| 783 | { |
| 784 | list_T *l = item->value->vval.v_list; |
| 785 | typval_T *tv = &l->lv_first->li_tv; |
| 786 | |
| 787 | 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] | 788 | || (id <= 0 && (tv->v_type != VAR_NUMBER |
Bram Moolenaar | f615728 | 2016-02-10 21:07:14 +0100 | [diff] [blame] | 789 | || tv->vval.v_number == 0 |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 790 | || tv->vval.v_number != channel->ch_block_id))) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 791 | { |
| 792 | *rettv = item->value; |
| 793 | remove_json_node(item); |
| 794 | return OK; |
| 795 | } |
| 796 | item = item->next; |
| 797 | } |
| 798 | return FAIL; |
| 799 | } |
| 800 | |
| 801 | /* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 802 | * Execute a command received over channel "idx". |
| 803 | * "cmd" is the command string, "arg2" the second argument. |
| 804 | * "arg3" is the third argument, NULL if missing. |
| 805 | */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 806 | static void |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 807 | channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 808 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 809 | char_u *arg; |
| 810 | |
| 811 | if (arg2->v_type != VAR_STRING) |
| 812 | { |
| 813 | if (p_verbose > 2) |
| 814 | EMSG("E903: received ex command with non-string argument"); |
| 815 | return; |
| 816 | } |
| 817 | arg = arg2->vval.v_string; |
Bram Moolenaar | 14ad611 | 2016-02-01 21:47:13 +0100 | [diff] [blame] | 818 | if (arg == NULL) |
| 819 | arg = (char_u *)""; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 820 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 821 | if (STRCMP(cmd, "ex") == 0) |
| 822 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 823 | do_cmdline_cmd(arg); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 824 | } |
| 825 | else if (STRCMP(cmd, "normal") == 0) |
| 826 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 827 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 828 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 829 | ea.arg = arg; |
| 830 | ea.addr_count = 0; |
| 831 | ea.forceit = TRUE; /* no mapping */ |
| 832 | ex_normal(&ea); |
| 833 | } |
| 834 | else if (STRCMP(cmd, "redraw") == 0) |
| 835 | { |
| 836 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 837 | |
Bram Moolenaar | 14ad611 | 2016-02-01 21:47:13 +0100 | [diff] [blame] | 838 | ea.forceit = *arg != NUL; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 839 | ex_redraw(&ea); |
| 840 | showruler(FALSE); |
| 841 | setcursor(); |
| 842 | out_flush(); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 843 | #ifdef FEAT_GUI |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 844 | if (gui.in_use) |
| 845 | { |
| 846 | gui_update_cursor(FALSE, FALSE); |
| 847 | gui_mch_flush(); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 848 | } |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 849 | #endif |
| 850 | } |
| 851 | else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0) |
| 852 | { |
| 853 | int is_eval = cmd[1] == 'v'; |
| 854 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 855 | if (is_eval && (arg3 == NULL || arg3->v_type != VAR_NUMBER)) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 856 | { |
| 857 | if (p_verbose > 2) |
| 858 | EMSG("E904: third argument for eval must be a number"); |
| 859 | } |
| 860 | else |
| 861 | { |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 862 | typval_T *tv; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 863 | typval_T err_tv; |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 864 | char_u *json = NULL; |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 865 | channel_T *channel = &channels[idx]; |
| 866 | int options = channel->ch_mode == MODE_JS ? JSON_JS : 0; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 867 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 868 | /* Don't pollute the display with errors. */ |
| 869 | ++emsg_skip; |
| 870 | tv = eval_expr(arg, NULL); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 871 | if (is_eval) |
| 872 | { |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 873 | if (tv != NULL) |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 874 | json = json_encode_nr_expr(arg3->vval.v_number, tv, |
| 875 | options); |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 876 | if (tv == NULL || (json != NULL && *json == NUL)) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 877 | { |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 878 | /* If evaluation failed or the result can't be encoded |
| 879 | * then return the string "ERROR". */ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 880 | err_tv.v_type = VAR_STRING; |
| 881 | err_tv.vval.v_string = (char_u *)"ERROR"; |
| 882 | tv = &err_tv; |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 883 | json = json_encode_nr_expr(arg3->vval.v_number, tv, |
| 884 | options); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 885 | } |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 886 | if (json != NULL) |
| 887 | { |
| 888 | channel_send(idx, json, "eval"); |
| 889 | vim_free(json); |
| 890 | } |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 891 | } |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 892 | --emsg_skip; |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 893 | if (tv != &err_tv) |
| 894 | free_tv(tv); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 895 | } |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 896 | } |
| 897 | else if (p_verbose > 2) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 898 | EMSG2("E905: received unknown command: %s", cmd); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Invoke a callback for channel "idx" if needed. |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 903 | * Return OK when a message was handled, there might be another one. |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 904 | */ |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 905 | static int |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 906 | may_invoke_callback(int idx) |
| 907 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 908 | char_u *msg = NULL; |
| 909 | typval_T *listtv = NULL; |
| 910 | list_T *list; |
| 911 | typval_T *typetv; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 912 | typval_T argv[3]; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 913 | int seq_nr = -1; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 914 | channel_T *channel = &channels[idx]; |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 915 | ch_mode_T ch_mode = channel->ch_mode; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 916 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 917 | if (channel->ch_close_cb != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 918 | /* this channel is handled elsewhere (netbeans) */ |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 919 | return FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 920 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 921 | if (ch_mode != MODE_RAW) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 922 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 923 | /* Get any json message in the queue. */ |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 924 | if (channel_get_json(idx, -1, &listtv) == FAIL) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 925 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 926 | /* Parse readahead, return when there is still no message. */ |
| 927 | channel_parse_json(idx); |
| 928 | if (channel_get_json(idx, -1, &listtv) == FAIL) |
| 929 | return FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 930 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 931 | |
| 932 | list = listtv->vval.v_list; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 933 | argv[1] = list->lv_first->li_next->li_tv; |
| 934 | typetv = &list->lv_first->li_tv; |
| 935 | if (typetv->v_type == VAR_STRING) |
| 936 | { |
| 937 | typval_T *arg3 = NULL; |
| 938 | char_u *cmd = typetv->vval.v_string; |
| 939 | |
Bram Moolenaar | 66624ff | 2016-02-03 23:59:43 +0100 | [diff] [blame] | 940 | /* ["cmd", arg] or ["cmd", arg, arg] */ |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 941 | if (list->lv_len == 3) |
| 942 | arg3 = &list->lv_last->li_tv; |
| 943 | channel_exe_cmd(idx, cmd, &argv[1], arg3); |
| 944 | clear_tv(listtv); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 945 | return TRUE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | if (typetv->v_type != VAR_NUMBER) |
| 949 | { |
| 950 | /* TODO: give error */ |
| 951 | clear_tv(listtv); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 952 | return FALSE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 953 | } |
| 954 | seq_nr = typetv->vval.v_number; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 955 | } |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 956 | else if (channel_peek(idx) == NULL) |
| 957 | { |
| 958 | /* nothing to read on raw channel */ |
| 959 | return FALSE; |
| 960 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 961 | else |
| 962 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 963 | /* For a raw channel we don't know where the message ends, just get |
| 964 | * everything. */ |
| 965 | msg = channel_get_all(idx); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 966 | argv[1].v_type = VAR_STRING; |
| 967 | argv[1].vval.v_string = msg; |
| 968 | } |
| 969 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 970 | if (seq_nr > 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 971 | { |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 972 | cbq_T *cbhead = &channel->ch_cb_head; |
| 973 | cbq_T *cbitem = cbhead->next; |
| 974 | |
| 975 | /* invoke the one-time callback with the matching nr */ |
| 976 | while (cbitem != cbhead) |
| 977 | { |
| 978 | if (cbitem->seq_nr == seq_nr) |
| 979 | { |
| 980 | invoke_callback(idx, cbitem->callback, argv); |
| 981 | remove_cb_node(cbitem); |
| 982 | break; |
| 983 | } |
| 984 | cbitem = cbitem->next; |
| 985 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 986 | } |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 987 | else if (channel->ch_callback != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 988 | { |
| 989 | /* invoke the channel callback */ |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 990 | invoke_callback(idx, channel->ch_callback, argv); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 991 | } |
| 992 | /* else: drop the message TODO: give error */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 993 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 994 | if (listtv != NULL) |
| 995 | clear_tv(listtv); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 996 | vim_free(msg); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 997 | |
| 998 | return TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1002 | * Return TRUE when channel "idx" is open. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1003 | * Also returns FALSE or invalid "idx". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1004 | */ |
| 1005 | int |
| 1006 | channel_is_open(int idx) |
| 1007 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1008 | return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Close channel "idx". |
| 1013 | * This does not trigger the close callback. |
| 1014 | */ |
| 1015 | void |
| 1016 | channel_close(int idx) |
| 1017 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1018 | channel_T *channel = &channels[idx]; |
| 1019 | jsonq_T *jhead; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1020 | cbq_T *cbhead; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1021 | |
| 1022 | if (channel->ch_fd >= 0) |
| 1023 | { |
| 1024 | sock_close(channel->ch_fd); |
| 1025 | channel->ch_fd = -1; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1026 | channel->ch_close_cb = NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1027 | #ifdef FEAT_GUI |
| 1028 | channel_gui_unregister(idx); |
| 1029 | #endif |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1030 | vim_free(channel->ch_callback); |
| 1031 | channel->ch_callback = NULL; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 1032 | channel->ch_timeout = 2000; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1033 | |
| 1034 | while (channel_peek(idx) != NULL) |
| 1035 | vim_free(channel_get(idx)); |
| 1036 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1037 | cbhead = &channel->ch_cb_head; |
| 1038 | while (cbhead->next != cbhead) |
| 1039 | remove_cb_node(cbhead->next); |
| 1040 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1041 | jhead = &channel->ch_json_head; |
| 1042 | while (jhead->next != jhead) |
| 1043 | { |
| 1044 | clear_tv(jhead->next->value); |
| 1045 | remove_json_node(jhead->next); |
| 1046 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1050 | /* |
| 1051 | * Store "buf[len]" on channel "idx". |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 1052 | * Returns OK or FAIL. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1053 | */ |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 1054 | int |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1055 | channel_save(int idx, char_u *buf, int len) |
| 1056 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1057 | readq_T *node; |
| 1058 | readq_T *head = &channels[idx].ch_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1059 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1060 | node = (readq_T *)alloc(sizeof(readq_T)); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1061 | if (node == NULL) |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 1062 | return FAIL; /* out of memory */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1063 | node->buffer = alloc(len + 1); |
| 1064 | if (node->buffer == NULL) |
| 1065 | { |
| 1066 | vim_free(node); |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 1067 | return FAIL; /* out of memory */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1068 | } |
| 1069 | mch_memmove(node->buffer, buf, (size_t)len); |
| 1070 | node->buffer[len] = NUL; |
| 1071 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1072 | /* insert node at tail of queue */ |
| 1073 | node->next = head; |
| 1074 | node->prev = head->prev; |
| 1075 | head->prev->next = node; |
| 1076 | head->prev = node; |
| 1077 | |
| 1078 | if (debugfd != NULL) |
| 1079 | { |
| 1080 | fprintf(debugfd, "RECV on %d: ", idx); |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 1081 | if (fwrite(buf, len, 1, debugfd) != 1) |
| 1082 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1083 | fprintf(debugfd, "\n"); |
| 1084 | } |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 1085 | return OK; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Return the first buffer from the channel without removing it. |
| 1090 | * Returns NULL if there is nothing. |
| 1091 | */ |
| 1092 | char_u * |
| 1093 | channel_peek(int idx) |
| 1094 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1095 | readq_T *head = &channels[idx].ch_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1096 | |
| 1097 | if (head->next == head || head->next == NULL) |
| 1098 | return NULL; |
| 1099 | return head->next->buffer; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1103 | * Clear the read buffer on channel "idx". |
| 1104 | */ |
| 1105 | void |
| 1106 | channel_clear(int idx) |
| 1107 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1108 | readq_T *head = &channels[idx].ch_head; |
| 1109 | readq_T *node = head->next; |
| 1110 | readq_T *next; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1111 | |
| 1112 | while (node != NULL && node != head) |
| 1113 | { |
| 1114 | next = node->next; |
| 1115 | vim_free(node->buffer); |
| 1116 | vim_free(node); |
| 1117 | if (next == head) |
| 1118 | { |
| 1119 | head->next = head; |
| 1120 | head->prev = head; |
| 1121 | break; |
| 1122 | } |
| 1123 | node = next; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /* Sent when the channel is found closed when reading. */ |
| 1128 | #define DETACH_MSG "\"DETACH\"\n" |
| 1129 | |
| 1130 | /* Buffer size for reading incoming messages. */ |
| 1131 | #define MAXMSGSIZE 4096 |
| 1132 | |
| 1133 | /* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1134 | * Check for reading from "fd" with "timeout" msec. |
| 1135 | * Return FAIL when there is nothing to read. |
Bram Moolenaar | a8343c1 | 2016-02-04 22:09:48 +0100 | [diff] [blame] | 1136 | * Always returns OK for FEAT_GUI_W32. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1137 | */ |
| 1138 | static int |
| 1139 | channel_wait(int fd, int timeout) |
| 1140 | { |
Bram Moolenaar | a8343c1 | 2016-02-04 22:09:48 +0100 | [diff] [blame] | 1141 | #if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1142 | struct timeval tval; |
| 1143 | fd_set rfds; |
| 1144 | int ret; |
| 1145 | |
| 1146 | FD_ZERO(&rfds); |
| 1147 | FD_SET(fd, &rfds); |
| 1148 | tval.tv_sec = timeout / 1000; |
| 1149 | tval.tv_usec = (timeout % 1000) * 1000; |
| 1150 | for (;;) |
| 1151 | { |
| 1152 | ret = select(fd + 1, &rfds, NULL, NULL, &tval); |
| 1153 | # ifdef EINTR |
| 1154 | if (ret == -1 && errno == EINTR) |
| 1155 | continue; |
| 1156 | # endif |
| 1157 | if (ret <= 0) |
| 1158 | return FAIL; |
| 1159 | break; |
| 1160 | } |
| 1161 | #else |
Bram Moolenaar | b8b6511 | 2016-01-28 23:01:49 +0100 | [diff] [blame] | 1162 | # ifdef HAVE_POLL |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1163 | struct pollfd fds; |
| 1164 | |
| 1165 | fds.fd = fd; |
| 1166 | fds.events = POLLIN; |
| 1167 | if (poll(&fds, 1, timeout) <= 0) |
| 1168 | return FAIL; |
Bram Moolenaar | b8b6511 | 2016-01-28 23:01:49 +0100 | [diff] [blame] | 1169 | # endif |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1170 | #endif |
| 1171 | return OK; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Return a unique ID to be used in a message. |
| 1176 | */ |
| 1177 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 1178 | channel_get_id(void) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1179 | { |
| 1180 | static int next_id = 1; |
| 1181 | |
| 1182 | return next_id++; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * Read from channel "idx" for as long as there is something to read. |
| 1187 | * The data is put in the read queue. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1188 | */ |
| 1189 | void |
| 1190 | channel_read(int idx) |
| 1191 | { |
| 1192 | static char_u *buf = NULL; |
| 1193 | int len = 0; |
| 1194 | int readlen = 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1195 | channel_T *channel = &channels[idx]; |
| 1196 | |
| 1197 | if (channel->ch_fd < 0) |
| 1198 | { |
| 1199 | CHLOG(idx, FALSE, "channel_read() called while socket is closed\n"); |
| 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | /* Allocate a buffer to read into. */ |
| 1204 | if (buf == NULL) |
| 1205 | { |
| 1206 | buf = alloc(MAXMSGSIZE); |
| 1207 | if (buf == NULL) |
| 1208 | return; /* out of memory! */ |
| 1209 | } |
| 1210 | |
| 1211 | /* Keep on reading for as long as there is something to read. |
| 1212 | * Use select() or poll() to avoid blocking on a message that is exactly |
| 1213 | * MAXMSGSIZE long. */ |
| 1214 | for (;;) |
| 1215 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1216 | if (channel_wait(channel->ch_fd, 0) == FAIL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1217 | break; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1218 | len = sock_read(channel->ch_fd, buf, MAXMSGSIZE); |
| 1219 | if (len <= 0) |
| 1220 | break; /* error or nothing more to read */ |
| 1221 | |
| 1222 | /* Store the read message in the queue. */ |
| 1223 | channel_save(idx, buf, len); |
| 1224 | readlen += len; |
| 1225 | if (len < MAXMSGSIZE) |
| 1226 | break; /* did read everything that's available */ |
| 1227 | } |
Bram Moolenaar | a8343c1 | 2016-02-04 22:09:48 +0100 | [diff] [blame] | 1228 | #ifdef FEAT_GUI_W32 |
| 1229 | if (len == SOCKET_ERROR) |
| 1230 | { |
| 1231 | /* For Win32 GUI channel_wait() always returns OK and we handle the |
| 1232 | * situation that there is nothing to read here. |
| 1233 | * TODO: how about a timeout? */ |
| 1234 | if (WSAGetLastError() == WSAEWOULDBLOCK) |
| 1235 | return; |
| 1236 | } |
| 1237 | #endif |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1238 | |
| 1239 | /* Reading a socket disconnection (readlen == 0), or a socket error. */ |
| 1240 | if (readlen <= 0) |
| 1241 | { |
| 1242 | /* Queue a "DETACH" netbeans message in the command queue in order to |
| 1243 | * terminate the netbeans session later. Do not end the session here |
| 1244 | * directly as we may be running in the context of a call to |
| 1245 | * netbeans_parse_messages(): |
| 1246 | * netbeans_parse_messages |
| 1247 | * -> autocmd triggered while processing the netbeans cmd |
| 1248 | * -> ui_breakcheck |
| 1249 | * -> gui event loop or select loop |
| 1250 | * -> channel_read() |
| 1251 | */ |
| 1252 | channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG)); |
| 1253 | |
| 1254 | channel_close(idx); |
| 1255 | if (channel->ch_close_cb != NULL) |
| 1256 | (*channel->ch_close_cb)(); |
| 1257 | |
| 1258 | if (len < 0) |
| 1259 | { |
| 1260 | /* Todo: which channel? */ |
| 1261 | CHERROR("%s(): cannot from channel\n", "channel_read"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1262 | PERROR(_("E896: read from channel")); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | #if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK) |
| 1267 | if (CH_HAS_GUI && gtk_main_level() > 0) |
| 1268 | gtk_main_quit(); |
| 1269 | #endif |
| 1270 | } |
| 1271 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1272 | /* |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1273 | * Read from raw channel "idx". Blocks until there is something to read or |
| 1274 | * the timeout expires. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1275 | * Returns what was read in allocated memory. |
| 1276 | * Returns NULL in case of error or timeout. |
| 1277 | */ |
| 1278 | char_u * |
| 1279 | channel_read_block(int idx) |
| 1280 | { |
| 1281 | if (channel_peek(idx) == NULL) |
| 1282 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 1283 | /* Wait for up to the channel timeout. */ |
| 1284 | if (channel_wait(channels[idx].ch_fd, channels[idx].ch_timeout) == FAIL) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1285 | return NULL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1286 | channel_read(idx); |
| 1287 | } |
| 1288 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1289 | return channel_get_all(idx); |
| 1290 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1291 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1292 | /* |
| 1293 | * Read one JSON message from channel "ch_idx" with ID "id" and store the |
| 1294 | * result in "rettv". |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 1295 | * Blocks until the message is received or the timeout is reached. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1296 | */ |
| 1297 | int |
| 1298 | channel_read_json_block(int ch_idx, int id, typval_T **rettv) |
| 1299 | { |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1300 | int more; |
| 1301 | channel_T *channel = &channels[ch_idx]; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1302 | |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1303 | channel->ch_block_id = id; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1304 | for (;;) |
| 1305 | { |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1306 | more = channel_parse_json(ch_idx); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1307 | |
| 1308 | /* search for messsage "id" */ |
| 1309 | if (channel_get_json(ch_idx, id, rettv) == OK) |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1310 | { |
| 1311 | channel->ch_block_id = 0; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1312 | return OK; |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1313 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1314 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1315 | if (!more) |
| 1316 | { |
| 1317 | /* Handle any other messages in the queue. If done some more |
| 1318 | * messages may have arrived. */ |
| 1319 | if (channel_parse_messages()) |
| 1320 | continue; |
| 1321 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 1322 | /* Wait for up to the channel timeout. */ |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1323 | if (channel->ch_fd < 0 || channel_wait(channel->ch_fd, |
| 1324 | channel->ch_timeout) == FAIL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1325 | break; |
| 1326 | channel_read(ch_idx); |
| 1327 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1328 | } |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 1329 | channel->ch_block_id = 0; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1330 | return FAIL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1333 | # if defined(WIN32) || defined(PROTO) |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 1334 | /* |
| 1335 | * Lookup the channel index from the socket. |
| 1336 | * Returns -1 when the socket isn't found. |
| 1337 | */ |
| 1338 | int |
| 1339 | channel_socket2idx(sock_T fd) |
| 1340 | { |
| 1341 | int i; |
| 1342 | |
| 1343 | if (fd >= 0) |
| 1344 | for (i = 0; i < channel_count; ++i) |
| 1345 | if (channels[i].ch_fd == fd) |
| 1346 | return i; |
| 1347 | return -1; |
| 1348 | } |
| 1349 | # endif |
| 1350 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1351 | /* |
| 1352 | * Write "buf" (NUL terminated string) to channel "idx". |
| 1353 | * When "fun" is not NULL an error message might be given. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1354 | * Return FAIL or OK. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1355 | */ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1356 | int |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1357 | channel_send(int idx, char_u *buf, char *fun) |
| 1358 | { |
| 1359 | channel_T *channel = &channels[idx]; |
| 1360 | int len = (int)STRLEN(buf); |
| 1361 | |
| 1362 | if (channel->ch_fd < 0) |
| 1363 | { |
| 1364 | if (!channel->ch_error && fun != NULL) |
| 1365 | { |
| 1366 | CHERROR(" %s(): write while not connected\n", fun); |
| 1367 | EMSG2("E630: %s(): write while not connected", fun); |
| 1368 | } |
| 1369 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1370 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1371 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1372 | |
| 1373 | if (sock_write(channel->ch_fd, buf, len) != len) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1374 | { |
| 1375 | if (!channel->ch_error && fun != NULL) |
| 1376 | { |
| 1377 | CHERROR(" %s(): write failed\n", fun); |
| 1378 | EMSG2("E631: %s(): write failed", fun); |
| 1379 | } |
| 1380 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1381 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1382 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1383 | |
| 1384 | channel->ch_error = FALSE; |
| 1385 | return OK; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | # if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1389 | /* |
| 1390 | * Add open channels to the poll struct. |
| 1391 | * Return the adjusted struct index. |
| 1392 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 1393 | */ |
| 1394 | int |
| 1395 | channel_poll_setup(int nfd_in, void *fds_in) |
| 1396 | { |
| 1397 | int nfd = nfd_in; |
| 1398 | int i; |
| 1399 | struct pollfd *fds = fds_in; |
| 1400 | |
| 1401 | for (i = 0; i < channel_count; ++i) |
| 1402 | if (channels[i].ch_fd >= 0) |
| 1403 | { |
| 1404 | channels[i].ch_idx = nfd; |
| 1405 | fds[nfd].fd = channels[i].ch_fd; |
| 1406 | fds[nfd].events = POLLIN; |
| 1407 | nfd++; |
| 1408 | } |
| 1409 | else |
| 1410 | channels[i].ch_idx = -1; |
| 1411 | |
| 1412 | return nfd; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 1417 | */ |
| 1418 | int |
| 1419 | channel_poll_check(int ret_in, void *fds_in) |
| 1420 | { |
| 1421 | int ret = ret_in; |
| 1422 | int i; |
| 1423 | struct pollfd *fds = fds_in; |
| 1424 | |
| 1425 | for (i = 0; i < channel_count; ++i) |
| 1426 | if (ret > 0 && channels[i].ch_idx != -1 |
| 1427 | && fds[channels[i].ch_idx].revents & POLLIN) |
| 1428 | { |
| 1429 | channel_read(i); |
| 1430 | --ret; |
| 1431 | } |
| 1432 | |
| 1433 | return ret; |
| 1434 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1435 | # endif /* UNIX && !HAVE_SELECT */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1436 | |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1437 | # if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1438 | /* |
| 1439 | * The type of "rfds" is hidden to avoid problems with the function proto. |
| 1440 | */ |
| 1441 | int |
| 1442 | channel_select_setup(int maxfd_in, void *rfds_in) |
| 1443 | { |
| 1444 | int maxfd = maxfd_in; |
| 1445 | int i; |
| 1446 | fd_set *rfds = rfds_in; |
| 1447 | |
| 1448 | for (i = 0; i < channel_count; ++i) |
| 1449 | if (channels[i].ch_fd >= 0) |
| 1450 | { |
| 1451 | FD_SET(channels[i].ch_fd, rfds); |
| 1452 | if (maxfd < channels[i].ch_fd) |
| 1453 | maxfd = channels[i].ch_fd; |
| 1454 | } |
| 1455 | |
| 1456 | return maxfd; |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * The type of "rfds" is hidden to avoid problems with the function proto. |
| 1461 | */ |
| 1462 | int |
| 1463 | channel_select_check(int ret_in, void *rfds_in) |
| 1464 | { |
| 1465 | int ret = ret_in; |
| 1466 | int i; |
| 1467 | fd_set *rfds = rfds_in; |
| 1468 | |
| 1469 | for (i = 0; i < channel_count; ++i) |
| 1470 | if (ret > 0 && channels[i].ch_fd >= 0 |
| 1471 | && FD_ISSET(channels[i].ch_fd, rfds)) |
| 1472 | { |
| 1473 | channel_read(i); |
| 1474 | --ret; |
| 1475 | } |
| 1476 | |
| 1477 | return ret; |
| 1478 | } |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1479 | # endif /* !FEAT_GUI_W32 && HAVE_SELECT */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1480 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1481 | /* |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1482 | * Execute queued up commands. |
| 1483 | * Invoked from the main loop when it's safe to execute received commands. |
| 1484 | * Return TRUE when something was done. |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1485 | */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1486 | int |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1487 | channel_parse_messages(void) |
| 1488 | { |
| 1489 | int i; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1490 | int ret = FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1491 | |
| 1492 | for (i = 0; i < channel_count; ++i) |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 1493 | while (may_invoke_callback(i) == OK) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1494 | { |
| 1495 | i = 0; /* start over */ |
| 1496 | ret = TRUE; |
| 1497 | } |
| 1498 | return ret; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 1501 | /* |
| 1502 | * Mark references to lists used in channels. |
| 1503 | */ |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 1504 | int |
| 1505 | set_ref_in_channel(int copyID) |
| 1506 | { |
| 1507 | int i; |
| 1508 | int abort = FALSE; |
| 1509 | |
| 1510 | for (i = 0; i < channel_count; ++i) |
| 1511 | { |
| 1512 | jsonq_T *head = &channels[i].ch_json_head; |
| 1513 | jsonq_T *item = head->next; |
| 1514 | |
| 1515 | while (item != head) |
| 1516 | { |
| 1517 | list_T *l = item->value->vval.v_list; |
| 1518 | |
| 1519 | if (l->lv_copyID != copyID) |
| 1520 | { |
| 1521 | l->lv_copyID = copyID; |
| 1522 | abort = abort || set_ref_in_list(l, copyID, NULL); |
| 1523 | } |
| 1524 | item = item->next; |
| 1525 | } |
| 1526 | } |
| 1527 | return abort; |
| 1528 | } |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 1529 | |
| 1530 | /* |
| 1531 | * Return the mode of channel "idx". |
| 1532 | * If "idx" is invalid returns MODE_JSON. |
| 1533 | */ |
| 1534 | ch_mode_T |
| 1535 | channel_get_mode(int idx) |
| 1536 | { |
| 1537 | if (idx < 0 || idx >= channel_count) |
| 1538 | return MODE_JSON; |
| 1539 | return channels[idx].ch_mode; |
| 1540 | } |
| 1541 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1542 | #endif /* FEAT_CHANNEL */ |