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