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