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 | /* |
| 526 | * Use the read buffer of channel "ch_idx" and parse JSON messages that are |
| 527 | * complete. The messages are added to the queue. |
| 528 | */ |
| 529 | void |
| 530 | channel_read_json(int ch_idx) |
| 531 | { |
| 532 | js_read_T reader; |
| 533 | typval_T listtv; |
| 534 | jsonq_T *item; |
| 535 | jsonq_T *head = &channels[ch_idx].ch_json_head; |
| 536 | |
| 537 | if (channel_peek(ch_idx) == NULL) |
| 538 | return; |
| 539 | |
| 540 | /* TODO: make reader work properly */ |
| 541 | /* reader.js_buf = channel_peek(ch_idx); */ |
| 542 | reader.js_buf = channel_get_all(ch_idx); |
| 543 | reader.js_eof = TRUE; |
| 544 | /* reader.js_eof = FALSE; */ |
| 545 | reader.js_used = 0; |
| 546 | /* reader.js_fill = channel_fill; */ |
| 547 | reader.js_cookie = &ch_idx; |
| 548 | if (json_decode(&reader, &listtv) == OK) |
| 549 | { |
| 550 | item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T)); |
| 551 | if (item == NULL) |
| 552 | clear_tv(&listtv); |
| 553 | else |
| 554 | { |
| 555 | item->value = alloc_tv(); |
| 556 | if (item->value == NULL) |
| 557 | { |
| 558 | vim_free(item); |
| 559 | clear_tv(&listtv); |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | *item->value = listtv; |
| 564 | item->prev = head->prev; |
| 565 | head->prev = item; |
| 566 | item->next = head; |
| 567 | item->prev->next = item; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Remove "node" from the queue that it is in and free it. |
| 575 | * Caller should have freed or used node->value. |
| 576 | */ |
| 577 | static void |
| 578 | remove_json_node(jsonq_T *node) |
| 579 | { |
| 580 | node->prev->next = node->next; |
| 581 | node->next->prev = node->prev; |
| 582 | vim_free(node); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Get a message from the JSON queue for channel "ch_idx". |
| 587 | * When "id" is positive it must match the first number in the list. |
| 588 | * When "id" is zero or negative jut get the first message. |
| 589 | * Return OK when found and return the value in "rettv". |
| 590 | * Return FAIL otherwise. |
| 591 | */ |
| 592 | static int |
| 593 | channel_get_json(int ch_idx, int id, typval_T **rettv) |
| 594 | { |
| 595 | jsonq_T *head = &channels[ch_idx].ch_json_head; |
| 596 | jsonq_T *item = head->next; |
| 597 | |
| 598 | while (item != head) |
| 599 | { |
| 600 | list_T *l = item->value->vval.v_list; |
| 601 | typval_T *tv = &l->lv_first->li_tv; |
| 602 | |
| 603 | if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id) |
| 604 | || id <= 0) |
| 605 | { |
| 606 | *rettv = item->value; |
| 607 | remove_json_node(item); |
| 608 | return OK; |
| 609 | } |
| 610 | item = item->next; |
| 611 | } |
| 612 | return FAIL; |
| 613 | } |
| 614 | |
| 615 | /* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 616 | * Execute a command received over channel "idx". |
| 617 | * "cmd" is the command string, "arg2" the second argument. |
| 618 | * "arg3" is the third argument, NULL if missing. |
| 619 | */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 620 | static void |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 621 | 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] | 622 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 623 | char_u *arg; |
| 624 | |
| 625 | if (arg2->v_type != VAR_STRING) |
| 626 | { |
| 627 | if (p_verbose > 2) |
| 628 | EMSG("E903: received ex command with non-string argument"); |
| 629 | return; |
| 630 | } |
| 631 | arg = arg2->vval.v_string; |
| 632 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 633 | if (STRCMP(cmd, "ex") == 0) |
| 634 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 635 | do_cmdline_cmd(arg); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 636 | } |
| 637 | else if (STRCMP(cmd, "normal") == 0) |
| 638 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 639 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 640 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 641 | ea.arg = arg; |
| 642 | ea.addr_count = 0; |
| 643 | ea.forceit = TRUE; /* no mapping */ |
| 644 | ex_normal(&ea); |
| 645 | } |
| 646 | else if (STRCMP(cmd, "redraw") == 0) |
| 647 | { |
| 648 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 649 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 650 | ea.forceit = arg != NULL && *arg != NUL; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 651 | ex_redraw(&ea); |
| 652 | showruler(FALSE); |
| 653 | setcursor(); |
| 654 | out_flush(); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 655 | #ifdef FEAT_GUI |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 656 | if (gui.in_use) |
| 657 | { |
| 658 | gui_update_cursor(FALSE, FALSE); |
| 659 | gui_mch_flush(); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 660 | } |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 661 | #endif |
| 662 | } |
| 663 | else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "eval") == 0) |
| 664 | { |
| 665 | int is_eval = cmd[1] == 'v'; |
| 666 | |
| 667 | if (is_eval && arg3->v_type != VAR_NUMBER) |
| 668 | { |
| 669 | if (p_verbose > 2) |
| 670 | EMSG("E904: third argument for eval must be a number"); |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | typval_T *tv = eval_expr(arg, NULL); |
| 675 | typval_T err_tv; |
| 676 | char_u *json; |
| 677 | |
| 678 | if (is_eval) |
| 679 | { |
| 680 | if (tv == NULL) |
| 681 | { |
| 682 | err_tv.v_type = VAR_STRING; |
| 683 | err_tv.vval.v_string = (char_u *)"ERROR"; |
| 684 | tv = &err_tv; |
| 685 | } |
| 686 | json = json_encode_nr_expr(arg3->vval.v_number, tv); |
| 687 | channel_send(idx, json, "eval"); |
| 688 | vim_free(json); |
| 689 | } |
| 690 | free_tv(tv); |
| 691 | } |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 692 | } |
| 693 | else if (p_verbose > 2) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 694 | EMSG2("E905: received unknown command: %s", cmd); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Invoke a callback for channel "idx" if needed. |
| 699 | */ |
| 700 | static void |
| 701 | may_invoke_callback(int idx) |
| 702 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 703 | char_u *msg = NULL; |
| 704 | typval_T *listtv = NULL; |
| 705 | list_T *list; |
| 706 | typval_T *typetv; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 707 | typval_T argv[3]; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 708 | int seq_nr = -1; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 709 | int json_mode = channels[idx].ch_json_mode; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 710 | |
| 711 | if (channel_peek(idx) == NULL) |
| 712 | return; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 713 | if (channels[idx].ch_close_cb != NULL) |
| 714 | /* this channel is handled elsewhere (netbeans) */ |
| 715 | return; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 716 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 717 | if (json_mode) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 718 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 719 | /* Get any json message. Return if there isn't one. */ |
| 720 | channel_read_json(idx); |
| 721 | if (channel_get_json(idx, -1, &listtv) == FAIL) |
| 722 | return; |
| 723 | if (listtv->v_type != VAR_LIST) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 724 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 725 | /* TODO: give error */ |
| 726 | clear_tv(listtv); |
| 727 | return; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 728 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 729 | |
| 730 | list = listtv->vval.v_list; |
| 731 | if (list->lv_len < 2) |
| 732 | { |
| 733 | /* TODO: give error */ |
| 734 | clear_tv(listtv); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | argv[1] = list->lv_first->li_next->li_tv; |
| 739 | typetv = &list->lv_first->li_tv; |
| 740 | if (typetv->v_type == VAR_STRING) |
| 741 | { |
| 742 | typval_T *arg3 = NULL; |
| 743 | char_u *cmd = typetv->vval.v_string; |
| 744 | |
| 745 | /* ["cmd", arg] */ |
| 746 | if (list->lv_len == 3) |
| 747 | arg3 = &list->lv_last->li_tv; |
| 748 | channel_exe_cmd(idx, cmd, &argv[1], arg3); |
| 749 | clear_tv(listtv); |
| 750 | return; |
| 751 | } |
| 752 | |
| 753 | if (typetv->v_type != VAR_NUMBER) |
| 754 | { |
| 755 | /* TODO: give error */ |
| 756 | clear_tv(listtv); |
| 757 | return; |
| 758 | } |
| 759 | seq_nr = typetv->vval.v_number; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 760 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 761 | else |
| 762 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 763 | /* For a raw channel we don't know where the message ends, just get |
| 764 | * everything. */ |
| 765 | msg = channel_get_all(idx); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 766 | argv[1].v_type = VAR_STRING; |
| 767 | argv[1].vval.v_string = msg; |
| 768 | } |
| 769 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 770 | if (channels[idx].ch_req_callback != NULL && seq_nr != 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 771 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 772 | /* TODO: check the sequence number */ |
| 773 | /* invoke the one-time callback */ |
| 774 | invoke_callback(idx, channels[idx].ch_req_callback, argv); |
| 775 | channels[idx].ch_req_callback = NULL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 776 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 777 | else if (channels[idx].ch_callback != NULL) |
| 778 | { |
| 779 | /* invoke the channel callback */ |
| 780 | invoke_callback(idx, channels[idx].ch_callback, argv); |
| 781 | } |
| 782 | /* else: drop the message TODO: give error */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 783 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 784 | if (listtv != NULL) |
| 785 | clear_tv(listtv); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 786 | vim_free(msg); |
| 787 | } |
| 788 | |
| 789 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 790 | * Return TRUE when channel "idx" is open. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 791 | * Also returns FALSE or invalid "idx". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 792 | */ |
| 793 | int |
| 794 | channel_is_open(int idx) |
| 795 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 796 | return idx >= 0 && idx < channel_count && channels[idx].ch_fd >= 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Close channel "idx". |
| 801 | * This does not trigger the close callback. |
| 802 | */ |
| 803 | void |
| 804 | channel_close(int idx) |
| 805 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 806 | channel_T *channel = &channels[idx]; |
| 807 | jsonq_T *jhead; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 808 | |
| 809 | if (channel->ch_fd >= 0) |
| 810 | { |
| 811 | sock_close(channel->ch_fd); |
| 812 | channel->ch_fd = -1; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 813 | channel->ch_close_cb = NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 814 | #ifdef FEAT_GUI |
| 815 | channel_gui_unregister(idx); |
| 816 | #endif |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 817 | vim_free(channel->ch_callback); |
| 818 | channel->ch_callback = NULL; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 819 | |
| 820 | while (channel_peek(idx) != NULL) |
| 821 | vim_free(channel_get(idx)); |
| 822 | |
| 823 | jhead = &channel->ch_json_head; |
| 824 | while (jhead->next != jhead) |
| 825 | { |
| 826 | clear_tv(jhead->next->value); |
| 827 | remove_json_node(jhead->next); |
| 828 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 832 | /* |
| 833 | * Store "buf[len]" on channel "idx". |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 834 | * Returns OK or FAIL. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 835 | */ |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 836 | int |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 837 | channel_save(int idx, char_u *buf, int len) |
| 838 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 839 | readq_T *node; |
| 840 | readq_T *head = &channels[idx].ch_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 841 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 842 | node = (readq_T *)alloc(sizeof(readq_T)); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 843 | if (node == NULL) |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 844 | return FAIL; /* out of memory */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 845 | node->buffer = alloc(len + 1); |
| 846 | if (node->buffer == NULL) |
| 847 | { |
| 848 | vim_free(node); |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 849 | return FAIL; /* out of memory */ |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 850 | } |
| 851 | mch_memmove(node->buffer, buf, (size_t)len); |
| 852 | node->buffer[len] = NUL; |
| 853 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 854 | /* insert node at tail of queue */ |
| 855 | node->next = head; |
| 856 | node->prev = head->prev; |
| 857 | head->prev->next = node; |
| 858 | head->prev = node; |
| 859 | |
| 860 | if (debugfd != NULL) |
| 861 | { |
| 862 | fprintf(debugfd, "RECV on %d: ", idx); |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 863 | if (fwrite(buf, len, 1, debugfd) != 1) |
| 864 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 865 | fprintf(debugfd, "\n"); |
| 866 | } |
Bram Moolenaar | 8316246 | 2016-01-28 23:10:07 +0100 | [diff] [blame] | 867 | return OK; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Return the first buffer from the channel without removing it. |
| 872 | * Returns NULL if there is nothing. |
| 873 | */ |
| 874 | char_u * |
| 875 | channel_peek(int idx) |
| 876 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 877 | readq_T *head = &channels[idx].ch_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 878 | |
| 879 | if (head->next == head || head->next == NULL) |
| 880 | return NULL; |
| 881 | return head->next->buffer; |
| 882 | } |
| 883 | |
| 884 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 885 | * Clear the read buffer on channel "idx". |
| 886 | */ |
| 887 | void |
| 888 | channel_clear(int idx) |
| 889 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 890 | readq_T *head = &channels[idx].ch_head; |
| 891 | readq_T *node = head->next; |
| 892 | readq_T *next; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 893 | |
| 894 | while (node != NULL && node != head) |
| 895 | { |
| 896 | next = node->next; |
| 897 | vim_free(node->buffer); |
| 898 | vim_free(node); |
| 899 | if (next == head) |
| 900 | { |
| 901 | head->next = head; |
| 902 | head->prev = head; |
| 903 | break; |
| 904 | } |
| 905 | node = next; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /* Sent when the channel is found closed when reading. */ |
| 910 | #define DETACH_MSG "\"DETACH\"\n" |
| 911 | |
| 912 | /* Buffer size for reading incoming messages. */ |
| 913 | #define MAXMSGSIZE 4096 |
| 914 | |
| 915 | /* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 916 | * Check for reading from "fd" with "timeout" msec. |
| 917 | * Return FAIL when there is nothing to read. |
| 918 | */ |
| 919 | static int |
| 920 | channel_wait(int fd, int timeout) |
| 921 | { |
| 922 | #ifdef HAVE_SELECT |
| 923 | struct timeval tval; |
| 924 | fd_set rfds; |
| 925 | int ret; |
| 926 | |
| 927 | FD_ZERO(&rfds); |
| 928 | FD_SET(fd, &rfds); |
| 929 | tval.tv_sec = timeout / 1000; |
| 930 | tval.tv_usec = (timeout % 1000) * 1000; |
| 931 | for (;;) |
| 932 | { |
| 933 | ret = select(fd + 1, &rfds, NULL, NULL, &tval); |
| 934 | # ifdef EINTR |
| 935 | if (ret == -1 && errno == EINTR) |
| 936 | continue; |
| 937 | # endif |
| 938 | if (ret <= 0) |
| 939 | return FAIL; |
| 940 | break; |
| 941 | } |
| 942 | #else |
Bram Moolenaar | b8b6511 | 2016-01-28 23:01:49 +0100 | [diff] [blame] | 943 | # ifdef HAVE_POLL |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 944 | struct pollfd fds; |
| 945 | |
| 946 | fds.fd = fd; |
| 947 | fds.events = POLLIN; |
| 948 | if (poll(&fds, 1, timeout) <= 0) |
| 949 | return FAIL; |
Bram Moolenaar | b8b6511 | 2016-01-28 23:01:49 +0100 | [diff] [blame] | 950 | # endif |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 951 | #endif |
| 952 | return OK; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Return a unique ID to be used in a message. |
| 957 | */ |
| 958 | int |
Bram Moolenaar | 7454a06 | 2016-01-30 15:14:10 +0100 | [diff] [blame] | 959 | channel_get_id(void) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 960 | { |
| 961 | static int next_id = 1; |
| 962 | |
| 963 | return next_id++; |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Read from channel "idx" for as long as there is something to read. |
| 968 | * The data is put in the read queue. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 969 | */ |
| 970 | void |
| 971 | channel_read(int idx) |
| 972 | { |
| 973 | static char_u *buf = NULL; |
| 974 | int len = 0; |
| 975 | int readlen = 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 976 | channel_T *channel = &channels[idx]; |
| 977 | |
| 978 | if (channel->ch_fd < 0) |
| 979 | { |
| 980 | CHLOG(idx, FALSE, "channel_read() called while socket is closed\n"); |
| 981 | return; |
| 982 | } |
| 983 | |
| 984 | /* Allocate a buffer to read into. */ |
| 985 | if (buf == NULL) |
| 986 | { |
| 987 | buf = alloc(MAXMSGSIZE); |
| 988 | if (buf == NULL) |
| 989 | return; /* out of memory! */ |
| 990 | } |
| 991 | |
| 992 | /* Keep on reading for as long as there is something to read. |
| 993 | * Use select() or poll() to avoid blocking on a message that is exactly |
| 994 | * MAXMSGSIZE long. */ |
| 995 | for (;;) |
| 996 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 997 | if (channel_wait(channel->ch_fd, 0) == FAIL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 998 | break; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 999 | len = sock_read(channel->ch_fd, buf, MAXMSGSIZE); |
| 1000 | if (len <= 0) |
| 1001 | break; /* error or nothing more to read */ |
| 1002 | |
| 1003 | /* Store the read message in the queue. */ |
| 1004 | channel_save(idx, buf, len); |
| 1005 | readlen += len; |
| 1006 | if (len < MAXMSGSIZE) |
| 1007 | break; /* did read everything that's available */ |
| 1008 | } |
| 1009 | |
| 1010 | /* Reading a socket disconnection (readlen == 0), or a socket error. */ |
| 1011 | if (readlen <= 0) |
| 1012 | { |
| 1013 | /* Queue a "DETACH" netbeans message in the command queue in order to |
| 1014 | * terminate the netbeans session later. Do not end the session here |
| 1015 | * directly as we may be running in the context of a call to |
| 1016 | * netbeans_parse_messages(): |
| 1017 | * netbeans_parse_messages |
| 1018 | * -> autocmd triggered while processing the netbeans cmd |
| 1019 | * -> ui_breakcheck |
| 1020 | * -> gui event loop or select loop |
| 1021 | * -> channel_read() |
| 1022 | */ |
| 1023 | channel_save(idx, (char_u *)DETACH_MSG, (int)STRLEN(DETACH_MSG)); |
| 1024 | |
| 1025 | channel_close(idx); |
| 1026 | if (channel->ch_close_cb != NULL) |
| 1027 | (*channel->ch_close_cb)(); |
| 1028 | |
| 1029 | if (len < 0) |
| 1030 | { |
| 1031 | /* Todo: which channel? */ |
| 1032 | CHERROR("%s(): cannot from channel\n", "channel_read"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1033 | PERROR(_("E896: read from channel")); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | #if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK) |
| 1038 | if (CH_HAS_GUI && gtk_main_level() > 0) |
| 1039 | gtk_main_quit(); |
| 1040 | #endif |
| 1041 | } |
| 1042 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1043 | /* |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1044 | * Read from raw channel "idx". Blocks until there is something to read or |
| 1045 | * the timeout expires. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1046 | * Returns what was read in allocated memory. |
| 1047 | * Returns NULL in case of error or timeout. |
| 1048 | */ |
| 1049 | char_u * |
| 1050 | channel_read_block(int idx) |
| 1051 | { |
| 1052 | if (channel_peek(idx) == NULL) |
| 1053 | { |
| 1054 | /* Wait for up to 2 seconds. |
| 1055 | * TODO: use timeout set on the channel. */ |
| 1056 | if (channel_wait(channels[idx].ch_fd, 2000) == FAIL) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1057 | return NULL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1058 | channel_read(idx); |
| 1059 | } |
| 1060 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1061 | return channel_get_all(idx); |
| 1062 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1063 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1064 | /* |
| 1065 | * Read one JSON message from channel "ch_idx" with ID "id" and store the |
| 1066 | * result in "rettv". |
| 1067 | * Blocks until the message is received. |
| 1068 | */ |
| 1069 | int |
| 1070 | channel_read_json_block(int ch_idx, int id, typval_T **rettv) |
| 1071 | { |
| 1072 | for (;;) |
| 1073 | { |
| 1074 | channel_read_json(ch_idx); |
| 1075 | |
| 1076 | /* search for messsage "id" */ |
| 1077 | if (channel_get_json(ch_idx, id, rettv) == OK) |
| 1078 | return OK; |
| 1079 | |
| 1080 | /* Wait for up to 2 seconds. |
| 1081 | * TODO: use timeout set on the channel. */ |
| 1082 | if (channel_wait(channels[ch_idx].ch_fd, 2000) == FAIL) |
| 1083 | break; |
| 1084 | channel_read(ch_idx); |
| 1085 | } |
| 1086 | return FAIL; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1089 | # if defined(WIN32) || defined(PROTO) |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 1090 | /* |
| 1091 | * Lookup the channel index from the socket. |
| 1092 | * Returns -1 when the socket isn't found. |
| 1093 | */ |
| 1094 | int |
| 1095 | channel_socket2idx(sock_T fd) |
| 1096 | { |
| 1097 | int i; |
| 1098 | |
| 1099 | if (fd >= 0) |
| 1100 | for (i = 0; i < channel_count; ++i) |
| 1101 | if (channels[i].ch_fd == fd) |
| 1102 | return i; |
| 1103 | return -1; |
| 1104 | } |
| 1105 | # endif |
| 1106 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1107 | /* |
| 1108 | * Write "buf" (NUL terminated string) to channel "idx". |
| 1109 | * When "fun" is not NULL an error message might be given. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1110 | * Return FAIL or OK. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1111 | */ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1112 | int |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1113 | channel_send(int idx, char_u *buf, char *fun) |
| 1114 | { |
| 1115 | channel_T *channel = &channels[idx]; |
| 1116 | int len = (int)STRLEN(buf); |
| 1117 | |
| 1118 | if (channel->ch_fd < 0) |
| 1119 | { |
| 1120 | if (!channel->ch_error && fun != NULL) |
| 1121 | { |
| 1122 | CHERROR(" %s(): write while not connected\n", fun); |
| 1123 | EMSG2("E630: %s(): write while not connected", fun); |
| 1124 | } |
| 1125 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1126 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1127 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1128 | |
| 1129 | if (sock_write(channel->ch_fd, buf, len) != len) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1130 | { |
| 1131 | if (!channel->ch_error && fun != NULL) |
| 1132 | { |
| 1133 | CHERROR(" %s(): write failed\n", fun); |
| 1134 | EMSG2("E631: %s(): write failed", fun); |
| 1135 | } |
| 1136 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1137 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1138 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1139 | |
| 1140 | channel->ch_error = FALSE; |
| 1141 | return OK; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | # if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1145 | /* |
| 1146 | * Add open channels to the poll struct. |
| 1147 | * Return the adjusted struct index. |
| 1148 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 1149 | */ |
| 1150 | int |
| 1151 | channel_poll_setup(int nfd_in, void *fds_in) |
| 1152 | { |
| 1153 | int nfd = nfd_in; |
| 1154 | int i; |
| 1155 | struct pollfd *fds = fds_in; |
| 1156 | |
| 1157 | for (i = 0; i < channel_count; ++i) |
| 1158 | if (channels[i].ch_fd >= 0) |
| 1159 | { |
| 1160 | channels[i].ch_idx = nfd; |
| 1161 | fds[nfd].fd = channels[i].ch_fd; |
| 1162 | fds[nfd].events = POLLIN; |
| 1163 | nfd++; |
| 1164 | } |
| 1165 | else |
| 1166 | channels[i].ch_idx = -1; |
| 1167 | |
| 1168 | return nfd; |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 1173 | */ |
| 1174 | int |
| 1175 | channel_poll_check(int ret_in, void *fds_in) |
| 1176 | { |
| 1177 | int ret = ret_in; |
| 1178 | int i; |
| 1179 | struct pollfd *fds = fds_in; |
| 1180 | |
| 1181 | for (i = 0; i < channel_count; ++i) |
| 1182 | if (ret > 0 && channels[i].ch_idx != -1 |
| 1183 | && fds[channels[i].ch_idx].revents & POLLIN) |
| 1184 | { |
| 1185 | channel_read(i); |
| 1186 | --ret; |
| 1187 | } |
| 1188 | |
| 1189 | return ret; |
| 1190 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1191 | # endif /* UNIX && !HAVE_SELECT */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1192 | |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1193 | # if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1194 | /* |
| 1195 | * The type of "rfds" is hidden to avoid problems with the function proto. |
| 1196 | */ |
| 1197 | int |
| 1198 | channel_select_setup(int maxfd_in, void *rfds_in) |
| 1199 | { |
| 1200 | int maxfd = maxfd_in; |
| 1201 | int i; |
| 1202 | fd_set *rfds = rfds_in; |
| 1203 | |
| 1204 | for (i = 0; i < channel_count; ++i) |
| 1205 | if (channels[i].ch_fd >= 0) |
| 1206 | { |
| 1207 | FD_SET(channels[i].ch_fd, rfds); |
| 1208 | if (maxfd < channels[i].ch_fd) |
| 1209 | maxfd = channels[i].ch_fd; |
| 1210 | } |
| 1211 | |
| 1212 | return maxfd; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * The type of "rfds" is hidden to avoid problems with the function proto. |
| 1217 | */ |
| 1218 | int |
| 1219 | channel_select_check(int ret_in, void *rfds_in) |
| 1220 | { |
| 1221 | int ret = ret_in; |
| 1222 | int i; |
| 1223 | fd_set *rfds = rfds_in; |
| 1224 | |
| 1225 | for (i = 0; i < channel_count; ++i) |
| 1226 | if (ret > 0 && channels[i].ch_fd >= 0 |
| 1227 | && FD_ISSET(channels[i].ch_fd, rfds)) |
| 1228 | { |
| 1229 | channel_read(i); |
| 1230 | --ret; |
| 1231 | } |
| 1232 | |
| 1233 | return ret; |
| 1234 | } |
Bram Moolenaar | f12d983 | 2016-01-29 21:11:25 +0100 | [diff] [blame] | 1235 | # endif /* !FEAT_GUI_W32 && HAVE_SELECT */ |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1236 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1237 | /* |
| 1238 | * Invoked from the main loop when it's save to execute received commands. |
| 1239 | */ |
| 1240 | void |
| 1241 | channel_parse_messages(void) |
| 1242 | { |
| 1243 | int i; |
| 1244 | |
| 1245 | for (i = 0; i < channel_count; ++i) |
| 1246 | may_invoke_callback(i); |
| 1247 | } |
| 1248 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 1249 | #endif /* FEAT_CHANNEL */ |