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