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