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