Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | |
| 24 | # if defined(HAVE_SYS_SELECT_H) && \ |
| 25 | (!defined(HAVE_SYS_TIME_H) || defined(SYS_SELECT_WITH_SYS_TIME)) |
| 26 | # include <sys/select.h> |
| 27 | # endif |
| 28 | |
| 29 | # ifndef HAVE_SELECT |
| 30 | # ifdef HAVE_SYS_POLL_H |
| 31 | # include <sys/poll.h> |
| 32 | # else |
| 33 | # ifdef HAVE_POLL_H |
| 34 | # include <poll.h> |
| 35 | # endif |
| 36 | # endif |
| 37 | # endif |
| 38 | |
| 39 | /* |
| 40 | * This file provides procedures that implement the command server functionality |
| 41 | * of Vim when in contact with an X11 server. |
| 42 | * |
| 43 | * Adapted from TCL/TK's send command in tkSend.c of the tk 3.6 distribution. |
| 44 | * Adapted for use in Vim by Flemming Madsen. Protocol changed to that of tk 4 |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * Copyright (c) 1989-1993 The Regents of the University of California. |
| 49 | * All rights reserved. |
| 50 | * |
| 51 | * Permission is hereby granted, without written agreement and without |
| 52 | * license or royalty fees, to use, copy, modify, and distribute this |
| 53 | * software and its documentation for any purpose, provided that the |
| 54 | * above copyright notice and the following two paragraphs appear in |
| 55 | * all copies of this software. |
| 56 | * |
| 57 | * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR |
| 58 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT |
| 59 | * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF |
| 60 | * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 61 | * |
| 62 | * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
| 63 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 64 | * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 65 | * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO |
| 66 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 67 | */ |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * When a result is being awaited from a sent command, one of |
| 72 | * the following structures is present on a list of all outstanding |
| 73 | * sent commands. The information in the structure is used to |
| 74 | * process the result when it arrives. You're probably wondering |
| 75 | * how there could ever be multiple outstanding sent commands. |
| 76 | * This could happen if Vim instances invoke each other recursively. |
| 77 | * It's unlikely, but possible. |
| 78 | */ |
| 79 | |
| 80 | typedef struct PendingCommand |
| 81 | { |
| 82 | int serial; /* Serial number expected in result. */ |
| 83 | int code; /* Result Code. 0 is OK */ |
| 84 | char_u *result; /* String result for command (malloc'ed). |
| 85 | * NULL means command still pending. */ |
| 86 | struct PendingCommand *nextPtr; |
| 87 | /* Next in list of all outstanding commands. |
| 88 | * NULL means end of list. */ |
| 89 | } PendingCommand; |
| 90 | |
| 91 | static PendingCommand *pendingCommands = NULL; |
| 92 | /* List of all commands currently |
| 93 | * being waited for. */ |
| 94 | |
| 95 | /* |
| 96 | * The information below is used for communication between processes |
| 97 | * during "send" commands. Each process keeps a private window, never |
| 98 | * even mapped, with one property, "Comm". When a command is sent to |
| 99 | * an interpreter, the command is appended to the comm property of the |
| 100 | * communication window associated with the interp's process. Similarly, |
| 101 | * when a result is returned from a sent command, it is also appended |
| 102 | * to the comm property. |
| 103 | * |
| 104 | * Each command and each result takes the form of ASCII text. For a |
| 105 | * command, the text consists of a nul character followed by several |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 106 | * nul-terminated ASCII strings. The first string consists of a |
| 107 | * single letter: |
| 108 | * "c" for an expression |
| 109 | * "k" for keystrokes |
| 110 | * "r" for reply |
| 111 | * "n" for notification. |
| 112 | * Subsequent strings have the form "option value" where the following options |
| 113 | * are supported: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | * |
| 115 | * -r commWindow serial |
| 116 | * |
| 117 | * This option means that a response should be sent to the window |
| 118 | * whose X identifier is "commWindow" (in hex), and the response should |
| 119 | * be identified with the serial number given by "serial" (in decimal). |
| 120 | * If this option isn't specified then the send is asynchronous and |
| 121 | * no response is sent. |
| 122 | * |
| 123 | * -n name |
| 124 | * "Name" gives the name of the application for which the command is |
| 125 | * intended. This option must be present. |
| 126 | * |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 127 | * -E encoding |
| 128 | * Encoding name used for the text. This is the 'encoding' of the |
| 129 | * sender. The receiver may want to do conversion to his 'encoding'. |
| 130 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | * -s script |
| 132 | * "Script" is the script to be executed. This option must be |
| 133 | * present. Taken as a series of keystrokes in a "k" command where |
| 134 | * <Key>'s are expanded |
| 135 | * |
| 136 | * The options may appear in any order. The -n and -s options must be |
| 137 | * present, but -r may be omitted for asynchronous RPCs. For compatibility |
| 138 | * with future releases that may add new features, there may be additional |
| 139 | * options present; as long as they start with a "-" character, they will |
| 140 | * be ignored. |
| 141 | * |
| 142 | * A result also consists of a zero character followed by several null- |
| 143 | * terminated ASCII strings. The first string consists of the single |
| 144 | * letter "r". Subsequent strings have the form "option value" where |
| 145 | * the following options are supported: |
| 146 | * |
| 147 | * -s serial |
| 148 | * Identifies the command for which this is the result. It is the |
| 149 | * same as the "serial" field from the -s option in the command. This |
| 150 | * option must be present. |
| 151 | * |
| 152 | * -r result |
| 153 | * "Result" is the result string for the script, which may be either |
| 154 | * a result or an error message. If this field is omitted then it |
| 155 | * defaults to an empty string. |
| 156 | * |
| 157 | * -c code |
| 158 | * 0: for OK. This is the default. |
| 159 | * 1: for error: Result is the last error |
| 160 | * |
| 161 | * -i errorInfo |
| 162 | * -e errorCode |
| 163 | * Not applicable for Vim |
| 164 | * |
| 165 | * Options may appear in any order, and only the -s option must be |
| 166 | * present. As with commands, there may be additional options besides |
| 167 | * these; unknown options are ignored. |
| 168 | */ |
| 169 | |
| 170 | /* |
| 171 | * Maximum size property that can be read at one time by |
| 172 | * this module: |
| 173 | */ |
| 174 | |
| 175 | #define MAX_PROP_WORDS 100000 |
| 176 | |
| 177 | struct ServerReply |
| 178 | { |
| 179 | Window id; |
| 180 | garray_T strings; |
| 181 | }; |
| 182 | static garray_T serverReply = { 0, 0, 0, 0, 0 }; |
| 183 | enum ServerReplyOp { SROP_Find, SROP_Add, SROP_Delete }; |
| 184 | |
| 185 | typedef int (*EndCond) __ARGS((void *)); |
| 186 | |
| 187 | /* |
| 188 | * Forward declarations for procedures defined later in this file: |
| 189 | */ |
| 190 | |
| 191 | static Window LookupName __ARGS((Display *dpy, char_u *name, int delete, char_u **loose)); |
| 192 | static int SendInit __ARGS((Display *dpy)); |
| 193 | static int DoRegisterName __ARGS((Display *dpy, char_u *name)); |
| 194 | static void DeleteAnyLingerer __ARGS((Display *dpy, Window w)); |
| 195 | static int GetRegProp __ARGS((Display *dpy, char_u **regPropp, long_u *numItemsp, int domsg)); |
| 196 | static int WaitForPend __ARGS((void *p)); |
| 197 | static int WaitForReply __ARGS((void *p)); |
| 198 | static int WindowValid __ARGS((Display *dpy, Window w)); |
| 199 | static void ServerWait __ARGS((Display *dpy, Window w, EndCond endCond, void *endData, int localLoop, int seconds)); |
| 200 | static struct ServerReply *ServerReplyFind __ARGS((Window w, enum ServerReplyOp op)); |
| 201 | static int AppendPropCarefully __ARGS((Display *display, Window window, Atom property, char_u *value, int length)); |
| 202 | static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event)); |
| 203 | static int IsSerialName __ARGS((char_u *name)); |
| 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 |
| 217 | serverRegisterName(dpy, name) |
| 218 | Display *dpy; /* display to register with */ |
| 219 | char_u *name; /* the name that will be used as a base */ |
| 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"), |
| 234 | hl_attr(HLF_W)); |
| 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 |
| 255 | DoRegisterName(dpy, name) |
| 256 | Display *dpy; |
| 257 | char_u *name; |
| 258 | { |
| 259 | Window w; |
| 260 | XErrorHandler old_handler; |
| 261 | #define MAX_NAME_LENGTH 100 |
| 262 | char_u propInfo[MAX_NAME_LENGTH + 20]; |
| 263 | |
| 264 | if (commProperty == None) |
| 265 | { |
| 266 | if (SendInit(dpy) < 0) |
| 267 | return -2; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Make sure the name is unique, and append info about it to |
| 272 | * the registry property. It's important to lock the server |
| 273 | * here to prevent conflicting changes to the registry property. |
| 274 | * WARNING: Do not step through this while debugging, it will hangup the X |
| 275 | * server! |
| 276 | */ |
| 277 | XGrabServer(dpy); |
| 278 | w = LookupName(dpy, name, FALSE, NULL); |
| 279 | if (w != (Window)0) |
| 280 | { |
| 281 | Status status; |
| 282 | int dummyInt; |
| 283 | unsigned int dummyUns; |
| 284 | Window dummyWin; |
| 285 | |
| 286 | /* |
| 287 | * The name is currently registered. See if the commWindow |
| 288 | * associated with the name exists. If not, or if the commWindow |
| 289 | * is *our* commWindow, then just unregister the old name (this |
| 290 | * could happen if an application dies without cleaning up the |
| 291 | * registry). |
| 292 | */ |
| 293 | old_handler = XSetErrorHandler(x_error_check); |
| 294 | status = XGetGeometry(dpy, w, &dummyWin, &dummyInt, &dummyInt, |
| 295 | &dummyUns, &dummyUns, &dummyUns, &dummyUns); |
| 296 | (void)XSetErrorHandler(old_handler); |
| 297 | if (status != Success && w != commWindow) |
| 298 | { |
| 299 | XUngrabServer(dpy); |
| 300 | XFlush(dpy); |
| 301 | return -1; |
| 302 | } |
| 303 | (void)LookupName(dpy, name, /*delete=*/TRUE, NULL); |
| 304 | } |
| 305 | sprintf((char *)propInfo, "%x %.*s", (int_u)commWindow, |
| 306 | MAX_NAME_LENGTH, name); |
| 307 | old_handler = XSetErrorHandler(x_error_check); |
| 308 | got_x_error = FALSE; |
| 309 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8, |
| 310 | PropModeAppend, propInfo, STRLEN(propInfo) + 1); |
| 311 | XUngrabServer(dpy); |
| 312 | XSync(dpy, False); |
| 313 | (void)XSetErrorHandler(old_handler); |
| 314 | |
| 315 | if (!got_x_error) |
| 316 | { |
| 317 | #ifdef FEAT_EVAL |
| 318 | set_vim_var_string(VV_SEND_SERVER, name, -1); |
| 319 | #endif |
| 320 | serverName = vim_strsave(name); |
| 321 | #ifdef FEAT_TITLE |
| 322 | need_maketitle = TRUE; |
| 323 | #endif |
| 324 | return 0; |
| 325 | } |
| 326 | return -2; |
| 327 | } |
| 328 | |
| 329 | #if defined(FEAT_GUI) || defined(PROTO) |
| 330 | /* |
| 331 | * Clean out new ID from registry and set it as comm win. |
| 332 | * Change any registered window ID. |
| 333 | */ |
| 334 | void |
| 335 | serverChangeRegisteredWindow(dpy, newwin) |
| 336 | Display *dpy; /* Display to register with */ |
| 337 | Window newwin; /* Re-register to this ID */ |
| 338 | { |
| 339 | char_u propInfo[MAX_NAME_LENGTH + 20]; |
| 340 | |
| 341 | commWindow = newwin; |
| 342 | |
| 343 | /* Always call SendInit() here, to make sure commWindow is marked as a Vim |
| 344 | * window. */ |
| 345 | if (SendInit(dpy) < 0) |
| 346 | return; |
| 347 | |
| 348 | /* WARNING: Do not step through this while debugging, it will hangup the X |
| 349 | * server! */ |
| 350 | XGrabServer(dpy); |
| 351 | DeleteAnyLingerer(dpy, newwin); |
| 352 | if (serverName != NULL) |
| 353 | { |
| 354 | /* Reinsert name if we was already registered */ |
| 355 | (void)LookupName(dpy, serverName, /*delete=*/TRUE, NULL); |
| 356 | sprintf((char *)propInfo, "%x %.*s", |
| 357 | (int_u)newwin, MAX_NAME_LENGTH, serverName); |
| 358 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8, |
| 359 | PropModeAppend, (char_u *)propInfo, |
| 360 | STRLEN(propInfo) + 1); |
| 361 | } |
| 362 | XUngrabServer(dpy); |
| 363 | } |
| 364 | #endif |
| 365 | |
| 366 | /* |
| 367 | * Send to an instance of Vim via the X display. |
| 368 | * Returns 0 for OK, negative for an error. |
| 369 | */ |
| 370 | int |
| 371 | serverSendToVim(dpy, name, cmd, result, server, asExpr, localLoop, silent) |
| 372 | Display *dpy; /* Where to send. */ |
| 373 | char_u *name; /* Where to send. */ |
| 374 | char_u *cmd; /* What to send. */ |
| 375 | char_u **result; /* Result of eval'ed expression */ |
| 376 | Window *server; /* Actual ID of receiving app */ |
| 377 | Bool asExpr; /* Interpret as keystrokes or expr ? */ |
| 378 | Bool localLoop; /* Throw away everything but result */ |
| 379 | int silent; /* don't complain about no server */ |
| 380 | { |
| 381 | Window w; |
| 382 | char_u *property; |
| 383 | int length; |
| 384 | int res; |
| 385 | static int serial = 0; /* Running count of sent commands. |
| 386 | * Used to give each command a |
| 387 | * different serial number. */ |
| 388 | PendingCommand pending; |
| 389 | char_u *loosename = NULL; |
| 390 | |
| 391 | if (result != NULL) |
| 392 | *result = NULL; |
| 393 | if (name == NULL || *name == NUL) |
| 394 | name = (char_u *)"GVIM"; /* use a default name */ |
| 395 | |
| 396 | if (commProperty == None && dpy != NULL) |
| 397 | { |
| 398 | if (SendInit(dpy) < 0) |
| 399 | return -1; |
| 400 | } |
| 401 | |
| 402 | /* Execute locally if no display or target is ourselves */ |
| 403 | if (dpy == NULL || (serverName != NULL && STRICMP(name, serverName) == 0)) |
| 404 | { |
| 405 | if (asExpr) |
| 406 | { |
| 407 | char_u *ret; |
| 408 | |
| 409 | ret = eval_client_expr_to_string(cmd); |
| 410 | if (result != NULL) |
| 411 | { |
| 412 | if (ret == NULL) |
| 413 | *result = vim_strsave((char_u *)_(e_invexprmsg)); |
| 414 | else |
| 415 | *result = ret; |
| 416 | } |
| 417 | else |
| 418 | vim_free(ret); |
| 419 | return ret == NULL ? -1 : 0; |
| 420 | } |
| 421 | else |
| 422 | server_to_input_buf(cmd); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Bind the server name to a communication window. |
| 428 | * |
| 429 | * Find any survivor with a serialno attached to the name if the |
| 430 | * original registrant of the wanted name is no longer present. |
| 431 | * |
| 432 | * Delete any lingering names from dead editors. |
| 433 | */ |
| 434 | while (TRUE) |
| 435 | { |
| 436 | w = LookupName(dpy, name, FALSE, &loosename); |
| 437 | /* Check that the window is hot */ |
| 438 | if (w != None) |
| 439 | { |
| 440 | if (!WindowValid(dpy, w)) |
| 441 | { |
| 442 | LookupName(dpy, loosename ? loosename : name, |
| 443 | /*DELETE=*/TRUE, NULL); |
| 444 | continue; |
| 445 | } |
| 446 | } |
| 447 | break; |
| 448 | } |
| 449 | if (w == None) |
| 450 | { |
| 451 | if (!silent) |
| 452 | EMSG2(_(e_noserver), name); |
| 453 | return -1; |
| 454 | } |
| 455 | else if (loosename != NULL) |
| 456 | name = loosename; |
| 457 | if (server != NULL) |
| 458 | *server = w; |
| 459 | |
| 460 | /* |
| 461 | * Send the command to target interpreter by appending it to the |
| 462 | * comm window in the communication window. |
| 463 | */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 464 | length = STRLEN(name) + STRLEN(cmd) + 14; |
| 465 | #ifdef FEAT_MBYTE |
| 466 | length += STRLEN(p_enc); |
| 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); |
| 483 | length += STRLEN(property + length + 1) + 1; |
| 484 | |
| 485 | res = AppendPropCarefully(dpy, w, commProperty, property, length + 1); |
| 486 | vim_free(property); |
| 487 | if (res < 0) |
| 488 | { |
| 489 | EMSG(_("E248: Failed to send command to the destination program")); |
| 490 | return -1; |
| 491 | } |
| 492 | |
| 493 | if (!asExpr) /* There is no answer for this - Keys are sent async */ |
| 494 | return 0; |
| 495 | |
| 496 | /* |
| 497 | * Register the fact that we're waiting for a command to |
| 498 | * complete (this is needed by SendEventProc and by |
| 499 | * AppendErrorProc to pass back the command's results). |
| 500 | */ |
| 501 | pending.serial = serial; |
| 502 | pending.code = 0; |
| 503 | pending.result = NULL; |
| 504 | pending.nextPtr = pendingCommands; |
| 505 | pendingCommands = &pending; |
| 506 | |
| 507 | ServerWait(dpy, w, WaitForPend, &pending, localLoop, 600); |
| 508 | |
| 509 | /* |
| 510 | * Unregister the information about the pending command |
| 511 | * and return the result. |
| 512 | */ |
| 513 | if (pendingCommands == &pending) |
| 514 | pendingCommands = pending.nextPtr; |
| 515 | else |
| 516 | { |
| 517 | PendingCommand *pcPtr; |
| 518 | |
| 519 | for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr) |
| 520 | if (pcPtr->nextPtr == &pending) |
| 521 | { |
| 522 | pcPtr->nextPtr = pending.nextPtr; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | if (result != NULL) |
| 527 | *result = pending.result; |
| 528 | else |
| 529 | vim_free(pending.result); |
| 530 | |
| 531 | return pending.code == 0 ? 0 : -1; |
| 532 | } |
| 533 | |
| 534 | static int |
| 535 | WaitForPend(p) |
| 536 | void *p; |
| 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 |
| 546 | WindowValid(dpy, w) |
| 547 | Display *dpy; |
| 548 | Window w; |
| 549 | { |
| 550 | XErrorHandler old_handler; |
| 551 | Atom *plist; |
| 552 | int numProp; |
| 553 | int i; |
| 554 | |
| 555 | old_handler = XSetErrorHandler(x_error_check); |
| 556 | got_x_error = 0; |
| 557 | plist = XListProperties(dpy, w, &numProp); |
| 558 | XSync(dpy, False); |
| 559 | XSetErrorHandler(old_handler); |
| 560 | if (plist == NULL || got_x_error) |
| 561 | return FALSE; |
| 562 | |
| 563 | for (i = 0; i < numProp; i++) |
| 564 | if (plist[i] == vimProperty) |
| 565 | { |
| 566 | XFree(plist); |
| 567 | return TRUE; |
| 568 | } |
| 569 | XFree(plist); |
| 570 | return FALSE; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Enter a loop processing X events & polling chars until we see a result |
| 575 | */ |
| 576 | static void |
| 577 | ServerWait(dpy, w, endCond, endData, localLoop, seconds) |
| 578 | Display *dpy; |
| 579 | Window w; |
| 580 | EndCond endCond; |
| 581 | void *endData; |
| 582 | int localLoop; |
| 583 | int seconds; |
| 584 | { |
| 585 | time_t start; |
| 586 | time_t now; |
| 587 | time_t lastChk = 0; |
| 588 | XEvent event; |
| 589 | XPropertyEvent *e = (XPropertyEvent *)&event; |
| 590 | # define SEND_MSEC_POLL 50 |
| 591 | |
| 592 | time(&start); |
| 593 | while (endCond(endData) == 0) |
| 594 | { |
| 595 | time(&now); |
| 596 | if (seconds >= 0 && (now - start) >= seconds) |
| 597 | break; |
| 598 | if (now != lastChk) |
| 599 | { |
| 600 | lastChk = now; |
| 601 | if (!WindowValid(dpy, w)) |
| 602 | break; |
| 603 | /* |
| 604 | * Sometimes the PropertyChange event doesn't come. |
| 605 | * This can be seen in eg: vim -c 'echo remote_expr("gvim", "3+2")' |
| 606 | */ |
| 607 | serverEventProc(dpy, NULL); |
| 608 | } |
| 609 | if (localLoop) |
| 610 | { |
| 611 | /* Just look out for the answer without calling back into Vim */ |
| 612 | #ifndef HAVE_SELECT |
| 613 | struct pollfd fds; |
| 614 | |
| 615 | fds.fd = ConnectionNumber(dpy); |
| 616 | fds.events = POLLIN; |
| 617 | if (poll(&fds, 1, SEND_MSEC_POLL) < 0) |
| 618 | break; |
| 619 | #else |
| 620 | fd_set fds; |
| 621 | struct timeval tv; |
| 622 | |
| 623 | tv.tv_sec = 0; |
| 624 | tv.tv_usec = SEND_MSEC_POLL * 1000; |
| 625 | FD_ZERO(&fds); |
| 626 | FD_SET(ConnectionNumber(dpy), &fds); |
| 627 | if (select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &tv) < 0) |
| 628 | break; |
| 629 | #endif |
| 630 | while (XEventsQueued(dpy, QueuedAfterReading) > 0) |
| 631 | { |
| 632 | XNextEvent(dpy, &event); |
| 633 | if (event.type == PropertyNotify && e->window == commWindow) |
| 634 | serverEventProc(dpy, &event); |
| 635 | } |
| 636 | } |
| 637 | else |
| 638 | { |
| 639 | if (got_int) |
| 640 | break; |
| 641 | ui_delay((long)SEND_MSEC_POLL, TRUE); |
| 642 | ui_breakcheck(); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | |
| 648 | /* |
| 649 | * Fetch a list of all the Vim instance names currently registered for the |
| 650 | * display. |
| 651 | * |
| 652 | * Returns a newline separated list in allocated memory or NULL. |
| 653 | */ |
| 654 | char_u * |
| 655 | serverGetVimNames(dpy) |
| 656 | Display *dpy; |
| 657 | { |
| 658 | char_u *regProp; |
| 659 | char_u *entry; |
| 660 | char_u *p; |
| 661 | long_u numItems; |
| 662 | int_u w; |
| 663 | garray_T ga; |
| 664 | |
| 665 | if (registryProperty == None) |
| 666 | { |
| 667 | if (SendInit(dpy) < 0) |
| 668 | return NULL; |
| 669 | } |
| 670 | ga_init2(&ga, 1, 100); |
| 671 | |
| 672 | /* |
| 673 | * Read the registry property. |
| 674 | */ |
| 675 | if (GetRegProp(dpy, ®Prop, &numItems, TRUE) == FAIL) |
| 676 | return NULL; |
| 677 | |
| 678 | /* |
| 679 | * Scan all of the names out of the property. |
| 680 | */ |
| 681 | ga_init2(&ga, 1, 100); |
| 682 | for (p = regProp; (p - regProp) < numItems; p++) |
| 683 | { |
| 684 | entry = p; |
| 685 | while (*p != 0 && !isspace(*p)) |
| 686 | p++; |
| 687 | if (*p != 0) |
| 688 | { |
| 689 | w = None; |
| 690 | sscanf((char *)entry, "%x", &w); |
| 691 | if (WindowValid(dpy, (Window)w)) |
| 692 | { |
| 693 | ga_concat(&ga, p + 1); |
| 694 | ga_concat(&ga, (char_u *)"\n"); |
| 695 | } |
| 696 | while (*p != 0) |
| 697 | p++; |
| 698 | } |
| 699 | } |
| 700 | if (regProp != empty_prop) |
| 701 | XFree(regProp); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 702 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 703 | return ga.ga_data; |
| 704 | } |
| 705 | |
| 706 | /* ---------------------------------------------------------- |
| 707 | * Reply stuff |
| 708 | */ |
| 709 | |
| 710 | static struct ServerReply * |
| 711 | ServerReplyFind(w, op) |
| 712 | Window w; |
| 713 | enum ServerReplyOp op; |
| 714 | { |
| 715 | struct ServerReply *p; |
| 716 | struct ServerReply e; |
| 717 | int i; |
| 718 | |
| 719 | p = (struct ServerReply *) serverReply.ga_data; |
| 720 | for (i = 0; i < serverReply.ga_len; i++, p++) |
| 721 | if (p->id == w) |
| 722 | break; |
| 723 | if (i >= serverReply.ga_len) |
| 724 | p = NULL; |
| 725 | |
| 726 | if (p == NULL && op == SROP_Add) |
| 727 | { |
| 728 | if (serverReply.ga_growsize == 0) |
| 729 | ga_init2(&serverReply, sizeof(struct ServerReply), 1); |
| 730 | if (ga_grow(&serverReply, 1) == OK) |
| 731 | { |
| 732 | p = ((struct ServerReply *) serverReply.ga_data) |
| 733 | + serverReply.ga_len; |
| 734 | e.id = w; |
| 735 | ga_init2(&e.strings, 1, 100); |
| 736 | memcpy(p, &e, sizeof(e)); |
| 737 | serverReply.ga_len++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | else if (p != NULL && op == SROP_Delete) |
| 741 | { |
| 742 | ga_clear(&p->strings); |
| 743 | mch_memmove(p, p + 1, (serverReply.ga_len - i - 1) * sizeof(*p)); |
| 744 | serverReply.ga_len--; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return p; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Convert string to windowid. |
| 752 | * Issue an error if the id is invalid. |
| 753 | */ |
| 754 | Window |
| 755 | serverStrToWin(str) |
| 756 | char_u *str; |
| 757 | { |
| 758 | unsigned id = None; |
| 759 | |
| 760 | sscanf((char *)str, "0x%x", &id); |
| 761 | if (id == None) |
| 762 | EMSG2(_("E573: Invalid server id used: %s"), str); |
| 763 | |
| 764 | return (Window)id; |
| 765 | } |
| 766 | |
| 767 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 768 | * Send a reply string (notification) to client with id "name". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 769 | * Return -1 if the window is invalid. |
| 770 | */ |
| 771 | int |
| 772 | serverSendReply(name, str) |
| 773 | char_u *name; |
| 774 | char_u *str; |
| 775 | { |
| 776 | char_u *property; |
| 777 | int length; |
| 778 | int res; |
| 779 | Display *dpy = X_DISPLAY; |
| 780 | Window win = serverStrToWin(name); |
| 781 | |
| 782 | if (commProperty == None) |
| 783 | { |
| 784 | if (SendInit(dpy) < 0) |
| 785 | return -2; |
| 786 | } |
| 787 | if (!WindowValid(dpy, win)) |
| 788 | return -1; |
| 789 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 790 | length = STRLEN(str) + 11; |
| 791 | #ifdef FEAT_MBYTE |
| 792 | length += STRLEN(p_enc); |
| 793 | #endif |
| 794 | if ((property = (char_u *)alloc((unsigned)length + 30)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 795 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 796 | #ifdef FEAT_MBYTE |
| 797 | sprintf((char *)property, "%cn%c-E %s%c-n %s%c-w %x", |
| 798 | 0, 0, p_enc, 0, str, 0, (unsigned int)commWindow); |
| 799 | #else |
| 800 | sprintf((char *)property, "%cn%c-n %s%c-w %x", |
| 801 | 0, 0, str, 0, (unsigned int)commWindow); |
| 802 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | length += STRLEN(property + length); |
| 804 | res = AppendPropCarefully(dpy, win, commProperty, property, length + 1); |
| 805 | vim_free(property); |
| 806 | return res; |
| 807 | } |
| 808 | return -1; |
| 809 | } |
| 810 | |
| 811 | static int |
| 812 | WaitForReply(p) |
| 813 | void *p; |
| 814 | { |
| 815 | Window *w = (Window *) p; |
| 816 | return ServerReplyFind(*w, SROP_Find) != NULL; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Wait for replies from id (win) |
| 821 | * Return 0 and the malloc'ed string when a reply is available. |
| 822 | * Return -1 if the window becomes invalid while waiting. |
| 823 | */ |
| 824 | int |
| 825 | serverReadReply(dpy, win, str, localLoop) |
| 826 | Display *dpy; |
| 827 | Window win; |
| 828 | char_u **str; |
| 829 | int localLoop; |
| 830 | { |
| 831 | int len; |
| 832 | char_u *s; |
| 833 | struct ServerReply *p; |
| 834 | |
| 835 | ServerWait(dpy, win, WaitForReply, &win, localLoop, -1); |
| 836 | |
| 837 | if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0) |
| 838 | { |
| 839 | *str = vim_strsave(p->strings.ga_data); |
| 840 | len = STRLEN(*str) + 1; |
| 841 | if (len < p->strings.ga_len) |
| 842 | { |
| 843 | s = (char_u *) p->strings.ga_data; |
| 844 | mch_memmove(s, s + len, p->strings.ga_len - len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 845 | p->strings.ga_len -= len; |
| 846 | } |
| 847 | else |
| 848 | { |
| 849 | /* Last string read. Remove from list */ |
| 850 | ga_clear(&p->strings); |
| 851 | ServerReplyFind(win, SROP_Delete); |
| 852 | } |
| 853 | return 0; |
| 854 | } |
| 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Check for replies from id (win). |
| 860 | * Return TRUE and a non-malloc'ed string if there is. Else return FALSE. |
| 861 | */ |
| 862 | int |
| 863 | serverPeekReply(dpy, win, str) |
| 864 | Display *dpy; |
| 865 | Window win; |
| 866 | char_u **str; |
| 867 | { |
| 868 | struct ServerReply *p; |
| 869 | |
| 870 | if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0) |
| 871 | { |
| 872 | if (str != NULL) |
| 873 | *str = p->strings.ga_data; |
| 874 | return 1; |
| 875 | } |
| 876 | if (!WindowValid(dpy, win)) |
| 877 | return -1; |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | |
| 882 | /* |
| 883 | * Initialize the communication channels for sending commands and receiving |
| 884 | * results. |
| 885 | */ |
| 886 | static int |
| 887 | SendInit(dpy) |
| 888 | Display *dpy; |
| 889 | { |
| 890 | XErrorHandler old_handler; |
| 891 | |
| 892 | /* |
| 893 | * Create the window used for communication, and set up an |
| 894 | * event handler for it. |
| 895 | */ |
| 896 | old_handler = XSetErrorHandler(x_error_check); |
| 897 | got_x_error = FALSE; |
| 898 | |
| 899 | if (commProperty == None) |
| 900 | commProperty = XInternAtom(dpy, "Comm", False); |
| 901 | if (vimProperty == None) |
| 902 | vimProperty = XInternAtom(dpy, "Vim", False); |
| 903 | if (registryProperty == None) |
| 904 | registryProperty = XInternAtom(dpy, "VimRegistry", False); |
| 905 | |
| 906 | if (commWindow == None) |
| 907 | { |
| 908 | commWindow = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), |
| 909 | getpid(), 0, 10, 10, 0, |
| 910 | WhitePixel(dpy, DefaultScreen(dpy)), |
| 911 | WhitePixel(dpy, DefaultScreen(dpy))); |
| 912 | XSelectInput(dpy, commWindow, PropertyChangeMask); |
| 913 | /* WARNING: Do not step through this while debugging, it will hangup |
| 914 | * the X server! */ |
| 915 | XGrabServer(dpy); |
| 916 | DeleteAnyLingerer(dpy, commWindow); |
| 917 | XUngrabServer(dpy); |
| 918 | } |
| 919 | |
| 920 | /* Make window recognizable as a vim window */ |
| 921 | XChangeProperty(dpy, commWindow, vimProperty, XA_STRING, |
| 922 | 8, PropModeReplace, (char_u *)VIM_VERSION_SHORT, |
| 923 | (int)STRLEN(VIM_VERSION_SHORT) + 1); |
| 924 | |
| 925 | XSync(dpy, False); |
| 926 | (void)XSetErrorHandler(old_handler); |
| 927 | |
| 928 | return got_x_error ? -1 : 0; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Given a server name, see if the name exists in the registry for a |
| 933 | * particular display. |
| 934 | * |
| 935 | * If the given name is registered, return the ID of the window associated |
| 936 | * with the name. If the name isn't registered, then return 0. |
| 937 | * |
| 938 | * Side effects: |
| 939 | * If the registry property is improperly formed, then it is deleted. |
| 940 | * If "delete" is non-zero, then if the named server is found it is |
| 941 | * removed from the registry property. |
| 942 | */ |
| 943 | static Window |
| 944 | LookupName(dpy, name, delete, loose) |
| 945 | Display *dpy; /* Display whose registry to check. */ |
| 946 | char_u *name; /* Name of a server. */ |
| 947 | int delete; /* If non-zero, delete info about name. */ |
| 948 | char_u **loose; /* Do another search matching -999 if not found |
| 949 | Return result here if a match is found */ |
| 950 | { |
| 951 | char_u *regProp, *entry; |
| 952 | char_u *p; |
| 953 | long_u numItems; |
| 954 | int_u returnValue; |
| 955 | |
| 956 | /* |
| 957 | * Read the registry property. |
| 958 | */ |
| 959 | if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL) |
| 960 | return 0; |
| 961 | |
| 962 | /* |
| 963 | * Scan the property for the desired name. |
| 964 | */ |
| 965 | returnValue = (int_u)None; |
| 966 | entry = NULL; /* Not needed, but eliminates compiler warning. */ |
| 967 | for (p = regProp; (p - regProp) < numItems; ) |
| 968 | { |
| 969 | entry = p; |
| 970 | while (*p != 0 && !isspace(*p)) |
| 971 | p++; |
| 972 | if (*p != 0 && STRICMP(name, p + 1) == 0) |
| 973 | { |
| 974 | sscanf((char *)entry, "%x", &returnValue); |
| 975 | break; |
| 976 | } |
| 977 | while (*p != 0) |
| 978 | p++; |
| 979 | p++; |
| 980 | } |
| 981 | |
| 982 | if (loose != NULL && returnValue == (int_u)None && !IsSerialName(name)) |
| 983 | { |
| 984 | for (p = regProp; (p - regProp) < numItems; ) |
| 985 | { |
| 986 | entry = p; |
| 987 | while (*p != 0 && !isspace(*p)) |
| 988 | p++; |
| 989 | if (*p != 0 && IsSerialName(p + 1) |
| 990 | && STRNICMP(name, p + 1, STRLEN(name)) == 0) |
| 991 | { |
| 992 | sscanf((char *)entry, "%x", &returnValue); |
| 993 | *loose = vim_strsave(p + 1); |
| 994 | break; |
| 995 | } |
| 996 | while (*p != 0) |
| 997 | p++; |
| 998 | p++; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * Delete the property, if that is desired (copy down the |
| 1004 | * remainder of the registry property to overlay the deleted |
| 1005 | * info, then rewrite the property). |
| 1006 | */ |
| 1007 | if (delete && returnValue != (int_u)None) |
| 1008 | { |
| 1009 | int count; |
| 1010 | |
| 1011 | while (*p != 0) |
| 1012 | p++; |
| 1013 | p++; |
| 1014 | count = numItems - (p - regProp); |
| 1015 | if (count > 0) |
| 1016 | memcpy(entry, p, count); |
| 1017 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, |
| 1018 | 8, PropModeReplace, regProp, |
| 1019 | (int)(numItems - (p - entry))); |
| 1020 | XSync(dpy, False); |
| 1021 | } |
| 1022 | |
| 1023 | if (regProp != empty_prop) |
| 1024 | XFree(regProp); |
| 1025 | return (Window)returnValue; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Delete any lingering occurences of window id. We promise that any |
| 1030 | * occurences is not ours since it is not yet put into the registry (by us) |
| 1031 | * |
| 1032 | * This is necessary in the following scenario: |
| 1033 | * 1. There is an old windowid for an exit'ed vim in the registry |
| 1034 | * 2. We get that id for our commWindow but only want to send, not register. |
| 1035 | * 3. The window will mistakenly be regarded valid because of own commWindow |
| 1036 | */ |
| 1037 | static void |
| 1038 | DeleteAnyLingerer(dpy, win) |
| 1039 | Display *dpy; /* Display whose registry to check. */ |
| 1040 | Window win; /* Window to remove */ |
| 1041 | { |
| 1042 | char_u *regProp, *entry = NULL; |
| 1043 | char_u *p; |
| 1044 | long_u numItems; |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 1045 | int_u wwin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* |
| 1048 | * Read the registry property. |
| 1049 | */ |
| 1050 | if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL) |
| 1051 | return; |
| 1052 | |
| 1053 | /* Scan the property for the window id. */ |
| 1054 | for (p = regProp; (p - regProp) < numItems; ) |
| 1055 | { |
| 1056 | if (*p != 0) |
| 1057 | { |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 1058 | sscanf((char *)p, "%x", &wwin); |
| 1059 | if ((Window)wwin == win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1060 | { |
| 1061 | int lastHalf; |
| 1062 | |
| 1063 | /* Copy down the remainder to delete entry */ |
| 1064 | entry = p; |
| 1065 | while (*p != 0) |
| 1066 | p++; |
| 1067 | p++; |
| 1068 | lastHalf = numItems - (p - regProp); |
| 1069 | if (lastHalf > 0) |
| 1070 | memcpy(entry, p, lastHalf); |
| 1071 | numItems = (entry - regProp) + lastHalf; |
| 1072 | p = entry; |
| 1073 | continue; |
| 1074 | } |
| 1075 | } |
| 1076 | while (*p != 0) |
| 1077 | p++; |
| 1078 | p++; |
| 1079 | } |
| 1080 | |
| 1081 | if (entry != NULL) |
| 1082 | { |
| 1083 | XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, |
| 1084 | XA_STRING, 8, PropModeReplace, regProp, |
| 1085 | (int)(p - regProp)); |
| 1086 | XSync(dpy, False); |
| 1087 | } |
| 1088 | |
| 1089 | if (regProp != empty_prop) |
| 1090 | XFree(regProp); |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Read the registry property. Delete it when it's formatted wrong. |
| 1095 | * Return the property in "regPropp". "empty_prop" is used when it doesn't |
| 1096 | * exist yet. |
| 1097 | * Return OK when successful. |
| 1098 | */ |
| 1099 | static int |
| 1100 | GetRegProp(dpy, regPropp, numItemsp, domsg) |
| 1101 | Display *dpy; |
| 1102 | char_u **regPropp; |
| 1103 | long_u *numItemsp; |
| 1104 | int domsg; /* When TRUE give error message. */ |
| 1105 | { |
| 1106 | int result, actualFormat; |
| 1107 | long_u bytesAfter; |
| 1108 | Atom actualType; |
| 1109 | |
| 1110 | *regPropp = NULL; |
| 1111 | result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0L, |
| 1112 | (long)MAX_PROP_WORDS, False, |
| 1113 | XA_STRING, &actualType, |
| 1114 | &actualFormat, numItemsp, &bytesAfter, |
| 1115 | regPropp); |
| 1116 | |
| 1117 | if (actualType == None) |
| 1118 | { |
| 1119 | /* No prop yet. Logically equal to the empty list */ |
| 1120 | *numItemsp = 0; |
| 1121 | *regPropp = empty_prop; |
| 1122 | return OK; |
| 1123 | } |
| 1124 | |
| 1125 | /* If the property is improperly formed, then delete it. */ |
| 1126 | if (result != Success || actualFormat != 8 || actualType != XA_STRING) |
| 1127 | { |
| 1128 | if (*regPropp != NULL) |
| 1129 | XFree(*regPropp); |
| 1130 | XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty); |
| 1131 | if (domsg) |
| 1132 | EMSG(_("E251: VIM instance registry property is badly formed. Deleted!")); |
| 1133 | return FAIL; |
| 1134 | } |
| 1135 | return OK; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * This procedure is invoked by the varous X event loops throughout Vims when |
| 1140 | * a property changes on the communication window. This procedure reads the |
| 1141 | * property and handles command requests and responses. |
| 1142 | */ |
| 1143 | void |
| 1144 | serverEventProc(dpy, eventPtr) |
| 1145 | Display *dpy; |
| 1146 | XEvent *eventPtr; /* Information about event. */ |
| 1147 | { |
| 1148 | char_u *propInfo; |
| 1149 | char_u *p; |
| 1150 | int result, actualFormat, code; |
| 1151 | long_u numItems, bytesAfter; |
| 1152 | Atom actualType; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1153 | char_u *tofree; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1154 | |
| 1155 | if (eventPtr != NULL) |
| 1156 | { |
| 1157 | if (eventPtr->xproperty.atom != commProperty |
| 1158 | || eventPtr->xproperty.state != PropertyNewValue) |
| 1159 | return; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Read the comm property and delete it. |
| 1164 | */ |
| 1165 | propInfo = NULL; |
| 1166 | result = XGetWindowProperty(dpy, commWindow, commProperty, 0L, |
| 1167 | (long)MAX_PROP_WORDS, True, |
| 1168 | XA_STRING, &actualType, |
| 1169 | &actualFormat, &numItems, &bytesAfter, |
| 1170 | &propInfo); |
| 1171 | |
| 1172 | /* If the property doesn't exist or is improperly formed then ignore it. */ |
| 1173 | if (result != Success || actualType != XA_STRING || actualFormat != 8) |
| 1174 | { |
| 1175 | if (propInfo != NULL) |
| 1176 | XFree(propInfo); |
| 1177 | return; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Several commands and results could arrive in the property at |
| 1182 | * one time; each iteration through the outer loop handles a |
| 1183 | * single command or result. |
| 1184 | */ |
| 1185 | for (p = propInfo; (p - propInfo) < numItems; ) |
| 1186 | { |
| 1187 | /* |
| 1188 | * Ignore leading NULs; each command or result starts with a |
| 1189 | * NUL so that no matter how badly formed a preceding command |
| 1190 | * is, we'll be able to tell that a new command/result is |
| 1191 | * starting. |
| 1192 | */ |
| 1193 | if (*p == 0) |
| 1194 | { |
| 1195 | p++; |
| 1196 | continue; |
| 1197 | } |
| 1198 | |
| 1199 | if ((*p == 'c' || *p == 'k') && (p[1] == 0)) |
| 1200 | { |
| 1201 | Window resWindow; |
| 1202 | char_u *name, *script, *serial, *end, *res; |
| 1203 | Bool asKeys = *p == 'k'; |
| 1204 | garray_T reply; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1205 | char_u *enc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | |
| 1207 | /* |
| 1208 | * This is an incoming command from some other application. |
| 1209 | * Iterate over all of its options. Stop when we reach |
| 1210 | * the end of the property or something that doesn't look |
| 1211 | * like an option. |
| 1212 | */ |
| 1213 | p += 2; |
| 1214 | name = NULL; |
| 1215 | resWindow = None; |
| 1216 | serial = (char_u *)""; |
| 1217 | script = NULL; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1218 | enc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | while (p - propInfo < numItems && *p == '-') |
| 1220 | { |
| 1221 | switch (p[1]) |
| 1222 | { |
| 1223 | case 'r': |
| 1224 | end = skipwhite(p + 2); |
| 1225 | resWindow = 0; |
| 1226 | while (vim_isxdigit(*end)) |
| 1227 | { |
| 1228 | resWindow = 16 * resWindow + (long_u)hex2nr(*end); |
| 1229 | ++end; |
| 1230 | } |
| 1231 | if (end == p + 2 || *end != ' ') |
| 1232 | resWindow = None; |
| 1233 | else |
| 1234 | { |
| 1235 | p = serial = end + 1; |
| 1236 | clientWindow = resWindow; /* Remember in global */ |
| 1237 | } |
| 1238 | break; |
| 1239 | case 'n': |
| 1240 | if (p[2] == ' ') |
| 1241 | name = p + 3; |
| 1242 | break; |
| 1243 | case 's': |
| 1244 | if (p[2] == ' ') |
| 1245 | script = p + 3; |
| 1246 | break; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1247 | case 'E': |
| 1248 | if (p[2] == ' ') |
| 1249 | enc = p + 3; |
| 1250 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1251 | } |
| 1252 | while (*p != 0) |
| 1253 | p++; |
| 1254 | p++; |
| 1255 | } |
| 1256 | |
| 1257 | if (script == NULL || name == NULL) |
| 1258 | continue; |
| 1259 | |
| 1260 | /* |
| 1261 | * Initialize the result property, so that we're ready at any |
| 1262 | * time if we need to return an error. |
| 1263 | */ |
| 1264 | if (resWindow != None) |
| 1265 | { |
| 1266 | ga_init2(&reply, 1, 100); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1267 | #ifdef FEAT_MBYTE |
| 1268 | ga_grow(&reply, 50 + STRLEN(p_enc)); |
| 1269 | sprintf(reply.ga_data, "%cr%c-E %s%c-s %s%c-r ", |
| 1270 | 0, 0, p_enc, 0, serial, 0); |
| 1271 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1272 | ga_grow(&reply, 50); |
| 1273 | sprintf(reply.ga_data, "%cr%c-s %s%c-r ", 0, 0, serial, 0); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1274 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | reply.ga_len = 10 + STRLEN(serial); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1276 | } |
| 1277 | res = NULL; |
| 1278 | if (serverName != NULL && STRICMP(name, serverName) == 0) |
| 1279 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1280 | script = serverConvert(enc, script, &tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | if (asKeys) |
| 1282 | server_to_input_buf(script); |
| 1283 | else |
| 1284 | res = eval_client_expr_to_string(script); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1285 | vim_free(tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1286 | } |
| 1287 | if (resWindow != None) |
| 1288 | { |
| 1289 | if (res != NULL) |
| 1290 | ga_concat(&reply, res); |
| 1291 | else if (asKeys == 0) |
| 1292 | { |
| 1293 | ga_concat(&reply, (char_u *)_(e_invexprmsg)); |
| 1294 | ga_append(&reply, 0); |
| 1295 | ga_concat(&reply, (char_u *)"-c 1"); |
| 1296 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 1297 | ga_append(&reply, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1298 | (void)AppendPropCarefully(dpy, resWindow, commProperty, |
| 1299 | reply.ga_data, reply.ga_len); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1300 | ga_clear(&reply); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | } |
| 1302 | vim_free(res); |
| 1303 | } |
| 1304 | else if (*p == 'r' && p[1] == 0) |
| 1305 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1306 | int serial, gotSerial; |
| 1307 | char_u *res; |
| 1308 | PendingCommand *pcPtr; |
| 1309 | char_u *enc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* |
| 1312 | * This is a reply to some command that we sent out. Iterate |
| 1313 | * over all of its options. Stop when we reach the end of the |
| 1314 | * property or something that doesn't look like an option. |
| 1315 | */ |
| 1316 | p += 2; |
| 1317 | gotSerial = 0; |
| 1318 | res = (char_u *)""; |
| 1319 | code = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1320 | enc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | while ((p-propInfo) < numItems && *p == '-') |
| 1322 | { |
| 1323 | switch (p[1]) |
| 1324 | { |
| 1325 | case 'r': |
| 1326 | if (p[2] == ' ') |
| 1327 | res = p + 3; |
| 1328 | break; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1329 | case 'E': |
| 1330 | if (p[2] == ' ') |
| 1331 | enc = p + 3; |
| 1332 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1333 | case 's': |
| 1334 | if (sscanf((char *)p + 2, " %d", &serial) == 1) |
| 1335 | gotSerial = 1; |
| 1336 | break; |
| 1337 | case 'c': |
| 1338 | if (sscanf((char *)p + 2, " %d", &code) != 1) |
| 1339 | code = 0; |
| 1340 | break; |
| 1341 | } |
| 1342 | while (*p != 0) |
| 1343 | p++; |
| 1344 | p++; |
| 1345 | } |
| 1346 | |
| 1347 | if (!gotSerial) |
| 1348 | continue; |
| 1349 | |
| 1350 | /* |
| 1351 | * Give the result information to anyone who's |
| 1352 | * waiting for it. |
| 1353 | */ |
| 1354 | for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr) |
| 1355 | { |
| 1356 | if (serial != pcPtr->serial || pcPtr->result != NULL) |
| 1357 | continue; |
| 1358 | |
| 1359 | pcPtr->code = code; |
| 1360 | if (res != NULL) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1361 | { |
| 1362 | res = serverConvert(enc, res, &tofree); |
| 1363 | if (tofree == NULL) |
| 1364 | res = vim_strsave(res); |
| 1365 | pcPtr->result = res; |
| 1366 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | else |
| 1368 | pcPtr->result = vim_strsave((char_u *)""); |
| 1369 | break; |
| 1370 | } |
| 1371 | } |
| 1372 | else if (*p == 'n' && p[1] == 0) |
| 1373 | { |
| 1374 | Window win = 0; |
| 1375 | unsigned int u; |
| 1376 | int gotWindow; |
| 1377 | char_u *str; |
| 1378 | char_u winstr[30]; |
| 1379 | struct ServerReply *r; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1380 | char_u *enc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1381 | |
| 1382 | /* |
| 1383 | * This is a (n)otification. Sent with serverreply_send in VimL. |
| 1384 | * Execute any autocommand and save it for later retrieval |
| 1385 | */ |
| 1386 | p += 2; |
| 1387 | gotWindow = 0; |
| 1388 | str = (char_u *)""; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1389 | enc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | while ((p-propInfo) < numItems && *p == '-') |
| 1391 | { |
| 1392 | switch (p[1]) |
| 1393 | { |
| 1394 | case 'n': |
| 1395 | if (p[2] == ' ') |
| 1396 | str = p + 3; |
| 1397 | break; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1398 | case 'E': |
| 1399 | if (p[2] == ' ') |
| 1400 | enc = p + 3; |
| 1401 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1402 | case 'w': |
| 1403 | if (sscanf((char *)p + 2, " %x", &u) == 1) |
| 1404 | { |
| 1405 | win = u; |
| 1406 | gotWindow = 1; |
| 1407 | } |
| 1408 | break; |
| 1409 | } |
| 1410 | while (*p != 0) |
| 1411 | p++; |
| 1412 | p++; |
| 1413 | } |
| 1414 | |
| 1415 | if (!gotWindow) |
| 1416 | continue; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1417 | str = serverConvert(enc, str, &tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | if ((r = ServerReplyFind(win, SROP_Add)) != NULL) |
| 1419 | { |
| 1420 | ga_concat(&(r->strings), str); |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 1421 | ga_append(&(r->strings), NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1422 | } |
| 1423 | #ifdef FEAT_AUTOCMD |
| 1424 | sprintf((char *)winstr, "0x%x", (unsigned int)win); |
| 1425 | apply_autocmds(EVENT_REMOTEREPLY, winstr, str, TRUE, curbuf); |
| 1426 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1427 | vim_free(tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1428 | } |
| 1429 | else |
| 1430 | { |
| 1431 | /* |
| 1432 | * Didn't recognize this thing. Just skip through the next |
| 1433 | * null character and try again. |
| 1434 | * Even if we get an 'r'(eply) we will throw it away as we |
| 1435 | * never specify (and thus expect) one |
| 1436 | */ |
| 1437 | while (*p != 0) |
| 1438 | p++; |
| 1439 | p++; |
| 1440 | } |
| 1441 | } |
| 1442 | XFree(propInfo); |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * Append a given property to a given window, but set up an X error handler so |
| 1447 | * that if the append fails this procedure can return an error code rather |
| 1448 | * than having Xlib panic. |
| 1449 | * Return: 0 for OK, -1 for error |
| 1450 | */ |
| 1451 | static int |
| 1452 | AppendPropCarefully(dpy, window, property, value, length) |
| 1453 | Display *dpy; /* Display on which to operate. */ |
| 1454 | Window window; /* Window whose property is to be modified. */ |
| 1455 | Atom property; /* Name of property. */ |
| 1456 | char_u *value; /* Characters to append to property. */ |
| 1457 | int length; /* How much to append */ |
| 1458 | { |
| 1459 | XErrorHandler old_handler; |
| 1460 | |
| 1461 | old_handler = XSetErrorHandler(x_error_check); |
| 1462 | got_x_error = FALSE; |
| 1463 | XChangeProperty(dpy, window, property, XA_STRING, 8, |
| 1464 | PropModeAppend, value, length); |
| 1465 | XSync(dpy, False); |
| 1466 | (void) XSetErrorHandler(old_handler); |
| 1467 | return got_x_error ? -1 : 0; |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | /* |
| 1472 | * Another X Error handler, just used to check for errors. |
| 1473 | */ |
| 1474 | /* ARGSUSED */ |
| 1475 | static int |
| 1476 | x_error_check(dpy, error_event) |
| 1477 | Display *dpy; |
| 1478 | XErrorEvent *error_event; |
| 1479 | { |
| 1480 | got_x_error = TRUE; |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * Check if "str" looks like it had a serial number appended. |
| 1486 | * Actually just checks if the name ends in a digit. |
| 1487 | */ |
| 1488 | static int |
| 1489 | IsSerialName(str) |
| 1490 | char_u *str; |
| 1491 | { |
| 1492 | int len = STRLEN(str); |
| 1493 | |
| 1494 | return (len > 1 && vim_isdigit(str[len - 1])); |
| 1495 | } |
| 1496 | #endif /* FEAT_CLIENTSERVER */ |