Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 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 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 13 | #ifdef WIN32 |
| 14 | // Must include winsock2.h before windows.h since it conflicts with winsock.h |
| 15 | // (included in windows.h). |
| 16 | # include <winsock2.h> |
| 17 | # include <ws2tcpip.h> |
| 18 | #endif |
| 19 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 20 | #include "vim.h" |
| 21 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 22 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 23 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 24 | // TRUE when netbeans is running with a GUI. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 25 | #ifdef FEAT_GUI |
| 26 | # define CH_HAS_GUI (gui.in_use || gui.starting) |
| 27 | #endif |
| 28 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 29 | // Note: when making changes here also adjust configure.ac. |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 30 | #ifdef MSWIN |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 31 | // WinSock API is separated from C API, thus we can't use read(), write(), |
| 32 | // errno... |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 33 | # define SOCK_ERRNO errno = WSAGetLastError() |
| 34 | # undef ECONNREFUSED |
| 35 | # define ECONNREFUSED WSAECONNREFUSED |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 36 | # undef EWOULDBLOCK |
| 37 | # define EWOULDBLOCK WSAEWOULDBLOCK |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 38 | # undef EINPROGRESS |
| 39 | # define EINPROGRESS WSAEINPROGRESS |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 40 | # ifdef EINTR |
| 41 | # undef EINTR |
| 42 | # endif |
| 43 | # define EINTR WSAEINTR |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 44 | # define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0) |
| 45 | # define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0) |
| 46 | # define sock_close(sd) closesocket((SOCKET)sd) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 47 | #else |
| 48 | # include <netdb.h> |
| 49 | # include <netinet/in.h> |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 50 | # include <arpa/inet.h> |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 51 | # include <sys/socket.h> |
| 52 | # ifdef HAVE_LIBGEN_H |
| 53 | # include <libgen.h> |
| 54 | # endif |
| 55 | # define SOCK_ERRNO |
| 56 | # define sock_write(sd, buf, len) write(sd, buf, len) |
| 57 | # define sock_read(sd, buf, len) read(sd, buf, len) |
| 58 | # define sock_close(sd) close(sd) |
Bram Moolenaar | 0943a09 | 2016-02-16 13:11:17 +0100 | [diff] [blame] | 59 | # define fd_read(fd, buf, len) read(fd, buf, len) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 60 | # define fd_write(sd, buf, len) write(sd, buf, len) |
| 61 | # define fd_close(sd) close(sd) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 62 | #endif |
| 63 | |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 64 | static void channel_read(channel_T *channel, ch_part_T part, char *func); |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 65 | static ch_mode_T channel_get_mode(channel_T *channel, ch_part_T part); |
| 66 | static int channel_get_timeout(channel_T *channel, ch_part_T part); |
| 67 | static ch_part_T channel_part_send(channel_T *channel); |
| 68 | static ch_part_T channel_part_read(channel_T *channel); |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 69 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 70 | #define FOR_ALL_CHANNELS(ch) \ |
| 71 | for ((ch) = first_channel; (ch) != NULL; (ch) = (ch)->ch_next) |
| 72 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 73 | // Whether we are inside channel_parse_messages() or another situation where it |
| 74 | // is safe to invoke callbacks. |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 75 | static int safe_to_invoke_callback = 0; |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 76 | |
Bram Moolenaar | de7eb0a | 2016-05-09 20:54:33 +0200 | [diff] [blame] | 77 | static char *part_names[] = {"sock", "out", "err", "in"}; |
| 78 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 79 | #ifdef MSWIN |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 80 | static int |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 81 | fd_read(sock_T fd, char *buf, size_t len) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 82 | { |
| 83 | HANDLE h = (HANDLE)fd; |
| 84 | DWORD nread; |
| 85 | |
| 86 | if (!ReadFile(h, buf, (DWORD)len, &nread, NULL)) |
| 87 | return -1; |
| 88 | return (int)nread; |
| 89 | } |
| 90 | |
| 91 | static int |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 92 | fd_write(sock_T fd, char *buf, size_t len) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 93 | { |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 94 | size_t todo = len; |
Bram Moolenaar | b091f30 | 2019-01-19 14:37:00 +0100 | [diff] [blame] | 95 | HANDLE h = (HANDLE)fd; |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 96 | DWORD nwrite, size, done = 0; |
Bram Moolenaar | b091f30 | 2019-01-19 14:37:00 +0100 | [diff] [blame] | 97 | OVERLAPPED ov; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 98 | |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 99 | while (todo > 0) |
Bram Moolenaar | b091f30 | 2019-01-19 14:37:00 +0100 | [diff] [blame] | 100 | { |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 101 | if (todo > MAX_NAMED_PIPE_SIZE) |
| 102 | size = MAX_NAMED_PIPE_SIZE; |
| 103 | else |
Bram Moolenaar | dec0120 | 2019-01-28 20:04:24 +0100 | [diff] [blame] | 104 | size = (DWORD)todo; |
Bram Moolenaar | 6524068 | 2019-02-10 22:23:26 +0100 | [diff] [blame] | 105 | // If the pipe overflows while the job does not read the data, |
| 106 | // WriteFile() will block forever. This abandons the write. |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 107 | memset(&ov, 0, sizeof(ov)); |
Bram Moolenaar | 6524068 | 2019-02-10 22:23:26 +0100 | [diff] [blame] | 108 | nwrite = 0; |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 109 | if (!WriteFile(h, buf + done, size, &nwrite, &ov)) |
| 110 | { |
| 111 | DWORD err = GetLastError(); |
Bram Moolenaar | b091f30 | 2019-01-19 14:37:00 +0100 | [diff] [blame] | 112 | |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 113 | if (err != ERROR_IO_PENDING) |
| 114 | return -1; |
| 115 | if (!GetOverlappedResult(h, &ov, &nwrite, FALSE)) |
| 116 | return -1; |
| 117 | FlushFileBuffers(h); |
| 118 | } |
Bram Moolenaar | 6524068 | 2019-02-10 22:23:26 +0100 | [diff] [blame] | 119 | else if (nwrite == 0) |
| 120 | // WriteFile() returns TRUE but did not write anything. This causes |
| 121 | // a hang, so bail out. |
| 122 | break; |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 123 | todo -= nwrite; |
| 124 | done += nwrite; |
Bram Moolenaar | b091f30 | 2019-01-19 14:37:00 +0100 | [diff] [blame] | 125 | } |
Bram Moolenaar | 2405838 | 2019-01-24 23:11:49 +0100 | [diff] [blame] | 126 | return (int)done; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void |
| 130 | fd_close(sock_T fd) |
| 131 | { |
| 132 | HANDLE h = (HANDLE)fd; |
| 133 | |
| 134 | CloseHandle(h); |
| 135 | } |
| 136 | #endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 138 | // Log file opened with ch_logfile(). |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 139 | static FILE *log_fd = NULL; |
Bram Moolenaar | 76603ba | 2020-08-30 17:24:37 +0200 | [diff] [blame] | 140 | static char_u *log_name = NULL; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 141 | #ifdef FEAT_RELTIME |
| 142 | static proftime_T log_start; |
| 143 | #endif |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 144 | |
| 145 | void |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 146 | ch_logfile(char_u *fname, char_u *opt) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 147 | { |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 148 | FILE *file = NULL; |
| 149 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 150 | if (log_fd != NULL) |
Bram Moolenaar | 2e47cab | 2019-09-27 23:12:45 +0200 | [diff] [blame] | 151 | { |
| 152 | if (*fname != NUL) |
Bram Moolenaar | 03a9f84 | 2020-05-13 13:40:16 +0200 | [diff] [blame] | 153 | ch_log(NULL, "closing this logfile, opening %s", fname); |
Bram Moolenaar | 2e47cab | 2019-09-27 23:12:45 +0200 | [diff] [blame] | 154 | else |
Bram Moolenaar | 76603ba | 2020-08-30 17:24:37 +0200 | [diff] [blame] | 155 | ch_log(NULL, "closing logfile %s", log_name); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 156 | fclose(log_fd); |
Bram Moolenaar | 2e47cab | 2019-09-27 23:12:45 +0200 | [diff] [blame] | 157 | } |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 158 | |
| 159 | if (*fname != NUL) |
| 160 | { |
| 161 | file = fopen((char *)fname, *opt == 'w' ? "w" : "a"); |
| 162 | if (file == NULL) |
| 163 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 164 | semsg(_(e_notopen), fname); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 165 | return; |
| 166 | } |
Bram Moolenaar | 76603ba | 2020-08-30 17:24:37 +0200 | [diff] [blame] | 167 | vim_free(log_name); |
| 168 | log_name = vim_strsave(fname); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 169 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 170 | log_fd = file; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 171 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 172 | if (log_fd != NULL) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 173 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 174 | fprintf(log_fd, "==== start log session ====\n"); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 175 | #ifdef FEAT_RELTIME |
| 176 | profile_start(&log_start); |
| 177 | #endif |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | int |
Bram Moolenaar | cf08946 | 2016-06-12 21:18:43 +0200 | [diff] [blame] | 182 | ch_log_active(void) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 183 | { |
| 184 | return log_fd != NULL; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void |
Bram Moolenaar | 4b16ee7 | 2018-08-09 22:15:34 +0200 | [diff] [blame] | 188 | ch_log_lead(const char *what, channel_T *ch, ch_part_T part) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 189 | { |
| 190 | if (log_fd != NULL) |
| 191 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 192 | #ifdef FEAT_RELTIME |
| 193 | proftime_T log_now; |
| 194 | |
| 195 | profile_start(&log_now); |
| 196 | profile_sub(&log_now, &log_start); |
| 197 | fprintf(log_fd, "%s ", profile_msg(&log_now)); |
| 198 | #endif |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 199 | if (ch != NULL) |
Bram Moolenaar | 4b16ee7 | 2018-08-09 22:15:34 +0200 | [diff] [blame] | 200 | { |
| 201 | if (part < PART_COUNT) |
| 202 | fprintf(log_fd, "%son %d(%s): ", |
| 203 | what, ch->ch_id, part_names[part]); |
| 204 | else |
| 205 | fprintf(log_fd, "%son %d: ", what, ch->ch_id); |
| 206 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 207 | else |
| 208 | fprintf(log_fd, "%s: ", what); |
| 209 | } |
| 210 | } |
| 211 | |
Bram Moolenaar | 8caa43d | 2019-02-20 22:45:06 +0100 | [diff] [blame] | 212 | #ifndef PROTO // prototype is in proto.h |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 213 | void |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 214 | ch_log(channel_T *ch, const char *fmt, ...) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 215 | { |
| 216 | if (log_fd != NULL) |
| 217 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 218 | va_list ap; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 219 | |
Bram Moolenaar | 4b16ee7 | 2018-08-09 22:15:34 +0200 | [diff] [blame] | 220 | ch_log_lead("", ch, PART_COUNT); |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 221 | va_start(ap, fmt); |
| 222 | vfprintf(log_fd, fmt, ap); |
| 223 | va_end(ap); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 224 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 225 | fflush(log_fd); |
Bram Moolenaar | 101e992 | 2019-09-25 21:43:11 +0200 | [diff] [blame] | 226 | did_repeated_msg = 0; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 227 | } |
| 228 | } |
Bram Moolenaar | 4ac2e8d | 2018-04-08 12:38:26 +0200 | [diff] [blame] | 229 | #endif |
| 230 | |
| 231 | static void |
| 232 | ch_error(channel_T *ch, const char *fmt, ...) |
Bram Moolenaar | e80757c | 2018-04-10 12:42:44 +0200 | [diff] [blame] | 233 | #ifdef USE_PRINTF_FORMAT_ATTRIBUTE |
| 234 | __attribute__((format(printf, 2, 3))) |
Bram Moolenaar | 4ac2e8d | 2018-04-08 12:38:26 +0200 | [diff] [blame] | 235 | #endif |
| 236 | ; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 237 | |
| 238 | static void |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 239 | ch_error(channel_T *ch, const char *fmt, ...) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 240 | { |
| 241 | if (log_fd != NULL) |
| 242 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 243 | va_list ap; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 244 | |
Bram Moolenaar | 4b16ee7 | 2018-08-09 22:15:34 +0200 | [diff] [blame] | 245 | ch_log_lead("ERR ", ch, PART_COUNT); |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 246 | va_start(ap, fmt); |
| 247 | vfprintf(log_fd, fmt, ap); |
| 248 | va_end(ap); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 249 | fputc('\n', log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 250 | fflush(log_fd); |
Bram Moolenaar | 101e992 | 2019-09-25 21:43:11 +0200 | [diff] [blame] | 251 | did_repeated_msg = 0; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 254 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 255 | #ifdef MSWIN |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 256 | # undef PERROR |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 257 | # define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno)) |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 258 | |
| 259 | static char * |
| 260 | strerror_win32(int eno) |
| 261 | { |
| 262 | static LPVOID msgbuf = NULL; |
| 263 | char_u *ptr; |
| 264 | |
| 265 | if (msgbuf) |
Bram Moolenaar | aad30bb | 2016-06-26 17:31:03 +0200 | [diff] [blame] | 266 | { |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 267 | LocalFree(msgbuf); |
Bram Moolenaar | aad30bb | 2016-06-26 17:31:03 +0200 | [diff] [blame] | 268 | msgbuf = NULL; |
| 269 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 270 | FormatMessage( |
| 271 | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 272 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 273 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 274 | NULL, |
| 275 | eno, |
| 276 | MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), |
| 277 | (LPTSTR) &msgbuf, |
| 278 | 0, |
| 279 | NULL); |
Bram Moolenaar | aad30bb | 2016-06-26 17:31:03 +0200 | [diff] [blame] | 280 | if (msgbuf != NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 281 | // chomp \r or \n |
Bram Moolenaar | aad30bb | 2016-06-26 17:31:03 +0200 | [diff] [blame] | 282 | for (ptr = (char_u *)msgbuf; *ptr; ptr++) |
| 283 | switch (*ptr) |
| 284 | { |
| 285 | case '\r': |
| 286 | STRMOVE(ptr, ptr + 1); |
| 287 | ptr--; |
| 288 | break; |
| 289 | case '\n': |
| 290 | if (*(ptr + 1) == '\0') |
| 291 | *ptr = '\0'; |
| 292 | else |
| 293 | *ptr = ' '; |
| 294 | break; |
| 295 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 296 | return msgbuf; |
| 297 | } |
| 298 | #endif |
| 299 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 300 | /* |
| 301 | * The list of all allocated channels. |
| 302 | */ |
| 303 | static channel_T *first_channel = NULL; |
| 304 | static int next_ch_id = 0; |
| 305 | |
| 306 | /* |
| 307 | * Allocate a new channel. The refcount is set to 1. |
| 308 | * The channel isn't actually used until it is opened. |
| 309 | * Returns NULL if out of memory. |
| 310 | */ |
| 311 | channel_T * |
| 312 | add_channel(void) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 313 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 314 | ch_part_T part; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 315 | channel_T *channel = ALLOC_CLEAR_ONE(channel_T); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 316 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 317 | if (channel == NULL) |
| 318 | return NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 319 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 320 | channel->ch_id = next_ch_id++; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 321 | ch_log(channel, "Created channel"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 322 | |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 323 | for (part = PART_SOCK; part < PART_COUNT; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 324 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 325 | channel->ch_part[part].ch_fd = INVALID_FD; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 326 | #ifdef FEAT_GUI_X11 |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 327 | channel->ch_part[part].ch_inputHandler = (XtInputId)NULL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 328 | #endif |
| 329 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 330 | channel->ch_part[part].ch_inputHandler = 0; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 331 | #endif |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 332 | channel->ch_part[part].ch_timeout = 2000; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 333 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 334 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 335 | if (first_channel != NULL) |
| 336 | { |
| 337 | first_channel->ch_prev = channel; |
| 338 | channel->ch_next = first_channel; |
| 339 | } |
| 340 | first_channel = channel; |
| 341 | |
| 342 | channel->ch_refcount = 1; |
| 343 | return channel; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 344 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 345 | |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 346 | int |
| 347 | has_any_channel(void) |
| 348 | { |
| 349 | return first_channel != NULL; |
| 350 | } |
| 351 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 352 | /* |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 353 | * Called when the refcount of a channel is zero. |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 354 | * Return TRUE if "channel" has a callback and the associated job wasn't |
| 355 | * killed. |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 356 | */ |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 357 | int |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 358 | channel_still_useful(channel_T *channel) |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 359 | { |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 360 | int has_sock_msg; |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 361 | int has_out_msg; |
| 362 | int has_err_msg; |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 363 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 364 | // If the job was killed the channel is not expected to work anymore. |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 365 | if (channel->ch_job_killed && channel->ch_job == NULL) |
| 366 | return FALSE; |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 367 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 368 | // If there is a close callback it may still need to be invoked. |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 369 | if (channel->ch_close_cb.cb_name != NULL) |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 370 | return TRUE; |
| 371 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 372 | // If reading from or a buffer it's still useful. |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 373 | if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL) |
Bram Moolenaar | 5d96e3a | 2016-05-08 21:47:01 +0200 | [diff] [blame] | 374 | return TRUE; |
| 375 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 376 | // If there is no callback then nobody can get readahead. If the fd is |
| 377 | // closed and there is no readahead then the callback won't be called. |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 378 | has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 379 | || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL |
| 380 | || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL; |
Bram Moolenaar | fdd6ce4 | 2016-02-28 22:21:38 +0100 | [diff] [blame] | 381 | has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD |
| 382 | || channel->ch_part[PART_OUT].ch_head.rq_next != NULL |
| 383 | || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL; |
| 384 | has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD |
| 385 | || channel->ch_part[PART_ERR].ch_head.rq_next != NULL |
| 386 | || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL; |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 387 | return (channel->ch_callback.cb_name != NULL && (has_sock_msg |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 388 | || has_out_msg || has_err_msg)) |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 389 | || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 390 | || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL) |
| 391 | && has_out_msg) |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 392 | || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 393 | || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL) |
| 394 | && has_err_msg); |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 398 | * Return TRUE if "channel" is closeable (i.e. all readable fds are closed). |
| 399 | */ |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 400 | int |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 401 | channel_can_close(channel_T *channel) |
| 402 | { |
| 403 | return channel->ch_to_be_closed == 0; |
| 404 | } |
| 405 | |
| 406 | /* |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 407 | * Close a channel and free all its resources. |
Bram Moolenaar | 71658f7 | 2020-03-24 20:35:19 +0100 | [diff] [blame] | 408 | * The "channel" pointer remains valid. |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 409 | */ |
| 410 | static void |
| 411 | channel_free_contents(channel_T *channel) |
| 412 | { |
| 413 | channel_close(channel, TRUE); |
| 414 | channel_clear(channel); |
| 415 | ch_log(channel, "Freeing channel"); |
| 416 | } |
| 417 | |
Bram Moolenaar | 71658f7 | 2020-03-24 20:35:19 +0100 | [diff] [blame] | 418 | /* |
| 419 | * Unlink "channel" from the list of channels and free it. |
| 420 | */ |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 421 | static void |
| 422 | channel_free_channel(channel_T *channel) |
| 423 | { |
| 424 | if (channel->ch_next != NULL) |
| 425 | channel->ch_next->ch_prev = channel->ch_prev; |
| 426 | if (channel->ch_prev == NULL) |
| 427 | first_channel = channel->ch_next; |
| 428 | else |
| 429 | channel->ch_prev->ch_next = channel->ch_next; |
| 430 | vim_free(channel); |
| 431 | } |
| 432 | |
| 433 | static void |
| 434 | channel_free(channel_T *channel) |
| 435 | { |
| 436 | if (!in_free_unref_items) |
| 437 | { |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 438 | if (safe_to_invoke_callback == 0) |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 439 | channel->ch_to_be_freed = TRUE; |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 440 | else |
| 441 | { |
| 442 | channel_free_contents(channel); |
| 443 | channel_free_channel(channel); |
| 444 | } |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | /* |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 449 | * Close a channel and free all its resources if there is no further action |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 450 | * possible, there is no callback to be invoked or the associated job was |
| 451 | * killed. |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 452 | * Return TRUE if the channel was freed. |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 453 | */ |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 454 | static int |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 455 | channel_may_free(channel_T *channel) |
| 456 | { |
Bram Moolenaar | 46c8543 | 2016-02-26 11:17:46 +0100 | [diff] [blame] | 457 | if (!channel_still_useful(channel)) |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 458 | { |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 459 | channel_free(channel); |
Bram Moolenaar | 7076594 | 2016-02-28 19:28:59 +0100 | [diff] [blame] | 460 | return TRUE; |
| 461 | } |
| 462 | return FALSE; |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 466 | * Decrement the reference count on "channel" and maybe free it when it goes |
| 467 | * down to zero. Don't free it if there is a pending action. |
| 468 | * Returns TRUE when the channel is no longer referenced. |
| 469 | */ |
| 470 | int |
| 471 | channel_unref(channel_T *channel) |
| 472 | { |
| 473 | if (channel != NULL && --channel->ch_refcount <= 0) |
| 474 | return channel_may_free(channel); |
| 475 | return FALSE; |
| 476 | } |
| 477 | |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 478 | int |
| 479 | free_unused_channels_contents(int copyID, int mask) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 480 | { |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 481 | int did_free = FALSE; |
| 482 | channel_T *ch; |
| 483 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 484 | // This is invoked from the garbage collector, which only runs at a safe |
| 485 | // point. |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 486 | ++safe_to_invoke_callback; |
| 487 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 488 | FOR_ALL_CHANNELS(ch) |
Bram Moolenaar | 674127e | 2016-04-26 20:30:07 +0200 | [diff] [blame] | 489 | if (!channel_still_useful(ch) |
| 490 | && (ch->ch_copyID & mask) != (copyID & mask)) |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 491 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 492 | // Free the channel and ordinary items it contains, but don't |
| 493 | // recurse into Lists, Dictionaries etc. |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 494 | channel_free_contents(ch); |
| 495 | did_free = TRUE; |
| 496 | } |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 497 | |
| 498 | --safe_to_invoke_callback; |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 499 | return did_free; |
| 500 | } |
| 501 | |
| 502 | void |
| 503 | free_unused_channels(int copyID, int mask) |
| 504 | { |
| 505 | channel_T *ch; |
| 506 | channel_T *ch_next; |
| 507 | |
| 508 | for (ch = first_channel; ch != NULL; ch = ch_next) |
| 509 | { |
| 510 | ch_next = ch->ch_next; |
Bram Moolenaar | 674127e | 2016-04-26 20:30:07 +0200 | [diff] [blame] | 511 | if (!channel_still_useful(ch) |
| 512 | && (ch->ch_copyID & mask) != (copyID & mask)) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 513 | // Free the channel struct itself. |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 514 | channel_free_channel(ch); |
Bram Moolenaar | 107e1ee | 2016-04-08 17:07:19 +0200 | [diff] [blame] | 515 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 516 | } |
| 517 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 518 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 519 | |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 520 | # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
| 521 | /* |
| 522 | * Lookup the channel from the socket. Set "partp" to the fd index. |
| 523 | * Returns NULL when the socket isn't found. |
| 524 | */ |
| 525 | static channel_T * |
| 526 | channel_fd2channel(sock_T fd, ch_part_T *partp) |
| 527 | { |
| 528 | channel_T *channel; |
| 529 | ch_part_T part; |
| 530 | |
| 531 | if (fd != INVALID_FD) |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 532 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 533 | { |
| 534 | for (part = PART_SOCK; part < PART_IN; ++part) |
| 535 | if (channel->ch_part[part].ch_fd == fd) |
| 536 | { |
| 537 | *partp = part; |
| 538 | return channel; |
| 539 | } |
| 540 | } |
| 541 | return NULL; |
| 542 | } |
| 543 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 544 | static void |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 545 | channel_read_fd(int fd) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 546 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 547 | channel_T *channel; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 548 | ch_part_T part; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 549 | |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 550 | channel = channel_fd2channel(fd, &part); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 551 | if (channel == NULL) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 552 | ch_error(NULL, "Channel for fd %d not found", fd); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 553 | else |
Bram Moolenaar | f8df45d | 2016-05-25 21:48:13 +0200 | [diff] [blame] | 554 | channel_read(channel, part, "channel_read_fd"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 555 | } |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 556 | # endif |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 557 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 558 | /* |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 559 | * Read a command from netbeans. |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 560 | */ |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 561 | # ifdef FEAT_GUI_X11 |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 562 | static void |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 563 | messageFromServerX11(XtPointer clientData, |
Bram Moolenaar | f8df45d | 2016-05-25 21:48:13 +0200 | [diff] [blame] | 564 | int *unused1 UNUSED, |
| 565 | XtInputId *unused2 UNUSED) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 566 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 567 | channel_read_fd((int)(long)clientData); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 568 | } |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 569 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 570 | |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 571 | # ifdef FEAT_GUI_GTK |
| 572 | # if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 573 | static gboolean |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 574 | messageFromServerGtk3(GIOChannel *unused1 UNUSED, |
Bram Moolenaar | f8df45d | 2016-05-25 21:48:13 +0200 | [diff] [blame] | 575 | GIOCondition unused2 UNUSED, |
| 576 | gpointer clientData) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 577 | { |
| 578 | channel_read_fd(GPOINTER_TO_INT(clientData)); |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 579 | return TRUE; // Return FALSE instead in case the event source is to |
| 580 | // be removed after this function returns. |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 581 | } |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 582 | # else |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 583 | static void |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 584 | messageFromServerGtk2(gpointer clientData, |
Bram Moolenaar | f8df45d | 2016-05-25 21:48:13 +0200 | [diff] [blame] | 585 | gint unused1 UNUSED, |
| 586 | GdkInputCondition unused2 UNUSED) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 587 | { |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 588 | channel_read_fd((int)(long)clientData); |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 589 | } |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 590 | # endif |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 591 | # endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 592 | |
| 593 | static void |
Bram Moolenaar | bd67aac | 2019-09-21 23:09:04 +0200 | [diff] [blame] | 594 | channel_gui_register_one(channel_T *channel, ch_part_T part UNUSED) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 595 | { |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 596 | if (!CH_HAS_GUI) |
| 597 | return; |
| 598 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 599 | // gets stuck in handling events for a not connected channel |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 600 | if (channel->ch_keep_open) |
| 601 | return; |
| 602 | |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 603 | # ifdef FEAT_GUI_X11 |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 604 | // Tell notifier we are interested in being called when there is input on |
| 605 | // the editor connection socket. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 606 | if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL) |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 607 | { |
| 608 | ch_log(channel, "Registering part %s with fd %d", |
| 609 | part_names[part], channel->ch_part[part].ch_fd); |
| 610 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 611 | channel->ch_part[part].ch_inputHandler = XtAppAddInput( |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 612 | (XtAppContext)app_context, |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 613 | channel->ch_part[part].ch_fd, |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 614 | (XtPointer)(XtInputReadMask + XtInputExceptMask), |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 615 | messageFromServerX11, |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 616 | (XtPointer)(long)channel->ch_part[part].ch_fd); |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 617 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 618 | # else |
| 619 | # ifdef FEAT_GUI_GTK |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 620 | // Tell gdk we are interested in being called when there is input on the |
| 621 | // editor connection socket. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 622 | if (channel->ch_part[part].ch_inputHandler == 0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 623 | { |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 624 | ch_log(channel, "Registering part %s with fd %d", |
| 625 | part_names[part], channel->ch_part[part].ch_fd); |
| 626 | # if GTK_CHECK_VERSION(3,0,0) |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 627 | GIOChannel *chnnl = g_io_channel_unix_new( |
| 628 | (gint)channel->ch_part[part].ch_fd); |
| 629 | |
| 630 | channel->ch_part[part].ch_inputHandler = g_io_add_watch( |
| 631 | chnnl, |
| 632 | G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI, |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 633 | messageFromServerGtk3, |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 634 | GINT_TO_POINTER(channel->ch_part[part].ch_fd)); |
| 635 | |
| 636 | g_io_channel_unref(chnnl); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 637 | # else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 638 | channel->ch_part[part].ch_inputHandler = gdk_input_add( |
| 639 | (gint)channel->ch_part[part].ch_fd, |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 640 | (GdkInputCondition) |
| 641 | ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION), |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 642 | messageFromServerGtk2, |
Bram Moolenaar | fffd556 | 2016-02-20 18:44:39 +0100 | [diff] [blame] | 643 | (gpointer)(long)channel->ch_part[part].ch_fd); |
Bram Moolenaar | 9892189 | 2016-02-23 17:14:37 +0100 | [diff] [blame] | 644 | # endif |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 645 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 646 | # endif |
| 647 | # endif |
| 648 | } |
| 649 | |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 650 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 651 | channel_gui_register(channel_T *channel) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 652 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 653 | if (channel->CH_SOCK_FD != INVALID_FD) |
| 654 | channel_gui_register_one(channel, PART_SOCK); |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 655 | if (channel->CH_OUT_FD != INVALID_FD |
| 656 | && channel->CH_OUT_FD != channel->CH_SOCK_FD) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 657 | channel_gui_register_one(channel, PART_OUT); |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 658 | if (channel->CH_ERR_FD != INVALID_FD |
| 659 | && channel->CH_ERR_FD != channel->CH_SOCK_FD |
| 660 | && channel->CH_ERR_FD != channel->CH_OUT_FD) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 661 | channel_gui_register_one(channel, PART_ERR); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Register any of our file descriptors with the GUI event handling system. |
| 666 | * Called when the GUI has started. |
| 667 | */ |
| 668 | void |
| 669 | channel_gui_register_all(void) |
| 670 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 671 | channel_T *channel; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 672 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 673 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 674 | channel_gui_register(channel); |
| 675 | } |
| 676 | |
| 677 | static void |
Bram Moolenaar | bd67aac | 2019-09-21 23:09:04 +0200 | [diff] [blame] | 678 | channel_gui_unregister_one(channel_T *channel UNUSED, ch_part_T part UNUSED) |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 679 | { |
| 680 | # ifdef FEAT_GUI_X11 |
| 681 | if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL) |
| 682 | { |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 683 | ch_log(channel, "Unregistering part %s", part_names[part]); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 684 | XtRemoveInput(channel->ch_part[part].ch_inputHandler); |
| 685 | channel->ch_part[part].ch_inputHandler = (XtInputId)NULL; |
| 686 | } |
| 687 | # else |
| 688 | # ifdef FEAT_GUI_GTK |
| 689 | if (channel->ch_part[part].ch_inputHandler != 0) |
| 690 | { |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 691 | ch_log(channel, "Unregistering part %s", part_names[part]); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 692 | # if GTK_CHECK_VERSION(3,0,0) |
| 693 | g_source_remove(channel->ch_part[part].ch_inputHandler); |
| 694 | # else |
| 695 | gdk_input_remove(channel->ch_part[part].ch_inputHandler); |
| 696 | # endif |
| 697 | channel->ch_part[part].ch_inputHandler = 0; |
| 698 | } |
| 699 | # endif |
| 700 | # endif |
| 701 | } |
| 702 | |
| 703 | static void |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 704 | channel_gui_unregister(channel_T *channel) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 705 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 706 | ch_part_T part; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 707 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 708 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 709 | channel_gui_unregister_one(channel, part); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Bram Moolenaar | ea78145 | 2019-09-04 18:53:12 +0200 | [diff] [blame] | 712 | #endif // FEAT_GUI |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 713 | |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 714 | static char *e_cannot_connect = N_("E902: Cannot connect to port"); |
| 715 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 716 | /* |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 717 | * For Unix we need to call connect() again after connect() failed. |
| 718 | * On Win32 one time is sufficient. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 719 | */ |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 720 | static int |
| 721 | channel_connect( |
| 722 | channel_T *channel, |
| 723 | const struct sockaddr *server_addr, |
| 724 | int server_addrlen, |
| 725 | int *waittime) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 726 | { |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 727 | int sd = -1; |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 728 | #ifdef MSWIN |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 729 | u_long val = 1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 730 | #endif |
| 731 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 732 | while (TRUE) |
| 733 | { |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 734 | long elapsed_msec = 0; |
| 735 | int waitnow; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 736 | int ret; |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 737 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 738 | if (sd >= 0) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 739 | sock_close(sd); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 740 | sd = socket(server_addr->sa_family, SOCK_STREAM, 0); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 741 | if (sd == -1) |
| 742 | { |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 743 | ch_error(channel, "in socket() in channel_connect()."); |
| 744 | PERROR(_("E898: socket() in channel_connect()")); |
| 745 | return -1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 746 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 747 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 748 | if (*waittime >= 0) |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 749 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 750 | // Make connect() non-blocking. |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 751 | if ( |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 752 | #ifdef MSWIN |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 753 | ioctlsocket(sd, FIONBIO, &val) < 0 |
| 754 | #else |
| 755 | fcntl(sd, F_SETFL, O_NONBLOCK) < 0 |
| 756 | #endif |
| 757 | ) |
| 758 | { |
| 759 | SOCK_ERRNO; |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 760 | ch_error(channel, |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 761 | "channel_connect: Connect failed with errno %d", errno); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 762 | sock_close(sd); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 763 | return -1; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 767 | // Try connecting to the server. |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 768 | ch_log(channel, "Connecting..."); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 769 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 770 | ret = connect(sd, server_addr, server_addrlen); |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 771 | if (ret == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 772 | // The connection could be established. |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 773 | break; |
| 774 | |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 775 | SOCK_ERRNO; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 776 | if (*waittime < 0 || (errno != EWOULDBLOCK |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 777 | && errno != ECONNREFUSED |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 778 | #ifdef EINPROGRESS |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 779 | && errno != EINPROGRESS |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 780 | #endif |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 781 | )) |
| 782 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 783 | ch_error(channel, |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 784 | "channel_connect: Connect failed with errno %d", errno); |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 785 | PERROR(_(e_cannot_connect)); |
| 786 | sock_close(sd); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 787 | return -1; |
| 788 | } |
| 789 | else if (errno == ECONNREFUSED) |
| 790 | { |
| 791 | ch_error(channel, "channel_connect: Connection refused"); |
| 792 | sock_close(sd); |
| 793 | return -1; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 794 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 795 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 796 | // Limit the waittime to 50 msec. If it doesn't work within this |
| 797 | // time we close the socket and try creating it again. |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 798 | waitnow = *waittime > 50 ? 50 : *waittime; |
Bram Moolenaar | 40e8cb2 | 2016-03-10 21:10:58 +0100 | [diff] [blame] | 799 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 800 | // If connect() didn't finish then try using select() to wait for the |
| 801 | // connection to be made. For Win32 always use select() to wait. |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 802 | { |
| 803 | struct timeval tv; |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 804 | fd_set rfds; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 805 | fd_set wfds; |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 806 | #ifndef MSWIN |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 807 | int so_error = 0; |
| 808 | socklen_t so_error_len = sizeof(so_error); |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 809 | struct timeval start_tv; |
| 810 | struct timeval end_tv; |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 811 | #endif |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 812 | FD_ZERO(&rfds); |
| 813 | FD_SET(sd, &rfds); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 814 | FD_ZERO(&wfds); |
| 815 | FD_SET(sd, &wfds); |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 816 | |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 817 | tv.tv_sec = waitnow / 1000; |
| 818 | tv.tv_usec = (waitnow % 1000) * 1000; |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 819 | #ifndef MSWIN |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 820 | gettimeofday(&start_tv, NULL); |
| 821 | #endif |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 822 | ch_log(channel, |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 823 | "Waiting for connection (waiting %d msec)...", waitnow); |
Bram Moolenaar | e74e8e7 | 2016-02-16 22:01:30 +0100 | [diff] [blame] | 824 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 825 | ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 826 | if (ret < 0) |
| 827 | { |
| 828 | SOCK_ERRNO; |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 829 | ch_error(channel, |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 830 | "channel_connect: Connect failed with errno %d", errno); |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 831 | PERROR(_(e_cannot_connect)); |
| 832 | sock_close(sd); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 833 | return -1; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 834 | } |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 835 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 836 | #ifdef MSWIN |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 837 | // On Win32: select() is expected to work and wait for up to |
| 838 | // "waitnow" msec for the socket to be open. |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 839 | if (FD_ISSET(sd, &wfds)) |
| 840 | break; |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 841 | elapsed_msec = waitnow; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 842 | if (*waittime > 1 && elapsed_msec < *waittime) |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 843 | { |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 844 | *waittime -= elapsed_msec; |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 845 | continue; |
| 846 | } |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 847 | #else |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 848 | // On Linux-like systems: See socket(7) for the behavior |
| 849 | // After putting the socket in non-blocking mode, connect() will |
| 850 | // return EINPROGRESS, select() will not wait (as if writing is |
| 851 | // possible), need to use getsockopt() to check if the socket is |
| 852 | // actually able to connect. |
| 853 | // We detect a failure to connect when either read and write fds |
| 854 | // are set. Use getsockopt() to find out what kind of failure. |
Bram Moolenaar | 42bc6dd | 2016-03-02 20:48:47 +0100 | [diff] [blame] | 855 | if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds)) |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 856 | { |
| 857 | ret = getsockopt(sd, |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 858 | SOL_SOCKET, SO_ERROR, &so_error, &so_error_len); |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 859 | if (ret < 0 || (so_error != 0 |
| 860 | && so_error != EWOULDBLOCK |
| 861 | && so_error != ECONNREFUSED |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 862 | # ifdef EINPROGRESS |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 863 | && so_error != EINPROGRESS |
Bram Moolenaar | e081e21 | 2016-02-28 22:33:46 +0100 | [diff] [blame] | 864 | # endif |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 865 | )) |
| 866 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 867 | ch_error(channel, |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 868 | "channel_connect: Connect failed with errno %d", |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 869 | so_error); |
| 870 | PERROR(_(e_cannot_connect)); |
| 871 | sock_close(sd); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 872 | return -1; |
| 873 | } |
| 874 | else if (errno == ECONNREFUSED) |
| 875 | { |
| 876 | ch_error(channel, "channel_connect: Connection refused"); |
| 877 | sock_close(sd); |
| 878 | return -1; |
Bram Moolenaar | d42119f | 2016-02-28 20:51:49 +0100 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 882 | if (FD_ISSET(sd, &wfds) && so_error == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 883 | // Did not detect an error, connection is established. |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 884 | break; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 885 | |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 886 | gettimeofday(&end_tv, NULL); |
| 887 | elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000 |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 888 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 889 | #endif |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 890 | } |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 891 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 892 | #ifndef MSWIN |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 893 | if (*waittime > 1 && elapsed_msec < *waittime) |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 894 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 895 | // The port isn't ready but we also didn't get an error. |
| 896 | // This happens when the server didn't open the socket |
| 897 | // yet. Select() may return early, wait until the remaining |
| 898 | // "waitnow" and try again. |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 899 | waitnow -= elapsed_msec; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 900 | *waittime -= elapsed_msec; |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 901 | if (waitnow > 0) |
| 902 | { |
Bram Moolenaar | 0981c87 | 2020-08-23 14:28:37 +0200 | [diff] [blame] | 903 | mch_delay((long)waitnow, MCH_DELAY_IGNOREINPUT); |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 904 | ui_breakcheck(); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 905 | *waittime -= waitnow; |
Bram Moolenaar | 562ca71 | 2016-03-09 21:50:05 +0100 | [diff] [blame] | 906 | } |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 907 | if (!got_int) |
| 908 | { |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 909 | if (*waittime <= 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 910 | // give it one more try |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 911 | *waittime = 1; |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 912 | continue; |
| 913 | } |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 914 | // we were interrupted, behave as if timed out |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 915 | } |
| 916 | #endif |
| 917 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 918 | // We timed out. |
Bram Moolenaar | 045a284 | 2016-03-08 22:33:07 +0100 | [diff] [blame] | 919 | ch_error(channel, "Connection timed out"); |
| 920 | sock_close(sd); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 921 | return -1; |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 922 | } |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 923 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 924 | if (*waittime >= 0) |
Bram Moolenaar | 7a84dbe | 2016-02-07 21:29:00 +0100 | [diff] [blame] | 925 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 926 | #ifdef MSWIN |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 927 | val = 0; |
| 928 | ioctlsocket(sd, FIONBIO, &val); |
| 929 | #else |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 930 | (void)fcntl(sd, F_SETFL, 0); |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 931 | #endif |
| 932 | } |
| 933 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 934 | return sd; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Open a socket channel to "hostname":"port". |
| 939 | * "waittime" is the time in msec to wait for the connection. |
| 940 | * When negative wait forever. |
| 941 | * Returns the channel for success. |
| 942 | * Returns NULL for failure. |
| 943 | */ |
| 944 | channel_T * |
| 945 | channel_open( |
| 946 | const char *hostname, |
| 947 | int port, |
| 948 | int waittime, |
| 949 | void (*nb_close_cb)(void)) |
| 950 | { |
| 951 | int sd = -1; |
| 952 | channel_T *channel = NULL; |
| 953 | #ifdef FEAT_IPV6 |
Bram Moolenaar | a38b2b7 | 2020-05-03 17:03:29 +0200 | [diff] [blame] | 954 | int err; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 955 | struct addrinfo hints; |
| 956 | struct addrinfo *res = NULL; |
| 957 | struct addrinfo *addr = NULL; |
| 958 | #else |
| 959 | struct sockaddr_in server; |
| 960 | struct hostent *host = NULL; |
| 961 | #endif |
| 962 | |
| 963 | #ifdef MSWIN |
| 964 | channel_init_winsock(); |
| 965 | #endif |
| 966 | |
| 967 | channel = add_channel(); |
| 968 | if (channel == NULL) |
| 969 | { |
| 970 | ch_error(NULL, "Cannot allocate channel."); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | // Get the server internet address and put into addr structure fill in the |
| 975 | // socket address structure and connect to server. |
| 976 | #ifdef FEAT_IPV6 |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 977 | CLEAR_FIELD(hints); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 978 | hints.ai_family = AF_UNSPEC; |
| 979 | hints.ai_socktype = SOCK_STREAM; |
Bram Moolenaar | c6a67c9 | 2020-09-11 19:28:19 +0200 | [diff] [blame] | 980 | # if defined(AI_ADDRCONFIG) && defined(AI_V4MAPPED) |
| 981 | hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 982 | # endif |
| 983 | // Set port number manually in order to prevent name resolution services |
| 984 | // from being invoked in the environment where AI_NUMERICSERV is not |
| 985 | // defined. |
Bram Moolenaar | a38b2b7 | 2020-05-03 17:03:29 +0200 | [diff] [blame] | 986 | if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 987 | { |
| 988 | ch_error(channel, "in getaddrinfo() in channel_open()"); |
Bram Moolenaar | a38b2b7 | 2020-05-03 17:03:29 +0200 | [diff] [blame] | 989 | semsg(_("E901: getaddrinfo() in channel_open(): %s"), |
| 990 | gai_strerror(err)); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 991 | channel_free(channel); |
| 992 | return NULL; |
| 993 | } |
| 994 | |
| 995 | for (addr = res; addr != NULL; addr = addr->ai_next) |
| 996 | { |
Bram Moolenaar | b6fb051 | 2020-04-18 18:24:18 +0200 | [diff] [blame] | 997 | const char *dst = hostname; |
Bram Moolenaar | b6fb051 | 2020-04-18 18:24:18 +0200 | [diff] [blame] | 998 | # ifdef HAVE_INET_NTOP |
Bram Moolenaar | b60db8b | 2020-05-29 21:38:42 +0200 | [diff] [blame] | 999 | const void *src = NULL; |
Bram Moolenaar | b6fb051 | 2020-04-18 18:24:18 +0200 | [diff] [blame] | 1000 | char buf[NUMBUFLEN]; |
Bram Moolenaar | 208534d | 2020-05-30 13:07:39 +0200 | [diff] [blame] | 1001 | # endif |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1002 | |
| 1003 | if (addr->ai_family == AF_INET6) |
| 1004 | { |
| 1005 | struct sockaddr_in6 *sai = (struct sockaddr_in6 *)addr->ai_addr; |
| 1006 | |
| 1007 | sai->sin6_port = htons(port); |
Bram Moolenaar | 208534d | 2020-05-30 13:07:39 +0200 | [diff] [blame] | 1008 | # ifdef HAVE_INET_NTOP |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1009 | src = &sai->sin6_addr; |
Bram Moolenaar | 208534d | 2020-05-30 13:07:39 +0200 | [diff] [blame] | 1010 | # endif |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1011 | } |
| 1012 | else if (addr->ai_family == AF_INET) |
| 1013 | { |
| 1014 | struct sockaddr_in *sai = (struct sockaddr_in *)addr->ai_addr; |
| 1015 | |
| 1016 | sai->sin_port = htons(port); |
Bram Moolenaar | 208534d | 2020-05-30 13:07:39 +0200 | [diff] [blame] | 1017 | # ifdef HAVE_INET_NTOP |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1018 | src = &sai->sin_addr; |
Bram Moolenaar | 208534d | 2020-05-30 13:07:39 +0200 | [diff] [blame] | 1019 | #endif |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1020 | } |
Bram Moolenaar | 208534d | 2020-05-30 13:07:39 +0200 | [diff] [blame] | 1021 | # ifdef HAVE_INET_NTOP |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1022 | if (src != NULL) |
| 1023 | { |
| 1024 | dst = inet_ntop(addr->ai_family, src, buf, sizeof(buf)); |
Bram Moolenaar | b6fb051 | 2020-04-18 18:24:18 +0200 | [diff] [blame] | 1025 | if (dst == NULL) |
| 1026 | dst = hostname; |
| 1027 | else if (STRCMP(hostname, dst) != 0) |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1028 | ch_log(channel, "Resolved %s to %s", hostname, dst); |
| 1029 | } |
Bram Moolenaar | b6fb051 | 2020-04-18 18:24:18 +0200 | [diff] [blame] | 1030 | # endif |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1031 | |
| 1032 | ch_log(channel, "Trying to connect to %s port %d", dst, port); |
| 1033 | |
| 1034 | // On Mac and Solaris a zero timeout almost never works. At least wait |
| 1035 | // one millisecond. Let's do it for all systems, because we don't know |
| 1036 | // why this is needed. |
| 1037 | if (waittime == 0) |
| 1038 | waittime = 1; |
| 1039 | |
Bram Moolenaar | ea554ca | 2020-05-07 17:46:59 +0200 | [diff] [blame] | 1040 | sd = channel_connect(channel, addr->ai_addr, (int)addr->ai_addrlen, |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1041 | &waittime); |
| 1042 | if (sd >= 0) |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | freeaddrinfo(res); |
| 1047 | #else |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 1048 | CLEAR_FIELD(server); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1049 | server.sin_family = AF_INET; |
| 1050 | server.sin_port = htons(port); |
| 1051 | if ((host = gethostbyname(hostname)) == NULL) |
| 1052 | { |
| 1053 | ch_error(channel, "in gethostbyname() in channel_open()"); |
| 1054 | PERROR(_("E901: gethostbyname() in channel_open()")); |
| 1055 | channel_free(channel); |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | { |
| 1059 | char *p; |
| 1060 | |
| 1061 | // When using host->h_addr_list[0] directly ubsan warns for it to not |
| 1062 | // be aligned. First copy the pointer to avoid that. |
| 1063 | memcpy(&p, &host->h_addr_list[0], sizeof(p)); |
| 1064 | memcpy((char *)&server.sin_addr, p, host->h_length); |
| 1065 | } |
| 1066 | |
| 1067 | ch_log(channel, "Trying to connect to %s port %d", hostname, port); |
| 1068 | |
| 1069 | // On Mac and Solaris a zero timeout almost never works. At least wait one |
| 1070 | // millisecond. Let's do it for all systems, because we don't know why |
| 1071 | // this is needed. |
| 1072 | if (waittime == 0) |
| 1073 | waittime = 1; |
| 1074 | |
| 1075 | sd = channel_connect(channel, (struct sockaddr *)&server, sizeof(server), |
| 1076 | &waittime); |
| 1077 | #endif |
| 1078 | |
| 1079 | if (sd < 0) |
| 1080 | { |
| 1081 | channel_free(channel); |
| 1082 | return NULL; |
| 1083 | } |
| 1084 | |
| 1085 | ch_log(channel, "Connection made"); |
| 1086 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1087 | channel->CH_SOCK_FD = (sock_T)sd; |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 1088 | channel->ch_nb_close_cb = nb_close_cb; |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 1089 | channel->ch_hostname = (char *)vim_strsave((char_u *)hostname); |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1090 | channel->ch_port = port; |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 1091 | channel->ch_to_be_closed |= (1U << PART_SOCK); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1092 | |
| 1093 | #ifdef FEAT_GUI |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1094 | channel_gui_register_one(channel, PART_SOCK); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1095 | #endif |
| 1096 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1097 | return channel; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1100 | static void |
| 1101 | free_set_callback(callback_T *cbp, callback_T *callback) |
| 1102 | { |
| 1103 | free_callback(cbp); |
| 1104 | |
| 1105 | if (callback->cb_name != NULL && *callback->cb_name != NUL) |
| 1106 | copy_callback(cbp, callback); |
| 1107 | else |
| 1108 | cbp->cb_name = NULL; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Prepare buffer "buf" for writing channel output to. |
| 1113 | */ |
| 1114 | static void |
| 1115 | prepare_buffer(buf_T *buf) |
| 1116 | { |
| 1117 | buf_T *save_curbuf = curbuf; |
| 1118 | |
| 1119 | buf_copy_options(buf, BCO_ENTER); |
| 1120 | curbuf = buf; |
| 1121 | #ifdef FEAT_QUICKFIX |
| 1122 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
| 1123 | set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL); |
| 1124 | #endif |
| 1125 | if (curbuf->b_ml.ml_mfp == NULL) |
| 1126 | ml_open(curbuf); |
| 1127 | curbuf = save_curbuf; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Find a buffer matching "name" or create a new one. |
| 1132 | * Returns NULL if there is something very wrong (error already reported). |
| 1133 | */ |
| 1134 | static buf_T * |
Bram Moolenaar | 261f346 | 2019-09-07 15:45:32 +0200 | [diff] [blame] | 1135 | channel_find_buffer(char_u *name, int err, int msg) |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1136 | { |
| 1137 | buf_T *buf = NULL; |
| 1138 | buf_T *save_curbuf = curbuf; |
| 1139 | |
| 1140 | if (name != NULL && *name != NUL) |
| 1141 | { |
| 1142 | buf = buflist_findname(name); |
| 1143 | if (buf == NULL) |
| 1144 | buf = buflist_findname_exp(name); |
| 1145 | } |
| 1146 | if (buf == NULL) |
| 1147 | { |
| 1148 | buf = buflist_new(name == NULL || *name == NUL ? NULL : name, |
| 1149 | NULL, (linenr_T)0, BLN_LISTED | BLN_NEW); |
| 1150 | if (buf == NULL) |
| 1151 | return NULL; |
| 1152 | prepare_buffer(buf); |
| 1153 | |
| 1154 | curbuf = buf; |
| 1155 | if (msg) |
| 1156 | ml_replace(1, (char_u *)(err ? "Reading from channel error..." |
| 1157 | : "Reading from channel output..."), TRUE); |
| 1158 | changed_bytes(1, 0); |
| 1159 | curbuf = save_curbuf; |
| 1160 | } |
| 1161 | |
| 1162 | return buf; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Set various properties from an "opt" argument. |
| 1167 | */ |
| 1168 | static void |
| 1169 | channel_set_options(channel_T *channel, jobopt_T *opt) |
| 1170 | { |
| 1171 | ch_part_T part; |
| 1172 | |
| 1173 | if (opt->jo_set & JO_MODE) |
| 1174 | for (part = PART_SOCK; part < PART_COUNT; ++part) |
| 1175 | channel->ch_part[part].ch_mode = opt->jo_mode; |
| 1176 | if (opt->jo_set & JO_IN_MODE) |
| 1177 | channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode; |
| 1178 | if (opt->jo_set & JO_OUT_MODE) |
| 1179 | channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode; |
| 1180 | if (opt->jo_set & JO_ERR_MODE) |
| 1181 | channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode; |
| 1182 | channel->ch_nonblock = opt->jo_noblock; |
| 1183 | |
| 1184 | if (opt->jo_set & JO_TIMEOUT) |
| 1185 | for (part = PART_SOCK; part < PART_COUNT; ++part) |
| 1186 | channel->ch_part[part].ch_timeout = opt->jo_timeout; |
| 1187 | if (opt->jo_set & JO_OUT_TIMEOUT) |
| 1188 | channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout; |
| 1189 | if (opt->jo_set & JO_ERR_TIMEOUT) |
| 1190 | channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout; |
| 1191 | if (opt->jo_set & JO_BLOCK_WRITE) |
| 1192 | channel->ch_part[PART_IN].ch_block_write = 1; |
| 1193 | |
| 1194 | if (opt->jo_set & JO_CALLBACK) |
| 1195 | free_set_callback(&channel->ch_callback, &opt->jo_callback); |
| 1196 | if (opt->jo_set & JO_OUT_CALLBACK) |
| 1197 | free_set_callback(&channel->ch_part[PART_OUT].ch_callback, |
| 1198 | &opt->jo_out_cb); |
| 1199 | if (opt->jo_set & JO_ERR_CALLBACK) |
| 1200 | free_set_callback(&channel->ch_part[PART_ERR].ch_callback, |
| 1201 | &opt->jo_err_cb); |
| 1202 | if (opt->jo_set & JO_CLOSE_CALLBACK) |
| 1203 | free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb); |
| 1204 | channel->ch_drop_never = opt->jo_drop_never; |
| 1205 | |
| 1206 | if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER) |
| 1207 | { |
| 1208 | buf_T *buf; |
| 1209 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1210 | // writing output to a buffer. Default mode is NL. |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1211 | if (!(opt->jo_set & JO_OUT_MODE)) |
| 1212 | channel->ch_part[PART_OUT].ch_mode = MODE_NL; |
| 1213 | if (opt->jo_set & JO_OUT_BUF) |
| 1214 | { |
| 1215 | buf = buflist_findnr(opt->jo_io_buf[PART_OUT]); |
| 1216 | if (buf == NULL) |
| 1217 | semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]); |
| 1218 | } |
| 1219 | else |
| 1220 | { |
| 1221 | int msg = TRUE; |
| 1222 | |
| 1223 | if (opt->jo_set2 & JO2_OUT_MSG) |
| 1224 | msg = opt->jo_message[PART_OUT]; |
Bram Moolenaar | 261f346 | 2019-09-07 15:45:32 +0200 | [diff] [blame] | 1225 | buf = channel_find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg); |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1226 | } |
| 1227 | if (buf != NULL) |
| 1228 | { |
| 1229 | if (opt->jo_set & JO_OUT_MODIFIABLE) |
| 1230 | channel->ch_part[PART_OUT].ch_nomodifiable = |
| 1231 | !opt->jo_modifiable[PART_OUT]; |
| 1232 | |
| 1233 | if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable) |
| 1234 | { |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 1235 | emsg(_(e_cannot_make_changes_modifiable_is_off)); |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1236 | } |
| 1237 | else |
| 1238 | { |
| 1239 | ch_log(channel, "writing out to buffer '%s'", |
| 1240 | (char *)buf->b_ffname); |
| 1241 | set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf); |
| 1242 | // if the buffer was deleted or unloaded resurrect it |
| 1243 | if (buf->b_ml.ml_mfp == NULL) |
| 1244 | prepare_buffer(buf); |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER |
| 1250 | || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO) |
| 1251 | && opt->jo_io[PART_OUT] == JIO_BUFFER))) |
| 1252 | { |
| 1253 | buf_T *buf; |
| 1254 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1255 | // writing err to a buffer. Default mode is NL. |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1256 | if (!(opt->jo_set & JO_ERR_MODE)) |
| 1257 | channel->ch_part[PART_ERR].ch_mode = MODE_NL; |
| 1258 | if (opt->jo_io[PART_ERR] == JIO_OUT) |
| 1259 | buf = channel->ch_part[PART_OUT].ch_bufref.br_buf; |
| 1260 | else if (opt->jo_set & JO_ERR_BUF) |
| 1261 | { |
| 1262 | buf = buflist_findnr(opt->jo_io_buf[PART_ERR]); |
| 1263 | if (buf == NULL) |
| 1264 | semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]); |
| 1265 | } |
| 1266 | else |
| 1267 | { |
| 1268 | int msg = TRUE; |
| 1269 | |
| 1270 | if (opt->jo_set2 & JO2_ERR_MSG) |
| 1271 | msg = opt->jo_message[PART_ERR]; |
Bram Moolenaar | 261f346 | 2019-09-07 15:45:32 +0200 | [diff] [blame] | 1272 | buf = channel_find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg); |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1273 | } |
| 1274 | if (buf != NULL) |
| 1275 | { |
| 1276 | if (opt->jo_set & JO_ERR_MODIFIABLE) |
| 1277 | channel->ch_part[PART_ERR].ch_nomodifiable = |
| 1278 | !opt->jo_modifiable[PART_ERR]; |
| 1279 | if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable) |
| 1280 | { |
Bram Moolenaar | 108010a | 2021-06-27 22:03:33 +0200 | [diff] [blame] | 1281 | emsg(_(e_cannot_make_changes_modifiable_is_off)); |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 1282 | } |
| 1283 | else |
| 1284 | { |
| 1285 | ch_log(channel, "writing err to buffer '%s'", |
| 1286 | (char *)buf->b_ffname); |
| 1287 | set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf); |
| 1288 | // if the buffer was deleted or unloaded resurrect it |
| 1289 | if (buf->b_ml.ml_mfp == NULL) |
| 1290 | prepare_buffer(buf); |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT]; |
| 1296 | channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR]; |
| 1297 | channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN]; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1301 | * Implements ch_open(). |
| 1302 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1303 | static channel_T * |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1304 | channel_open_func(typval_T *argvars) |
| 1305 | { |
| 1306 | char_u *address; |
| 1307 | char_u *p; |
| 1308 | char *rest; |
| 1309 | int port; |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1310 | int is_ipv6 = FALSE; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1311 | jobopt_T opt; |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 1312 | channel_T *channel = NULL; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1313 | |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 1314 | address = tv_get_string(&argvars[0]); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1315 | if (argvars[1].v_type != VAR_UNKNOWN |
| 1316 | && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)) |
| 1317 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1318 | emsg(_(e_invarg)); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1319 | return NULL; |
| 1320 | } |
| 1321 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1322 | // parse address |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1323 | if (*address == '[') |
| 1324 | { |
| 1325 | // ipv6 address |
| 1326 | is_ipv6 = TRUE; |
| 1327 | p = vim_strchr(address + 1, ']'); |
| 1328 | if (p == NULL || *++p != ':') |
| 1329 | { |
| 1330 | semsg(_(e_invarg2), address); |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | } |
| 1334 | else |
| 1335 | { |
| 1336 | p = vim_strchr(address, ':'); |
| 1337 | if (p == NULL) |
| 1338 | { |
| 1339 | semsg(_(e_invarg2), address); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | } |
| 1343 | port = strtol((char *)(p + 1), &rest, 10); |
| 1344 | if (*address == NUL || port <= 0 || port >= 65536 || *rest != NUL) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1345 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1346 | semsg(_(e_invarg2), address); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1347 | return NULL; |
| 1348 | } |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1349 | if (is_ipv6) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1350 | { |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1351 | // strip '[' and ']' |
| 1352 | ++address; |
| 1353 | *(p - 1) = NUL; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1354 | } |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 1355 | else |
| 1356 | *p = NUL; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1357 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1358 | // parse options |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1359 | clear_job_options(&opt); |
| 1360 | opt.jo_mode = MODE_JSON; |
| 1361 | opt.jo_timeout = 2000; |
| 1362 | if (get_job_options(&argvars[1], &opt, |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 1363 | JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL) |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 1364 | goto theend; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1365 | if (opt.jo_timeout < 0) |
| 1366 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1367 | emsg(_(e_invarg)); |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 1368 | goto theend; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | channel = channel_open((char *)address, port, opt.jo_waittime, NULL); |
| 1372 | if (channel != NULL) |
| 1373 | { |
| 1374 | opt.jo_set = JO_ALL; |
| 1375 | channel_set_options(channel, &opt); |
| 1376 | } |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 1377 | theend: |
| 1378 | free_job_options(&opt); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 1379 | return channel; |
| 1380 | } |
| 1381 | |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1382 | void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1383 | ch_close_part(channel_T *channel, ch_part_T part) |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1384 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1385 | sock_T *fd = &channel->ch_part[part].ch_fd; |
| 1386 | |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1387 | if (*fd != INVALID_FD) |
| 1388 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1389 | if (part == PART_SOCK) |
| 1390 | sock_close(*fd); |
| 1391 | else |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 1392 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1393 | // When using a pty the same FD is set on multiple parts, only |
| 1394 | // close it when the last reference is closed. |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 1395 | if ((part == PART_IN || channel->CH_IN_FD != *fd) |
| 1396 | && (part == PART_OUT || channel->CH_OUT_FD != *fd) |
| 1397 | && (part == PART_ERR || channel->CH_ERR_FD != *fd)) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 1398 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1399 | #ifdef MSWIN |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 1400 | if (channel->ch_named_pipe) |
| 1401 | DisconnectNamedPipe((HANDLE)fd); |
| 1402 | #endif |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 1403 | fd_close(*fd); |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 1404 | } |
Bram Moolenaar | 5a1feb8 | 2017-07-22 18:04:08 +0200 | [diff] [blame] | 1405 | } |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1406 | *fd = INVALID_FD; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1407 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1408 | // channel is closed, may want to end the job if it was the last |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 1409 | channel->ch_to_be_closed &= ~(1U << part); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1413 | void |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 1414 | channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1415 | { |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1416 | if (in != INVALID_FD) |
| 1417 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1418 | ch_close_part(channel, PART_IN); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1419 | channel->CH_IN_FD = in; |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 1420 | # if defined(UNIX) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1421 | // Do not end the job when all output channels are closed, wait until |
| 1422 | // the job ended. |
Bram Moolenaar | 1ecc5e4 | 2019-01-26 15:12:55 +0100 | [diff] [blame] | 1423 | if (mch_isatty(in)) |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 1424 | channel->ch_to_be_closed |= (1U << PART_IN); |
| 1425 | # endif |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1426 | } |
| 1427 | if (out != INVALID_FD) |
| 1428 | { |
| 1429 | # if defined(FEAT_GUI) |
| 1430 | channel_gui_unregister_one(channel, PART_OUT); |
| 1431 | # endif |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1432 | ch_close_part(channel, PART_OUT); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1433 | channel->CH_OUT_FD = out; |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 1434 | channel->ch_to_be_closed |= (1U << PART_OUT); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1435 | # if defined(FEAT_GUI) |
| 1436 | channel_gui_register_one(channel, PART_OUT); |
| 1437 | # endif |
| 1438 | } |
| 1439 | if (err != INVALID_FD) |
| 1440 | { |
| 1441 | # if defined(FEAT_GUI) |
| 1442 | channel_gui_unregister_one(channel, PART_ERR); |
| 1443 | # endif |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1444 | ch_close_part(channel, PART_ERR); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1445 | channel->CH_ERR_FD = err; |
Bram Moolenaar | 4e9d443 | 2018-04-24 20:54:07 +0200 | [diff] [blame] | 1446 | channel->ch_to_be_closed |= (1U << PART_ERR); |
Bram Moolenaar | de27989 | 2016-03-11 22:19:44 +0100 | [diff] [blame] | 1447 | # if defined(FEAT_GUI) |
| 1448 | channel_gui_register_one(channel, PART_ERR); |
| 1449 | # endif |
| 1450 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1451 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1452 | |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1453 | /* |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1454 | * Sets the job the channel is associated with and associated options. |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 1455 | * This does not keep a refcount, when the job is freed ch_job is cleared. |
| 1456 | */ |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1457 | void |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1458 | channel_set_job(channel_T *channel, job_T *job, jobopt_T *options) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 1459 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1460 | channel->ch_job = job; |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1461 | |
| 1462 | channel_set_options(channel, options); |
| 1463 | |
| 1464 | if (job->jv_in_buf != NULL) |
| 1465 | { |
| 1466 | chanpart_T *in_part = &channel->ch_part[PART_IN]; |
| 1467 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1468 | set_bufref(&in_part->ch_bufref, job->jv_in_buf); |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 1469 | ch_log(channel, "reading from buffer '%s'", |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1470 | (char *)in_part->ch_bufref.br_buf->b_ffname); |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1471 | if (options->jo_set & JO_IN_TOP) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1472 | { |
| 1473 | if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT)) |
| 1474 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1475 | // Special mode: send last-but-one line when appending a line |
| 1476 | // to the buffer. |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1477 | in_part->ch_bufref.br_buf->b_write_to_channel = TRUE; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1478 | in_part->ch_buf_append = TRUE; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1479 | in_part->ch_buf_top = |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1480 | in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1481 | } |
| 1482 | else |
| 1483 | in_part->ch_buf_top = options->jo_in_top; |
| 1484 | } |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1485 | else |
| 1486 | in_part->ch_buf_top = 1; |
| 1487 | if (options->jo_set & JO_IN_BOT) |
| 1488 | in_part->ch_buf_bot = options->jo_in_bot; |
| 1489 | else |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1490 | in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count; |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1491 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1495 | * Set the callback for "channel"/"part" for the response with "id". |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1496 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1497 | static void |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1498 | channel_set_req_callback( |
Bram Moolenaar | 1735bc9 | 2016-03-14 23:05:14 +0100 | [diff] [blame] | 1499 | channel_T *channel, |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1500 | ch_part_T part, |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 1501 | callback_T *callback, |
Bram Moolenaar | 1735bc9 | 2016-03-14 23:05:14 +0100 | [diff] [blame] | 1502 | int id) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1503 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1504 | cbq_T *head = &channel->ch_part[part].ch_cb_head; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1505 | cbq_T *item = ALLOC_ONE(cbq_T); |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1506 | |
| 1507 | if (item != NULL) |
| 1508 | { |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 1509 | copy_callback(&item->cq_callback, callback); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1510 | item->cq_seq_nr = id; |
| 1511 | item->cq_prev = head->cq_prev; |
| 1512 | head->cq_prev = item; |
| 1513 | item->cq_next = NULL; |
| 1514 | if (item->cq_prev == NULL) |
| 1515 | head->cq_next = item; |
| 1516 | else |
| 1517 | item->cq_prev->cq_next = item; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 1518 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1521 | static void |
| 1522 | write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel) |
| 1523 | { |
| 1524 | char_u *line = ml_get_buf(buf, lnum, FALSE); |
Bram Moolenaar | 367aabd | 2016-03-08 17:13:06 +0100 | [diff] [blame] | 1525 | int len = (int)STRLEN(line); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1526 | char_u *p; |
Bram Moolenaar | bf2cc5f | 2016-07-07 20:45:06 +0200 | [diff] [blame] | 1527 | int i; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1528 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1529 | // Need to make a copy to be able to append a NL. |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1530 | if ((p = alloc(len + 2)) == NULL) |
| 1531 | return; |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1532 | memcpy((char *)p, (char *)line, len); |
Bram Moolenaar | bf2cc5f | 2016-07-07 20:45:06 +0200 | [diff] [blame] | 1533 | |
Bram Moolenaar | ef68e4f | 2017-09-02 16:28:36 +0200 | [diff] [blame] | 1534 | if (channel->ch_write_text_mode) |
| 1535 | p[len] = CAR; |
| 1536 | else |
| 1537 | { |
| 1538 | for (i = 0; i < len; ++i) |
| 1539 | if (p[i] == NL) |
| 1540 | p[i] = NUL; |
Bram Moolenaar | bf2cc5f | 2016-07-07 20:45:06 +0200 | [diff] [blame] | 1541 | |
Bram Moolenaar | ef68e4f | 2017-09-02 16:28:36 +0200 | [diff] [blame] | 1542 | p[len] = NL; |
| 1543 | } |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1544 | p[len + 1] = NUL; |
Bram Moolenaar | 79cbdcb | 2016-11-11 21:14:03 +0100 | [diff] [blame] | 1545 | channel_send(channel, PART_IN, p, len + 1, "write_buf_line"); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1546 | vim_free(p); |
| 1547 | } |
| 1548 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1549 | /* |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1550 | * Return TRUE if "channel" can be written to. |
| 1551 | * Returns FALSE if the input is closed or the write would block. |
| 1552 | */ |
| 1553 | static int |
| 1554 | can_write_buf_line(channel_T *channel) |
| 1555 | { |
| 1556 | chanpart_T *in_part = &channel->ch_part[PART_IN]; |
| 1557 | |
| 1558 | if (in_part->ch_fd == INVALID_FD) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1559 | return FALSE; // pipe was closed |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1560 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1561 | // for testing: block every other attempt to write |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1562 | if (in_part->ch_block_write == 1) |
| 1563 | in_part->ch_block_write = -1; |
| 1564 | else if (in_part->ch_block_write == -1) |
| 1565 | in_part->ch_block_write = 1; |
| 1566 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1567 | // TODO: Win32 implementation, probably using WaitForMultipleObjects() |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1568 | #ifndef MSWIN |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1569 | { |
| 1570 | # if defined(HAVE_SELECT) |
| 1571 | struct timeval tval; |
| 1572 | fd_set wfds; |
| 1573 | int ret; |
| 1574 | |
| 1575 | FD_ZERO(&wfds); |
| 1576 | FD_SET((int)in_part->ch_fd, &wfds); |
| 1577 | tval.tv_sec = 0; |
| 1578 | tval.tv_usec = 0; |
| 1579 | for (;;) |
| 1580 | { |
| 1581 | ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval); |
| 1582 | # ifdef EINTR |
| 1583 | SOCK_ERRNO; |
| 1584 | if (ret == -1 && errno == EINTR) |
| 1585 | continue; |
| 1586 | # endif |
| 1587 | if (ret <= 0 || in_part->ch_block_write == 1) |
| 1588 | { |
| 1589 | if (ret > 0) |
| 1590 | ch_log(channel, "FAKED Input not ready for writing"); |
| 1591 | else |
| 1592 | ch_log(channel, "Input not ready for writing"); |
| 1593 | return FALSE; |
| 1594 | } |
| 1595 | break; |
| 1596 | } |
| 1597 | # else |
| 1598 | struct pollfd fds; |
| 1599 | |
| 1600 | fds.fd = in_part->ch_fd; |
| 1601 | fds.events = POLLOUT; |
| 1602 | if (poll(&fds, 1, 0) <= 0) |
| 1603 | { |
| 1604 | ch_log(channel, "Input not ready for writing"); |
| 1605 | return FALSE; |
| 1606 | } |
| 1607 | if (in_part->ch_block_write == 1) |
| 1608 | { |
| 1609 | ch_log(channel, "FAKED Input not ready for writing"); |
| 1610 | return FALSE; |
| 1611 | } |
| 1612 | # endif |
| 1613 | } |
| 1614 | #endif |
| 1615 | return TRUE; |
| 1616 | } |
| 1617 | |
| 1618 | /* |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 1619 | * Write any buffer lines to the input channel. |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1620 | */ |
Bram Moolenaar | 8b5866d | 2020-09-05 15:48:51 +0200 | [diff] [blame] | 1621 | void |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1622 | channel_write_in(channel_T *channel) |
| 1623 | { |
| 1624 | chanpart_T *in_part = &channel->ch_part[PART_IN]; |
| 1625 | linenr_T lnum; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1626 | buf_T *buf = in_part->ch_bufref.br_buf; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1627 | int written = 0; |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1628 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1629 | if (buf == NULL || in_part->ch_buf_append) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1630 | return; // no buffer or using appending |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1631 | if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL) |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1632 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1633 | // buffer was wiped out or unloaded |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 1634 | ch_log(channel, "input buffer has been wiped out"); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1635 | in_part->ch_bufref.br_buf = NULL; |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1636 | return; |
| 1637 | } |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1638 | |
| 1639 | for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot |
| 1640 | && lnum <= buf->b_ml.ml_line_count; ++lnum) |
| 1641 | { |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1642 | if (!can_write_buf_line(channel)) |
| 1643 | break; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1644 | write_buf_line(buf, lnum, channel); |
| 1645 | ++written; |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1646 | } |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1647 | |
| 1648 | if (written == 1) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 1649 | ch_log(channel, "written line %d to channel", (int)lnum - 1); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1650 | else if (written > 1) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 1651 | ch_log(channel, "written %d lines to channel", written); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1652 | |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1653 | in_part->ch_buf_top = lnum; |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1654 | if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1655 | { |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 1656 | #if defined(FEAT_TERMINAL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1657 | // Send CTRL-D or "eof_chars" to close stdin on MS-Windows. |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 1658 | if (channel->ch_job != NULL) |
| 1659 | term_send_eof(channel); |
| 1660 | #endif |
| 1661 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1662 | // Writing is done, no longer need the buffer. |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1663 | in_part->ch_bufref.br_buf = NULL; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1664 | ch_log(channel, "Finished writing all lines to channel"); |
Bram Moolenaar | d8b5549 | 2016-09-01 14:35:22 +0200 | [diff] [blame] | 1665 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1666 | // Close the pipe/socket, so that the other side gets EOF. |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1667 | ch_close_part(channel, PART_IN); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1668 | } |
| 1669 | else |
Bram Moolenaar | 4ac2e8d | 2018-04-08 12:38:26 +0200 | [diff] [blame] | 1670 | ch_log(channel, "Still %ld more lines to write", |
| 1671 | (long)(buf->b_ml.ml_line_count - lnum + 1)); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | /* |
Bram Moolenaar | aad30bb | 2016-06-26 17:31:03 +0200 | [diff] [blame] | 1675 | * Handle buffer "buf" being freed, remove it from any channels. |
Bram Moolenaar | e0f76d0 | 2016-05-09 20:38:53 +0200 | [diff] [blame] | 1676 | */ |
| 1677 | void |
| 1678 | channel_buffer_free(buf_T *buf) |
| 1679 | { |
| 1680 | channel_T *channel; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1681 | ch_part_T part; |
Bram Moolenaar | e0f76d0 | 2016-05-09 20:38:53 +0200 | [diff] [blame] | 1682 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1683 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1684 | for (part = PART_SOCK; part < PART_COUNT; ++part) |
Bram Moolenaar | e0f76d0 | 2016-05-09 20:38:53 +0200 | [diff] [blame] | 1685 | { |
| 1686 | chanpart_T *ch_part = &channel->ch_part[part]; |
| 1687 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1688 | if (ch_part->ch_bufref.br_buf == buf) |
Bram Moolenaar | de7eb0a | 2016-05-09 20:54:33 +0200 | [diff] [blame] | 1689 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 1690 | ch_log(channel, "%s buffer has been wiped out", |
Bram Moolenaar | de7eb0a | 2016-05-09 20:54:33 +0200 | [diff] [blame] | 1691 | part_names[part]); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1692 | ch_part->ch_bufref.br_buf = NULL; |
Bram Moolenaar | de7eb0a | 2016-05-09 20:54:33 +0200 | [diff] [blame] | 1693 | } |
Bram Moolenaar | e0f76d0 | 2016-05-09 20:38:53 +0200 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /* |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 1698 | * Write any lines waiting to be written to "channel". |
| 1699 | */ |
| 1700 | static void |
| 1701 | channel_write_input(channel_T *channel) |
| 1702 | { |
| 1703 | chanpart_T *in_part = &channel->ch_part[PART_IN]; |
| 1704 | |
| 1705 | if (in_part->ch_writeque.wq_next != NULL) |
| 1706 | channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input"); |
| 1707 | else if (in_part->ch_bufref.br_buf != NULL) |
| 1708 | { |
| 1709 | if (in_part->ch_buf_append) |
| 1710 | channel_write_new_lines(in_part->ch_bufref.br_buf); |
| 1711 | else |
| 1712 | channel_write_in(channel); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /* |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1717 | * Write any lines waiting to be written to a channel. |
| 1718 | */ |
| 1719 | void |
Bram Moolenaar | cf08946 | 2016-06-12 21:18:43 +0200 | [diff] [blame] | 1720 | channel_write_any_lines(void) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1721 | { |
| 1722 | channel_T *channel; |
| 1723 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1724 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 1725 | channel_write_input(channel); |
Bram Moolenaar | 014069a | 2016-03-03 22:51:40 +0100 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1729 | * Write appended lines above the last one in "buf" to the channel. |
| 1730 | */ |
| 1731 | void |
| 1732 | channel_write_new_lines(buf_T *buf) |
| 1733 | { |
| 1734 | channel_T *channel; |
| 1735 | int found_one = FALSE; |
| 1736 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1737 | // There could be more than one channel for the buffer, loop over all of |
| 1738 | // them. |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 1739 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1740 | { |
| 1741 | chanpart_T *in_part = &channel->ch_part[PART_IN]; |
| 1742 | linenr_T lnum; |
| 1743 | int written = 0; |
| 1744 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1745 | if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1746 | { |
| 1747 | if (in_part->ch_fd == INVALID_FD) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1748 | continue; // pipe was closed |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1749 | found_one = TRUE; |
| 1750 | for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count; |
| 1751 | ++lnum) |
| 1752 | { |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1753 | if (!can_write_buf_line(channel)) |
| 1754 | break; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1755 | write_buf_line(buf, lnum, channel); |
| 1756 | ++written; |
| 1757 | } |
| 1758 | |
| 1759 | if (written == 1) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 1760 | ch_log(channel, "written line %d to channel", (int)lnum - 1); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1761 | else if (written > 1) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 1762 | ch_log(channel, "written %d lines to channel", written); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 1763 | if (lnum < buf->b_ml.ml_line_count) |
Bram Moolenaar | 4ac2e8d | 2018-04-08 12:38:26 +0200 | [diff] [blame] | 1764 | ch_log(channel, "Still %ld more lines to write", |
| 1765 | (long)(buf->b_ml.ml_line_count - lnum)); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 1766 | |
| 1767 | in_part->ch_buf_bot = lnum; |
| 1768 | } |
| 1769 | } |
| 1770 | if (!found_one) |
| 1771 | buf->b_write_to_channel = FALSE; |
| 1772 | } |
| 1773 | |
| 1774 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1775 | * Invoke the "callback" on channel "channel". |
Bram Moolenaar | 7f7c332 | 2016-04-18 19:27:24 +0200 | [diff] [blame] | 1776 | * This does not redraw but sets channel_need_redraw; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1777 | */ |
| 1778 | static void |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 1779 | invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1780 | { |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1781 | typval_T rettv; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1782 | |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 1783 | if (safe_to_invoke_callback == 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1784 | iemsg("INTERNAL: Invoking callback when it is not safe"); |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 1785 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1786 | argv[0].v_type = VAR_CHANNEL; |
| 1787 | argv[0].vval.v_channel = channel; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1788 | |
Bram Moolenaar | c6538bc | 2019-08-03 18:17:11 +0200 | [diff] [blame] | 1789 | call_callback(callback, -1, &rettv, 2, argv); |
Bram Moolenaar | ee1cffc | 2016-02-21 19:14:41 +0100 | [diff] [blame] | 1790 | clear_tv(&rettv); |
Bram Moolenaar | 7f7c332 | 2016-04-18 19:27:24 +0200 | [diff] [blame] | 1791 | channel_need_redraw = TRUE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 1792 | } |
| 1793 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 1794 | /* |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1795 | * Return the first node from "channel"/"part" without removing it. |
| 1796 | * Returns NULL if there is nothing. |
| 1797 | */ |
| 1798 | readq_T * |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1799 | channel_peek(channel_T *channel, ch_part_T part) |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1800 | { |
| 1801 | readq_T *head = &channel->ch_part[part].ch_head; |
| 1802 | |
| 1803 | return head->rq_next; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * Return a pointer to the first NL in "node". |
| 1808 | * Skips over NUL characters. |
| 1809 | * Returns NULL if there is no NL. |
| 1810 | */ |
| 1811 | char_u * |
| 1812 | channel_first_nl(readq_T *node) |
| 1813 | { |
| 1814 | char_u *buffer = node->rq_buffer; |
| 1815 | long_u i; |
| 1816 | |
| 1817 | for (i = 0; i < node->rq_buflen; ++i) |
| 1818 | if (buffer[i] == NL) |
| 1819 | return buffer + i; |
| 1820 | return NULL; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1824 | * Return the first buffer from channel "channel"/"part" and remove it. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1825 | * The caller must free it. |
| 1826 | * Returns NULL if there is nothing. |
| 1827 | */ |
| 1828 | char_u * |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 1829 | channel_get(channel_T *channel, ch_part_T part, int *outlen) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1830 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1831 | readq_T *head = &channel->ch_part[part].ch_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1832 | readq_T *node = head->rq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1833 | char_u *p; |
| 1834 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1835 | if (node == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1836 | return NULL; |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 1837 | if (outlen != NULL) |
| 1838 | *outlen += node->rq_buflen; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1839 | // dispose of the node but keep the buffer |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1840 | p = node->rq_buffer; |
| 1841 | head->rq_next = node->rq_next; |
| 1842 | if (node->rq_next == NULL) |
| 1843 | head->rq_prev = NULL; |
| 1844 | else |
| 1845 | node->rq_next->rq_prev = NULL; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1846 | vim_free(node); |
| 1847 | return p; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1851 | * Returns the whole buffer contents concatenated for "channel"/"part". |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1852 | * Replaces NUL bytes with NL. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1853 | */ |
| 1854 | static char_u * |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 1855 | channel_get_all(channel_T *channel, ch_part_T part, int *outlen) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1856 | { |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1857 | readq_T *head = &channel->ch_part[part].ch_head; |
Bram Moolenaar | 2c519cf | 2019-03-21 21:45:34 +0100 | [diff] [blame] | 1858 | readq_T *node; |
Bram Moolenaar | adb78a7 | 2016-06-27 21:10:31 +0200 | [diff] [blame] | 1859 | long_u len = 0; |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1860 | char_u *res; |
| 1861 | char_u *p; |
| 1862 | |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1863 | // Concatenate everything into one buffer. |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1864 | for (node = head->rq_next; node != NULL; node = node->rq_next) |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1865 | len += node->rq_buflen; |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 1866 | res = alloc(len + 1); |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1867 | if (res == NULL) |
| 1868 | return NULL; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1869 | p = res; |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1870 | for (node = head->rq_next; node != NULL; node = node->rq_next) |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1871 | { |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1872 | mch_memmove(p, node->rq_buffer, node->rq_buflen); |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1873 | p += node->rq_buflen; |
| 1874 | } |
| 1875 | *p = NUL; |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1876 | |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1877 | // Free all buffers |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1878 | do |
| 1879 | { |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 1880 | p = channel_get(channel, part, NULL); |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1881 | vim_free(p); |
| 1882 | } while (p != NULL); |
| 1883 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 1884 | if (outlen != NULL) |
| 1885 | { |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1886 | // Returning the length, keep NUL characters. |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 1887 | *outlen += len; |
| 1888 | return res; |
| 1889 | } |
| 1890 | |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1891 | // Turn all NUL into NL, so that the result can be used as a string. |
| 1892 | p = res; |
| 1893 | while (p < res + len) |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1894 | { |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1895 | if (*p == NUL) |
| 1896 | *p = NL; |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1897 | #ifdef MSWIN |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1898 | else if (*p == 0x1b) |
| 1899 | { |
| 1900 | // crush the escape sequence OSC 0/1/2: ESC ]0; |
| 1901 | if (p + 3 < res + len |
| 1902 | && p[1] == ']' |
| 1903 | && (p[2] == '0' || p[2] == '1' || p[2] == '2') |
| 1904 | && p[3] == ';') |
| 1905 | { |
| 1906 | // '\a' becomes a NL |
| 1907 | while (p < res + (len - 1) && *p != '\a') |
| 1908 | ++p; |
| 1909 | // BEL is zero width characters, suppress display mistake |
| 1910 | // ConPTY (after 10.0.18317) requires advance checking |
| 1911 | if (p[-1] == NUL) |
| 1912 | p[-1] = 0x07; |
| 1913 | } |
| 1914 | } |
| 1915 | #endif |
| 1916 | ++p; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1917 | } |
| 1918 | |
Bram Moolenaar | ee1f7b3 | 2016-03-28 14:42:14 +0200 | [diff] [blame] | 1919 | return res; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /* |
Bram Moolenaar | cf08946 | 2016-06-12 21:18:43 +0200 | [diff] [blame] | 1923 | * Consume "len" bytes from the head of "node". |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1924 | * Caller must check these bytes are available. |
| 1925 | */ |
| 1926 | void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1927 | channel_consume(channel_T *channel, ch_part_T part, int len) |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1928 | { |
| 1929 | readq_T *head = &channel->ch_part[part].ch_head; |
| 1930 | readq_T *node = head->rq_next; |
| 1931 | char_u *buf = node->rq_buffer; |
| 1932 | |
| 1933 | mch_memmove(buf, buf + len, node->rq_buflen - len); |
| 1934 | node->rq_buflen -= len; |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1935 | node->rq_buffer[node->rq_buflen] = NUL; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1939 | * Collapses the first and second buffer for "channel"/"part". |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1940 | * Returns FAIL if that is not possible. |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1941 | * When "want_nl" is TRUE collapse more buffers until a NL is found. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1942 | */ |
| 1943 | int |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 1944 | channel_collapse(channel_T *channel, ch_part_T part, int want_nl) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1945 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 1946 | readq_T *head = &channel->ch_part[part].ch_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1947 | readq_T *node = head->rq_next; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1948 | readq_T *last_node; |
| 1949 | readq_T *n; |
| 1950 | char_u *newbuf; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1951 | char_u *p; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1952 | long_u len; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1953 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1954 | if (node == NULL || node->rq_next == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1955 | return FAIL; |
| 1956 | |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1957 | last_node = node->rq_next; |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1958 | len = node->rq_buflen + last_node->rq_buflen; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1959 | if (want_nl) |
| 1960 | while (last_node->rq_next != NULL |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1961 | && channel_first_nl(last_node) == NULL) |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1962 | { |
| 1963 | last_node = last_node->rq_next; |
| 1964 | len += last_node->rq_buflen; |
| 1965 | } |
| 1966 | |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1967 | p = newbuf = alloc(len + 1); |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1968 | if (newbuf == NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1969 | return FAIL; // out of memory |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1970 | mch_memmove(p, node->rq_buffer, node->rq_buflen); |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1971 | p += node->rq_buflen; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 1972 | vim_free(node->rq_buffer); |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1973 | node->rq_buffer = newbuf; |
| 1974 | for (n = node; n != last_node; ) |
| 1975 | { |
| 1976 | n = n->rq_next; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 1977 | mch_memmove(p, n->rq_buffer, n->rq_buflen); |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1978 | p += n->rq_buflen; |
| 1979 | vim_free(n->rq_buffer); |
| 1980 | } |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 1981 | *p = NUL; |
Bram Moolenaar | bbe8d91 | 2016-06-05 16:10:57 +0200 | [diff] [blame] | 1982 | node->rq_buflen = (long_u)(p - newbuf); |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1983 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1984 | // dispose of the collapsed nodes and their buffers |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 1985 | for (n = node->rq_next; n != last_node; ) |
| 1986 | { |
| 1987 | n = n->rq_next; |
| 1988 | vim_free(n->rq_prev); |
| 1989 | } |
| 1990 | node->rq_next = last_node->rq_next; |
| 1991 | if (last_node->rq_next == NULL) |
| 1992 | head->rq_prev = node; |
| 1993 | else |
| 1994 | last_node->rq_next->rq_prev = node; |
| 1995 | vim_free(last_node); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 1996 | return OK; |
| 1997 | } |
| 1998 | |
| 1999 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2000 | * Store "buf[len]" on "channel"/"part". |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2001 | * When "prepend" is TRUE put in front, otherwise append at the end. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2002 | * Returns OK or FAIL. |
| 2003 | */ |
| 2004 | static int |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2005 | channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len, |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2006 | int prepend, char *lead) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2007 | { |
| 2008 | readq_T *node; |
| 2009 | readq_T *head = &channel->ch_part[part].ch_head; |
| 2010 | char_u *p; |
| 2011 | int i; |
| 2012 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2013 | node = ALLOC_ONE(readq_T); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2014 | if (node == NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2015 | return FAIL; // out of memory |
| 2016 | // A NUL is added at the end, because netbeans code expects that. |
| 2017 | // Otherwise a NUL may appear inside the text. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2018 | node->rq_buffer = alloc(len + 1); |
| 2019 | if (node->rq_buffer == NULL) |
| 2020 | { |
| 2021 | vim_free(node); |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2022 | return FAIL; // out of memory |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | if (channel->ch_part[part].ch_mode == MODE_NL) |
| 2026 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2027 | // Drop any CR before a NL. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2028 | p = node->rq_buffer; |
| 2029 | for (i = 0; i < len; ++i) |
| 2030 | if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL) |
| 2031 | *p++ = buf[i]; |
| 2032 | *p = NUL; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 2033 | node->rq_buflen = (long_u)(p - node->rq_buffer); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2034 | } |
| 2035 | else |
| 2036 | { |
| 2037 | mch_memmove(node->rq_buffer, buf, len); |
| 2038 | node->rq_buffer[len] = NUL; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 2039 | node->rq_buflen = (long_u)len; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2040 | } |
| 2041 | |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2042 | if (prepend) |
| 2043 | { |
Bram Moolenaar | ad3ec76 | 2019-04-21 00:00:13 +0200 | [diff] [blame] | 2044 | // prepend node to the head of the queue |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2045 | node->rq_next = head->rq_next; |
| 2046 | node->rq_prev = NULL; |
| 2047 | if (head->rq_next == NULL) |
| 2048 | head->rq_prev = node; |
| 2049 | else |
| 2050 | head->rq_next->rq_prev = node; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2051 | head->rq_next = node; |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2052 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2053 | else |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2054 | { |
Bram Moolenaar | ad3ec76 | 2019-04-21 00:00:13 +0200 | [diff] [blame] | 2055 | // append node to the tail of the queue |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2056 | node->rq_next = NULL; |
| 2057 | node->rq_prev = head->rq_prev; |
| 2058 | if (head->rq_prev == NULL) |
| 2059 | head->rq_next = node; |
| 2060 | else |
| 2061 | head->rq_prev->rq_next = node; |
| 2062 | head->rq_prev = node; |
| 2063 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2064 | |
Bram Moolenaar | 71eeb74 | 2017-09-13 22:18:01 +0200 | [diff] [blame] | 2065 | if (ch_log_active() && lead != NULL) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2066 | { |
Bram Moolenaar | 4b16ee7 | 2018-08-09 22:15:34 +0200 | [diff] [blame] | 2067 | ch_log_lead(lead, channel, part); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2068 | fprintf(log_fd, "'"); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 2069 | vim_ignored = (int)fwrite(buf, len, 1, log_fd); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2070 | fprintf(log_fd, "'\n"); |
| 2071 | } |
| 2072 | return OK; |
| 2073 | } |
| 2074 | |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2075 | /* |
| 2076 | * Try to fill the buffer of "reader". |
| 2077 | * Returns FALSE when nothing was added. |
| 2078 | */ |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2079 | static int |
| 2080 | channel_fill(js_read_T *reader) |
| 2081 | { |
| 2082 | channel_T *channel = (channel_T *)reader->js_cookie; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2083 | ch_part_T part = reader->js_cookie_arg; |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 2084 | char_u *next = channel_get(channel, part, NULL); |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2085 | int keeplen; |
| 2086 | int addlen; |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2087 | char_u *p; |
| 2088 | |
| 2089 | if (next == NULL) |
| 2090 | return FALSE; |
| 2091 | |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2092 | keeplen = reader->js_end - reader->js_buf; |
| 2093 | if (keeplen > 0) |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2094 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2095 | // Prepend unused text. |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2096 | addlen = (int)STRLEN(next); |
| 2097 | p = alloc(keeplen + addlen + 1); |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2098 | if (p == NULL) |
| 2099 | { |
| 2100 | vim_free(next); |
| 2101 | return FALSE; |
| 2102 | } |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2103 | mch_memmove(p, reader->js_buf, keeplen); |
| 2104 | mch_memmove(p + keeplen, next, addlen + 1); |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2105 | vim_free(next); |
| 2106 | next = p; |
| 2107 | } |
| 2108 | |
| 2109 | vim_free(reader->js_buf); |
| 2110 | reader->js_buf = next; |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2111 | return TRUE; |
| 2112 | } |
| 2113 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2114 | /* |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2115 | * Use the read buffer of "channel"/"part" and parse a JSON message that is |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2116 | * complete. The messages are added to the queue. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2117 | * Return TRUE if there is more to read. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2118 | */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2119 | static int |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2120 | channel_parse_json(channel_T *channel, ch_part_T part) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2121 | { |
| 2122 | js_read_T reader; |
| 2123 | typval_T listtv; |
| 2124 | jsonq_T *item; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2125 | chanpart_T *chanpart = &channel->ch_part[part]; |
| 2126 | jsonq_T *head = &chanpart->ch_json_head; |
| 2127 | int status; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2128 | int ret; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2129 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2130 | if (channel_peek(channel, part) == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2131 | return FALSE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2132 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 2133 | reader.js_buf = channel_get(channel, part, NULL); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2134 | reader.js_used = 0; |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2135 | reader.js_fill = channel_fill; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2136 | reader.js_cookie = channel; |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2137 | reader.js_cookie_arg = part; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2138 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2139 | // When a message is incomplete we wait for a short while for more to |
| 2140 | // arrive. After the delay drop the input, otherwise a truncated string |
| 2141 | // or list will make us hang. |
| 2142 | // Do not generate error messages, they will be written in a channel log. |
Bram Moolenaar | 03c60c1 | 2017-01-10 15:15:37 +0100 | [diff] [blame] | 2143 | ++emsg_silent; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2144 | status = json_decode(&reader, &listtv, |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2145 | chanpart->ch_mode == MODE_JS ? JSON_JS : 0); |
Bram Moolenaar | 03c60c1 | 2017-01-10 15:15:37 +0100 | [diff] [blame] | 2146 | --emsg_silent; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2147 | if (status == OK) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2148 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2149 | // Only accept the response when it is a list with at least two |
| 2150 | // items. |
Bram Moolenaar | 6076fe1 | 2016-02-05 22:49:56 +0100 | [diff] [blame] | 2151 | 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] | 2152 | { |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2153 | if (listtv.v_type != VAR_LIST) |
| 2154 | ch_error(channel, "Did not receive a list, discarding"); |
| 2155 | else |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2156 | ch_error(channel, "Expected list with two items, got %d", |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2157 | listtv.vval.v_list->lv_len); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2158 | clear_tv(&listtv); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2159 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2160 | else |
| 2161 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2162 | item = ALLOC_ONE(jsonq_T); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2163 | if (item == NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2164 | clear_tv(&listtv); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2165 | else |
| 2166 | { |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2167 | item->jq_no_callback = FALSE; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2168 | item->jq_value = alloc_tv(); |
| 2169 | if (item->jq_value == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2170 | { |
| 2171 | vim_free(item); |
| 2172 | clear_tv(&listtv); |
| 2173 | } |
| 2174 | else |
| 2175 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2176 | *item->jq_value = listtv; |
| 2177 | item->jq_prev = head->jq_prev; |
| 2178 | head->jq_prev = item; |
| 2179 | item->jq_next = NULL; |
| 2180 | if (item->jq_prev == NULL) |
| 2181 | head->jq_next = item; |
| 2182 | else |
| 2183 | item->jq_prev->jq_next = item; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2184 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | } |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2188 | |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2189 | if (status == OK) |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2190 | chanpart->ch_wait_len = 0; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2191 | else if (status == MAYBE) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2192 | { |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2193 | size_t buflen = STRLEN(reader.js_buf); |
| 2194 | |
| 2195 | if (chanpart->ch_wait_len < buflen) |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2196 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2197 | // First time encountering incomplete message or after receiving |
| 2198 | // more (but still incomplete): set a deadline of 100 msec. |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2199 | ch_log(channel, |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2200 | "Incomplete message (%d bytes) - wait 100 msec for more", |
Bram Moolenaar | b113c3a | 2017-02-28 21:26:17 +0100 | [diff] [blame] | 2201 | (int)buflen); |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2202 | reader.js_used = 0; |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2203 | chanpart->ch_wait_len = buflen; |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 2204 | #ifdef MSWIN |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2205 | chanpart->ch_deadline = GetTickCount() + 100L; |
| 2206 | #else |
| 2207 | gettimeofday(&chanpart->ch_deadline, NULL); |
| 2208 | chanpart->ch_deadline.tv_usec += 100 * 1000; |
| 2209 | if (chanpart->ch_deadline.tv_usec > 1000 * 1000) |
| 2210 | { |
| 2211 | chanpart->ch_deadline.tv_usec -= 1000 * 1000; |
| 2212 | ++chanpart->ch_deadline.tv_sec; |
| 2213 | } |
| 2214 | #endif |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2215 | } |
| 2216 | else |
| 2217 | { |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2218 | int timeout; |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 2219 | #ifdef MSWIN |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2220 | timeout = GetTickCount() > chanpart->ch_deadline; |
| 2221 | #else |
| 2222 | { |
| 2223 | struct timeval now_tv; |
| 2224 | |
| 2225 | gettimeofday(&now_tv, NULL); |
| 2226 | timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec |
| 2227 | || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec |
| 2228 | && now_tv.tv_usec > chanpart->ch_deadline.tv_usec); |
| 2229 | } |
| 2230 | #endif |
| 2231 | if (timeout) |
| 2232 | { |
| 2233 | status = FAIL; |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2234 | chanpart->ch_wait_len = 0; |
| 2235 | ch_log(channel, "timed out"); |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2236 | } |
| 2237 | else |
| 2238 | { |
| 2239 | reader.js_used = 0; |
| 2240 | ch_log(channel, "still waiting on incomplete message"); |
| 2241 | } |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2242 | } |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2243 | } |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2244 | |
| 2245 | if (status == FAIL) |
| 2246 | { |
| 2247 | ch_error(channel, "Decoding failed - discarding input"); |
| 2248 | ret = FALSE; |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 2249 | chanpart->ch_wait_len = 0; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2250 | } |
| 2251 | else if (reader.js_buf[reader.js_used] != NUL) |
| 2252 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2253 | // Put the unread part back into the channel. |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2254 | channel_save(channel, part, reader.js_buf + reader.js_used, |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 2255 | (int)(reader.js_end - reader.js_buf) - reader.js_used, |
| 2256 | TRUE, NULL); |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2257 | ret = status == MAYBE ? FALSE: TRUE; |
| 2258 | } |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2259 | else |
| 2260 | ret = FALSE; |
| 2261 | |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2262 | vim_free(reader.js_buf); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2263 | return ret; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /* |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 2267 | * Remove "node" from the queue that it is in. Does not free it. |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2268 | */ |
| 2269 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2270 | remove_cb_node(cbq_T *head, cbq_T *node) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2271 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2272 | if (node->cq_prev == NULL) |
| 2273 | head->cq_next = node->cq_next; |
| 2274 | else |
| 2275 | node->cq_prev->cq_next = node->cq_next; |
| 2276 | if (node->cq_next == NULL) |
| 2277 | head->cq_prev = node->cq_prev; |
| 2278 | else |
| 2279 | node->cq_next->cq_prev = node->cq_prev; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /* |
| 2283 | * Remove "node" from the queue that it is in and free it. |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2284 | * Caller should have freed or used node->jq_value. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2285 | */ |
| 2286 | static void |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2287 | remove_json_node(jsonq_T *head, jsonq_T *node) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2288 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2289 | if (node->jq_prev == NULL) |
| 2290 | head->jq_next = node->jq_next; |
| 2291 | else |
| 2292 | node->jq_prev->jq_next = node->jq_next; |
| 2293 | if (node->jq_next == NULL) |
| 2294 | head->jq_prev = node->jq_prev; |
| 2295 | else |
| 2296 | node->jq_next->jq_prev = node->jq_prev; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2297 | vim_free(node); |
| 2298 | } |
| 2299 | |
| 2300 | /* |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 2301 | * Add "id" to the list of JSON message IDs we are waiting on. |
| 2302 | */ |
| 2303 | static void |
| 2304 | channel_add_block_id(chanpart_T *chanpart, int id) |
| 2305 | { |
| 2306 | garray_T *gap = &chanpart->ch_block_ids; |
| 2307 | |
| 2308 | if (gap->ga_growsize == 0) |
| 2309 | ga_init2(gap, (int)sizeof(int), 10); |
| 2310 | if (ga_grow(gap, 1) == OK) |
| 2311 | { |
| 2312 | ((int *)gap->ga_data)[gap->ga_len] = id; |
| 2313 | ++gap->ga_len; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | /* |
| 2318 | * Remove "id" from the list of JSON message IDs we are waiting on. |
| 2319 | */ |
| 2320 | static void |
| 2321 | channel_remove_block_id(chanpart_T *chanpart, int id) |
| 2322 | { |
| 2323 | garray_T *gap = &chanpart->ch_block_ids; |
| 2324 | int i; |
| 2325 | |
| 2326 | for (i = 0; i < gap->ga_len; ++i) |
| 2327 | if (((int *)gap->ga_data)[i] == id) |
| 2328 | { |
| 2329 | --gap->ga_len; |
| 2330 | if (i < gap->ga_len) |
| 2331 | { |
| 2332 | int *p = ((int *)gap->ga_data) + i; |
| 2333 | |
| 2334 | mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int)); |
| 2335 | } |
| 2336 | return; |
| 2337 | } |
| 2338 | siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id); |
| 2339 | } |
| 2340 | |
| 2341 | /* |
| 2342 | * Return TRUE if "id" is in the list of JSON message IDs we are waiting on. |
| 2343 | */ |
| 2344 | static int |
| 2345 | channel_has_block_id(chanpart_T *chanpart, int id) |
| 2346 | { |
| 2347 | garray_T *gap = &chanpart->ch_block_ids; |
| 2348 | int i; |
| 2349 | |
| 2350 | for (i = 0; i < gap->ga_len; ++i) |
| 2351 | if (((int *)gap->ga_data)[i] == id) |
| 2352 | return TRUE; |
| 2353 | return FALSE; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2357 | * Get a message from the JSON queue for channel "channel". |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2358 | * When "id" is positive it must match the first number in the list. |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 2359 | * When "id" is zero or negative jut get the first message. But not one |
| 2360 | * in the ch_block_ids list. |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2361 | * When "without_callback" is TRUE also get messages that were pushed back. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2362 | * Return OK when found and return the value in "rettv". |
| 2363 | * Return FAIL otherwise. |
| 2364 | */ |
| 2365 | static int |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2366 | channel_get_json( |
| 2367 | channel_T *channel, |
| 2368 | ch_part_T part, |
| 2369 | int id, |
| 2370 | int without_callback, |
| 2371 | typval_T **rettv) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2372 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2373 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2374 | jsonq_T *item = head->jq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2375 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2376 | while (item != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2377 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2378 | list_T *l = item->jq_value->vval.v_list; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2379 | typval_T *tv; |
| 2380 | |
Bram Moolenaar | 7e9f351 | 2020-05-13 22:44:22 +0200 | [diff] [blame] | 2381 | CHECK_LIST_MATERIALIZE(l); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2382 | tv = &l->lv_first->li_tv; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2383 | |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2384 | if ((without_callback || !item->jq_no_callback) |
| 2385 | && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id) |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 2386 | || (id <= 0 && (tv->v_type != VAR_NUMBER |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2387 | || tv->vval.v_number == 0 |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 2388 | || !channel_has_block_id( |
| 2389 | &channel->ch_part[part], tv->vval.v_number))))) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2390 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2391 | *rettv = item->jq_value; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 2392 | if (tv->v_type == VAR_NUMBER) |
Bram Moolenaar | 4ac2e8d | 2018-04-08 12:38:26 +0200 | [diff] [blame] | 2393 | ch_log(channel, "Getting JSON message %ld", |
| 2394 | (long)tv->vval.v_number); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2395 | remove_json_node(head, item); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2396 | return OK; |
| 2397 | } |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2398 | item = item->jq_next; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2399 | } |
| 2400 | return FAIL; |
| 2401 | } |
| 2402 | |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2403 | /* |
| 2404 | * Put back "rettv" into the JSON queue, there was no callback for it. |
| 2405 | * Takes over the values in "rettv". |
| 2406 | */ |
| 2407 | static void |
| 2408 | channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv) |
| 2409 | { |
| 2410 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
| 2411 | jsonq_T *item = head->jq_next; |
| 2412 | jsonq_T *newitem; |
| 2413 | |
| 2414 | if (head->jq_prev != NULL && head->jq_prev->jq_no_callback) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2415 | // last item was pushed back, append to the end |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2416 | item = NULL; |
| 2417 | else while (item != NULL && item->jq_no_callback) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2418 | // append after the last item that was pushed back |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2419 | item = item->jq_next; |
| 2420 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2421 | newitem = ALLOC_ONE(jsonq_T); |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2422 | if (newitem == NULL) |
| 2423 | clear_tv(rettv); |
| 2424 | else |
| 2425 | { |
| 2426 | newitem->jq_value = alloc_tv(); |
| 2427 | if (newitem->jq_value == NULL) |
| 2428 | { |
| 2429 | vim_free(newitem); |
| 2430 | clear_tv(rettv); |
| 2431 | } |
| 2432 | else |
| 2433 | { |
| 2434 | newitem->jq_no_callback = FALSE; |
| 2435 | *newitem->jq_value = *rettv; |
| 2436 | if (item == NULL) |
| 2437 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2438 | // append to the end |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2439 | newitem->jq_prev = head->jq_prev; |
| 2440 | head->jq_prev = newitem; |
| 2441 | newitem->jq_next = NULL; |
| 2442 | if (newitem->jq_prev == NULL) |
| 2443 | head->jq_next = newitem; |
| 2444 | else |
| 2445 | newitem->jq_prev->jq_next = newitem; |
| 2446 | } |
| 2447 | else |
| 2448 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2449 | // append after "item" |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2450 | newitem->jq_prev = item; |
| 2451 | newitem->jq_next = item->jq_next; |
| 2452 | item->jq_next = newitem; |
| 2453 | if (newitem->jq_next == NULL) |
| 2454 | head->jq_prev = newitem; |
| 2455 | else |
| 2456 | newitem->jq_next->jq_prev = newitem; |
| 2457 | } |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2462 | #define CH_JSON_MAX_ARGS 4 |
| 2463 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2464 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2465 | * Execute a command received over "channel"/"part" |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2466 | * "argv[0]" is the command string. |
| 2467 | * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing. |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2468 | */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2469 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2470 | channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2471 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2472 | char_u *cmd = argv[0].vval.v_string; |
| 2473 | char_u *arg; |
| 2474 | int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2475 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2476 | if (argv[1].v_type != VAR_STRING) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2477 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2478 | ch_error(channel, "received command with non-string argument"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2479 | if (p_verbose > 2) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2480 | emsg(_("E903: received command with non-string argument")); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2481 | return; |
| 2482 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2483 | arg = argv[1].vval.v_string; |
Bram Moolenaar | 14ad611 | 2016-02-01 21:47:13 +0100 | [diff] [blame] | 2484 | if (arg == NULL) |
| 2485 | arg = (char_u *)""; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2486 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2487 | if (STRCMP(cmd, "ex") == 0) |
| 2488 | { |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 2489 | int called_emsg_before = called_emsg; |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 2490 | |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2491 | ch_log(channel, "Executing ex command '%s'", (char *)arg); |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 2492 | ++emsg_silent; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2493 | do_cmdline_cmd(arg); |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 2494 | --emsg_silent; |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 2495 | if (called_emsg > called_emsg_before) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2496 | ch_log(channel, "Ex command error: '%s'", |
Bram Moolenaar | c4dcd60 | 2016-03-26 22:56:46 +0100 | [diff] [blame] | 2497 | (char *)get_vim_var_str(VV_ERRMSG)); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2498 | } |
| 2499 | else if (STRCMP(cmd, "normal") == 0) |
| 2500 | { |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2501 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2502 | |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2503 | ch_log(channel, "Executing normal command '%s'", (char *)arg); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 2504 | CLEAR_FIELD(ea); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2505 | ea.arg = arg; |
| 2506 | ea.addr_count = 0; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2507 | ea.forceit = TRUE; // no mapping |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2508 | ex_normal(&ea); |
| 2509 | } |
| 2510 | else if (STRCMP(cmd, "redraw") == 0) |
| 2511 | { |
| 2512 | exarg_T ea; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2513 | |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2514 | ch_log(channel, "redraw"); |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 2515 | CLEAR_FIELD(ea); |
Bram Moolenaar | 14ad611 | 2016-02-01 21:47:13 +0100 | [diff] [blame] | 2516 | ea.forceit = *arg != NUL; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2517 | ex_redraw(&ea); |
| 2518 | showruler(FALSE); |
| 2519 | setcursor(); |
Bram Moolenaar | a338adc | 2018-01-31 20:51:47 +0100 | [diff] [blame] | 2520 | out_flush_cursor(TRUE, FALSE); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2521 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2522 | else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2523 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2524 | int is_call = cmd[0] == 'c'; |
| 2525 | int id_idx = is_call ? 3 : 2; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2526 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2527 | if (argv[id_idx].v_type != VAR_UNKNOWN |
| 2528 | && argv[id_idx].v_type != VAR_NUMBER) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2529 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2530 | ch_error(channel, "last argument for expr/call must be a number"); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2531 | if (p_verbose > 2) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2532 | emsg(_("E904: last argument for expr/call must be a number")); |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2533 | } |
| 2534 | else if (is_call && argv[2].v_type != VAR_LIST) |
| 2535 | { |
| 2536 | ch_error(channel, "third argument for call must be a list"); |
| 2537 | if (p_verbose > 2) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2538 | emsg(_("E904: third argument for call must be a list")); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2539 | } |
| 2540 | else |
| 2541 | { |
Bram Moolenaar | c8fe338 | 2016-09-04 20:44:42 +0200 | [diff] [blame] | 2542 | typval_T *tv = NULL; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2543 | typval_T res_tv; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2544 | typval_T err_tv; |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 2545 | char_u *json = NULL; |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2546 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2547 | // Don't pollute the display with errors. |
Alisue | 11a632d | 2021-06-30 22:01:02 +0200 | [diff] [blame^] | 2548 | // Do generate the errors so that try/catch works. |
| 2549 | ++emsg_silent; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2550 | if (!is_call) |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2551 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2552 | ch_log(channel, "Evaluating expression '%s'", (char *)arg); |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2553 | tv = eval_expr(arg, NULL); |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2554 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2555 | else |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2556 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2557 | ch_log(channel, "Calling '%s'", (char *)arg); |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2558 | if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK) |
| 2559 | tv = &res_tv; |
Bram Moolenaar | ac74d5e | 2016-03-20 14:31:00 +0100 | [diff] [blame] | 2560 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2561 | |
| 2562 | if (argv[id_idx].v_type == VAR_NUMBER) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2563 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2564 | int id = argv[id_idx].vval.v_number; |
| 2565 | |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 2566 | if (tv != NULL) |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 2567 | json = json_encode_nr_expr(id, tv, options | JSON_NL); |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 2568 | if (tv == NULL || (json != NULL && *json == NUL)) |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2569 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2570 | // If evaluation failed or the result can't be encoded |
| 2571 | // then return the string "ERROR". |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2572 | vim_free(json); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2573 | err_tv.v_type = VAR_STRING; |
| 2574 | err_tv.vval.v_string = (char_u *)"ERROR"; |
Bram Moolenaar | c8fe338 | 2016-09-04 20:44:42 +0200 | [diff] [blame] | 2575 | json = json_encode_nr_expr(id, &err_tv, options | JSON_NL); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2576 | } |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 2577 | if (json != NULL) |
| 2578 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2579 | channel_send(channel, |
| 2580 | part == PART_SOCK ? PART_SOCK : PART_IN, |
Bram Moolenaar | bf2cc5f | 2016-07-07 20:45:06 +0200 | [diff] [blame] | 2581 | json, (int)STRLEN(json), (char *)cmd); |
Bram Moolenaar | 55fab43 | 2016-02-07 16:53:13 +0100 | [diff] [blame] | 2582 | vim_free(json); |
| 2583 | } |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2584 | } |
Alisue | 11a632d | 2021-06-30 22:01:02 +0200 | [diff] [blame^] | 2585 | --emsg_silent; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2586 | if (tv == &res_tv) |
| 2587 | clear_tv(tv); |
Bram Moolenaar | c8fe338 | 2016-09-04 20:44:42 +0200 | [diff] [blame] | 2588 | else |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 2589 | free_tv(tv); |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 2590 | } |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2591 | } |
| 2592 | else if (p_verbose > 2) |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2593 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2594 | ch_error(channel, "Received unknown command: %s", (char *)cmd); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2595 | semsg(_("E905: received unknown command: %s"), cmd); |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2596 | } |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2597 | } |
| 2598 | |
Bram Moolenaar | 7f7c332 | 2016-04-18 19:27:24 +0200 | [diff] [blame] | 2599 | /* |
| 2600 | * Invoke the callback at "cbhead". |
| 2601 | * Does not redraw but sets channel_need_redraw. |
| 2602 | */ |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2603 | static void |
| 2604 | invoke_one_time_callback( |
| 2605 | channel_T *channel, |
| 2606 | cbq_T *cbhead, |
| 2607 | cbq_T *item, |
| 2608 | typval_T *argv) |
| 2609 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2610 | ch_log(channel, "Invoking one-time callback %s", |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 2611 | (char *)item->cq_callback.cb_name); |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2612 | // Remove the item from the list first, if the callback |
| 2613 | // invokes ch_close() the list will be cleared. |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2614 | remove_cb_node(cbhead, item); |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 2615 | invoke_callback(channel, &item->cq_callback, argv); |
| 2616 | free_callback(&item->cq_callback); |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2617 | vim_free(item); |
| 2618 | } |
| 2619 | |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2620 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2621 | append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2622 | { |
Bram Moolenaar | ad9ec5e | 2020-09-10 21:25:45 +0200 | [diff] [blame] | 2623 | aco_save_T aco; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2624 | linenr_T lnum = buffer->b_ml.ml_line_count; |
| 2625 | int save_write_to = buffer->b_write_to_channel; |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 2626 | chanpart_T *ch_part = &channel->ch_part[part]; |
| 2627 | int save_p_ma = buffer->b_p_ma; |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 2628 | int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0; |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 2629 | |
| 2630 | if (!buffer->b_p_ma && !ch_part->ch_nomodifiable) |
| 2631 | { |
| 2632 | if (!ch_part->ch_nomod_error) |
| 2633 | { |
| 2634 | ch_error(channel, "Buffer is not modifiable, cannot append"); |
| 2635 | ch_part->ch_nomod_error = TRUE; |
| 2636 | } |
| 2637 | return; |
| 2638 | } |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2639 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2640 | // If the buffer is also used as input insert above the last |
| 2641 | // line. Don't write these lines. |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2642 | if (save_write_to) |
| 2643 | { |
| 2644 | --lnum; |
| 2645 | buffer->b_write_to_channel = FALSE; |
| 2646 | } |
| 2647 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2648 | // Append to the buffer |
Bram Moolenaar | ad9ec5e | 2020-09-10 21:25:45 +0200 | [diff] [blame] | 2649 | ch_log(channel, "appending line %d to buffer %s", |
| 2650 | (int)lnum + 1 - empty, buffer->b_fname); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2651 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 2652 | buffer->b_p_ma = TRUE; |
Bram Moolenaar | 6b7355a | 2017-08-04 21:37:54 +0200 | [diff] [blame] | 2653 | |
Bram Moolenaar | ad9ec5e | 2020-09-10 21:25:45 +0200 | [diff] [blame] | 2654 | // set curbuf to be our buf, temporarily |
| 2655 | aucmd_prepbuf(&aco, buffer); |
Bram Moolenaar | 6b7355a | 2017-08-04 21:37:54 +0200 | [diff] [blame] | 2656 | |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2657 | u_sync(TRUE); |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2658 | // ignore undo failure, undo is not very useful here |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 2659 | vim_ignored = u_save(lnum - empty, lnum + 1); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2660 | |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 2661 | if (empty) |
| 2662 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2663 | // The buffer is empty, replace the first (dummy) line. |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 2664 | ml_replace(lnum, msg, TRUE); |
| 2665 | lnum = 0; |
| 2666 | } |
| 2667 | else |
| 2668 | ml_append(lnum, msg, 0, FALSE); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2669 | appended_lines_mark(lnum, 1L); |
Bram Moolenaar | 6b7355a | 2017-08-04 21:37:54 +0200 | [diff] [blame] | 2670 | |
Bram Moolenaar | ad9ec5e | 2020-09-10 21:25:45 +0200 | [diff] [blame] | 2671 | // reset notion of buffer |
| 2672 | aucmd_restbuf(&aco); |
Bram Moolenaar | 6b7355a | 2017-08-04 21:37:54 +0200 | [diff] [blame] | 2673 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 2674 | if (ch_part->ch_nomodifiable) |
| 2675 | buffer->b_p_ma = FALSE; |
| 2676 | else |
| 2677 | buffer->b_p_ma = save_p_ma; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2678 | |
| 2679 | if (buffer->b_nwindows > 0) |
| 2680 | { |
| 2681 | win_T *wp; |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2682 | |
| 2683 | FOR_ALL_WINDOWS(wp) |
| 2684 | { |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2685 | if (wp->w_buffer == buffer) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2686 | { |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2687 | int move_cursor = save_write_to |
| 2688 | ? wp->w_cursor.lnum == lnum + 1 |
| 2689 | : (wp->w_cursor.lnum == lnum |
| 2690 | && wp->w_cursor.col == 0); |
| 2691 | |
| 2692 | // If the cursor is at or above the new line, move it one line |
| 2693 | // down. If the topline is outdated update it now. |
| 2694 | if (move_cursor || wp->w_topline > buffer->b_ml.ml_line_count) |
| 2695 | { |
Bram Moolenaar | ad9ec5e | 2020-09-10 21:25:45 +0200 | [diff] [blame] | 2696 | win_T *save_curwin = curwin; |
| 2697 | |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2698 | if (move_cursor) |
| 2699 | ++wp->w_cursor.lnum; |
Bram Moolenaar | 4641a12 | 2019-07-29 22:10:23 +0200 | [diff] [blame] | 2700 | curwin = wp; |
| 2701 | curbuf = curwin->w_buffer; |
| 2702 | scroll_cursor_bot(0, FALSE); |
| 2703 | curwin = save_curwin; |
| 2704 | curbuf = curwin->w_buffer; |
| 2705 | } |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2706 | } |
| 2707 | } |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 2708 | redraw_buf_and_status_later(buffer, VALID); |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2709 | channel_need_redraw = TRUE; |
| 2710 | } |
| 2711 | |
| 2712 | if (save_write_to) |
| 2713 | { |
| 2714 | channel_T *ch; |
| 2715 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2716 | // Find channels reading from this buffer and adjust their |
| 2717 | // next-to-read line number. |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2718 | buffer->b_write_to_channel = TRUE; |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 2719 | FOR_ALL_CHANNELS(ch) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2720 | { |
| 2721 | chanpart_T *in_part = &ch->ch_part[PART_IN]; |
| 2722 | |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2723 | if (in_part->ch_bufref.br_buf == buffer) |
Bram Moolenaar | 99ef062 | 2016-03-06 20:22:25 +0100 | [diff] [blame] | 2724 | in_part->ch_buf_bot = buffer->b_ml.ml_line_count; |
| 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 2729 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2730 | drop_messages(channel_T *channel, ch_part_T part) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 2731 | { |
| 2732 | char_u *msg; |
| 2733 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 2734 | while ((msg = channel_get(channel, part, NULL)) != NULL) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 2735 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2736 | ch_log(channel, "Dropping message '%s'", (char *)msg); |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 2737 | vim_free(msg); |
| 2738 | } |
| 2739 | } |
| 2740 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2741 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2742 | * Invoke a callback for "channel"/"part" if needed. |
Bram Moolenaar | 7f7c332 | 2016-04-18 19:27:24 +0200 | [diff] [blame] | 2743 | * This does not redraw but sets channel_need_redraw when redraw is needed. |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2744 | * Return TRUE when a message was handled, there might be another one. |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2745 | */ |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2746 | static int |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 2747 | may_invoke_callback(channel_T *channel, ch_part_T part) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2748 | { |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2749 | char_u *msg = NULL; |
| 2750 | typval_T *listtv = NULL; |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2751 | typval_T argv[CH_JSON_MAX_ARGS]; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2752 | int seq_nr = -1; |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 2753 | chanpart_T *ch_part = &channel->ch_part[part]; |
| 2754 | ch_mode_T ch_mode = ch_part->ch_mode; |
| 2755 | cbq_T *cbhead = &ch_part->ch_cb_head; |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 2756 | cbq_T *cbitem; |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 2757 | callback_T *callback = NULL; |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2758 | buf_T *buffer = NULL; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2759 | char_u *p; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2760 | |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 2761 | if (channel->ch_nb_close_cb != NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2762 | // this channel is handled elsewhere (netbeans) |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2763 | return FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2764 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2765 | // Use a message-specific callback, part callback or channel callback |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 2766 | for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next) |
| 2767 | if (cbitem->cq_seq_nr == 0) |
| 2768 | break; |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2769 | if (cbitem != NULL) |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 2770 | callback = &cbitem->cq_callback; |
| 2771 | else if (ch_part->ch_callback.cb_name != NULL) |
| 2772 | callback = &ch_part->ch_callback; |
| 2773 | else if (channel->ch_callback.cb_name != NULL) |
| 2774 | callback = &channel->ch_callback; |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 2775 | |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 2776 | buffer = ch_part->ch_bufref.br_buf; |
Bram Moolenaar | c4da113 | 2017-07-15 19:39:43 +0200 | [diff] [blame] | 2777 | if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref) |
| 2778 | || buffer->b_ml.ml_mfp == NULL)) |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 2779 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2780 | // buffer was wiped out or unloaded |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2781 | ch_log(channel, "%s buffer has been wiped out", part_names[part]); |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 2782 | ch_part->ch_bufref.br_buf = NULL; |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 2783 | buffer = NULL; |
| 2784 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2785 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2786 | if (ch_mode == MODE_JSON || ch_mode == MODE_JS) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2787 | { |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2788 | listitem_T *item; |
| 2789 | int argc = 0; |
| 2790 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2791 | // Get any json message in the queue. |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2792 | if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL) |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2793 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2794 | // Parse readahead, return when there is still no message. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2795 | channel_parse_json(channel, part); |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2796 | if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2797 | return FALSE; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2798 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2799 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2800 | for (item = listtv->vval.v_list->lv_first; |
| 2801 | item != NULL && argc < CH_JSON_MAX_ARGS; |
| 2802 | item = item->li_next) |
| 2803 | argv[argc++] = item->li_tv; |
| 2804 | while (argc < CH_JSON_MAX_ARGS) |
| 2805 | argv[argc++].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2806 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2807 | if (argv[0].v_type == VAR_STRING) |
| 2808 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2809 | // ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2810 | channel_exe_cmd(channel, part, argv); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2811 | free_tv(listtv); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2812 | return TRUE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2815 | if (argv[0].v_type != VAR_NUMBER) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2816 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2817 | ch_error(channel, |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 2818 | "Dropping message with invalid sequence number type"); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2819 | free_tv(listtv); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2820 | return FALSE; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2821 | } |
Bram Moolenaar | ece61b0 | 2016-02-20 21:39:05 +0100 | [diff] [blame] | 2822 | seq_nr = argv[0].vval.v_number; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2823 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2824 | else if (channel_peek(channel, part) == NULL) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2825 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2826 | // nothing to read on RAW or NL channel |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 2827 | return FALSE; |
| 2828 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2829 | else |
| 2830 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2831 | // If there is no callback or buffer drop the message. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2832 | if (callback == NULL && buffer == NULL) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2833 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2834 | // If there is a close callback it may use ch_read() to get the |
| 2835 | // messages. |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 2836 | if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 2837 | drop_messages(channel, part); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2838 | return FALSE; |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2839 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2840 | |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2841 | if (ch_mode == MODE_NL) |
| 2842 | { |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 2843 | char_u *nl = NULL; |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2844 | char_u *buf; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2845 | readq_T *node; |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2846 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2847 | // See if we have a message ending in NL in the first buffer. If |
| 2848 | // not try to concatenate the first and the second buffer. |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2849 | while (TRUE) |
| 2850 | { |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2851 | node = channel_peek(channel, part); |
| 2852 | nl = channel_first_nl(node); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2853 | if (nl != NULL) |
| 2854 | break; |
Bram Moolenaar | 9ed96ef | 2016-06-04 17:17:11 +0200 | [diff] [blame] | 2855 | if (channel_collapse(channel, part, TRUE) == FAIL) |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 2856 | { |
| 2857 | if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0) |
| 2858 | break; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2859 | return FALSE; // incomplete message |
Bram Moolenaar | ec68a99 | 2016-10-03 21:37:41 +0200 | [diff] [blame] | 2860 | } |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2861 | } |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2862 | buf = node->rq_buffer; |
| 2863 | |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 2864 | // Convert NUL to NL, the internal representation. |
| 2865 | for (p = buf; (nl == NULL || p < nl) |
| 2866 | && p < buf + node->rq_buflen; ++p) |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2867 | if (*p == NUL) |
| 2868 | *p = NL; |
| 2869 | |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 2870 | if (nl == NULL) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2871 | { |
Bram Moolenaar | 772153f | 2019-03-04 12:09:49 +0100 | [diff] [blame] | 2872 | // get the whole buffer, drop the NL |
| 2873 | msg = channel_get(channel, part, NULL); |
| 2874 | } |
| 2875 | else if (nl + 1 == buf + node->rq_buflen) |
| 2876 | { |
| 2877 | // get the whole buffer |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 2878 | msg = channel_get(channel, part, NULL); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2879 | *nl = NUL; |
| 2880 | } |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2881 | else |
| 2882 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2883 | // Copy the message into allocated memory (excluding the NL) |
| 2884 | // and remove it from the buffer (including the NL). |
Bram Moolenaar | df44a27 | 2020-06-07 20:49:05 +0200 | [diff] [blame] | 2885 | msg = vim_strnsave(buf, nl - buf); |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2886 | channel_consume(channel, part, (int)(nl - buf) + 1); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | else |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2890 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2891 | // For a raw channel we don't know where the message ends, just |
| 2892 | // get everything we have. |
| 2893 | // Convert NUL to NL, the internal representation. |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 2894 | msg = channel_get_all(channel, part, NULL); |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 2895 | } |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 2896 | |
Bram Moolenaar | bf73b91 | 2016-03-02 21:16:59 +0100 | [diff] [blame] | 2897 | if (msg == NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2898 | return FALSE; // out of memory (and avoids Coverity warning) |
Bram Moolenaar | bf73b91 | 2016-03-02 21:16:59 +0100 | [diff] [blame] | 2899 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2900 | argv[1].v_type = VAR_STRING; |
| 2901 | argv[1].vval.v_string = msg; |
| 2902 | } |
| 2903 | |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2904 | if (seq_nr > 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2905 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2906 | int done = FALSE; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2907 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2908 | // JSON or JS mode: invoke the one-time callback with the matching nr |
Bram Moolenaar | 5983ad0 | 2016-03-05 20:54:36 +0100 | [diff] [blame] | 2909 | for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next) |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2910 | if (cbitem->cq_seq_nr == seq_nr) |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2911 | { |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2912 | invoke_one_time_callback(channel, cbhead, cbitem, argv); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2913 | done = TRUE; |
Bram Moolenaar | a07fec9 | 2016-02-05 21:04:08 +0100 | [diff] [blame] | 2914 | break; |
| 2915 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2916 | if (!done) |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2917 | { |
| 2918 | if (channel->ch_drop_never) |
| 2919 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2920 | // message must be read with ch_read() |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2921 | channel_push_json(channel, part, listtv); |
| 2922 | listtv = NULL; |
| 2923 | } |
| 2924 | else |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2925 | ch_log(channel, "Dropping message %d without callback", |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 2926 | seq_nr); |
| 2927 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2928 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2929 | else if (callback != NULL || buffer != NULL) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2930 | { |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2931 | if (buffer != NULL) |
| 2932 | { |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 2933 | if (msg == NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2934 | // JSON or JS mode: re-encode the message. |
Bram Moolenaar | cc7f8be | 2016-02-29 22:55:56 +0100 | [diff] [blame] | 2935 | msg = json_encode(listtv, ch_mode); |
| 2936 | if (msg != NULL) |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 2937 | { |
Bram Moolenaar | c0aa482 | 2017-07-16 14:04:29 +0200 | [diff] [blame] | 2938 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 2939 | if (buffer->b_term != NULL) |
| 2940 | write_to_term(buffer, msg, channel); |
| 2941 | else |
Bram Moolenaar | c0aa482 | 2017-07-16 14:04:29 +0200 | [diff] [blame] | 2942 | #endif |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 2943 | append_to_buffer(buffer, msg, channel, part); |
| 2944 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2945 | } |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2946 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2947 | if (callback != NULL) |
| 2948 | { |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2949 | if (cbitem != NULL) |
| 2950 | invoke_one_time_callback(channel, cbhead, cbitem, argv); |
| 2951 | else |
| 2952 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2953 | // invoke the channel callback |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2954 | ch_log(channel, "Invoking channel callback %s", |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 2955 | (char *)callback->cb_name); |
| 2956 | invoke_callback(channel, callback, argv); |
Bram Moolenaar | d6547fc | 2016-03-03 19:35:02 +0100 | [diff] [blame] | 2957 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 2958 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2959 | } |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2960 | else |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 2961 | ch_log(channel, "Dropping message %d", seq_nr); |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 2962 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 2963 | if (listtv != NULL) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2964 | free_tv(listtv); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2965 | vim_free(msg); |
Bram Moolenaar | df5b27b | 2016-02-02 18:43:17 +0100 | [diff] [blame] | 2966 | |
| 2967 | return TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2968 | } |
| 2969 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 2970 | #if defined(FEAT_NETBEANS_INTG) || defined(PROTO) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2971 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2972 | * Return TRUE when channel "channel" is open for writing to. |
| 2973 | * Also returns FALSE or invalid "channel". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2974 | */ |
| 2975 | int |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2976 | channel_can_write_to(channel_T *channel) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2977 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2978 | return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 2979 | || channel->CH_IN_FD != INVALID_FD); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2980 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 2981 | #endif |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 2982 | |
| 2983 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2984 | * Return TRUE when channel "channel" is open for reading or writing. |
| 2985 | * Also returns FALSE for invalid "channel". |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2986 | */ |
| 2987 | int |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 2988 | channel_is_open(channel_T *channel) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2989 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2990 | return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 2991 | || channel->CH_IN_FD != INVALID_FD |
| 2992 | || channel->CH_OUT_FD != INVALID_FD |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 2993 | || channel->CH_ERR_FD != INVALID_FD); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | /* |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 2997 | * Return TRUE if "channel" has JSON or other typeahead. |
| 2998 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 2999 | static int |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3000 | channel_has_readahead(channel_T *channel, ch_part_T part) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3001 | { |
| 3002 | ch_mode_T ch_mode = channel->ch_part[part].ch_mode; |
| 3003 | |
| 3004 | if (ch_mode == MODE_JSON || ch_mode == MODE_JS) |
| 3005 | { |
| 3006 | jsonq_T *head = &channel->ch_part[part].ch_json_head; |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3007 | |
Bram Moolenaar | 4340fc9 | 2019-06-28 22:06:49 +0200 | [diff] [blame] | 3008 | if (head->jq_next == NULL) |
| 3009 | // Parse json from readahead, there might be a complete message to |
| 3010 | // process. |
| 3011 | channel_parse_json(channel, part); |
| 3012 | |
| 3013 | return head->jq_next != NULL; |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3014 | } |
| 3015 | return channel_peek(channel, part) != NULL; |
| 3016 | } |
| 3017 | |
| 3018 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3019 | * Return a string indicating the status of the channel. |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3020 | * If "req_part" is not negative check that part. |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3021 | */ |
| 3022 | char * |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3023 | channel_status(channel_T *channel, int req_part) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3024 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3025 | ch_part_T part; |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3026 | int has_readahead = FALSE; |
| 3027 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3028 | if (channel == NULL) |
| 3029 | return "fail"; |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3030 | if (req_part == PART_OUT) |
| 3031 | { |
| 3032 | if (channel->CH_OUT_FD != INVALID_FD) |
| 3033 | return "open"; |
| 3034 | if (channel_has_readahead(channel, PART_OUT)) |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3035 | has_readahead = TRUE; |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3036 | } |
| 3037 | else if (req_part == PART_ERR) |
| 3038 | { |
| 3039 | if (channel->CH_ERR_FD != INVALID_FD) |
| 3040 | return "open"; |
| 3041 | if (channel_has_readahead(channel, PART_ERR)) |
| 3042 | has_readahead = TRUE; |
| 3043 | } |
| 3044 | else |
| 3045 | { |
| 3046 | if (channel_is_open(channel)) |
| 3047 | return "open"; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3048 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3049 | if (channel_has_readahead(channel, part)) |
| 3050 | { |
| 3051 | has_readahead = TRUE; |
| 3052 | break; |
| 3053 | } |
| 3054 | } |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3055 | |
| 3056 | if (has_readahead) |
| 3057 | return "buffered"; |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3058 | return "closed"; |
| 3059 | } |
| 3060 | |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3061 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3062 | channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part) |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3063 | { |
| 3064 | chanpart_T *chanpart = &channel->ch_part[part]; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3065 | char namebuf[20]; // longest is "sock_timeout" |
Bram Moolenaar | 3f3fbd3 | 2016-03-21 12:36:28 +0100 | [diff] [blame] | 3066 | size_t tail; |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3067 | char *status; |
Bram Moolenaar | 573e445 | 2016-03-21 22:35:10 +0100 | [diff] [blame] | 3068 | char *s = ""; |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3069 | |
Bram Moolenaar | 925ccfd | 2016-03-28 22:38:02 +0200 | [diff] [blame] | 3070 | vim_strncpy((char_u *)namebuf, (char_u *)name, 4); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3071 | STRCAT(namebuf, "_"); |
| 3072 | tail = STRLEN(namebuf); |
| 3073 | |
| 3074 | STRCPY(namebuf + tail, "status"); |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 3075 | if (chanpart->ch_fd != INVALID_FD) |
| 3076 | status = "open"; |
| 3077 | else if (channel_has_readahead(channel, part)) |
| 3078 | status = "buffered"; |
| 3079 | else |
| 3080 | status = "closed"; |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3081 | dict_add_string(dict, namebuf, (char_u *)status); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3082 | |
| 3083 | STRCPY(namebuf + tail, "mode"); |
| 3084 | switch (chanpart->ch_mode) |
| 3085 | { |
| 3086 | case MODE_NL: s = "NL"; break; |
| 3087 | case MODE_RAW: s = "RAW"; break; |
| 3088 | case MODE_JSON: s = "JSON"; break; |
| 3089 | case MODE_JS: s = "JS"; break; |
| 3090 | } |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3091 | dict_add_string(dict, namebuf, (char_u *)s); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3092 | |
| 3093 | STRCPY(namebuf + tail, "io"); |
| 3094 | if (part == PART_SOCK) |
| 3095 | s = "socket"; |
| 3096 | else switch (chanpart->ch_io) |
| 3097 | { |
| 3098 | case JIO_NULL: s = "null"; break; |
| 3099 | case JIO_PIPE: s = "pipe"; break; |
| 3100 | case JIO_FILE: s = "file"; break; |
| 3101 | case JIO_BUFFER: s = "buffer"; break; |
| 3102 | case JIO_OUT: s = "out"; break; |
| 3103 | } |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3104 | dict_add_string(dict, namebuf, (char_u *)s); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3105 | |
| 3106 | STRCPY(namebuf + tail, "timeout"); |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3107 | dict_add_number(dict, namebuf, chanpart->ch_timeout); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3108 | } |
| 3109 | |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 3110 | static void |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3111 | channel_info(channel_T *channel, dict_T *dict) |
| 3112 | { |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3113 | dict_add_number(dict, "id", channel->ch_id); |
| 3114 | dict_add_string(dict, "status", (char_u *)channel_status(channel, -1)); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3115 | |
| 3116 | if (channel->ch_hostname != NULL) |
| 3117 | { |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 3118 | dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname); |
| 3119 | dict_add_number(dict, "port", channel->ch_port); |
Bram Moolenaar | 03602ec | 2016-03-20 20:57:45 +0100 | [diff] [blame] | 3120 | channel_part_info(channel, dict, "sock", PART_SOCK); |
| 3121 | } |
| 3122 | else |
| 3123 | { |
| 3124 | channel_part_info(channel, dict, "out", PART_OUT); |
| 3125 | channel_part_info(channel, dict, "err", PART_ERR); |
| 3126 | channel_part_info(channel, dict, "in", PART_IN); |
| 3127 | } |
| 3128 | } |
| 3129 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3130 | /* |
| 3131 | * Close channel "channel". |
Bram Moolenaar | c8dcbb1 | 2016-02-25 23:10:17 +0100 | [diff] [blame] | 3132 | * Trigger the close callback if "invoke_close_cb" is TRUE. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 3133 | * Does not clear the buffers. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3134 | */ |
| 3135 | void |
Bram Moolenaar | 8b37421 | 2016-02-24 20:43:06 +0100 | [diff] [blame] | 3136 | channel_close(channel_T *channel, int invoke_close_cb) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3137 | { |
Bram Moolenaar | 81661fb | 2016-02-18 22:23:34 +0100 | [diff] [blame] | 3138 | ch_log(channel, "Closing channel"); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3139 | |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 3140 | #ifdef FEAT_GUI |
| 3141 | channel_gui_unregister(channel); |
| 3142 | #endif |
| 3143 | |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3144 | ch_close_part(channel, PART_SOCK); |
| 3145 | ch_close_part(channel, PART_IN); |
| 3146 | ch_close_part(channel, PART_OUT); |
| 3147 | ch_close_part(channel, PART_ERR); |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 3148 | |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3149 | if (invoke_close_cb) |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 3150 | { |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3151 | ch_part_T part; |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 3152 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3153 | // Invoke callbacks and flush buffers before the close callback. |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3154 | if (channel->ch_close_cb.cb_name != NULL) |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3155 | ch_log(channel, |
| 3156 | "Invoking callbacks and flushing buffers before closing"); |
| 3157 | for (part = PART_SOCK; part < PART_IN; ++part) |
| 3158 | { |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3159 | if (channel->ch_close_cb.cb_name != NULL |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3160 | || channel->ch_part[part].ch_bufref.br_buf != NULL) |
| 3161 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3162 | // Increment the refcount to avoid the channel being freed |
| 3163 | // halfway. |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3164 | ++channel->ch_refcount; |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3165 | if (channel->ch_close_cb.cb_name == NULL) |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3166 | ch_log(channel, "flushing %s buffers before closing", |
| 3167 | part_names[part]); |
| 3168 | while (may_invoke_callback(channel, part)) |
| 3169 | ; |
| 3170 | --channel->ch_refcount; |
| 3171 | } |
| 3172 | } |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3173 | |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3174 | if (channel->ch_close_cb.cb_name != NULL) |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3175 | { |
| 3176 | typval_T argv[1]; |
| 3177 | typval_T rettv; |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3178 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3179 | // Increment the refcount to avoid the channel being freed |
| 3180 | // halfway. |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3181 | ++channel->ch_refcount; |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3182 | ch_log(channel, "Invoking close callback %s", |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3183 | (char *)channel->ch_close_cb.cb_name); |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3184 | argv[0].v_type = VAR_CHANNEL; |
| 3185 | argv[0].vval.v_channel = channel; |
Bram Moolenaar | c6538bc | 2019-08-03 18:17:11 +0200 | [diff] [blame] | 3186 | call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv); |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3187 | clear_tv(&rettv); |
Bram Moolenaar | cefe4f9 | 2016-05-04 21:49:19 +0200 | [diff] [blame] | 3188 | channel_need_redraw = TRUE; |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 3189 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3190 | // the callback is only called once |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3191 | free_callback(&channel->ch_close_cb); |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3192 | |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3193 | if (channel_need_redraw) |
| 3194 | { |
| 3195 | channel_need_redraw = FALSE; |
Bram Moolenaar | 02e177d | 2017-08-26 23:43:28 +0200 | [diff] [blame] | 3196 | redraw_after_callback(TRUE); |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3197 | } |
Bram Moolenaar | cefe4f9 | 2016-05-04 21:49:19 +0200 | [diff] [blame] | 3198 | |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3199 | if (!channel->ch_drop_never) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3200 | // any remaining messages are useless now |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3201 | for (part = PART_SOCK; part < PART_IN; ++part) |
| 3202 | drop_messages(channel, part); |
Bram Moolenaar | 5fd8b78 | 2017-11-11 15:54:00 +0100 | [diff] [blame] | 3203 | |
| 3204 | --channel->ch_refcount; |
Bram Moolenaar | a9f0281 | 2017-08-05 17:40:38 +0200 | [diff] [blame] | 3205 | } |
Bram Moolenaar | 4e221c9 | 2016-02-23 13:20:22 +0100 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | channel->ch_nb_close_cb = NULL; |
Bram Moolenaar | d85f271 | 2017-07-28 21:51:57 +0200 | [diff] [blame] | 3209 | |
| 3210 | #ifdef FEAT_TERMINAL |
| 3211 | term_channel_closed(channel); |
| 3212 | #endif |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 3213 | } |
| 3214 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3215 | /* |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 3216 | * Close the "in" part channel "channel". |
| 3217 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 3218 | static void |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 3219 | channel_close_in(channel_T *channel) |
| 3220 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3221 | ch_close_part(channel, PART_IN); |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Bram Moolenaar | aba680a | 2017-09-09 16:42:53 +0200 | [diff] [blame] | 3224 | static void |
| 3225 | remove_from_writeque(writeq_T *wq, writeq_T *entry) |
| 3226 | { |
| 3227 | ga_clear(&entry->wq_ga); |
| 3228 | wq->wq_next = entry->wq_next; |
| 3229 | if (wq->wq_next == NULL) |
| 3230 | wq->wq_prev = NULL; |
| 3231 | else |
| 3232 | wq->wq_next->wq_prev = NULL; |
Bram Moolenaar | 5b5adf5 | 2017-09-09 18:16:43 +0200 | [diff] [blame] | 3233 | vim_free(entry); |
Bram Moolenaar | aba680a | 2017-09-09 16:42:53 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 3236 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3237 | * Clear the read buffer on "channel"/"part". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3238 | */ |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3239 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3240 | channel_clear_one(channel_T *channel, ch_part_T part) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3241 | { |
Bram Moolenaar | aba680a | 2017-09-09 16:42:53 +0200 | [diff] [blame] | 3242 | chanpart_T *ch_part = &channel->ch_part[part]; |
| 3243 | jsonq_T *json_head = &ch_part->ch_json_head; |
| 3244 | cbq_T *cb_head = &ch_part->ch_cb_head; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3245 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3246 | while (channel_peek(channel, part) != NULL) |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3247 | vim_free(channel_get(channel, part, NULL)); |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3248 | |
| 3249 | while (cb_head->cq_next != NULL) |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 3250 | { |
| 3251 | cbq_T *node = cb_head->cq_next; |
| 3252 | |
| 3253 | remove_cb_node(cb_head, node); |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3254 | free_callback(&node->cq_callback); |
Bram Moolenaar | d46ae14 | 2016-02-16 13:33:52 +0100 | [diff] [blame] | 3255 | vim_free(node); |
| 3256 | } |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3257 | |
| 3258 | while (json_head->jq_next != NULL) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3259 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3260 | free_tv(json_head->jq_next->jq_value); |
| 3261 | remove_json_node(json_head, json_head->jq_next); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3262 | } |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 3263 | |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3264 | free_callback(&ch_part->ch_callback); |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3265 | ga_clear(&ch_part->ch_block_ids); |
Bram Moolenaar | aba680a | 2017-09-09 16:42:53 +0200 | [diff] [blame] | 3266 | |
| 3267 | while (ch_part->ch_writeque.wq_next != NULL) |
| 3268 | remove_from_writeque(&ch_part->ch_writeque, |
| 3269 | ch_part->ch_writeque.wq_next); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | /* |
| 3273 | * Clear all the read buffers on "channel". |
| 3274 | */ |
| 3275 | void |
| 3276 | channel_clear(channel_T *channel) |
| 3277 | { |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 3278 | ch_log(channel, "Clearing channel"); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3279 | VIM_CLEAR(channel->ch_hostname); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3280 | channel_clear_one(channel, PART_SOCK); |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3281 | channel_clear_one(channel, PART_OUT); |
| 3282 | channel_clear_one(channel, PART_ERR); |
Bram Moolenaar | 5b5adf5 | 2017-09-09 18:16:43 +0200 | [diff] [blame] | 3283 | channel_clear_one(channel, PART_IN); |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 3284 | free_callback(&channel->ch_callback); |
| 3285 | free_callback(&channel->ch_close_cb); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3286 | } |
| 3287 | |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3288 | #if defined(EXITFREE) || defined(PROTO) |
| 3289 | void |
| 3290 | channel_free_all(void) |
| 3291 | { |
| 3292 | channel_T *channel; |
| 3293 | |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 3294 | ch_log(NULL, "channel_free_all()"); |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3295 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3296 | channel_clear(channel); |
| 3297 | } |
| 3298 | #endif |
| 3299 | |
| 3300 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3301 | // Sent when the netbeans channel is found closed when reading. |
Bram Moolenaar | eed284a | 2016-02-22 23:13:33 +0100 | [diff] [blame] | 3302 | #define DETACH_MSG_RAW "DETACH\n" |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3303 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3304 | // Buffer size for reading incoming messages. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3305 | #define MAXMSGSIZE 4096 |
| 3306 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3307 | #if defined(HAVE_SELECT) |
| 3308 | /* |
| 3309 | * Add write fds where we are waiting for writing to be possible. |
| 3310 | */ |
| 3311 | static int |
| 3312 | channel_fill_wfds(int maxfd_arg, fd_set *wfds) |
| 3313 | { |
| 3314 | int maxfd = maxfd_arg; |
| 3315 | channel_T *ch; |
| 3316 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3317 | FOR_ALL_CHANNELS(ch) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3318 | { |
| 3319 | chanpart_T *in_part = &ch->ch_part[PART_IN]; |
| 3320 | |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 3321 | if (in_part->ch_fd != INVALID_FD |
| 3322 | && (in_part->ch_bufref.br_buf != NULL |
| 3323 | || in_part->ch_writeque.wq_next != NULL)) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3324 | { |
| 3325 | FD_SET((int)in_part->ch_fd, wfds); |
| 3326 | if ((int)in_part->ch_fd >= maxfd) |
| 3327 | maxfd = (int)in_part->ch_fd + 1; |
| 3328 | } |
| 3329 | } |
| 3330 | return maxfd; |
| 3331 | } |
| 3332 | #else |
| 3333 | /* |
| 3334 | * Add write fds where we are waiting for writing to be possible. |
| 3335 | */ |
| 3336 | static int |
| 3337 | channel_fill_poll_write(int nfd_in, struct pollfd *fds) |
| 3338 | { |
| 3339 | int nfd = nfd_in; |
| 3340 | channel_T *ch; |
| 3341 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3342 | FOR_ALL_CHANNELS(ch) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3343 | { |
| 3344 | chanpart_T *in_part = &ch->ch_part[PART_IN]; |
| 3345 | |
Bram Moolenaar | 683b796 | 2017-08-19 15:51:59 +0200 | [diff] [blame] | 3346 | if (in_part->ch_fd != INVALID_FD |
| 3347 | && (in_part->ch_bufref.br_buf != NULL |
| 3348 | || in_part->ch_writeque.wq_next != NULL)) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3349 | { |
| 3350 | in_part->ch_poll_idx = nfd; |
| 3351 | fds[nfd].fd = in_part->ch_fd; |
| 3352 | fds[nfd].events = POLLOUT; |
| 3353 | ++nfd; |
| 3354 | } |
| 3355 | else |
| 3356 | in_part->ch_poll_idx = -1; |
| 3357 | } |
| 3358 | return nfd; |
| 3359 | } |
| 3360 | #endif |
| 3361 | |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3362 | typedef enum { |
| 3363 | CW_READY, |
| 3364 | CW_NOT_READY, |
| 3365 | CW_ERROR |
| 3366 | } channel_wait_result; |
| 3367 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3368 | /* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3369 | * Check for reading from "fd" with "timeout" msec. |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3370 | * Return CW_READY when there is something to read. |
| 3371 | * Return CW_NOT_READY when there is nothing to read. |
| 3372 | * Return CW_ERROR when there is an error. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3373 | */ |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3374 | static channel_wait_result |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3375 | channel_wait(channel_T *channel, sock_T fd, int timeout) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3376 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 3377 | if (timeout > 0) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3378 | ch_log(channel, "Waiting for up to %d msec", timeout); |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3379 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3380 | # ifdef MSWIN |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3381 | if (fd != channel->CH_SOCK_FD) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3382 | { |
| 3383 | DWORD nread; |
Bram Moolenaar | 84e1d2b | 2016-03-28 14:20:41 +0200 | [diff] [blame] | 3384 | int sleep_time; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3385 | DWORD deadline = GetTickCount() + timeout; |
Bram Moolenaar | 84e1d2b | 2016-03-28 14:20:41 +0200 | [diff] [blame] | 3386 | int delay = 1; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3387 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3388 | // reading from a pipe, not a socket |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3389 | while (TRUE) |
| 3390 | { |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3391 | int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL); |
| 3392 | |
| 3393 | if (r && nread > 0) |
| 3394 | return CW_READY; |
Bram Moolenaar | 31faed6 | 2019-01-22 23:01:40 +0100 | [diff] [blame] | 3395 | |
| 3396 | if (channel->ch_named_pipe) |
| 3397 | { |
| 3398 | DisconnectNamedPipe((HANDLE)fd); |
| 3399 | ConnectNamedPipe((HANDLE)fd, NULL); |
| 3400 | } |
| 3401 | else if (r == 0) |
Bram Moolenaar | b091f30 | 2019-01-19 14:37:00 +0100 | [diff] [blame] | 3402 | return CW_ERROR; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3403 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3404 | // perhaps write some buffer lines |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3405 | channel_write_any_lines(); |
| 3406 | |
Bram Moolenaar | 84e1d2b | 2016-03-28 14:20:41 +0200 | [diff] [blame] | 3407 | sleep_time = deadline - GetTickCount(); |
| 3408 | if (sleep_time <= 0) |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3409 | break; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3410 | // Wait for a little while. Very short at first, up to 10 msec |
| 3411 | // after looping a few times. |
Bram Moolenaar | 84e1d2b | 2016-03-28 14:20:41 +0200 | [diff] [blame] | 3412 | if (sleep_time > delay) |
| 3413 | sleep_time = delay; |
| 3414 | Sleep(sleep_time); |
| 3415 | delay = delay * 2; |
| 3416 | if (delay > 10) |
| 3417 | delay = 10; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3418 | } |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3419 | } |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3420 | else |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3421 | #endif |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3422 | { |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 3423 | #if defined(HAVE_SELECT) |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3424 | struct timeval tval; |
| 3425 | fd_set rfds; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3426 | fd_set wfds; |
| 3427 | int ret; |
| 3428 | int maxfd; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3429 | |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3430 | tval.tv_sec = timeout / 1000; |
| 3431 | tval.tv_usec = (timeout % 1000) * 1000; |
| 3432 | for (;;) |
| 3433 | { |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3434 | FD_ZERO(&rfds); |
| 3435 | FD_SET((int)fd, &rfds); |
| 3436 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3437 | // Write lines to a pipe when a pipe can be written to. Need to |
| 3438 | // set this every time, some buffers may be done. |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3439 | maxfd = (int)fd + 1; |
| 3440 | FD_ZERO(&wfds); |
| 3441 | maxfd = channel_fill_wfds(maxfd, &wfds); |
| 3442 | |
| 3443 | ret = select(maxfd, &rfds, &wfds, NULL, &tval); |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 3444 | # ifdef EINTR |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3445 | SOCK_ERRNO; |
| 3446 | if (ret == -1 && errno == EINTR) |
| 3447 | continue; |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 3448 | # endif |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3449 | if (ret > 0) |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3450 | { |
| 3451 | if (FD_ISSET(fd, &rfds)) |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3452 | return CW_READY; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3453 | channel_write_any_lines(); |
| 3454 | continue; |
| 3455 | } |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3456 | break; |
| 3457 | } |
Bram Moolenaar | 9186a27 | 2016-02-23 19:34:01 +0100 | [diff] [blame] | 3458 | #else |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3459 | for (;;) |
| 3460 | { |
| 3461 | struct pollfd fds[MAX_OPEN_CHANNELS + 1]; |
| 3462 | int nfd = 1; |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3463 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3464 | fds[0].fd = fd; |
| 3465 | fds[0].events = POLLIN; |
| 3466 | nfd = channel_fill_poll_write(nfd, fds); |
| 3467 | if (poll(fds, nfd, timeout) > 0) |
| 3468 | { |
| 3469 | if (fds[0].revents & POLLIN) |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3470 | return CW_READY; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 3471 | channel_write_any_lines(); |
| 3472 | continue; |
| 3473 | } |
| 3474 | break; |
| 3475 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3476 | #endif |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3477 | } |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3478 | return CW_NOT_READY; |
| 3479 | } |
| 3480 | |
| 3481 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3482 | ch_close_part_on_error( |
| 3483 | channel_T *channel, ch_part_T part, int is_err, char *func) |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3484 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3485 | char msg[] = "%s(): Read %s from ch_part[%d], closing"; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3486 | |
| 3487 | if (is_err) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3488 | // Do not call emsg(), most likely the other end just exited. |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3489 | ch_error(channel, msg, func, "error", part); |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3490 | else |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3491 | ch_log(channel, msg, func, "EOF", part); |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3492 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3493 | // Queue a "DETACH" netbeans message in the command queue in order to |
| 3494 | // terminate the netbeans session later. Do not end the session here |
| 3495 | // directly as we may be running in the context of a call to |
| 3496 | // netbeans_parse_messages(): |
| 3497 | // netbeans_parse_messages |
| 3498 | // -> autocmd triggered while processing the netbeans cmd |
| 3499 | // -> ui_breakcheck |
| 3500 | // -> gui event loop or select loop |
| 3501 | // -> channel_read() |
| 3502 | // Only send "DETACH" for a netbeans channel. |
Bram Moolenaar | 715d285 | 2016-04-30 17:06:31 +0200 | [diff] [blame] | 3503 | if (channel->ch_nb_close_cb != NULL) |
Bram Moolenaar | 8ddef48 | 2016-10-09 15:43:25 +0200 | [diff] [blame] | 3504 | channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW, |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3505 | (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT "); |
| 3506 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3507 | // When reading is not possible close this part of the channel. Don't |
| 3508 | // close the channel yet, there may be something to read on another part. |
| 3509 | // When stdout and stderr use the same FD we get the error only on one of |
| 3510 | // them, also close the other. |
Bram Moolenaar | b0b98d5 | 2018-05-05 21:01:00 +0200 | [diff] [blame] | 3511 | if (part == PART_OUT || part == PART_ERR) |
| 3512 | { |
| 3513 | ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT; |
| 3514 | |
| 3515 | if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd) |
| 3516 | ch_close_part(channel, other); |
| 3517 | } |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3518 | ch_close_part(channel, part); |
Bram Moolenaar | bf981ee | 2016-05-28 13:20:31 +0200 | [diff] [blame] | 3519 | |
| 3520 | #ifdef FEAT_GUI |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3521 | // Stop listening to GUI events right away. |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3522 | channel_gui_unregister_one(channel, part); |
Bram Moolenaar | bf981ee | 2016-05-28 13:20:31 +0200 | [diff] [blame] | 3523 | #endif |
Bram Moolenaar | cf7ff70 | 2016-05-09 17:20:14 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | static void |
| 3527 | channel_close_now(channel_T *channel) |
| 3528 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3529 | ch_log(channel, "Closing channel because all readable fds are closed"); |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3530 | if (channel->ch_nb_close_cb != NULL) |
| 3531 | (*channel->ch_nb_close_cb)(); |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 3532 | channel_close(channel, TRUE); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | /* |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 3536 | * Read from channel "channel" for as long as there is something to read. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3537 | * "part" is PART_SOCK, PART_OUT or PART_ERR. |
Bram Moolenaar | 655da31 | 2016-05-28 22:22:34 +0200 | [diff] [blame] | 3538 | * The data is put in the read queue. No callbacks are invoked here. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3539 | */ |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3540 | static void |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3541 | channel_read(channel_T *channel, ch_part_T part, char *func) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3542 | { |
| 3543 | static char_u *buf = NULL; |
| 3544 | int len = 0; |
| 3545 | int readlen = 0; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3546 | sock_T fd; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 3547 | int use_socket = FALSE; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3548 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3549 | fd = channel->ch_part[part].ch_fd; |
| 3550 | if (fd == INVALID_FD) |
| 3551 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3552 | ch_error(channel, "channel_read() called while %s part is closed", |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3553 | part_names[part]); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3554 | return; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3555 | } |
| 3556 | use_socket = fd == channel->CH_SOCK_FD; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3557 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3558 | // Allocate a buffer to read into. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3559 | if (buf == NULL) |
| 3560 | { |
| 3561 | buf = alloc(MAXMSGSIZE); |
| 3562 | if (buf == NULL) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3563 | return; // out of memory! |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3564 | } |
| 3565 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3566 | // Keep on reading for as long as there is something to read. |
| 3567 | // Use select() or poll() to avoid blocking on a message that is exactly |
| 3568 | // MAXMSGSIZE long. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3569 | for (;;) |
| 3570 | { |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3571 | if (channel_wait(channel, fd, 0) != CW_READY) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3572 | break; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 3573 | if (use_socket) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3574 | len = sock_read(fd, (char *)buf, MAXMSGSIZE); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 3575 | else |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3576 | len = fd_read(fd, (char *)buf, MAXMSGSIZE); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3577 | if (len <= 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3578 | break; // error or nothing more to read |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3579 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3580 | // Store the read message in the queue. |
Bram Moolenaar | 46c00a6 | 2016-03-28 14:11:42 +0200 | [diff] [blame] | 3581 | channel_save(channel, part, buf, len, FALSE, "RECV "); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3582 | readlen += len; |
| 3583 | if (len < MAXMSGSIZE) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3584 | break; // did read everything that's available |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3585 | } |
| 3586 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3587 | // Reading a disconnection (readlen == 0), or an error. |
Bram Moolenaar | bd73ae1 | 2016-02-22 22:19:22 +0100 | [diff] [blame] | 3588 | if (readlen <= 0) |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 3589 | { |
| 3590 | if (!channel->ch_keep_open) |
| 3591 | ch_close_part_on_error(channel, part, (len < 0), func); |
| 3592 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3593 | #if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 3594 | else if (CH_HAS_GUI && gtk_main_level() > 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3595 | // signal the main loop that there is something to read |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3596 | gtk_main_quit(); |
| 3597 | #endif |
| 3598 | } |
| 3599 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3600 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3601 | * Read from RAW or NL "channel"/"part". Blocks until there is something to |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3602 | * read or the timeout expires. |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3603 | * When "raw" is TRUE don't block waiting on a NL. |
Bram Moolenaar | 0e57dd8 | 2019-09-16 22:56:03 +0200 | [diff] [blame] | 3604 | * Does not trigger timers or handle messages. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3605 | * Returns what was read in allocated memory. |
| 3606 | * Returns NULL in case of error or timeout. |
| 3607 | */ |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3608 | static char_u * |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3609 | channel_read_block( |
| 3610 | channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3611 | { |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3612 | char_u *buf; |
| 3613 | char_u *msg; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3614 | ch_mode_T mode = channel->ch_part[part].ch_mode; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3615 | sock_T fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3616 | char_u *nl; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3617 | readq_T *node; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 3618 | |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3619 | ch_log(channel, "Blocking %s read, timeout: %d msec", |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3620 | mode == MODE_RAW ? "RAW" : "NL", timeout); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3621 | |
| 3622 | while (TRUE) |
| 3623 | { |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3624 | node = channel_peek(channel, part); |
| 3625 | if (node != NULL) |
| 3626 | { |
| 3627 | if (mode == MODE_RAW || (mode == MODE_NL |
| 3628 | && channel_first_nl(node) != NULL)) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3629 | // got a complete message |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3630 | break; |
| 3631 | if (channel_collapse(channel, part, mode == MODE_NL) == OK) |
| 3632 | continue; |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3633 | // If not blocking or nothing more is coming then return what we |
| 3634 | // have. |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3635 | if (raw || fd == INVALID_FD) |
| 3636 | break; |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3637 | } |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3638 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3639 | // Wait for up to the channel timeout. |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3640 | if (fd == INVALID_FD) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3641 | return NULL; |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3642 | if (channel_wait(channel, fd, timeout) != CW_READY) |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3643 | { |
| 3644 | ch_log(channel, "Timed out"); |
| 3645 | return NULL; |
| 3646 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3647 | channel_read(channel, part, "channel_read_block"); |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3648 | } |
| 3649 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3650 | // We have a complete message now. |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3651 | if (mode == MODE_RAW || outlen != NULL) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3652 | { |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3653 | msg = channel_get_all(channel, part, outlen); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3654 | } |
| 3655 | else |
| 3656 | { |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3657 | char_u *p; |
| 3658 | |
| 3659 | buf = node->rq_buffer; |
| 3660 | nl = channel_first_nl(node); |
| 3661 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3662 | // Convert NUL to NL, the internal representation. |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3663 | for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p) |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3664 | if (*p == NUL) |
| 3665 | *p = NL; |
| 3666 | |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3667 | if (nl == NULL) |
| 3668 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3669 | // must be a closed channel with missing NL |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3670 | msg = channel_get(channel, part, NULL); |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3671 | } |
| 3672 | else if (nl + 1 == buf + node->rq_buflen) |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3673 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3674 | // get the whole buffer |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3675 | msg = channel_get(channel, part, NULL); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3676 | *nl = NUL; |
| 3677 | } |
| 3678 | else |
| 3679 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3680 | // Copy the message into allocated memory and remove it from the |
| 3681 | // buffer. |
Bram Moolenaar | df44a27 | 2020-06-07 20:49:05 +0200 | [diff] [blame] | 3682 | msg = vim_strnsave(buf, nl - buf); |
Bram Moolenaar | 5f1032d | 2016-06-07 22:16:36 +0200 | [diff] [blame] | 3683 | channel_consume(channel, part, (int)(nl - buf) + 1); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3684 | } |
| 3685 | } |
Bram Moolenaar | 71eeb74 | 2017-09-13 22:18:01 +0200 | [diff] [blame] | 3686 | if (ch_log_active()) |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 3687 | ch_log(channel, "Returning %d bytes", (int)STRLEN(msg)); |
Bram Moolenaar | 9a6e33a | 2016-02-16 19:25:12 +0100 | [diff] [blame] | 3688 | return msg; |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3689 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3690 | |
Bram Moolenaar | 0e57dd8 | 2019-09-16 22:56:03 +0200 | [diff] [blame] | 3691 | static int channel_blocking_wait = 0; |
| 3692 | |
| 3693 | /* |
| 3694 | * Return TRUE if in a blocking wait that might trigger callbacks. |
| 3695 | */ |
| 3696 | int |
| 3697 | channel_in_blocking_wait(void) |
| 3698 | { |
| 3699 | return channel_blocking_wait > 0; |
| 3700 | } |
| 3701 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3702 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3703 | * Read one JSON message with ID "id" from "channel"/"part" and store the |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3704 | * result in "rettv". |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 3705 | * When "id" is -1 accept any message; |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 3706 | * Blocks until the message is received or the timeout is reached. |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3707 | * In corner cases this can be called recursively, that is why ch_block_ids is |
| 3708 | * a list. |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3709 | */ |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 3710 | static int |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 3711 | channel_read_json_block( |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 3712 | channel_T *channel, |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3713 | ch_part_T part, |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3714 | int timeout_arg, |
Bram Moolenaar | d6051b5 | 2016-02-28 15:49:03 +0100 | [diff] [blame] | 3715 | int id, |
| 3716 | typval_T **rettv) |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3717 | { |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 3718 | int more; |
Bram Moolenaar | d807036 | 2016-02-15 21:56:54 +0100 | [diff] [blame] | 3719 | sock_T fd; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3720 | int timeout; |
| 3721 | chanpart_T *chanpart = &channel->ch_part[part]; |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 3722 | int retval = FAIL; |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 3723 | |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3724 | ch_log(channel, "Blocking read JSON for id %d", id); |
Bram Moolenaar | 0e57dd8 | 2019-09-16 22:56:03 +0200 | [diff] [blame] | 3725 | ++channel_blocking_wait; |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 3726 | |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3727 | if (id >= 0) |
| 3728 | channel_add_block_id(chanpart, id); |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 3729 | |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3730 | for (;;) |
| 3731 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 3732 | more = channel_parse_json(channel, part); |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3733 | |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3734 | // search for message "id" |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 3735 | if (channel_get_json(channel, part, id, TRUE, rettv) == OK) |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 3736 | { |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3737 | ch_log(channel, "Received JSON for id %d", id); |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 3738 | retval = OK; |
| 3739 | break; |
Bram Moolenaar | e56bf15 | 2016-02-08 23:23:42 +0100 | [diff] [blame] | 3740 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3741 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 3742 | if (!more) |
| 3743 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3744 | // Handle any other messages in the queue. If done some more |
| 3745 | // messages may have arrived. |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 3746 | if (channel_parse_messages()) |
| 3747 | continue; |
| 3748 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3749 | // Wait for up to the timeout. If there was an incomplete message |
| 3750 | // use the deadline for that. |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3751 | timeout = timeout_arg; |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 3752 | if (chanpart->ch_wait_len > 0) |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3753 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3754 | #ifdef MSWIN |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3755 | timeout = chanpart->ch_deadline - GetTickCount() + 1; |
| 3756 | #else |
| 3757 | { |
| 3758 | struct timeval now_tv; |
| 3759 | |
| 3760 | gettimeofday(&now_tv, NULL); |
| 3761 | timeout = (chanpart->ch_deadline.tv_sec |
| 3762 | - now_tv.tv_sec) * 1000 |
| 3763 | + (chanpart->ch_deadline.tv_usec |
| 3764 | - now_tv.tv_usec) / 1000 |
| 3765 | + 1; |
| 3766 | } |
| 3767 | #endif |
| 3768 | if (timeout < 0) |
| 3769 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3770 | // Something went wrong, channel_parse_json() didn't |
| 3771 | // discard message. Cancel waiting. |
Bram Moolenaar | 88989cc | 2017-02-06 21:56:09 +0100 | [diff] [blame] | 3772 | chanpart->ch_wait_len = 0; |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3773 | timeout = timeout_arg; |
| 3774 | } |
| 3775 | else if (timeout > timeout_arg) |
| 3776 | timeout = timeout_arg; |
| 3777 | } |
| 3778 | fd = chanpart->ch_fd; |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3779 | if (fd == INVALID_FD |
| 3780 | || channel_wait(channel, fd, timeout) != CW_READY) |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3781 | { |
| 3782 | if (timeout == timeout_arg) |
| 3783 | { |
| 3784 | if (fd != INVALID_FD) |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3785 | ch_log(channel, "Timed out on id %d", id); |
Bram Moolenaar | ba61ac0 | 2016-03-20 16:40:37 +0100 | [diff] [blame] | 3786 | break; |
| 3787 | } |
| 3788 | } |
| 3789 | else |
| 3790 | channel_read(channel, part, "channel_read_json_block"); |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 3791 | } |
Bram Moolenaar | 19d2f15 | 2016-02-01 21:38:19 +0100 | [diff] [blame] | 3792 | } |
Bram Moolenaar | 38ea784 | 2019-06-09 19:51:58 +0200 | [diff] [blame] | 3793 | if (id >= 0) |
| 3794 | channel_remove_block_id(chanpart, id); |
Bram Moolenaar | 0e57dd8 | 2019-09-16 22:56:03 +0200 | [diff] [blame] | 3795 | --channel_blocking_wait; |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 3796 | |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 3797 | return retval; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 3798 | } |
| 3799 | |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3800 | /* |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 3801 | * Get the channel from the argument. |
| 3802 | * Returns NULL if the handle is invalid. |
| 3803 | * When "check_open" is TRUE check that the channel can be used. |
| 3804 | * When "reading" is TRUE "check_open" considers typeahead useful. |
| 3805 | * "part" is used to check typeahead, when PART_COUNT use the default part. |
| 3806 | */ |
| 3807 | static channel_T * |
| 3808 | get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part) |
| 3809 | { |
| 3810 | channel_T *channel = NULL; |
| 3811 | int has_readahead = FALSE; |
| 3812 | |
| 3813 | if (tv->v_type == VAR_JOB) |
| 3814 | { |
| 3815 | if (tv->vval.v_job != NULL) |
| 3816 | channel = tv->vval.v_job->jv_channel; |
| 3817 | } |
| 3818 | else if (tv->v_type == VAR_CHANNEL) |
| 3819 | { |
| 3820 | channel = tv->vval.v_channel; |
| 3821 | } |
| 3822 | else |
| 3823 | { |
| 3824 | semsg(_(e_invarg2), tv_get_string(tv)); |
| 3825 | return NULL; |
| 3826 | } |
| 3827 | if (channel != NULL && reading) |
| 3828 | has_readahead = channel_has_readahead(channel, |
| 3829 | part != PART_COUNT ? part : channel_part_read(channel)); |
| 3830 | |
| 3831 | if (check_open && (channel == NULL || (!channel_is_open(channel) |
| 3832 | && !(reading && has_readahead)))) |
| 3833 | { |
| 3834 | emsg(_("E906: not an open channel")); |
| 3835 | return NULL; |
| 3836 | } |
| 3837 | return channel; |
| 3838 | } |
| 3839 | |
| 3840 | /* |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3841 | * Common for ch_read() and ch_readraw(). |
| 3842 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 3843 | static void |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3844 | common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3845 | { |
| 3846 | channel_T *channel; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3847 | ch_part_T part = PART_COUNT; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3848 | jobopt_T opt; |
| 3849 | int mode; |
| 3850 | int timeout; |
| 3851 | int id = -1; |
| 3852 | typval_T *listtv = NULL; |
| 3853 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3854 | // return an empty string by default |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3855 | rettv->v_type = VAR_STRING; |
| 3856 | rettv->vval.v_string = NULL; |
| 3857 | |
| 3858 | clear_job_options(&opt); |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 3859 | if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3860 | == FAIL) |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 3861 | goto theend; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3862 | |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 3863 | if (opt.jo_set & JO_PART) |
| 3864 | part = opt.jo_part; |
| 3865 | channel = get_channel_arg(&argvars[0], TRUE, TRUE, part); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3866 | if (channel != NULL) |
| 3867 | { |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3868 | if (part == PART_COUNT) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3869 | part = channel_part_read(channel); |
| 3870 | mode = channel_get_mode(channel, part); |
| 3871 | timeout = channel_get_timeout(channel, part); |
| 3872 | if (opt.jo_set & JO_TIMEOUT) |
| 3873 | timeout = opt.jo_timeout; |
| 3874 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3875 | if (blob) |
| 3876 | { |
| 3877 | int outlen = 0; |
| 3878 | char_u *p = channel_read_block(channel, part, |
| 3879 | timeout, TRUE, &outlen); |
| 3880 | if (p != NULL) |
| 3881 | { |
| 3882 | blob_T *b = blob_alloc(); |
| 3883 | |
| 3884 | if (b != NULL) |
| 3885 | { |
| 3886 | b->bv_ga.ga_len = outlen; |
| 3887 | if (ga_grow(&b->bv_ga, outlen) == FAIL) |
| 3888 | blob_free(b); |
| 3889 | else |
| 3890 | { |
| 3891 | memcpy(b->bv_ga.ga_data, p, outlen); |
| 3892 | rettv_blob_set(rettv, b); |
| 3893 | } |
| 3894 | } |
| 3895 | vim_free(p); |
| 3896 | } |
| 3897 | } |
| 3898 | else if (raw || mode == MODE_RAW || mode == MODE_NL) |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 3899 | rettv->vval.v_string = channel_read_block(channel, part, |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 3900 | timeout, raw, NULL); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3901 | else |
| 3902 | { |
| 3903 | if (opt.jo_set & JO_ID) |
| 3904 | id = opt.jo_id; |
| 3905 | channel_read_json_block(channel, part, timeout, id, &listtv); |
| 3906 | if (listtv != NULL) |
| 3907 | { |
| 3908 | *rettv = *listtv; |
| 3909 | vim_free(listtv); |
| 3910 | } |
| 3911 | else |
| 3912 | { |
| 3913 | rettv->v_type = VAR_SPECIAL; |
| 3914 | rettv->vval.v_number = VVAL_NONE; |
| 3915 | } |
| 3916 | } |
| 3917 | } |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 3918 | |
| 3919 | theend: |
| 3920 | free_job_options(&opt); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
Bram Moolenaar | 80a8d38 | 2020-05-03 22:57:32 +0200 | [diff] [blame] | 3923 | #if defined(MSWIN) || defined(__HAIKU__) || defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 3924 | /* |
| 3925 | * Check the channels for anything that is ready to be read. |
| 3926 | * The data is put in the read queue. |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 3927 | * if "only_keep_open" is TRUE only check channels where ch_keep_open is set. |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 3928 | */ |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3929 | void |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 3930 | channel_handle_events(int only_keep_open) |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3931 | { |
| 3932 | channel_T *channel; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3933 | ch_part_T part; |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 3934 | sock_T fd; |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3935 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3936 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3937 | { |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 3938 | if (only_keep_open && !channel->ch_keep_open) |
| 3939 | continue; |
| 3940 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 3941 | // check the socket and pipes |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3942 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 3943 | { |
| 3944 | fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3945 | if (fd != INVALID_FD) |
| 3946 | { |
| 3947 | int r = channel_wait(channel, fd, 0); |
| 3948 | |
| 3949 | if (r == CW_READY) |
| 3950 | channel_read(channel, part, "channel_handle_events"); |
| 3951 | else if (r == CW_ERROR) |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 3952 | ch_close_part_on_error(channel, part, TRUE, |
| 3953 | "channel_handle_events"); |
Bram Moolenaar | b2658a1 | 2016-04-26 17:16:24 +0200 | [diff] [blame] | 3954 | } |
Bram Moolenaar | b7522a2 | 2016-02-21 17:20:55 +0100 | [diff] [blame] | 3955 | } |
Bram Moolenaar | 80a8d38 | 2020-05-03 22:57:32 +0200 | [diff] [blame] | 3956 | |
| 3957 | # ifdef __HAIKU__ |
| 3958 | // Workaround for Haiku: Since select/poll cannot detect EOF from tty, |
| 3959 | // should close fds when the job has finished if 'channel' connects to |
| 3960 | // the pty. |
| 3961 | if (channel->ch_job != NULL) |
| 3962 | { |
| 3963 | job_T *job = channel->ch_job; |
| 3964 | |
| 3965 | if (job->jv_tty_out != NULL && job->jv_status == JOB_FINISHED) |
| 3966 | for (part = PART_SOCK; part < PART_COUNT; ++part) |
| 3967 | ch_close_part(channel, part); |
| 3968 | } |
| 3969 | # endif |
Bram Moolenaar | ed5a78e | 2016-02-19 21:05:03 +0100 | [diff] [blame] | 3970 | } |
| 3971 | } |
Bram Moolenaar | 80a8d38 | 2020-05-03 22:57:32 +0200 | [diff] [blame] | 3972 | #endif |
Bram Moolenaar | 85be35f | 2016-01-27 21:08:18 +0100 | [diff] [blame] | 3973 | |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 3974 | # if defined(FEAT_GUI) || defined(PROTO) |
| 3975 | /* |
| 3976 | * Return TRUE when there is any channel with a keep_open flag. |
| 3977 | */ |
| 3978 | int |
| 3979 | channel_any_keep_open() |
| 3980 | { |
| 3981 | channel_T *channel; |
| 3982 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 3983 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 4ab7968 | 2017-08-27 14:50:47 +0200 | [diff] [blame] | 3984 | if (channel->ch_keep_open) |
| 3985 | return TRUE; |
| 3986 | return FALSE; |
| 3987 | } |
| 3988 | # endif |
| 3989 | |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 3990 | /* |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 3991 | * Set "channel"/"part" to non-blocking. |
Bram Moolenaar | f66a2cd | 2017-08-18 21:53:22 +0200 | [diff] [blame] | 3992 | * Only works for sockets and pipes. |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 3993 | */ |
| 3994 | void |
| 3995 | channel_set_nonblock(channel_T *channel, ch_part_T part) |
| 3996 | { |
| 3997 | chanpart_T *ch_part = &channel->ch_part[part]; |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 3998 | int fd = ch_part->ch_fd; |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 3999 | |
| 4000 | if (fd != INVALID_FD) |
| 4001 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4002 | #ifdef MSWIN |
Bram Moolenaar | f66a2cd | 2017-08-18 21:53:22 +0200 | [diff] [blame] | 4003 | u_long val = 1; |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4004 | |
Bram Moolenaar | f66a2cd | 2017-08-18 21:53:22 +0200 | [diff] [blame] | 4005 | ioctlsocket(fd, FIONBIO, &val); |
| 4006 | #else |
Bram Moolenaar | dc926dd | 2017-08-19 21:26:44 +0200 | [diff] [blame] | 4007 | (void)fcntl(fd, F_SETFL, O_NONBLOCK); |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4008 | #endif |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4009 | ch_part->ch_nonblocking = TRUE; |
| 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4014 | * Write "buf" (NUL terminated string) to "channel"/"part". |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4015 | * When "fun" is not NULL an error message might be given. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 4016 | * Return FAIL or OK. |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4017 | */ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 4018 | int |
Bram Moolenaar | 79cbdcb | 2016-11-11 21:14:03 +0100 | [diff] [blame] | 4019 | channel_send( |
| 4020 | channel_T *channel, |
| 4021 | ch_part_T part, |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4022 | char_u *buf_arg, |
| 4023 | int len_arg, |
Bram Moolenaar | 79cbdcb | 2016-11-11 21:14:03 +0100 | [diff] [blame] | 4024 | char *fun) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4025 | { |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4026 | int res; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4027 | sock_T fd; |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4028 | chanpart_T *ch_part = &channel->ch_part[part]; |
| 4029 | int did_use_queue = FALSE; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4030 | |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4031 | fd = ch_part->ch_fd; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4032 | if (fd == INVALID_FD) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4033 | { |
| 4034 | if (!channel->ch_error && fun != NULL) |
| 4035 | { |
Bram Moolenaar | 2f3a90a | 2017-08-03 14:49:29 +0200 | [diff] [blame] | 4036 | ch_error(channel, "%s(): write while not connected", fun); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4037 | semsg(_("E630: %s(): write while not connected"), fun); |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4038 | } |
| 4039 | channel->ch_error = TRUE; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 4040 | return FAIL; |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4041 | } |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 4042 | |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 4043 | if (channel->ch_nonblock && !ch_part->ch_nonblocking) |
| 4044 | channel_set_nonblock(channel, part); |
| 4045 | |
Bram Moolenaar | 71eeb74 | 2017-09-13 22:18:01 +0200 | [diff] [blame] | 4046 | if (ch_log_active()) |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4047 | { |
Bram Moolenaar | 4b16ee7 | 2018-08-09 22:15:34 +0200 | [diff] [blame] | 4048 | ch_log_lead("SEND ", channel, part); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4049 | fprintf(log_fd, "'"); |
Bram Moolenaar | 42335f5 | 2018-09-13 15:33:43 +0200 | [diff] [blame] | 4050 | vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd); |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4051 | fprintf(log_fd, "'\n"); |
| 4052 | fflush(log_fd); |
Bram Moolenaar | 101e992 | 2019-09-25 21:43:11 +0200 | [diff] [blame] | 4053 | did_repeated_msg = 0; |
Bram Moolenaar | 6463ca2 | 2016-02-13 17:04:46 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4056 | for (;;) |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4057 | { |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4058 | writeq_T *wq = &ch_part->ch_writeque; |
| 4059 | char_u *buf; |
| 4060 | int len; |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 4061 | |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4062 | if (wq->wq_next != NULL) |
| 4063 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4064 | // first write what was queued |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4065 | buf = wq->wq_next->wq_ga.ga_data; |
| 4066 | len = wq->wq_next->wq_ga.ga_len; |
| 4067 | did_use_queue = TRUE; |
| 4068 | } |
| 4069 | else |
| 4070 | { |
| 4071 | if (len_arg == 0) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4072 | // nothing to write, called from channel_select_check() |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4073 | return OK; |
| 4074 | buf = buf_arg; |
| 4075 | len = len_arg; |
| 4076 | } |
| 4077 | |
| 4078 | if (part == PART_SOCK) |
| 4079 | res = sock_write(fd, (char *)buf, len); |
| 4080 | else |
Bram Moolenaar | 31faed6 | 2019-01-22 23:01:40 +0100 | [diff] [blame] | 4081 | { |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4082 | res = fd_write(fd, (char *)buf, len); |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4083 | #ifdef MSWIN |
Bram Moolenaar | 31faed6 | 2019-01-22 23:01:40 +0100 | [diff] [blame] | 4084 | if (channel->ch_named_pipe && res < 0) |
| 4085 | { |
| 4086 | DisconnectNamedPipe((HANDLE)fd); |
| 4087 | ConnectNamedPipe((HANDLE)fd, NULL); |
| 4088 | } |
| 4089 | #endif |
| 4090 | } |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4091 | if (res < 0 && (errno == EWOULDBLOCK |
| 4092 | #ifdef EAGAIN |
| 4093 | || errno == EAGAIN |
| 4094 | #endif |
| 4095 | )) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4096 | res = 0; // nothing got written |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4097 | |
| 4098 | if (res >= 0 && ch_part->ch_nonblocking) |
| 4099 | { |
| 4100 | writeq_T *entry = wq->wq_next; |
| 4101 | |
| 4102 | if (did_use_queue) |
| 4103 | ch_log(channel, "Sent %d bytes now", res); |
| 4104 | if (res == len) |
| 4105 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4106 | // Wrote all the buf[len] bytes. |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4107 | if (entry != NULL) |
| 4108 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4109 | // Remove the entry from the write queue. |
Bram Moolenaar | aba680a | 2017-09-09 16:42:53 +0200 | [diff] [blame] | 4110 | remove_from_writeque(wq, entry); |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4111 | continue; |
| 4112 | } |
| 4113 | if (did_use_queue) |
| 4114 | ch_log(channel, "Write queue empty"); |
| 4115 | } |
| 4116 | else |
| 4117 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4118 | // Wrote only buf[res] bytes, can't write more now. |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4119 | if (entry != NULL) |
| 4120 | { |
| 4121 | if (res > 0) |
| 4122 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4123 | // Remove the bytes that were written. |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4124 | mch_memmove(entry->wq_ga.ga_data, |
| 4125 | (char *)entry->wq_ga.ga_data + res, |
| 4126 | len - res); |
| 4127 | entry->wq_ga.ga_len -= res; |
| 4128 | } |
| 4129 | buf = buf_arg; |
| 4130 | len = len_arg; |
| 4131 | } |
| 4132 | else |
| 4133 | { |
| 4134 | buf += res; |
| 4135 | len -= res; |
| 4136 | } |
| 4137 | ch_log(channel, "Adding %d bytes to the write queue", len); |
| 4138 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4139 | // Append the not written bytes of the argument to the write |
| 4140 | // buffer. Limit entries to 4000 bytes. |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4141 | if (wq->wq_prev != NULL |
| 4142 | && wq->wq_prev->wq_ga.ga_len + len < 4000) |
| 4143 | { |
| 4144 | writeq_T *last = wq->wq_prev; |
| 4145 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4146 | // append to the last entry |
Bram Moolenaar | 0d07155 | 2019-07-08 22:04:03 +0200 | [diff] [blame] | 4147 | if (len > 0 && ga_grow(&last->wq_ga, len) == OK) |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4148 | { |
| 4149 | mch_memmove((char *)last->wq_ga.ga_data |
| 4150 | + last->wq_ga.ga_len, |
| 4151 | buf, len); |
| 4152 | last->wq_ga.ga_len += len; |
| 4153 | } |
| 4154 | } |
| 4155 | else |
| 4156 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 4157 | writeq_T *last = ALLOC_ONE(writeq_T); |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4158 | |
| 4159 | if (last != NULL) |
| 4160 | { |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4161 | last->wq_prev = wq->wq_prev; |
| 4162 | last->wq_next = NULL; |
| 4163 | if (wq->wq_prev == NULL) |
| 4164 | wq->wq_next = last; |
| 4165 | else |
| 4166 | wq->wq_prev->wq_next = last; |
| 4167 | wq->wq_prev = last; |
| 4168 | ga_init2(&last->wq_ga, 1, 1000); |
Bram Moolenaar | 0d07155 | 2019-07-08 22:04:03 +0200 | [diff] [blame] | 4169 | if (len > 0 && ga_grow(&last->wq_ga, len) == OK) |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4170 | { |
| 4171 | mch_memmove(last->wq_ga.ga_data, buf, len); |
| 4172 | last->wq_ga.ga_len = len; |
| 4173 | } |
| 4174 | } |
| 4175 | } |
| 4176 | } |
| 4177 | } |
| 4178 | else if (res != len) |
| 4179 | { |
| 4180 | if (!channel->ch_error && fun != NULL) |
| 4181 | { |
| 4182 | ch_error(channel, "%s(): write failed", fun); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4183 | semsg(_("E631: %s(): write failed"), fun); |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 4184 | } |
| 4185 | channel->ch_error = TRUE; |
| 4186 | return FAIL; |
| 4187 | } |
| 4188 | |
| 4189 | channel->ch_error = FALSE; |
| 4190 | return OK; |
| 4191 | } |
Bram Moolenaar | d04a020 | 2016-01-26 23:30:18 +0100 | [diff] [blame] | 4192 | } |
| 4193 | |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4194 | /* |
| 4195 | * Common for "ch_sendexpr()" and "ch_sendraw()". |
| 4196 | * Returns the channel if the caller should read the response. |
Bram Moolenaar | aad30bb | 2016-06-26 17:31:03 +0200 | [diff] [blame] | 4197 | * Sets "part_read" to the read fd. |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4198 | * Otherwise returns NULL. |
| 4199 | */ |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4200 | static channel_T * |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4201 | send_common( |
| 4202 | typval_T *argvars, |
| 4203 | char_u *text, |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4204 | int len, |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4205 | int id, |
| 4206 | int eval, |
| 4207 | jobopt_T *opt, |
| 4208 | char *fun, |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4209 | ch_part_T *part_read) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4210 | { |
| 4211 | channel_T *channel; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4212 | ch_part_T part_send; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4213 | |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 4214 | clear_job_options(opt); |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 4215 | channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4216 | if (channel == NULL) |
| 4217 | return NULL; |
| 4218 | part_send = channel_part_send(channel); |
| 4219 | *part_read = channel_part_read(channel); |
| 4220 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 4221 | if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4222 | return NULL; |
| 4223 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4224 | // Set the callback. An empty callback means no callback and not reading |
| 4225 | // the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not |
| 4226 | // allowed. |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 4227 | if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4228 | { |
| 4229 | if (eval) |
| 4230 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4231 | semsg(_("E917: Cannot use a callback with %s()"), fun); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4232 | return NULL; |
| 4233 | } |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 4234 | channel_set_req_callback(channel, *part_read, &opt->jo_callback, id); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4235 | } |
| 4236 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4237 | if (channel_send(channel, part_send, text, len, fun) == OK |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 4238 | && opt->jo_callback.cb_name == NULL) |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4239 | return channel; |
| 4240 | return NULL; |
| 4241 | } |
| 4242 | |
| 4243 | /* |
| 4244 | * common for "ch_evalexpr()" and "ch_sendexpr()" |
| 4245 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 4246 | static void |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4247 | ch_expr_common(typval_T *argvars, typval_T *rettv, int eval) |
| 4248 | { |
| 4249 | char_u *text; |
| 4250 | typval_T *listtv; |
| 4251 | channel_T *channel; |
| 4252 | int id; |
| 4253 | ch_mode_T ch_mode; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4254 | ch_part_T part_send; |
| 4255 | ch_part_T part_read; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4256 | jobopt_T opt; |
| 4257 | int timeout; |
| 4258 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4259 | // return an empty string by default |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4260 | rettv->v_type = VAR_STRING; |
| 4261 | rettv->vval.v_string = NULL; |
| 4262 | |
Bram Moolenaar | 437905c | 2016-04-26 19:01:05 +0200 | [diff] [blame] | 4263 | channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4264 | if (channel == NULL) |
| 4265 | return; |
| 4266 | part_send = channel_part_send(channel); |
| 4267 | |
| 4268 | ch_mode = channel_get_mode(channel, part_send); |
| 4269 | if (ch_mode == MODE_RAW || ch_mode == MODE_NL) |
| 4270 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4271 | emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel")); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4272 | return; |
| 4273 | } |
| 4274 | |
Bram Moolenaar | e9d6a29 | 2016-03-20 19:31:33 +0100 | [diff] [blame] | 4275 | id = ++channel->ch_last_msg_id; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4276 | text = json_encode_nr_expr(id, &argvars[1], |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 4277 | (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4278 | if (text == NULL) |
| 4279 | return; |
| 4280 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4281 | channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt, |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4282 | eval ? "ch_evalexpr" : "ch_sendexpr", &part_read); |
| 4283 | vim_free(text); |
| 4284 | if (channel != NULL && eval) |
| 4285 | { |
| 4286 | if (opt.jo_set & JO_TIMEOUT) |
| 4287 | timeout = opt.jo_timeout; |
| 4288 | else |
| 4289 | timeout = channel_get_timeout(channel, part_read); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4290 | if (channel_read_json_block(channel, part_read, timeout, id, &listtv) |
| 4291 | == OK) |
| 4292 | { |
| 4293 | list_T *list = listtv->vval.v_list; |
| 4294 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4295 | // Move the item from the list and then change the type to |
| 4296 | // avoid the value being freed. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 4297 | *rettv = list->lv_u.mat.lv_last->li_tv; |
| 4298 | list->lv_u.mat.lv_last->li_tv.v_type = VAR_NUMBER; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4299 | free_tv(listtv); |
| 4300 | } |
| 4301 | } |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 4302 | free_job_options(&opt); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | /* |
| 4306 | * common for "ch_evalraw()" and "ch_sendraw()" |
| 4307 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 4308 | static void |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4309 | ch_raw_common(typval_T *argvars, typval_T *rettv, int eval) |
| 4310 | { |
| 4311 | char_u buf[NUMBUFLEN]; |
| 4312 | char_u *text; |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4313 | int len; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4314 | channel_T *channel; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4315 | ch_part_T part_read; |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4316 | jobopt_T opt; |
| 4317 | int timeout; |
| 4318 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4319 | // return an empty string by default |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4320 | rettv->v_type = VAR_STRING; |
| 4321 | rettv->vval.v_string = NULL; |
| 4322 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4323 | if (argvars[1].v_type == VAR_BLOB) |
| 4324 | { |
| 4325 | text = argvars[1].vval.v_blob->bv_ga.ga_data; |
| 4326 | len = argvars[1].vval.v_blob->bv_ga.ga_len; |
| 4327 | } |
| 4328 | else |
| 4329 | { |
| 4330 | text = tv_get_string_buf(&argvars[1], buf); |
Bram Moolenaar | e407425 | 2019-01-17 14:31:14 +0100 | [diff] [blame] | 4331 | len = (int)STRLEN(text); |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4332 | } |
| 4333 | channel = send_common(argvars, text, len, 0, eval, &opt, |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4334 | eval ? "ch_evalraw" : "ch_sendraw", &part_read); |
| 4335 | if (channel != NULL && eval) |
| 4336 | { |
| 4337 | if (opt.jo_set & JO_TIMEOUT) |
| 4338 | timeout = opt.jo_timeout; |
| 4339 | else |
| 4340 | timeout = channel_get_timeout(channel, part_read); |
Bram Moolenaar | 620ca2d | 2017-12-09 19:13:13 +0100 | [diff] [blame] | 4341 | rettv->vval.v_string = channel_read_block(channel, part_read, |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 4342 | timeout, TRUE, NULL); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4343 | } |
Bram Moolenaar | 0e4c1de | 2016-04-07 21:40:38 +0200 | [diff] [blame] | 4344 | free_job_options(&opt); |
Bram Moolenaar | 8e2c942 | 2016-03-12 13:43:33 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4347 | #define KEEP_OPEN_TIME 20 // msec |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4348 | |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 4349 | #if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4350 | /* |
| 4351 | * Add open channels to the poll struct. |
| 4352 | * Return the adjusted struct index. |
| 4353 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 4354 | */ |
| 4355 | int |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4356 | channel_poll_setup(int nfd_in, void *fds_in, int *towait) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4357 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4358 | int nfd = nfd_in; |
| 4359 | channel_T *channel; |
| 4360 | struct pollfd *fds = fds_in; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4361 | ch_part_T part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4362 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 4363 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4364 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4365 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4366 | { |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4367 | chanpart_T *ch_part = &channel->ch_part[part]; |
| 4368 | |
| 4369 | if (ch_part->ch_fd != INVALID_FD) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4370 | { |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4371 | if (channel->ch_keep_open) |
| 4372 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4373 | // For unknown reason poll() returns immediately for a |
| 4374 | // keep-open channel. Instead of adding it to the fds add |
| 4375 | // a short timeout and check, like polling. |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4376 | if (*towait < 0 || *towait > KEEP_OPEN_TIME) |
| 4377 | *towait = KEEP_OPEN_TIME; |
| 4378 | } |
| 4379 | else |
| 4380 | { |
| 4381 | ch_part->ch_poll_idx = nfd; |
| 4382 | fds[nfd].fd = ch_part->ch_fd; |
| 4383 | fds[nfd].events = POLLIN; |
| 4384 | nfd++; |
| 4385 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4386 | } |
| 4387 | else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4388 | channel->ch_part[part].ch_poll_idx = -1; |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4389 | } |
| 4390 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4391 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4392 | nfd = channel_fill_poll_write(nfd, fds); |
| 4393 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4394 | return nfd; |
| 4395 | } |
| 4396 | |
| 4397 | /* |
| 4398 | * The type of "fds" is hidden to avoid problems with the function proto. |
| 4399 | */ |
| 4400 | int |
| 4401 | channel_poll_check(int ret_in, void *fds_in) |
| 4402 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4403 | int ret = ret_in; |
| 4404 | channel_T *channel; |
| 4405 | struct pollfd *fds = fds_in; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4406 | ch_part_T part; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4407 | int idx; |
| 4408 | chanpart_T *in_part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4409 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 4410 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4411 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4412 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4413 | { |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4414 | idx = channel->ch_part[part].ch_poll_idx; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4415 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4416 | if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN)) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4417 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4418 | channel_read(channel, part, "channel_poll_check"); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4419 | --ret; |
| 4420 | } |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4421 | else if (channel->ch_part[part].ch_fd != INVALID_FD |
| 4422 | && channel->ch_keep_open) |
| 4423 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4424 | // polling a keep-open channel |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4425 | channel_read(channel, part, "channel_poll_check_keep_open"); |
| 4426 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4427 | } |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4428 | |
| 4429 | in_part = &channel->ch_part[PART_IN]; |
| 4430 | idx = in_part->ch_poll_idx; |
| 4431 | if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT)) |
| 4432 | { |
Bram Moolenaar | 683b796 | 2017-08-19 15:51:59 +0200 | [diff] [blame] | 4433 | channel_write_input(channel); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4434 | --ret; |
| 4435 | } |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4436 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4437 | |
| 4438 | return ret; |
| 4439 | } |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4440 | #endif // UNIX && !HAVE_SELECT |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4441 | |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 4442 | #if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO) |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4443 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4444 | /* |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4445 | * The "fd_set" type is hidden to avoid problems with the function proto. |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4446 | */ |
| 4447 | int |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4448 | channel_select_setup( |
| 4449 | int maxfd_in, |
| 4450 | void *rfds_in, |
| 4451 | void *wfds_in, |
| 4452 | struct timeval *tv, |
| 4453 | struct timeval **tvp) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4454 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4455 | int maxfd = maxfd_in; |
| 4456 | channel_T *channel; |
| 4457 | fd_set *rfds = rfds_in; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4458 | fd_set *wfds = wfds_in; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4459 | ch_part_T part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4460 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 4461 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4462 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4463 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4464 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4465 | sock_T fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4466 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4467 | if (fd != INVALID_FD) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4468 | { |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4469 | if (channel->ch_keep_open) |
| 4470 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4471 | // For unknown reason select() returns immediately for a |
| 4472 | // keep-open channel. Instead of adding it to the rfds add |
| 4473 | // a short timeout and check, like polling. |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4474 | if (*tvp == NULL || tv->tv_sec > 0 |
| 4475 | || tv->tv_usec > KEEP_OPEN_TIME * 1000) |
| 4476 | { |
| 4477 | *tvp = tv; |
| 4478 | tv->tv_sec = 0; |
| 4479 | tv->tv_usec = KEEP_OPEN_TIME * 1000; |
| 4480 | } |
| 4481 | } |
| 4482 | else |
| 4483 | { |
| 4484 | FD_SET((int)fd, rfds); |
| 4485 | if (maxfd < (int)fd) |
| 4486 | maxfd = (int)fd; |
| 4487 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4488 | } |
| 4489 | } |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4490 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4491 | |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4492 | maxfd = channel_fill_wfds(maxfd, wfds); |
| 4493 | |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4494 | return maxfd; |
| 4495 | } |
| 4496 | |
| 4497 | /* |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4498 | * The "fd_set" type is hidden to avoid problems with the function proto. |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4499 | */ |
| 4500 | int |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4501 | channel_select_check(int ret_in, void *rfds_in, void *wfds_in) |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4502 | { |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4503 | int ret = ret_in; |
| 4504 | channel_T *channel; |
| 4505 | fd_set *rfds = rfds_in; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4506 | fd_set *wfds = wfds_in; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4507 | ch_part_T part; |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4508 | chanpart_T *in_part; |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4509 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 4510 | FOR_ALL_CHANNELS(channel) |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4511 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4512 | for (part = PART_SOCK; part < PART_IN; ++part) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4513 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4514 | sock_T fd = channel->ch_part[part].ch_fd; |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4515 | |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4516 | if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds)) |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4517 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4518 | channel_read(channel, part, "channel_select_check"); |
Bram Moolenaar | 3c51840 | 2017-09-08 20:47:00 +0200 | [diff] [blame] | 4519 | FD_CLR(fd, rfds); |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4520 | --ret; |
| 4521 | } |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4522 | else if (fd != INVALID_FD && channel->ch_keep_open) |
| 4523 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4524 | // polling a keep-open channel |
Bram Moolenaar | f336061 | 2017-10-01 16:21:31 +0200 | [diff] [blame] | 4525 | channel_read(channel, part, "channel_select_check_keep_open"); |
| 4526 | } |
Bram Moolenaar | 7b3ca76 | 2016-02-14 19:13:43 +0100 | [diff] [blame] | 4527 | } |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4528 | |
| 4529 | in_part = &channel->ch_part[PART_IN]; |
| 4530 | if (ret > 0 && in_part->ch_fd != INVALID_FD |
| 4531 | && FD_ISSET(in_part->ch_fd, wfds)) |
| 4532 | { |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 4533 | // Clear the flag first, ch_fd may change in channel_write_input(). |
Bram Moolenaar | 3c51840 | 2017-09-08 20:47:00 +0200 | [diff] [blame] | 4534 | FD_CLR(in_part->ch_fd, wfds); |
Bram Moolenaar | 9af9778 | 2018-04-03 12:51:01 +0200 | [diff] [blame] | 4535 | channel_write_input(channel); |
Bram Moolenaar | 8b877ac | 2016-03-28 19:16:20 +0200 | [diff] [blame] | 4536 | --ret; |
| 4537 | } |
Bram Moolenaar | 80a8d38 | 2020-05-03 22:57:32 +0200 | [diff] [blame] | 4538 | |
| 4539 | # ifdef __HAIKU__ |
| 4540 | // Workaround for Haiku: Since select/poll cannot detect EOF from tty, |
| 4541 | // should close fds when the job has finished if 'channel' connects to |
| 4542 | // the pty. |
| 4543 | if (channel->ch_job != NULL) |
| 4544 | { |
| 4545 | job_T *job = channel->ch_job; |
| 4546 | |
| 4547 | if (job->jv_tty_out != NULL && job->jv_status == JOB_FINISHED) |
| 4548 | for (part = PART_SOCK; part < PART_COUNT; ++part) |
| 4549 | ch_close_part(channel, part); |
| 4550 | } |
| 4551 | # endif |
Bram Moolenaar | 16eb4f8 | 2016-02-14 23:02:34 +0100 | [diff] [blame] | 4552 | } |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4553 | |
| 4554 | return ret; |
| 4555 | } |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 4556 | #endif // !MSWIN && HAVE_SELECT |
Bram Moolenaar | e0874f8 | 2016-01-24 20:36:41 +0100 | [diff] [blame] | 4557 | |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 4558 | /* |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 4559 | * Execute queued up commands. |
Bram Moolenaar | 8aeec40 | 2019-09-15 23:02:04 +0200 | [diff] [blame] | 4560 | * Invoked from the main loop when it's safe to execute received commands, |
| 4561 | * and during a blocking wait for ch_evalexpr(). |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 4562 | * Return TRUE when something was done. |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 4563 | */ |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 4564 | int |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 4565 | channel_parse_messages(void) |
| 4566 | { |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 4567 | channel_T *channel = first_channel; |
| 4568 | int ret = FALSE; |
| 4569 | int r; |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4570 | ch_part_T part = PART_SOCK; |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4571 | static int recursive = 0; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4572 | #ifdef ELAPSED_FUNC |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 4573 | elapsed_T start_tv; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4574 | #endif |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 4575 | |
Bram Moolenaar | a9c3a30 | 2020-03-26 16:03:45 +0100 | [diff] [blame] | 4576 | // The code below may invoke callbacks, which might call us back. |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4577 | // In a recursive call channels will not be closed. |
| 4578 | ++recursive; |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 4579 | ++safe_to_invoke_callback; |
| 4580 | |
Bram Moolenaar | a9c3a30 | 2020-03-26 16:03:45 +0100 | [diff] [blame] | 4581 | #ifdef ELAPSED_FUNC |
| 4582 | ELAPSED_INIT(start_tv); |
| 4583 | #endif |
| 4584 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4585 | // Only do this message when another message was given, otherwise we get |
| 4586 | // lots of them. |
Bram Moolenaar | 101e992 | 2019-09-25 21:43:11 +0200 | [diff] [blame] | 4587 | if ((did_repeated_msg & REPEATED_MSG_LOOKING) == 0) |
Bram Moolenaar | d0b6502 | 2016-03-06 21:50:33 +0100 | [diff] [blame] | 4588 | { |
| 4589 | ch_log(NULL, "looking for messages on channels"); |
Bram Moolenaar | 101e992 | 2019-09-25 21:43:11 +0200 | [diff] [blame] | 4590 | // now we should also give the message for SafeState |
| 4591 | did_repeated_msg = REPEATED_MSG_LOOKING; |
Bram Moolenaar | d0b6502 | 2016-03-06 21:50:33 +0100 | [diff] [blame] | 4592 | } |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 4593 | while (channel != NULL) |
| 4594 | { |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4595 | if (recursive == 1) |
Bram Moolenaar | cf7ff70 | 2016-05-09 17:20:14 +0200 | [diff] [blame] | 4596 | { |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4597 | if (channel_can_close(channel)) |
| 4598 | { |
| 4599 | channel->ch_to_be_closed = (1U << PART_COUNT); |
| 4600 | channel_close_now(channel); |
| 4601 | // channel may have been freed, start over |
| 4602 | channel = first_channel; |
| 4603 | continue; |
| 4604 | } |
| 4605 | if (channel->ch_to_be_freed || channel->ch_killing) |
| 4606 | { |
| 4607 | channel_free_contents(channel); |
| 4608 | if (channel->ch_job != NULL) |
| 4609 | channel->ch_job->jv_channel = NULL; |
Bram Moolenaar | 71658f7 | 2020-03-24 20:35:19 +0100 | [diff] [blame] | 4610 | |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4611 | // free the channel and then start over |
| 4612 | channel_free_channel(channel); |
| 4613 | channel = first_channel; |
| 4614 | continue; |
| 4615 | } |
| 4616 | if (channel->ch_refcount == 0 && !channel_still_useful(channel)) |
| 4617 | { |
| 4618 | // channel is no longer useful, free it |
| 4619 | channel_free(channel); |
| 4620 | channel = first_channel; |
| 4621 | part = PART_SOCK; |
| 4622 | continue; |
| 4623 | } |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 4624 | } |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4625 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 4626 | if (channel->ch_part[part].ch_fd != INVALID_FD |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4627 | || channel_has_readahead(channel, part)) |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 4628 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4629 | // Increase the refcount, in case the handler causes the channel |
| 4630 | // to be unreferenced or closed. |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4631 | ++channel->ch_refcount; |
| 4632 | r = may_invoke_callback(channel, part); |
| 4633 | if (r == OK) |
| 4634 | ret = TRUE; |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4635 | if (channel_unref(channel) || (r == OK |
| 4636 | #ifdef ELAPSED_FUNC |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4637 | // Limit the time we loop here to 100 msec, otherwise |
| 4638 | // Vim becomes unresponsive when the callback takes |
| 4639 | // more than a bit of time. |
Bram Moolenaar | 833eb1d | 2016-11-24 17:22:50 +0100 | [diff] [blame] | 4640 | && ELAPSED_FUNC(start_tv) < 100L |
| 4641 | #endif |
| 4642 | )) |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4643 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4644 | // channel was freed or something was done, start over |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4645 | channel = first_channel; |
| 4646 | part = PART_SOCK; |
| 4647 | continue; |
| 4648 | } |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 4649 | } |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4650 | if (part < PART_ERR) |
| 4651 | ++part; |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 4652 | else |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4653 | { |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 4654 | channel = channel->ch_next; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4655 | part = PART_SOCK; |
| 4656 | } |
Bram Moolenaar | 3bece9f | 2016-02-15 20:39:46 +0100 | [diff] [blame] | 4657 | } |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 4658 | |
Bram Moolenaar | 7f7c332 | 2016-04-18 19:27:24 +0200 | [diff] [blame] | 4659 | if (channel_need_redraw) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 4660 | { |
| 4661 | channel_need_redraw = FALSE; |
Bram Moolenaar | 02e177d | 2017-08-26 23:43:28 +0200 | [diff] [blame] | 4662 | redraw_after_callback(TRUE); |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 4665 | --safe_to_invoke_callback; |
Bram Moolenaar | 09c5690 | 2020-03-28 18:06:31 +0100 | [diff] [blame] | 4666 | --recursive; |
Bram Moolenaar | fb6ffc7 | 2016-05-09 17:58:04 +0200 | [diff] [blame] | 4667 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 4668 | return ret; |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 4669 | } |
| 4670 | |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 4671 | /* |
Bram Moolenaar | 8a8199e | 2016-11-26 15:13:33 +0100 | [diff] [blame] | 4672 | * Return TRUE if any channel has readahead. That means we should not block on |
| 4673 | * waiting for input. |
| 4674 | */ |
| 4675 | int |
| 4676 | channel_any_readahead(void) |
| 4677 | { |
| 4678 | channel_T *channel = first_channel; |
| 4679 | ch_part_T part = PART_SOCK; |
| 4680 | |
| 4681 | while (channel != NULL) |
| 4682 | { |
| 4683 | if (channel_has_readahead(channel, part)) |
| 4684 | return TRUE; |
| 4685 | if (part < PART_ERR) |
| 4686 | ++part; |
| 4687 | else |
| 4688 | { |
| 4689 | channel = channel->ch_next; |
| 4690 | part = PART_SOCK; |
| 4691 | } |
| 4692 | } |
| 4693 | return FALSE; |
| 4694 | } |
| 4695 | |
| 4696 | /* |
Bram Moolenaar | fbc4b4d | 2016-02-07 15:14:01 +0100 | [diff] [blame] | 4697 | * Mark references to lists used in channels. |
| 4698 | */ |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 4699 | int |
| 4700 | set_ref_in_channel(int copyID) |
| 4701 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 4702 | int abort = FALSE; |
| 4703 | channel_T *channel; |
Bram Moolenaar | b8d4905 | 2016-05-01 14:22:16 +0200 | [diff] [blame] | 4704 | typval_T tv; |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 4705 | |
Bram Moolenaar | 75a1a94 | 2019-06-20 03:45:36 +0200 | [diff] [blame] | 4706 | for (channel = first_channel; !abort && channel != NULL; |
| 4707 | channel = channel->ch_next) |
Bram Moolenaar | b8d4905 | 2016-05-01 14:22:16 +0200 | [diff] [blame] | 4708 | if (channel_still_useful(channel)) |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 4709 | { |
Bram Moolenaar | b8d4905 | 2016-05-01 14:22:16 +0200 | [diff] [blame] | 4710 | tv.v_type = VAR_CHANNEL; |
| 4711 | tv.vval.v_channel = channel; |
| 4712 | abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL); |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 4713 | } |
Bram Moolenaar | 4b6a6dc | 2016-02-04 22:49:49 +0100 | [diff] [blame] | 4714 | return abort; |
| 4715 | } |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 4716 | |
| 4717 | /* |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4718 | * Return the "part" to write to for "channel". |
| 4719 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 4720 | static ch_part_T |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4721 | channel_part_send(channel_T *channel) |
| 4722 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4723 | if (channel->CH_SOCK_FD == INVALID_FD) |
| 4724 | return PART_IN; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4725 | return PART_SOCK; |
| 4726 | } |
| 4727 | |
| 4728 | /* |
| 4729 | * Return the default "part" to read from for "channel". |
| 4730 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 4731 | static ch_part_T |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4732 | channel_part_read(channel_T *channel) |
| 4733 | { |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4734 | if (channel->CH_SOCK_FD == INVALID_FD) |
| 4735 | return PART_OUT; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4736 | return PART_SOCK; |
| 4737 | } |
| 4738 | |
| 4739 | /* |
| 4740 | * Return the mode of "channel"/"part" |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 4741 | * If "channel" is invalid returns MODE_JSON. |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 4742 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 4743 | static ch_mode_T |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4744 | channel_get_mode(channel_T *channel, ch_part_T part) |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 4745 | { |
Bram Moolenaar | 7707344 | 2016-02-13 23:23:53 +0100 | [diff] [blame] | 4746 | if (channel == NULL) |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 4747 | return MODE_JSON; |
Bram Moolenaar | 42d38a2 | 2016-02-20 18:18:59 +0100 | [diff] [blame] | 4748 | return channel->ch_part[part].ch_mode; |
Bram Moolenaar | ae8eb3c | 2016-02-07 21:59:26 +0100 | [diff] [blame] | 4749 | } |
| 4750 | |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 4751 | /* |
| 4752 | * Return the timeout of "channel"/"part" |
| 4753 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 4754 | static int |
Bram Moolenaar | dc0ccae | 2016-10-09 17:28:01 +0200 | [diff] [blame] | 4755 | channel_get_timeout(channel_T *channel, ch_part_T part) |
Bram Moolenaar | 6f3a544 | 2016-02-20 19:56:13 +0100 | [diff] [blame] | 4756 | { |
| 4757 | return channel->ch_part[part].ch_timeout; |
| 4758 | } |
| 4759 | |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 4760 | /* |
| 4761 | * "ch_canread()" function |
| 4762 | */ |
| 4763 | void |
| 4764 | f_ch_canread(typval_T *argvars, typval_T *rettv) |
| 4765 | { |
| 4766 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 4767 | |
| 4768 | rettv->vval.v_number = 0; |
| 4769 | if (channel != NULL) |
| 4770 | rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK) |
| 4771 | || channel_has_readahead(channel, PART_OUT) |
| 4772 | || channel_has_readahead(channel, PART_ERR); |
| 4773 | } |
| 4774 | |
| 4775 | /* |
| 4776 | * "ch_close()" function |
| 4777 | */ |
| 4778 | void |
| 4779 | f_ch_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 4780 | { |
| 4781 | channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); |
| 4782 | |
| 4783 | if (channel != NULL) |
| 4784 | { |
| 4785 | channel_close(channel, FALSE); |
| 4786 | channel_clear(channel); |
| 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | /* |
| 4791 | * "ch_close()" function |
| 4792 | */ |
| 4793 | void |
| 4794 | f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED) |
| 4795 | { |
| 4796 | channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); |
| 4797 | |
| 4798 | if (channel != NULL) |
| 4799 | channel_close_in(channel); |
| 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | * "ch_getbufnr()" function |
| 4804 | */ |
| 4805 | void |
| 4806 | f_ch_getbufnr(typval_T *argvars, typval_T *rettv) |
| 4807 | { |
| 4808 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 4809 | |
| 4810 | rettv->vval.v_number = -1; |
| 4811 | if (channel != NULL) |
| 4812 | { |
| 4813 | char_u *what = tv_get_string(&argvars[1]); |
| 4814 | int part; |
| 4815 | |
| 4816 | if (STRCMP(what, "err") == 0) |
| 4817 | part = PART_ERR; |
| 4818 | else if (STRCMP(what, "out") == 0) |
| 4819 | part = PART_OUT; |
| 4820 | else if (STRCMP(what, "in") == 0) |
| 4821 | part = PART_IN; |
| 4822 | else |
| 4823 | part = PART_SOCK; |
| 4824 | if (channel->ch_part[part].ch_bufref.br_buf != NULL) |
| 4825 | rettv->vval.v_number = |
| 4826 | channel->ch_part[part].ch_bufref.br_buf->b_fnum; |
| 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | /* |
| 4831 | * "ch_getjob()" function |
| 4832 | */ |
| 4833 | void |
| 4834 | f_ch_getjob(typval_T *argvars, typval_T *rettv) |
| 4835 | { |
| 4836 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 4837 | |
| 4838 | if (channel != NULL) |
| 4839 | { |
| 4840 | rettv->v_type = VAR_JOB; |
| 4841 | rettv->vval.v_job = channel->ch_job; |
| 4842 | if (channel->ch_job != NULL) |
| 4843 | ++channel->ch_job->jv_refcount; |
| 4844 | } |
| 4845 | } |
| 4846 | |
| 4847 | /* |
| 4848 | * "ch_info()" function |
| 4849 | */ |
| 4850 | void |
| 4851 | f_ch_info(typval_T *argvars, typval_T *rettv UNUSED) |
| 4852 | { |
| 4853 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 4854 | |
| 4855 | if (channel != NULL && rettv_dict_alloc(rettv) != FAIL) |
| 4856 | channel_info(channel, rettv->vval.v_dict); |
| 4857 | } |
| 4858 | |
| 4859 | /* |
| 4860 | * "ch_log()" function |
| 4861 | */ |
| 4862 | void |
| 4863 | f_ch_log(typval_T *argvars, typval_T *rettv UNUSED) |
| 4864 | { |
| 4865 | char_u *msg = tv_get_string(&argvars[0]); |
| 4866 | channel_T *channel = NULL; |
| 4867 | |
| 4868 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 4869 | channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0); |
| 4870 | |
| 4871 | ch_log(channel, "%s", msg); |
| 4872 | } |
| 4873 | |
| 4874 | /* |
| 4875 | * "ch_logfile()" function |
| 4876 | */ |
| 4877 | void |
| 4878 | f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED) |
| 4879 | { |
| 4880 | char_u *fname; |
| 4881 | char_u *opt = (char_u *)""; |
| 4882 | char_u buf[NUMBUFLEN]; |
| 4883 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 4884 | // Don't open a file in restricted mode. |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 4885 | if (check_restricted() || check_secure()) |
| 4886 | return; |
Bram Moolenaar | c580943 | 2021-03-27 21:23:30 +0100 | [diff] [blame] | 4887 | if (in_vim9script() |
| 4888 | && (check_for_string_arg(argvars, 0) == FAIL |
| 4889 | || check_for_string_arg(argvars, 1) == FAIL)) |
| 4890 | return; |
| 4891 | |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 4892 | fname = tv_get_string(&argvars[0]); |
| 4893 | if (argvars[1].v_type == VAR_STRING) |
| 4894 | opt = tv_get_string_buf(&argvars[1], buf); |
| 4895 | ch_logfile(fname, opt); |
| 4896 | } |
| 4897 | |
| 4898 | /* |
| 4899 | * "ch_open()" function |
| 4900 | */ |
| 4901 | void |
| 4902 | f_ch_open(typval_T *argvars, typval_T *rettv) |
| 4903 | { |
| 4904 | rettv->v_type = VAR_CHANNEL; |
| 4905 | if (check_restricted() || check_secure()) |
| 4906 | return; |
| 4907 | rettv->vval.v_channel = channel_open_func(argvars); |
| 4908 | } |
| 4909 | |
| 4910 | /* |
| 4911 | * "ch_read()" function |
| 4912 | */ |
| 4913 | void |
| 4914 | f_ch_read(typval_T *argvars, typval_T *rettv) |
| 4915 | { |
| 4916 | common_channel_read(argvars, rettv, FALSE, FALSE); |
| 4917 | } |
| 4918 | |
| 4919 | /* |
| 4920 | * "ch_readblob()" function |
| 4921 | */ |
| 4922 | void |
| 4923 | f_ch_readblob(typval_T *argvars, typval_T *rettv) |
| 4924 | { |
| 4925 | common_channel_read(argvars, rettv, TRUE, TRUE); |
| 4926 | } |
| 4927 | |
| 4928 | /* |
| 4929 | * "ch_readraw()" function |
| 4930 | */ |
| 4931 | void |
| 4932 | f_ch_readraw(typval_T *argvars, typval_T *rettv) |
| 4933 | { |
| 4934 | common_channel_read(argvars, rettv, TRUE, FALSE); |
| 4935 | } |
| 4936 | |
| 4937 | /* |
| 4938 | * "ch_evalexpr()" function |
| 4939 | */ |
| 4940 | void |
| 4941 | f_ch_evalexpr(typval_T *argvars, typval_T *rettv) |
| 4942 | { |
| 4943 | ch_expr_common(argvars, rettv, TRUE); |
| 4944 | } |
| 4945 | |
| 4946 | /* |
| 4947 | * "ch_sendexpr()" function |
| 4948 | */ |
| 4949 | void |
| 4950 | f_ch_sendexpr(typval_T *argvars, typval_T *rettv) |
| 4951 | { |
| 4952 | ch_expr_common(argvars, rettv, FALSE); |
| 4953 | } |
| 4954 | |
| 4955 | /* |
| 4956 | * "ch_evalraw()" function |
| 4957 | */ |
| 4958 | void |
| 4959 | f_ch_evalraw(typval_T *argvars, typval_T *rettv) |
| 4960 | { |
| 4961 | ch_raw_common(argvars, rettv, TRUE); |
| 4962 | } |
| 4963 | |
| 4964 | /* |
| 4965 | * "ch_sendraw()" function |
| 4966 | */ |
| 4967 | void |
| 4968 | f_ch_sendraw(typval_T *argvars, typval_T *rettv) |
| 4969 | { |
| 4970 | ch_raw_common(argvars, rettv, FALSE); |
| 4971 | } |
| 4972 | |
| 4973 | /* |
| 4974 | * "ch_setoptions()" function |
| 4975 | */ |
| 4976 | void |
| 4977 | f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED) |
| 4978 | { |
| 4979 | channel_T *channel; |
| 4980 | jobopt_T opt; |
| 4981 | |
| 4982 | channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 4983 | if (channel == NULL) |
| 4984 | return; |
| 4985 | clear_job_options(&opt); |
| 4986 | if (get_job_options(&argvars[1], &opt, |
| 4987 | JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK) |
| 4988 | channel_set_options(channel, &opt); |
| 4989 | free_job_options(&opt); |
| 4990 | } |
| 4991 | |
| 4992 | /* |
| 4993 | * "ch_status()" function |
| 4994 | */ |
| 4995 | void |
| 4996 | f_ch_status(typval_T *argvars, typval_T *rettv) |
| 4997 | { |
| 4998 | channel_T *channel; |
| 4999 | jobopt_T opt; |
| 5000 | int part = -1; |
| 5001 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 5002 | // return an empty string by default |
Bram Moolenaar | 0a1f56f | 2019-06-24 00:43:35 +0200 | [diff] [blame] | 5003 | rettv->v_type = VAR_STRING; |
| 5004 | rettv->vval.v_string = NULL; |
| 5005 | |
| 5006 | channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 5007 | |
| 5008 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 5009 | { |
| 5010 | clear_job_options(&opt); |
| 5011 | if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK |
| 5012 | && (opt.jo_set & JO_PART)) |
| 5013 | part = opt.jo_part; |
| 5014 | } |
| 5015 | |
| 5016 | rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part)); |
| 5017 | } |
| 5018 | |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 5019 | /* |
| 5020 | * Get a string with information about the channel in "varp" in "buf". |
| 5021 | * "buf" must be at least NUMBUFLEN long. |
| 5022 | */ |
| 5023 | char_u * |
| 5024 | channel_to_string_buf(typval_T *varp, char_u *buf) |
| 5025 | { |
| 5026 | channel_T *channel = varp->vval.v_channel; |
| 5027 | char *status = channel_status(channel, -1); |
| 5028 | |
| 5029 | if (channel == NULL) |
| 5030 | vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status); |
| 5031 | else |
| 5032 | vim_snprintf((char *)buf, NUMBUFLEN, |
| 5033 | "channel %d %s", channel->ch_id, status); |
| 5034 | return buf; |
| 5035 | } |
| 5036 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 5037 | #endif // FEAT_JOB_CHANNEL |