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