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