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