Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * X command server by Flemming Madsen |
| 5 | * |
| 6 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 7 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 8 | * See README.txt for an overview of the Vim source code. |
| 9 | * |
| 10 | * if_xcmdsrv.c: Functions for passing commands through an X11 display. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | #include "version.h" |
| 16 | |
| 17 | #if defined(FEAT_CLIENTSERVER) || defined(PROTO) |
| 18 | |
| 19 | # ifdef FEAT_X11 |
| 20 | # include <X11/Intrinsic.h> |
| 21 | # include <X11/Xatom.h> |
| 22 | # endif |
| 23 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | /* |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 25 | * This file provides procedures that implement the command server |
| 26 | * functionality of Vim when in contact with an X11 server. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | * |
| 28 | * Adapted from TCL/TK's send command in tkSend.c of the tk 3.6 distribution. |
| 29 | * Adapted for use in Vim by Flemming Madsen. Protocol changed to that of tk 4 |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * Copyright (c) 1989-1993 The Regents of the University of California. |
| 34 | * All rights reserved. |
| 35 | * |
| 36 | * Permission is hereby granted, without written agreement and without |
| 37 | * license or royalty fees, to use, copy, modify, and distribute this |
| 38 | * software and its documentation for any purpose, provided that the |
| 39 | * above copyright notice and the following two paragraphs appear in |
| 40 | * all copies of this software. |
| 41 | * |
| 42 | * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR |
| 43 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT |
| 44 | * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF |
| 45 | * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 46 | * |
| 47 | * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
| 48 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 49 | * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 50 | * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO |
| 51 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 52 | */ |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * When a result is being awaited from a sent command, one of |
| 57 | * the following structures is present on a list of all outstanding |
| 58 | * sent commands. The information in the structure is used to |
| 59 | * process the result when it arrives. You're probably wondering |
| 60 | * how there could ever be multiple outstanding sent commands. |
| 61 | * This could happen if Vim instances invoke each other recursively. |
| 62 | * It's unlikely, but possible. |
| 63 | */ |
| 64 | |
| 65 | typedef struct PendingCommand |
| 66 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 67 | int serial; // Serial number expected in result. |
| 68 | int code; // Result Code. 0 is OK |
| 69 | char_u *result; // String result for command (malloc'ed). |
| 70 | // NULL means command still pending. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | struct PendingCommand *nextPtr; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 72 | // Next in list of all outstanding commands. |
| 73 | // NULL means end of list. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | } PendingCommand; |
| 75 | |
| 76 | static PendingCommand *pendingCommands = NULL; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 77 | // List of all commands currently |
| 78 | // being waited for. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * The information below is used for communication between processes |
| 82 | * during "send" commands. Each process keeps a private window, never |
| 83 | * even mapped, with one property, "Comm". When a command is sent to |
| 84 | * an interpreter, the command is appended to the comm property of the |
| 85 | * communication window associated with the interp's process. Similarly, |
| 86 | * when a result is returned from a sent command, it is also appended |
| 87 | * to the comm property. |
| 88 | * |
| 89 | * Each command and each result takes the form of ASCII text. For a |
| 90 | * command, the text consists of a nul character followed by several |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 91 | * nul-terminated ASCII strings. The first string consists of a |
| 92 | * single letter: |
| 93 | * "c" for an expression |
| 94 | * "k" for keystrokes |
| 95 | * "r" for reply |
| 96 | * "n" for notification. |
| 97 | * Subsequent strings have the form "option value" where the following options |
| 98 | * are supported: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | * |
| 100 | * -r commWindow serial |
| 101 | * |
| 102 | * This option means that a response should be sent to the window |
| 103 | * whose X identifier is "commWindow" (in hex), and the response should |
| 104 | * be identified with the serial number given by "serial" (in decimal). |
| 105 | * If this option isn't specified then the send is asynchronous and |
| 106 | * no response is sent. |
| 107 | * |
| 108 | * -n name |
| 109 | * "Name" gives the name of the application for which the command is |
| 110 | * intended. This option must be present. |
| 111 | * |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 112 | * -E encoding |
| 113 | * Encoding name used for the text. This is the 'encoding' of the |
| 114 | * sender. The receiver may want to do conversion to his 'encoding'. |
| 115 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 116 | * -s script |
| 117 | * "Script" is the script to be executed. This option must be |
| 118 | * present. Taken as a series of keystrokes in a "k" command where |
| 119 | * <Key>'s are expanded |
| 120 | * |
| 121 | * The options may appear in any order. The -n and -s options must be |
| 122 | * present, but -r may be omitted for asynchronous RPCs. For compatibility |
| 123 | * with future releases that may add new features, there may be additional |
| 124 | * options present; as long as they start with a "-" character, they will |
| 125 | * be ignored. |
| 126 | * |
| 127 | * A result also consists of a zero character followed by several null- |
| 128 | * terminated ASCII strings. The first string consists of the single |
| 129 | * letter "r". Subsequent strings have the form "option value" where |
| 130 | * the following options are supported: |
| 131 | * |
| 132 | * -s serial |
| 133 | * Identifies the command for which this is the result. It is the |
| 134 | * same as the "serial" field from the -s option in the command. This |
| 135 | * option must be present. |
| 136 | * |
| 137 | * -r result |
| 138 | * "Result" is the result string for the script, which may be either |
| 139 | * a result or an error message. If this field is omitted then it |
| 140 | * defaults to an empty string. |
| 141 | * |
| 142 | * -c code |
| 143 | * 0: for OK. This is the default. |
| 144 | * 1: for error: Result is the last error |
| 145 | * |
| 146 | * -i errorInfo |
| 147 | * -e errorCode |
| 148 | * Not applicable for Vim |
| 149 | * |
| 150 | * Options may appear in any order, and only the -s option must be |
| 151 | * present. As with commands, there may be additional options besides |
| 152 | * these; unknown options are ignored. |
| 153 | */ |
| 154 | |
| 155 | /* |
| 156 | * Maximum size property that can be read at one time by |
| 157 | * this module: |
| 158 | */ |
| 159 | |
| 160 | #define MAX_PROP_WORDS 100000 |
| 161 | |
| 162 | struct ServerReply |
| 163 | { |
| 164 | Window id; |
| 165 | garray_T strings; |
| 166 | }; |
| 167 | static garray_T serverReply = { 0, 0, 0, 0, 0 }; |
| 168 | enum ServerReplyOp { SROP_Find, SROP_Add, SROP_Delete }; |
| 169 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 170 | typedef int (*EndCond)(void *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 171 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 172 | struct x_cmdqueue |
| 173 | { |
| 174 | char_u *propInfo; |
Bram Moolenaar | b860388 | 2015-09-17 23:20:42 +0200 | [diff] [blame] | 175 | long_u len; |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 176 | struct x_cmdqueue *next; |
| 177 | struct x_cmdqueue *prev; |
| 178 | }; |
| 179 | |
| 180 | typedef struct x_cmdqueue x_queue_T; |
| 181 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 182 | // dummy node, header for circular queue |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 183 | static x_queue_T head = {NULL, 0, NULL, NULL}; |
| 184 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 185 | /* |
| 186 | * Forward declarations for procedures defined later in this file: |
| 187 | */ |
| 188 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 189 | static Window LookupName(Display *dpy, char_u *name, int delete, char_u **loose); |
| 190 | static int SendInit(Display *dpy); |
| 191 | static int DoRegisterName(Display *dpy, char_u *name); |
| 192 | static void DeleteAnyLingerer(Display *dpy, Window w); |
| 193 | static int GetRegProp(Display *dpy, char_u **regPropp, long_u *numItemsp, int domsg); |
| 194 | static int WaitForPend(void *p); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 195 | static int WindowValid(Display *dpy, Window w); |
| 196 | static void ServerWait(Display *dpy, Window w, EndCond endCond, void *endData, int localLoop, int seconds); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 197 | static int AppendPropCarefully(Display *display, Window window, Atom property, char_u *value, int length); |
| 198 | static int x_error_check(Display *dpy, XErrorEvent *error_event); |
| 199 | static int IsSerialName(char_u *name); |
| 200 | static void save_in_queue(char_u *buf, long_u len); |
| 201 | static void server_parse_message(Display *dpy, char_u *propInfo, long_u numItems); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 203 | // Private variables for the "server" functionality |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | static Atom registryProperty = None; |
| 205 | static Atom vimProperty = None; |
| 206 | static int got_x_error = FALSE; |
| 207 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 208 | static char_u *empty_prop = (char_u *)""; // empty GetRegProp() result |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Associate an ASCII name with Vim. Try real hard to get a unique one. |
| 212 | * Returns FAIL or OK. |
| 213 | */ |
| 214 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 215 | serverRegisterName( |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 216 | Display *dpy, // display to register with |
| 217 | char_u *name) // the name that will be used as a base |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 218 | { |
| 219 | int i; |
| 220 | int res; |
| 221 | char_u *p = NULL; |
| 222 | |
| 223 | res = DoRegisterName(dpy, name); |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 224 | if (res >= 0) |
| 225 | return OK; |
| 226 | |
| 227 | i = 1; |
| 228 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 230 | if (res < -1 || i >= 1000) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 232 | msg_attr(_("Unable to register a command server name"), |
| 233 | HL_ATTR(HLF_W)); |
| 234 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | } |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 236 | if (p == NULL) |
| 237 | p = alloc(STRLEN(name) + 10); |
| 238 | if (p == NULL) |
| 239 | { |
| 240 | res = -10; |
| 241 | continue; |
| 242 | } |
| 243 | sprintf((char *)p, "%s%d", name, i++); |
| 244 | res = DoRegisterName(dpy, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | } |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 246 | while (res < 0) |
| 247 | ; |
| 248 | vim_free(p); |
| 249 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 250 | return OK; |
| 251 | } |
| 252 | |
| 253 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 254 | DoRegisterName(Display *dpy, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 255 | { |
| 256 | Window w; |
| 257 | XErrorHandler old_handler; |
| 258 | #define MAX_NAME_LENGTH 100 |
| 259 | char_u propInfo[MAX_NAME_LENGTH + 20]; |
| 260 | |
| 261 | if (commProperty == None) |
| 262 | { |
| 263 | if (SendInit(dpy) < 0) |
| 264 | return -2; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Make sure the name is unique, and append info about it to |
| 269 | * the registry property. It's important to lock the server |
| 270 | * here to prevent conflicting changes to the registry property. |
| 271 | * WARNING: Do not step through this while debugging, it will hangup the X |
| 272 | * server! |
| 273 | */ |
| 274 | XGrabServer(dpy); |
| 275 | w = LookupName(dpy, name, FALSE, NULL); |
| 276 | if (w != (Window)0) |
| 277 | { |
| 278 | Status status; |
| 279 | int dummyInt; |
| 280 | unsigned int dummyUns; |
| 281 | Window dummyWin; |
| 282 | |
| 283 | /* |
| 284 | * The name is currently registered. See if the commWindow |
| 285 | * associated with the name exists. If not, or if the commWindow |
| 286 | * is *our* commWindow, then just unregister the old name (this |
| 287 | * could happen if an application dies without cleaning up the |
| 288 | * registry). |
| 289 | */ |
| 290 | old_handler = XSetErrorHandler(x_error_check); |
| 291 | status = XGetGeometry(dpy, w, &dummyWin, &dummyInt, &dummyInt, |
| 292 | &dummyUns, &dummyUns, &dummyUns, &dummyUns); |
| 293 | (void)XSetErrorHandler(old_handler); |
| 294 | if (status != Success && w != commWindow) |
| 295 | { |
| 296 | XUngrabServer(dpy); |
| 297 | XFlush(dpy); |
| 298 | return -1; |
| 299 | } |
| 300 | (void)LookupName(dpy, name, /*delete=*/TRUE, NULL); |
| 301 | } |
| 302 | sprintf((char *)propInfo, "%x %.*s", (int_u)commWindow, |
| 303 | MAX_NAME_LENGTH, name); |
| 304 | old_handler = XSetErrorHandler(x_error_check); |
| 305 | got_x_error = FALSE; |
| 306 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8, |
| 307 | PropModeAppend, propInfo, STRLEN(propInfo) + 1); |
| 308 | XUngrabServer(dpy); |
| 309 | XSync(dpy, False); |
| 310 | (void)XSetErrorHandler(old_handler); |
| 311 | |
| 312 | if (!got_x_error) |
| 313 | { |
| 314 | #ifdef FEAT_EVAL |
| 315 | set_vim_var_string(VV_SEND_SERVER, name, -1); |
| 316 | #endif |
| 317 | serverName = vim_strsave(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 318 | need_maketitle = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | return -2; |
| 322 | } |
| 323 | |
| 324 | #if defined(FEAT_GUI) || defined(PROTO) |
| 325 | /* |
| 326 | * Clean out new ID from registry and set it as comm win. |
| 327 | * Change any registered window ID. |
| 328 | */ |
| 329 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 330 | serverChangeRegisteredWindow( |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 331 | Display *dpy, // Display to register with |
| 332 | Window newwin) // Re-register to this ID |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | { |
| 334 | char_u propInfo[MAX_NAME_LENGTH + 20]; |
| 335 | |
| 336 | commWindow = newwin; |
| 337 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 338 | // Always call SendInit() here, to make sure commWindow is marked as a Vim |
| 339 | // window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | if (SendInit(dpy) < 0) |
| 341 | return; |
| 342 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 343 | // WARNING: Do not step through this while debugging, it will hangup the X |
| 344 | // server! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 345 | XGrabServer(dpy); |
| 346 | DeleteAnyLingerer(dpy, newwin); |
| 347 | if (serverName != NULL) |
| 348 | { |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 349 | // Reinsert name if it was already registered |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | (void)LookupName(dpy, serverName, /*delete=*/TRUE, NULL); |
| 351 | sprintf((char *)propInfo, "%x %.*s", |
| 352 | (int_u)newwin, MAX_NAME_LENGTH, serverName); |
| 353 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8, |
| 354 | PropModeAppend, (char_u *)propInfo, |
| 355 | STRLEN(propInfo) + 1); |
| 356 | } |
| 357 | XUngrabServer(dpy); |
| 358 | } |
| 359 | #endif |
| 360 | |
| 361 | /* |
| 362 | * Send to an instance of Vim via the X display. |
| 363 | * Returns 0 for OK, negative for an error. |
| 364 | */ |
| 365 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 366 | serverSendToVim( |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 367 | Display *dpy, // Where to send. |
| 368 | char_u *name, // Where to send. |
| 369 | char_u *cmd, // What to send. |
| 370 | char_u **result, // Result of eval'ed expression |
| 371 | Window *server, // Actual ID of receiving app |
| 372 | Bool asExpr, // Interpret as keystrokes or expr ? |
| 373 | int timeout, // seconds to wait or zero |
| 374 | Bool localLoop, // Throw away everything but result |
| 375 | int silent) // don't complain about no server |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 376 | { |
| 377 | Window w; |
| 378 | char_u *property; |
| 379 | int length; |
| 380 | int res; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 381 | static int serial = 0; // Running count of sent commands. |
| 382 | // Used to give each command a |
| 383 | // different serial number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 384 | PendingCommand pending; |
| 385 | char_u *loosename = NULL; |
| 386 | |
| 387 | if (result != NULL) |
| 388 | *result = NULL; |
| 389 | if (name == NULL || *name == NUL) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 390 | name = (char_u *)"GVIM"; // use a default name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 391 | |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 392 | if (commProperty == None && dpy != NULL && SendInit(dpy) < 0) |
| 393 | return -1; |
| 394 | |
| 395 | #if defined(FEAT_EVAL) |
| 396 | ch_log(NULL, "serverSendToVim(%s, %s)", name, cmd); |
| 397 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 398 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 399 | // Execute locally if no display or target is ourselves |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 400 | if (dpy == NULL || (serverName != NULL && STRICMP(name, serverName) == 0)) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 401 | return sendToLocalVim(cmd, asExpr, result); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * Bind the server name to a communication window. |
| 405 | * |
| 406 | * Find any survivor with a serialno attached to the name if the |
| 407 | * original registrant of the wanted name is no longer present. |
| 408 | * |
| 409 | * Delete any lingering names from dead editors. |
| 410 | */ |
| 411 | while (TRUE) |
| 412 | { |
| 413 | w = LookupName(dpy, name, FALSE, &loosename); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 414 | // Check that the window is hot |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 415 | if (w != None) |
| 416 | { |
| 417 | if (!WindowValid(dpy, w)) |
| 418 | { |
| 419 | LookupName(dpy, loosename ? loosename : name, |
| 420 | /*DELETE=*/TRUE, NULL); |
Bram Moolenaar | 09d6c38 | 2017-09-09 16:25:54 +0200 | [diff] [blame] | 421 | vim_free(loosename); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | continue; |
| 423 | } |
| 424 | } |
| 425 | break; |
| 426 | } |
| 427 | if (w == None) |
| 428 | { |
| 429 | if (!silent) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 430 | semsg(_(e_no_registered_server_named_str), name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 431 | return -1; |
| 432 | } |
| 433 | else if (loosename != NULL) |
| 434 | name = loosename; |
| 435 | if (server != NULL) |
| 436 | *server = w; |
| 437 | |
| 438 | /* |
| 439 | * Send the command to target interpreter by appending it to the |
| 440 | * comm window in the communication window. |
Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 441 | * Length must be computed exactly! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 442 | */ |
Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 443 | length = STRLEN(name) + STRLEN(p_enc) + STRLEN(cmd) + 14; |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 444 | property = alloc(length + 30); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 445 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 446 | sprintf((char *)property, "%c%c%c-n %s%c-E %s%c-s %s", |
| 447 | 0, asExpr ? 'c' : 'k', 0, name, 0, p_enc, 0, cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 448 | if (name == loosename) |
| 449 | vim_free(loosename); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 450 | // Add a back reference to our comm window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | serial++; |
| 452 | sprintf((char *)property + length, "%c-r %x %d", |
| 453 | 0, (int_u)commWindow, serial); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 454 | // Add length of what "-r %x %d" resulted in, skipping the NUL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 455 | length += STRLEN(property + length + 1) + 1; |
| 456 | |
| 457 | res = AppendPropCarefully(dpy, w, commProperty, property, length + 1); |
| 458 | vim_free(property); |
| 459 | if (res < 0) |
| 460 | { |
Bram Moolenaar | cbadefe | 2022-01-01 19:33:50 +0000 | [diff] [blame] | 461 | emsg(_(e_failed_to_send_command_to_destination_program)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 462 | return -1; |
| 463 | } |
| 464 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 465 | if (!asExpr) // There is no answer for this - Keys are sent async |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 466 | return 0; |
| 467 | |
| 468 | /* |
| 469 | * Register the fact that we're waiting for a command to |
| 470 | * complete (this is needed by SendEventProc and by |
| 471 | * AppendErrorProc to pass back the command's results). |
| 472 | */ |
| 473 | pending.serial = serial; |
| 474 | pending.code = 0; |
| 475 | pending.result = NULL; |
| 476 | pending.nextPtr = pendingCommands; |
| 477 | pendingCommands = &pending; |
| 478 | |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 479 | ServerWait(dpy, w, WaitForPend, &pending, localLoop, |
| 480 | timeout > 0 ? timeout : 600); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * Unregister the information about the pending command |
| 484 | * and return the result. |
| 485 | */ |
| 486 | if (pendingCommands == &pending) |
| 487 | pendingCommands = pending.nextPtr; |
| 488 | else |
| 489 | { |
| 490 | PendingCommand *pcPtr; |
| 491 | |
| 492 | for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr) |
| 493 | if (pcPtr->nextPtr == &pending) |
| 494 | { |
| 495 | pcPtr->nextPtr = pending.nextPtr; |
| 496 | break; |
| 497 | } |
| 498 | } |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 499 | |
| 500 | #if defined(FEAT_EVAL) |
| 501 | ch_log(NULL, "serverSendToVim() result: %s", |
| 502 | pending.result == NULL ? "NULL" : (char *)pending.result); |
| 503 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 504 | if (result != NULL) |
| 505 | *result = pending.result; |
| 506 | else |
| 507 | vim_free(pending.result); |
| 508 | |
| 509 | return pending.code == 0 ? 0 : -1; |
| 510 | } |
| 511 | |
| 512 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 513 | WaitForPend(void *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 514 | { |
| 515 | PendingCommand *pending = (PendingCommand *) p; |
| 516 | return pending->result != NULL; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Return TRUE if window "w" exists and has a "Vim" property on it. |
| 521 | */ |
| 522 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 523 | WindowValid(Display *dpy, Window w) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | { |
| 525 | XErrorHandler old_handler; |
| 526 | Atom *plist; |
| 527 | int numProp; |
| 528 | int i; |
| 529 | |
| 530 | old_handler = XSetErrorHandler(x_error_check); |
| 531 | got_x_error = 0; |
| 532 | plist = XListProperties(dpy, w, &numProp); |
| 533 | XSync(dpy, False); |
| 534 | XSetErrorHandler(old_handler); |
| 535 | if (plist == NULL || got_x_error) |
| 536 | return FALSE; |
| 537 | |
| 538 | for (i = 0; i < numProp; i++) |
| 539 | if (plist[i] == vimProperty) |
| 540 | { |
| 541 | XFree(plist); |
| 542 | return TRUE; |
| 543 | } |
| 544 | XFree(plist); |
| 545 | return FALSE; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Enter a loop processing X events & polling chars until we see a result |
| 550 | */ |
| 551 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 552 | ServerWait( |
| 553 | Display *dpy, |
| 554 | Window w, |
| 555 | EndCond endCond, |
| 556 | void *endData, |
| 557 | int localLoop, |
| 558 | int seconds) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 559 | { |
| 560 | time_t start; |
| 561 | time_t now; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 562 | XEvent event; |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 563 | |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 564 | #define UI_MSEC_DELAY 53 |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 565 | #define SEND_MSEC_POLL 500 |
jsecchiero | 6f95363 | 2022-03-20 11:07:17 +0000 | [diff] [blame] | 566 | #ifdef HAVE_SELECT |
| 567 | fd_set fds; |
| 568 | |
| 569 | FD_ZERO(&fds); |
| 570 | FD_SET(ConnectionNumber(dpy), &fds); |
| 571 | #else |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 572 | struct pollfd fds; |
| 573 | |
| 574 | fds.fd = ConnectionNumber(dpy); |
| 575 | fds.events = POLLIN; |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 576 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 577 | |
| 578 | time(&start); |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 579 | while (TRUE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 580 | { |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 581 | while (XCheckWindowEvent(dpy, commWindow, PropertyChangeMask, &event)) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 582 | serverEventProc(dpy, &event, 1); |
Bram Moolenaar | 1e7885a | 2016-03-25 19:03:03 +0100 | [diff] [blame] | 583 | server_parse_messages(); |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 584 | |
| 585 | if (endCond(endData) != 0) |
| 586 | break; |
| 587 | if (!WindowValid(dpy, w)) |
| 588 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 589 | time(&now); |
| 590 | if (seconds >= 0 && (now - start) >= seconds) |
| 591 | break; |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 592 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 593 | #ifdef FEAT_TIMERS |
| 594 | check_due_timer(); |
| 595 | #endif |
| 596 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 597 | // Just look out for the answer without calling back into Vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | if (localLoop) |
| 599 | { |
jsecchiero | 6f95363 | 2022-03-20 11:07:17 +0000 | [diff] [blame] | 600 | #ifdef HAVE_SELECT |
| 601 | struct timeval tv; |
| 602 | |
| 603 | // Set the time every call, select() may change it to the remaining |
| 604 | // time. |
| 605 | tv.tv_sec = 0; |
| 606 | tv.tv_usec = SEND_MSEC_POLL * 1000; |
| 607 | if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 608 | break; |
| 609 | #else |
jsecchiero | 6f95363 | 2022-03-20 11:07:17 +0000 | [diff] [blame] | 610 | if (poll(&fds, 1, SEND_MSEC_POLL) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 611 | break; |
| 612 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 613 | } |
| 614 | else |
| 615 | { |
| 616 | if (got_int) |
| 617 | break; |
Bram Moolenaar | 773c1ef | 2012-07-10 14:56:45 +0200 | [diff] [blame] | 618 | ui_delay((long)UI_MSEC_DELAY, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 619 | ui_breakcheck(); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /* |
| 626 | * Fetch a list of all the Vim instance names currently registered for the |
| 627 | * display. |
| 628 | * |
| 629 | * Returns a newline separated list in allocated memory or NULL. |
| 630 | */ |
| 631 | char_u * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 632 | serverGetVimNames(Display *dpy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | { |
| 634 | char_u *regProp; |
| 635 | char_u *entry; |
| 636 | char_u *p; |
| 637 | long_u numItems; |
| 638 | int_u w; |
| 639 | garray_T ga; |
| 640 | |
| 641 | if (registryProperty == None) |
| 642 | { |
| 643 | if (SendInit(dpy) < 0) |
| 644 | return NULL; |
| 645 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 646 | |
| 647 | /* |
| 648 | * Read the registry property. |
| 649 | */ |
| 650 | if (GetRegProp(dpy, ®Prop, &numItems, TRUE) == FAIL) |
| 651 | return NULL; |
| 652 | |
| 653 | /* |
| 654 | * Scan all of the names out of the property. |
| 655 | */ |
| 656 | ga_init2(&ga, 1, 100); |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 657 | for (p = regProp; (long_u)(p - regProp) < numItems; p++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 658 | { |
| 659 | entry = p; |
| 660 | while (*p != 0 && !isspace(*p)) |
| 661 | p++; |
| 662 | if (*p != 0) |
| 663 | { |
| 664 | w = None; |
| 665 | sscanf((char *)entry, "%x", &w); |
| 666 | if (WindowValid(dpy, (Window)w)) |
| 667 | { |
| 668 | ga_concat(&ga, p + 1); |
| 669 | ga_concat(&ga, (char_u *)"\n"); |
| 670 | } |
| 671 | while (*p != 0) |
| 672 | p++; |
| 673 | } |
| 674 | } |
| 675 | if (regProp != empty_prop) |
| 676 | XFree(regProp); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 677 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | return ga.ga_data; |
| 679 | } |
| 680 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 681 | ///////////////////////////////////////////////////////////// |
| 682 | // Reply stuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | |
| 684 | static struct ServerReply * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 685 | ServerReplyFind(Window w, enum ServerReplyOp op) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 686 | { |
| 687 | struct ServerReply *p; |
| 688 | struct ServerReply e; |
| 689 | int i; |
| 690 | |
| 691 | p = (struct ServerReply *) serverReply.ga_data; |
| 692 | for (i = 0; i < serverReply.ga_len; i++, p++) |
| 693 | if (p->id == w) |
| 694 | break; |
| 695 | if (i >= serverReply.ga_len) |
| 696 | p = NULL; |
| 697 | |
| 698 | if (p == NULL && op == SROP_Add) |
| 699 | { |
| 700 | if (serverReply.ga_growsize == 0) |
| 701 | ga_init2(&serverReply, sizeof(struct ServerReply), 1); |
| 702 | if (ga_grow(&serverReply, 1) == OK) |
| 703 | { |
| 704 | p = ((struct ServerReply *) serverReply.ga_data) |
| 705 | + serverReply.ga_len; |
| 706 | e.id = w; |
| 707 | ga_init2(&e.strings, 1, 100); |
Bram Moolenaar | fbc0cfa | 2008-11-12 13:52:46 +0000 | [diff] [blame] | 708 | mch_memmove(p, &e, sizeof(e)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | serverReply.ga_len++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | else if (p != NULL && op == SROP_Delete) |
| 713 | { |
| 714 | ga_clear(&p->strings); |
| 715 | mch_memmove(p, p + 1, (serverReply.ga_len - i - 1) * sizeof(*p)); |
| 716 | serverReply.ga_len--; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | return p; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Convert string to windowid. |
| 724 | * Issue an error if the id is invalid. |
| 725 | */ |
| 726 | Window |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 727 | serverStrToWin(char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | { |
| 729 | unsigned id = None; |
| 730 | |
| 731 | sscanf((char *)str, "0x%x", &id); |
| 732 | if (id == None) |
Bram Moolenaar | 1d423ef | 2022-01-02 21:26:16 +0000 | [diff] [blame] | 733 | semsg(_(e_invalid_server_id_used_str), str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | |
| 735 | return (Window)id; |
| 736 | } |
| 737 | |
| 738 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 739 | * Send a reply string (notification) to client with id "name". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | * Return -1 if the window is invalid. |
| 741 | */ |
| 742 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 743 | serverSendReply(char_u *name, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 744 | { |
| 745 | char_u *property; |
| 746 | int length; |
| 747 | int res; |
| 748 | Display *dpy = X_DISPLAY; |
| 749 | Window win = serverStrToWin(name); |
| 750 | |
| 751 | if (commProperty == None) |
| 752 | { |
| 753 | if (SendInit(dpy) < 0) |
| 754 | return -2; |
| 755 | } |
| 756 | if (!WindowValid(dpy, win)) |
| 757 | return -1; |
| 758 | |
Bram Moolenaar | ac6e65f | 2005-08-29 22:25:38 +0000 | [diff] [blame] | 759 | length = STRLEN(p_enc) + STRLEN(str) + 14; |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 760 | if ((property = alloc(length + 30)) == NULL) |
| 761 | return -1; |
| 762 | |
| 763 | sprintf((char *)property, "%cn%c-E %s%c-n %s%c-w %x", |
| 764 | 0, 0, p_enc, 0, str, 0, (unsigned int)commWindow); |
| 765 | // Add length of what "%x" resulted in. |
| 766 | length += STRLEN(property + length); |
| 767 | res = AppendPropCarefully(dpy, win, commProperty, property, length + 1); |
| 768 | vim_free(property); |
| 769 | |
| 770 | return res; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 774 | WaitForReply(void *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 775 | { |
| 776 | Window *w = (Window *) p; |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 777 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | return ServerReplyFind(*w, SROP_Find) != NULL; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Wait for replies from id (win) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 783 | * When "timeout" is non-zero wait up to this many seconds. |
Bram Moolenaar | 747f110 | 2022-09-18 13:06:41 +0100 | [diff] [blame] | 784 | * Return 0 and the allocated string in "*str" when a reply is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 785 | * Return -1 if the window becomes invalid while waiting. |
| 786 | */ |
| 787 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 788 | serverReadReply( |
| 789 | Display *dpy, |
| 790 | Window win, |
| 791 | char_u **str, |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 792 | int localLoop, |
| 793 | int timeout) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | { |
| 795 | int len; |
| 796 | char_u *s; |
| 797 | struct ServerReply *p; |
| 798 | |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 799 | ServerWait(dpy, win, WaitForReply, &win, localLoop, |
| 800 | timeout > 0 ? timeout : -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 801 | |
| 802 | if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0) |
| 803 | { |
| 804 | *str = vim_strsave(p->strings.ga_data); |
| 805 | len = STRLEN(*str) + 1; |
| 806 | if (len < p->strings.ga_len) |
| 807 | { |
| 808 | s = (char_u *) p->strings.ga_data; |
| 809 | mch_memmove(s, s + len, p->strings.ga_len - len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 810 | p->strings.ga_len -= len; |
| 811 | } |
| 812 | else |
| 813 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 814 | // Last string read. Remove from list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 815 | ga_clear(&p->strings); |
| 816 | ServerReplyFind(win, SROP_Delete); |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | return -1; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Check for replies from id (win). |
| 825 | * Return TRUE and a non-malloc'ed string if there is. Else return FALSE. |
| 826 | */ |
| 827 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 828 | serverPeekReply(Display *dpy, Window win, char_u **str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | { |
| 830 | struct ServerReply *p; |
| 831 | |
| 832 | if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0) |
| 833 | { |
| 834 | if (str != NULL) |
| 835 | *str = p->strings.ga_data; |
| 836 | return 1; |
| 837 | } |
| 838 | if (!WindowValid(dpy, win)) |
| 839 | return -1; |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | |
| 844 | /* |
| 845 | * Initialize the communication channels for sending commands and receiving |
| 846 | * results. |
| 847 | */ |
| 848 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 849 | SendInit(Display *dpy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | { |
| 851 | XErrorHandler old_handler; |
| 852 | |
| 853 | /* |
| 854 | * Create the window used for communication, and set up an |
| 855 | * event handler for it. |
| 856 | */ |
| 857 | old_handler = XSetErrorHandler(x_error_check); |
| 858 | got_x_error = FALSE; |
| 859 | |
| 860 | if (commProperty == None) |
| 861 | commProperty = XInternAtom(dpy, "Comm", False); |
| 862 | if (vimProperty == None) |
| 863 | vimProperty = XInternAtom(dpy, "Vim", False); |
| 864 | if (registryProperty == None) |
| 865 | registryProperty = XInternAtom(dpy, "VimRegistry", False); |
| 866 | |
| 867 | if (commWindow == None) |
| 868 | { |
| 869 | commWindow = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), |
| 870 | getpid(), 0, 10, 10, 0, |
| 871 | WhitePixel(dpy, DefaultScreen(dpy)), |
| 872 | WhitePixel(dpy, DefaultScreen(dpy))); |
| 873 | XSelectInput(dpy, commWindow, PropertyChangeMask); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 874 | // WARNING: Do not step through this while debugging, it will hangup |
| 875 | // the X server! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | XGrabServer(dpy); |
| 877 | DeleteAnyLingerer(dpy, commWindow); |
| 878 | XUngrabServer(dpy); |
| 879 | } |
| 880 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 881 | // Make window recognizable as a vim window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 882 | XChangeProperty(dpy, commWindow, vimProperty, XA_STRING, |
| 883 | 8, PropModeReplace, (char_u *)VIM_VERSION_SHORT, |
| 884 | (int)STRLEN(VIM_VERSION_SHORT) + 1); |
| 885 | |
| 886 | XSync(dpy, False); |
| 887 | (void)XSetErrorHandler(old_handler); |
| 888 | |
| 889 | return got_x_error ? -1 : 0; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Given a server name, see if the name exists in the registry for a |
| 894 | * particular display. |
| 895 | * |
| 896 | * If the given name is registered, return the ID of the window associated |
| 897 | * with the name. If the name isn't registered, then return 0. |
| 898 | * |
| 899 | * Side effects: |
| 900 | * If the registry property is improperly formed, then it is deleted. |
| 901 | * If "delete" is non-zero, then if the named server is found it is |
| 902 | * removed from the registry property. |
| 903 | */ |
| 904 | static Window |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 905 | LookupName( |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 906 | Display *dpy, // Display whose registry to check. |
| 907 | char_u *name, // Name of a server. |
| 908 | int delete, // If non-zero, delete info about name. |
| 909 | char_u **loose) // Do another search matching -999 if not found |
| 910 | // Return result here if a match is found |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | { |
| 912 | char_u *regProp, *entry; |
| 913 | char_u *p; |
| 914 | long_u numItems; |
| 915 | int_u returnValue; |
| 916 | |
| 917 | /* |
| 918 | * Read the registry property. |
| 919 | */ |
| 920 | if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL) |
| 921 | return 0; |
| 922 | |
| 923 | /* |
| 924 | * Scan the property for the desired name. |
| 925 | */ |
| 926 | returnValue = (int_u)None; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 927 | entry = NULL; // Not needed, but eliminates compiler warning. |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 928 | for (p = regProp; (long_u)(p - regProp) < numItems; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | { |
| 930 | entry = p; |
| 931 | while (*p != 0 && !isspace(*p)) |
| 932 | p++; |
| 933 | if (*p != 0 && STRICMP(name, p + 1) == 0) |
| 934 | { |
| 935 | sscanf((char *)entry, "%x", &returnValue); |
| 936 | break; |
| 937 | } |
| 938 | while (*p != 0) |
| 939 | p++; |
| 940 | p++; |
| 941 | } |
| 942 | |
| 943 | if (loose != NULL && returnValue == (int_u)None && !IsSerialName(name)) |
| 944 | { |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 945 | for (p = regProp; (long_u)(p - regProp) < numItems; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 946 | { |
| 947 | entry = p; |
| 948 | while (*p != 0 && !isspace(*p)) |
| 949 | p++; |
| 950 | if (*p != 0 && IsSerialName(p + 1) |
| 951 | && STRNICMP(name, p + 1, STRLEN(name)) == 0) |
| 952 | { |
| 953 | sscanf((char *)entry, "%x", &returnValue); |
| 954 | *loose = vim_strsave(p + 1); |
| 955 | break; |
| 956 | } |
| 957 | while (*p != 0) |
| 958 | p++; |
| 959 | p++; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Delete the property, if that is desired (copy down the |
| 965 | * remainder of the registry property to overlay the deleted |
| 966 | * info, then rewrite the property). |
| 967 | */ |
| 968 | if (delete && returnValue != (int_u)None) |
| 969 | { |
| 970 | int count; |
| 971 | |
| 972 | while (*p != 0) |
| 973 | p++; |
| 974 | p++; |
| 975 | count = numItems - (p - regProp); |
| 976 | if (count > 0) |
Bram Moolenaar | fbc0cfa | 2008-11-12 13:52:46 +0000 | [diff] [blame] | 977 | mch_memmove(entry, p, count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 978 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, |
| 979 | 8, PropModeReplace, regProp, |
| 980 | (int)(numItems - (p - entry))); |
| 981 | XSync(dpy, False); |
| 982 | } |
| 983 | |
| 984 | if (regProp != empty_prop) |
| 985 | XFree(regProp); |
| 986 | return (Window)returnValue; |
| 987 | } |
| 988 | |
| 989 | /* |
Bram Moolenaar | 82038d7 | 2007-05-10 17:15:45 +0000 | [diff] [blame] | 990 | * Delete any lingering occurrence of window id. We promise that any |
| 991 | * occurrence is not ours since it is not yet put into the registry (by us) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | * |
| 993 | * This is necessary in the following scenario: |
| 994 | * 1. There is an old windowid for an exit'ed vim in the registry |
| 995 | * 2. We get that id for our commWindow but only want to send, not register. |
| 996 | * 3. The window will mistakenly be regarded valid because of own commWindow |
| 997 | */ |
| 998 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 999 | DeleteAnyLingerer( |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1000 | Display *dpy, // Display whose registry to check. |
| 1001 | Window win) // Window to remove |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | { |
| 1003 | char_u *regProp, *entry = NULL; |
| 1004 | char_u *p; |
| 1005 | long_u numItems; |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 1006 | int_u wwin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1007 | |
| 1008 | /* |
| 1009 | * Read the registry property. |
| 1010 | */ |
| 1011 | if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL) |
| 1012 | return; |
| 1013 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1014 | // Scan the property for the window id. |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 1015 | for (p = regProp; (long_u)(p - regProp) < numItems; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | { |
| 1017 | if (*p != 0) |
| 1018 | { |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 1019 | sscanf((char *)p, "%x", &wwin); |
| 1020 | if ((Window)wwin == win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | { |
| 1022 | int lastHalf; |
| 1023 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1024 | // Copy down the remainder to delete entry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1025 | entry = p; |
| 1026 | while (*p != 0) |
| 1027 | p++; |
| 1028 | p++; |
| 1029 | lastHalf = numItems - (p - regProp); |
| 1030 | if (lastHalf > 0) |
Bram Moolenaar | fbc0cfa | 2008-11-12 13:52:46 +0000 | [diff] [blame] | 1031 | mch_memmove(entry, p, lastHalf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1032 | numItems = (entry - regProp) + lastHalf; |
| 1033 | p = entry; |
| 1034 | continue; |
| 1035 | } |
| 1036 | } |
| 1037 | while (*p != 0) |
| 1038 | p++; |
| 1039 | p++; |
| 1040 | } |
| 1041 | |
| 1042 | if (entry != NULL) |
| 1043 | { |
| 1044 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, |
| 1045 | XA_STRING, 8, PropModeReplace, regProp, |
| 1046 | (int)(p - regProp)); |
| 1047 | XSync(dpy, False); |
| 1048 | } |
| 1049 | |
| 1050 | if (regProp != empty_prop) |
| 1051 | XFree(regProp); |
| 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | * Read the registry property. Delete it when it's formatted wrong. |
| 1056 | * Return the property in "regPropp". "empty_prop" is used when it doesn't |
| 1057 | * exist yet. |
| 1058 | * Return OK when successful. |
| 1059 | */ |
| 1060 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1061 | GetRegProp( |
| 1062 | Display *dpy, |
| 1063 | char_u **regPropp, |
| 1064 | long_u *numItemsp, |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1065 | int domsg) // When TRUE give error message. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1066 | { |
| 1067 | int result, actualFormat; |
| 1068 | long_u bytesAfter; |
| 1069 | Atom actualType; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1070 | XErrorHandler old_handler; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1071 | |
| 1072 | *regPropp = NULL; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1073 | old_handler = XSetErrorHandler(x_error_check); |
| 1074 | got_x_error = FALSE; |
| 1075 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1076 | result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0L, |
| 1077 | (long)MAX_PROP_WORDS, False, |
| 1078 | XA_STRING, &actualType, |
| 1079 | &actualFormat, numItemsp, &bytesAfter, |
| 1080 | regPropp); |
| 1081 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1082 | XSync(dpy, FALSE); |
| 1083 | (void)XSetErrorHandler(old_handler); |
| 1084 | if (got_x_error) |
| 1085 | return FAIL; |
| 1086 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | if (actualType == None) |
| 1088 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1089 | // No prop yet. Logically equal to the empty list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | *numItemsp = 0; |
| 1091 | *regPropp = empty_prop; |
| 1092 | return OK; |
| 1093 | } |
| 1094 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1095 | // If the property is improperly formed, then delete it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | if (result != Success || actualFormat != 8 || actualType != XA_STRING) |
| 1097 | { |
| 1098 | if (*regPropp != NULL) |
| 1099 | XFree(*regPropp); |
| 1100 | XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty); |
| 1101 | if (domsg) |
Bram Moolenaar | cbadefe | 2022-01-01 19:33:50 +0000 | [diff] [blame] | 1102 | emsg(_(e_vim_instance_registry_property_is_badly_formed_deleted)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1103 | return FAIL; |
| 1104 | } |
| 1105 | return OK; |
| 1106 | } |
| 1107 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1108 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1109 | /* |
Bram Moolenaar | 82038d7 | 2007-05-10 17:15:45 +0000 | [diff] [blame] | 1110 | * This procedure is invoked by the various X event loops throughout Vims when |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1111 | * a property changes on the communication window. This procedure reads the |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1112 | * property and enqueues command requests and responses. If immediate is true, |
Bram Moolenaar | 792f0e3 | 2018-02-27 17:27:13 +0100 | [diff] [blame] | 1113 | * it runs the event immediately instead of enqueuing it. Immediate can cause |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1114 | * unintended behavior and should only be used for code that blocks for a |
| 1115 | * response. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1116 | */ |
| 1117 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1118 | serverEventProc( |
| 1119 | Display *dpy, |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1120 | XEvent *eventPtr, // Information about event. |
| 1121 | int immediate) // Run event immediately. Should mostly be 0. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1122 | { |
| 1123 | char_u *propInfo; |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1124 | int result, actualFormat; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1125 | long_u numItems, bytesAfter; |
| 1126 | Atom actualType; |
| 1127 | |
| 1128 | if (eventPtr != NULL) |
| 1129 | { |
| 1130 | if (eventPtr->xproperty.atom != commProperty |
| 1131 | || eventPtr->xproperty.state != PropertyNewValue) |
| 1132 | return; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * Read the comm property and delete it. |
| 1137 | */ |
| 1138 | propInfo = NULL; |
| 1139 | result = XGetWindowProperty(dpy, commWindow, commProperty, 0L, |
| 1140 | (long)MAX_PROP_WORDS, True, |
| 1141 | XA_STRING, &actualType, |
| 1142 | &actualFormat, &numItems, &bytesAfter, |
| 1143 | &propInfo); |
| 1144 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1145 | // If the property doesn't exist or is improperly formed then ignore it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1146 | if (result != Success || actualType != XA_STRING || actualFormat != 8) |
| 1147 | { |
| 1148 | if (propInfo != NULL) |
| 1149 | XFree(propInfo); |
| 1150 | return; |
| 1151 | } |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1152 | if (immediate) |
| 1153 | server_parse_message(dpy, propInfo, numItems); |
| 1154 | else |
| 1155 | save_in_queue(propInfo, numItems); |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Saves x clientserver commands in a queue so that they can be called when |
| 1160 | * vim is idle. |
| 1161 | */ |
| 1162 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1163 | save_in_queue(char_u *propInfo, long_u len) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1164 | { |
| 1165 | x_queue_T *node; |
| 1166 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1167 | node = ALLOC_ONE(x_queue_T); |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1168 | if (node == NULL) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1169 | return; // out of memory |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1170 | node->propInfo = propInfo; |
| 1171 | node->len = len; |
| 1172 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1173 | if (head.next == NULL) // initialize circular queue |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1174 | { |
| 1175 | head.next = &head; |
| 1176 | head.prev = &head; |
| 1177 | } |
| 1178 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1179 | // insert node at tail of queue |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1180 | node->next = &head; |
| 1181 | node->prev = head.prev; |
| 1182 | head.prev->next = node; |
| 1183 | head.prev = node; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * Parses queued clientserver messages. |
| 1188 | */ |
| 1189 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1190 | server_parse_messages(void) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1191 | { |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1192 | x_queue_T *node; |
| 1193 | |
| 1194 | if (!X_DISPLAY) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1195 | return; // cannot happen? |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1196 | while (head.next != NULL && head.next != &head) |
| 1197 | { |
| 1198 | node = head.next; |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1199 | head.next = node->next; |
| 1200 | node->next->prev = node->prev; |
Bram Moolenaar | 4e86150 | 2015-10-13 20:21:49 +0200 | [diff] [blame] | 1201 | server_parse_message(X_DISPLAY, node->propInfo, node->len); |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1202 | vim_free(node); |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Returns a non-zero value if there are clientserver messages waiting |
| 1208 | * int the queue. |
| 1209 | */ |
| 1210 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1211 | server_waiting(void) |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1212 | { |
| 1213 | return head.next != NULL && head.next != &head; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Prases a single clientserver message. A single message may contain multiple |
| 1218 | * commands. |
| 1219 | * "propInfo" will be freed. |
| 1220 | */ |
| 1221 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1222 | server_parse_message( |
| 1223 | Display *dpy, |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1224 | char_u *propInfo, // A string containing 0 or more X commands |
| 1225 | long_u numItems) // The size of propInfo in bytes. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1226 | { |
| 1227 | char_u *p; |
| 1228 | int code; |
| 1229 | char_u *tofree; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 1231 | #if defined(FEAT_EVAL) |
| 1232 | ch_log(NULL, "server_parse_message() numItems: %ld", numItems); |
| 1233 | #endif |
| 1234 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1235 | /* |
| 1236 | * Several commands and results could arrive in the property at |
| 1237 | * one time; each iteration through the outer loop handles a |
| 1238 | * single command or result. |
| 1239 | */ |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 1240 | for (p = propInfo; (long_u)(p - propInfo) < numItems; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1241 | { |
| 1242 | /* |
| 1243 | * Ignore leading NULs; each command or result starts with a |
| 1244 | * NUL so that no matter how badly formed a preceding command |
| 1245 | * is, we'll be able to tell that a new command/result is |
| 1246 | * starting. |
| 1247 | */ |
| 1248 | if (*p == 0) |
| 1249 | { |
| 1250 | p++; |
| 1251 | continue; |
| 1252 | } |
| 1253 | |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 1254 | if ((*p == 'c' || *p == 'k') && p[1] == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1255 | { |
| 1256 | Window resWindow; |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1257 | char_u *name, *script, *serial, *end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1258 | Bool asKeys = *p == 'k'; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1259 | char_u *enc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | |
| 1261 | /* |
| 1262 | * This is an incoming command from some other application. |
| 1263 | * Iterate over all of its options. Stop when we reach |
| 1264 | * the end of the property or something that doesn't look |
| 1265 | * like an option. |
| 1266 | */ |
| 1267 | p += 2; |
| 1268 | name = NULL; |
| 1269 | resWindow = None; |
| 1270 | serial = (char_u *)""; |
| 1271 | script = NULL; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1272 | enc = NULL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 1273 | while ((long_u)(p - propInfo) < numItems && *p == '-') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1274 | { |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 1275 | #if defined(FEAT_EVAL) |
| 1276 | ch_log(NULL, "server_parse_message() item: %c, %s", p[-2], p); |
| 1277 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | switch (p[1]) |
| 1279 | { |
| 1280 | case 'r': |
| 1281 | end = skipwhite(p + 2); |
| 1282 | resWindow = 0; |
| 1283 | while (vim_isxdigit(*end)) |
| 1284 | { |
| 1285 | resWindow = 16 * resWindow + (long_u)hex2nr(*end); |
| 1286 | ++end; |
| 1287 | } |
| 1288 | if (end == p + 2 || *end != ' ') |
| 1289 | resWindow = None; |
| 1290 | else |
| 1291 | { |
| 1292 | p = serial = end + 1; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1293 | clientWindow = resWindow; // Remember in global |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1294 | } |
| 1295 | break; |
| 1296 | case 'n': |
| 1297 | if (p[2] == ' ') |
| 1298 | name = p + 3; |
| 1299 | break; |
| 1300 | case 's': |
| 1301 | if (p[2] == ' ') |
| 1302 | script = p + 3; |
| 1303 | break; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1304 | case 'E': |
| 1305 | if (p[2] == ' ') |
| 1306 | enc = p + 3; |
| 1307 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | } |
| 1309 | while (*p != 0) |
| 1310 | p++; |
| 1311 | p++; |
| 1312 | } |
| 1313 | |
| 1314 | if (script == NULL || name == NULL) |
| 1315 | continue; |
| 1316 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1317 | if (serverName != NULL && STRICMP(name, serverName) == 0) |
| 1318 | { |
| 1319 | script = serverConvert(enc, script, &tofree); |
| 1320 | if (asKeys) |
| 1321 | server_to_input_buf(script); |
| 1322 | else |
| 1323 | { |
| 1324 | char_u *res; |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1325 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1326 | res = eval_client_expr_to_string(script); |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1327 | if (resWindow != None) |
| 1328 | { |
| 1329 | garray_T reply; |
| 1330 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1331 | // Initialize the result property. |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1332 | ga_init2(&reply, 1, 100); |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 1333 | (void)ga_grow(&reply, 50 + STRLEN(p_enc)); |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1334 | sprintf(reply.ga_data, "%cr%c-E %s%c-s %s%c-r ", |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1335 | 0, 0, p_enc, 0, serial, 0); |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1336 | reply.ga_len = 14 + STRLEN(p_enc) + STRLEN(serial); |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1337 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1338 | // Evaluate the expression and return the result. |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1339 | if (res != NULL) |
| 1340 | ga_concat(&reply, res); |
| 1341 | else |
| 1342 | { |
Bram Moolenaar | 4c5678f | 2022-11-30 18:12:19 +0000 | [diff] [blame] | 1343 | ga_concat(&reply, |
| 1344 | (char_u *)_(e_invalid_expression_received)); |
Bram Moolenaar | 52bf469 | 2012-07-10 14:25:04 +0200 | [diff] [blame] | 1345 | ga_append(&reply, 0); |
| 1346 | ga_concat(&reply, (char_u *)"-c 1"); |
| 1347 | } |
| 1348 | ga_append(&reply, NUL); |
| 1349 | (void)AppendPropCarefully(dpy, resWindow, commProperty, |
| 1350 | reply.ga_data, reply.ga_len); |
| 1351 | ga_clear(&reply); |
| 1352 | } |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 1353 | vim_free(res); |
| 1354 | } |
| 1355 | vim_free(tofree); |
| 1356 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1357 | } |
| 1358 | else if (*p == 'r' && p[1] == 0) |
| 1359 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1360 | int serial, gotSerial; |
| 1361 | char_u *res; |
| 1362 | PendingCommand *pcPtr; |
| 1363 | char_u *enc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* |
| 1366 | * This is a reply to some command that we sent out. Iterate |
| 1367 | * over all of its options. Stop when we reach the end of the |
| 1368 | * property or something that doesn't look like an option. |
| 1369 | */ |
| 1370 | p += 2; |
| 1371 | gotSerial = 0; |
| 1372 | res = (char_u *)""; |
| 1373 | code = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1374 | enc = NULL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 1375 | while ((long_u)(p - propInfo) < numItems && *p == '-') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1376 | { |
| 1377 | switch (p[1]) |
| 1378 | { |
| 1379 | case 'r': |
| 1380 | if (p[2] == ' ') |
| 1381 | res = p + 3; |
| 1382 | break; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1383 | case 'E': |
| 1384 | if (p[2] == ' ') |
| 1385 | enc = p + 3; |
| 1386 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | case 's': |
| 1388 | if (sscanf((char *)p + 2, " %d", &serial) == 1) |
| 1389 | gotSerial = 1; |
| 1390 | break; |
| 1391 | case 'c': |
| 1392 | if (sscanf((char *)p + 2, " %d", &code) != 1) |
| 1393 | code = 0; |
| 1394 | break; |
| 1395 | } |
| 1396 | while (*p != 0) |
| 1397 | p++; |
| 1398 | p++; |
| 1399 | } |
| 1400 | |
| 1401 | if (!gotSerial) |
| 1402 | continue; |
| 1403 | |
| 1404 | /* |
| 1405 | * Give the result information to anyone who's |
| 1406 | * waiting for it. |
| 1407 | */ |
| 1408 | for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr) |
| 1409 | { |
| 1410 | if (serial != pcPtr->serial || pcPtr->result != NULL) |
| 1411 | continue; |
| 1412 | |
| 1413 | pcPtr->code = code; |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 1414 | res = serverConvert(enc, res, &tofree); |
| 1415 | if (tofree == NULL) |
| 1416 | res = vim_strsave(res); |
| 1417 | pcPtr->result = res; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | else if (*p == 'n' && p[1] == 0) |
| 1422 | { |
| 1423 | Window win = 0; |
| 1424 | unsigned int u; |
| 1425 | int gotWindow; |
| 1426 | char_u *str; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | struct ServerReply *r; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1428 | char_u *enc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1429 | |
| 1430 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 1431 | * This is a (n)otification. Sent with serverreply_send in Vim |
| 1432 | * script. Execute any autocommand and save it for later retrieval |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1433 | */ |
| 1434 | p += 2; |
| 1435 | gotWindow = 0; |
| 1436 | str = (char_u *)""; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1437 | enc = NULL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 1438 | while ((long_u)(p - propInfo) < numItems && *p == '-') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | { |
| 1440 | switch (p[1]) |
| 1441 | { |
| 1442 | case 'n': |
| 1443 | if (p[2] == ' ') |
| 1444 | str = p + 3; |
| 1445 | break; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1446 | case 'E': |
| 1447 | if (p[2] == ' ') |
| 1448 | enc = p + 3; |
| 1449 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1450 | case 'w': |
| 1451 | if (sscanf((char *)p + 2, " %x", &u) == 1) |
| 1452 | { |
| 1453 | win = u; |
| 1454 | gotWindow = 1; |
| 1455 | } |
| 1456 | break; |
| 1457 | } |
| 1458 | while (*p != 0) |
| 1459 | p++; |
| 1460 | p++; |
| 1461 | } |
| 1462 | |
| 1463 | if (!gotWindow) |
| 1464 | continue; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1465 | str = serverConvert(enc, str, &tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1466 | if ((r = ServerReplyFind(win, SROP_Add)) != NULL) |
| 1467 | { |
| 1468 | ga_concat(&(r->strings), str); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 1469 | ga_append(&(r->strings), NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1470 | } |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 1471 | { |
| 1472 | char_u winstr[30]; |
| 1473 | |
| 1474 | sprintf((char *)winstr, "0x%x", (unsigned int)win); |
| 1475 | apply_autocmds(EVENT_REMOTEREPLY, winstr, str, TRUE, curbuf); |
| 1476 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1477 | vim_free(tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1478 | } |
| 1479 | else |
| 1480 | { |
| 1481 | /* |
| 1482 | * Didn't recognize this thing. Just skip through the next |
| 1483 | * null character and try again. |
| 1484 | * Even if we get an 'r'(eply) we will throw it away as we |
| 1485 | * never specify (and thus expect) one |
| 1486 | */ |
| 1487 | while (*p != 0) |
| 1488 | p++; |
| 1489 | p++; |
| 1490 | } |
| 1491 | } |
| 1492 | XFree(propInfo); |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * Append a given property to a given window, but set up an X error handler so |
| 1497 | * that if the append fails this procedure can return an error code rather |
| 1498 | * than having Xlib panic. |
| 1499 | * Return: 0 for OK, -1 for error |
| 1500 | */ |
| 1501 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1502 | AppendPropCarefully( |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1503 | Display *dpy, // Display on which to operate. |
| 1504 | Window window, // Window whose property is to be modified. |
| 1505 | Atom property, // Name of property. |
| 1506 | char_u *value, // Characters to append to property. |
| 1507 | int length) // How much to append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1508 | { |
| 1509 | XErrorHandler old_handler; |
| 1510 | |
| 1511 | old_handler = XSetErrorHandler(x_error_check); |
| 1512 | got_x_error = FALSE; |
| 1513 | XChangeProperty(dpy, window, property, XA_STRING, 8, |
| 1514 | PropModeAppend, value, length); |
| 1515 | XSync(dpy, False); |
| 1516 | (void) XSetErrorHandler(old_handler); |
| 1517 | return got_x_error ? -1 : 0; |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | /* |
| 1522 | * Another X Error handler, just used to check for errors. |
| 1523 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1524 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1525 | x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1526 | { |
| 1527 | got_x_error = TRUE; |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Check if "str" looks like it had a serial number appended. |
| 1533 | * Actually just checks if the name ends in a digit. |
| 1534 | */ |
| 1535 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1536 | IsSerialName(char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | { |
| 1538 | int len = STRLEN(str); |
| 1539 | |
| 1540 | return (len > 1 && vim_isdigit(str[len - 1])); |
| 1541 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1542 | #endif // FEAT_CLIENTSERVER |