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