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