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