7
|
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 /*
|
1121
|
25 * This file provides procedures that implement the command server
|
|
26 * functionality of Vim when in contact with an X11 server.
|
7
|
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
|
39
|
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:
|
7
|
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 *
|
39
|
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 *
|
7
|
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.
|
500
|
448 * Length must be computed exactly!
|
7
|
449 */
|
39
|
450 #ifdef FEAT_MBYTE
|
500
|
451 length = STRLEN(name) + STRLEN(p_enc) + STRLEN(cmd) + 14;
|
|
452 #else
|
|
453 length = STRLEN(name) + STRLEN(cmd) + 10;
|
39
|
454 #endif
|
|
455 property = (char_u *)alloc((unsigned)length + 30);
|
7
|
456
|
39
|
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
|
7
|
461 sprintf((char *)property, "%c%c%c-n %s%c-s %s",
|
|
462 0, asExpr ? 'c' : 'k', 0, name, 0, cmd);
|
39
|
463 #endif
|
7
|
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);
|
500
|
470 /* Add length of what "-r %x %d" resulted in, skipping the NUL. */
|
7
|
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 XEvent event;
|
3670
|
576
|
|
577 #define UI_MSEC_DELAY 50
|
|
578 #define SEND_MSEC_POLL 500
|
|
579 #ifndef HAVE_SELECT
|
|
580 struct pollfd fds;
|
|
581
|
|
582 fds.fd = ConnectionNumber(dpy);
|
|
583 fds.events = POLLIN;
|
|
584 #else
|
|
585 fd_set fds;
|
|
586 struct timeval tv;
|
|
587
|
|
588 tv.tv_sec = 0;
|
|
589 tv.tv_usec = SEND_MSEC_POLL * 1000;
|
|
590 FD_ZERO(&fds);
|
|
591 FD_SET(ConnectionNumber(dpy), &fds);
|
|
592 #endif
|
7
|
593
|
|
594 time(&start);
|
3670
|
595 while (TRUE)
|
7
|
596 {
|
3670
|
597 while (XCheckWindowEvent(dpy, commWindow, PropertyChangeMask, &event))
|
|
598 serverEventProc(dpy, &event);
|
|
599
|
|
600 if (endCond(endData) != 0)
|
|
601 break;
|
|
602 if (!WindowValid(dpy, w))
|
|
603 break;
|
7
|
604 time(&now);
|
|
605 if (seconds >= 0 && (now - start) >= seconds)
|
|
606 break;
|
3670
|
607
|
|
608 /* Just look out for the answer without calling back into Vim */
|
7
|
609 if (localLoop)
|
|
610 {
|
|
611 #ifndef HAVE_SELECT
|
|
612 if (poll(&fds, 1, SEND_MSEC_POLL) < 0)
|
|
613 break;
|
|
614 #else
|
3670
|
615 if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0)
|
7
|
616 break;
|
|
617 #endif
|
|
618 }
|
|
619 else
|
|
620 {
|
|
621 if (got_int)
|
|
622 break;
|
3670
|
623 ui_delay((long)UI_MSEC_DELAY, TRUE);
|
7
|
624 ui_breakcheck();
|
|
625 }
|
|
626 }
|
|
627 }
|
|
628
|
|
629
|
|
630 /*
|
|
631 * Fetch a list of all the Vim instance names currently registered for the
|
|
632 * display.
|
|
633 *
|
|
634 * Returns a newline separated list in allocated memory or NULL.
|
|
635 */
|
|
636 char_u *
|
|
637 serverGetVimNames(dpy)
|
|
638 Display *dpy;
|
|
639 {
|
|
640 char_u *regProp;
|
|
641 char_u *entry;
|
|
642 char_u *p;
|
|
643 long_u numItems;
|
|
644 int_u w;
|
|
645 garray_T ga;
|
|
646
|
|
647 if (registryProperty == None)
|
|
648 {
|
|
649 if (SendInit(dpy) < 0)
|
|
650 return NULL;
|
|
651 }
|
|
652
|
|
653 /*
|
|
654 * Read the registry property.
|
|
655 */
|
|
656 if (GetRegProp(dpy, ®Prop, &numItems, TRUE) == FAIL)
|
|
657 return NULL;
|
|
658
|
|
659 /*
|
|
660 * Scan all of the names out of the property.
|
|
661 */
|
|
662 ga_init2(&ga, 1, 100);
|
1880
|
663 for (p = regProp; (long_u)(p - regProp) < numItems; p++)
|
7
|
664 {
|
|
665 entry = p;
|
|
666 while (*p != 0 && !isspace(*p))
|
|
667 p++;
|
|
668 if (*p != 0)
|
|
669 {
|
|
670 w = None;
|
|
671 sscanf((char *)entry, "%x", &w);
|
|
672 if (WindowValid(dpy, (Window)w))
|
|
673 {
|
|
674 ga_concat(&ga, p + 1);
|
|
675 ga_concat(&ga, (char_u *)"\n");
|
|
676 }
|
|
677 while (*p != 0)
|
|
678 p++;
|
|
679 }
|
|
680 }
|
|
681 if (regProp != empty_prop)
|
|
682 XFree(regProp);
|
21
|
683 ga_append(&ga, NUL);
|
7
|
684 return ga.ga_data;
|
|
685 }
|
|
686
|
|
687 /* ----------------------------------------------------------
|
|
688 * Reply stuff
|
|
689 */
|
|
690
|
|
691 static struct ServerReply *
|
|
692 ServerReplyFind(w, op)
|
|
693 Window w;
|
|
694 enum ServerReplyOp op;
|
|
695 {
|
|
696 struct ServerReply *p;
|
|
697 struct ServerReply e;
|
|
698 int i;
|
|
699
|
|
700 p = (struct ServerReply *) serverReply.ga_data;
|
|
701 for (i = 0; i < serverReply.ga_len; i++, p++)
|
|
702 if (p->id == w)
|
|
703 break;
|
|
704 if (i >= serverReply.ga_len)
|
|
705 p = NULL;
|
|
706
|
|
707 if (p == NULL && op == SROP_Add)
|
|
708 {
|
|
709 if (serverReply.ga_growsize == 0)
|
|
710 ga_init2(&serverReply, sizeof(struct ServerReply), 1);
|
|
711 if (ga_grow(&serverReply, 1) == OK)
|
|
712 {
|
|
713 p = ((struct ServerReply *) serverReply.ga_data)
|
|
714 + serverReply.ga_len;
|
|
715 e.id = w;
|
|
716 ga_init2(&e.strings, 1, 100);
|
1740
|
717 mch_memmove(p, &e, sizeof(e));
|
7
|
718 serverReply.ga_len++;
|
|
719 }
|
|
720 }
|
|
721 else if (p != NULL && op == SROP_Delete)
|
|
722 {
|
|
723 ga_clear(&p->strings);
|
|
724 mch_memmove(p, p + 1, (serverReply.ga_len - i - 1) * sizeof(*p));
|
|
725 serverReply.ga_len--;
|
|
726 }
|
|
727
|
|
728 return p;
|
|
729 }
|
|
730
|
|
731 /*
|
|
732 * Convert string to windowid.
|
|
733 * Issue an error if the id is invalid.
|
|
734 */
|
|
735 Window
|
|
736 serverStrToWin(str)
|
|
737 char_u *str;
|
|
738 {
|
|
739 unsigned id = None;
|
|
740
|
|
741 sscanf((char *)str, "0x%x", &id);
|
|
742 if (id == None)
|
|
743 EMSG2(_("E573: Invalid server id used: %s"), str);
|
|
744
|
|
745 return (Window)id;
|
|
746 }
|
|
747
|
|
748 /*
|
39
|
749 * Send a reply string (notification) to client with id "name".
|
7
|
750 * Return -1 if the window is invalid.
|
|
751 */
|
|
752 int
|
|
753 serverSendReply(name, str)
|
|
754 char_u *name;
|
|
755 char_u *str;
|
|
756 {
|
|
757 char_u *property;
|
|
758 int length;
|
|
759 int res;
|
|
760 Display *dpy = X_DISPLAY;
|
|
761 Window win = serverStrToWin(name);
|
|
762
|
|
763 if (commProperty == None)
|
|
764 {
|
|
765 if (SendInit(dpy) < 0)
|
|
766 return -2;
|
|
767 }
|
|
768 if (!WindowValid(dpy, win))
|
|
769 return -1;
|
|
770
|
39
|
771 #ifdef FEAT_MBYTE
|
500
|
772 length = STRLEN(p_enc) + STRLEN(str) + 14;
|
|
773 #else
|
|
774 length = STRLEN(str) + 10;
|
39
|
775 #endif
|
|
776 if ((property = (char_u *)alloc((unsigned)length + 30)) != NULL)
|
7
|
777 {
|
39
|
778 #ifdef FEAT_MBYTE
|
|
779 sprintf((char *)property, "%cn%c-E %s%c-n %s%c-w %x",
|
|
780 0, 0, p_enc, 0, str, 0, (unsigned int)commWindow);
|
|
781 #else
|
|
782 sprintf((char *)property, "%cn%c-n %s%c-w %x",
|
|
783 0, 0, str, 0, (unsigned int)commWindow);
|
|
784 #endif
|
500
|
785 /* Add length of what "%x" resulted in. */
|
7
|
786 length += STRLEN(property + length);
|
|
787 res = AppendPropCarefully(dpy, win, commProperty, property, length + 1);
|
|
788 vim_free(property);
|
|
789 return res;
|
|
790 }
|
|
791 return -1;
|
|
792 }
|
|
793
|
|
794 static int
|
|
795 WaitForReply(p)
|
|
796 void *p;
|
|
797 {
|
|
798 Window *w = (Window *) p;
|
|
799 return ServerReplyFind(*w, SROP_Find) != NULL;
|
|
800 }
|
|
801
|
|
802 /*
|
|
803 * Wait for replies from id (win)
|
|
804 * Return 0 and the malloc'ed string when a reply is available.
|
|
805 * Return -1 if the window becomes invalid while waiting.
|
|
806 */
|
|
807 int
|
|
808 serverReadReply(dpy, win, str, localLoop)
|
|
809 Display *dpy;
|
|
810 Window win;
|
|
811 char_u **str;
|
|
812 int localLoop;
|
|
813 {
|
|
814 int len;
|
|
815 char_u *s;
|
|
816 struct ServerReply *p;
|
|
817
|
|
818 ServerWait(dpy, win, WaitForReply, &win, localLoop, -1);
|
|
819
|
|
820 if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0)
|
|
821 {
|
|
822 *str = vim_strsave(p->strings.ga_data);
|
|
823 len = STRLEN(*str) + 1;
|
|
824 if (len < p->strings.ga_len)
|
|
825 {
|
|
826 s = (char_u *) p->strings.ga_data;
|
|
827 mch_memmove(s, s + len, p->strings.ga_len - len);
|
|
828 p->strings.ga_len -= len;
|
|
829 }
|
|
830 else
|
|
831 {
|
|
832 /* Last string read. Remove from list */
|
|
833 ga_clear(&p->strings);
|
|
834 ServerReplyFind(win, SROP_Delete);
|
|
835 }
|
|
836 return 0;
|
|
837 }
|
|
838 return -1;
|
|
839 }
|
|
840
|
|
841 /*
|
|
842 * Check for replies from id (win).
|
|
843 * Return TRUE and a non-malloc'ed string if there is. Else return FALSE.
|
|
844 */
|
|
845 int
|
|
846 serverPeekReply(dpy, win, str)
|
|
847 Display *dpy;
|
|
848 Window win;
|
|
849 char_u **str;
|
|
850 {
|
|
851 struct ServerReply *p;
|
|
852
|
|
853 if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0)
|
|
854 {
|
|
855 if (str != NULL)
|
|
856 *str = p->strings.ga_data;
|
|
857 return 1;
|
|
858 }
|
|
859 if (!WindowValid(dpy, win))
|
|
860 return -1;
|
|
861 return 0;
|
|
862 }
|
|
863
|
|
864
|
|
865 /*
|
|
866 * Initialize the communication channels for sending commands and receiving
|
|
867 * results.
|
|
868 */
|
|
869 static int
|
|
870 SendInit(dpy)
|
|
871 Display *dpy;
|
|
872 {
|
|
873 XErrorHandler old_handler;
|
|
874
|
|
875 /*
|
|
876 * Create the window used for communication, and set up an
|
|
877 * event handler for it.
|
|
878 */
|
|
879 old_handler = XSetErrorHandler(x_error_check);
|
|
880 got_x_error = FALSE;
|
|
881
|
|
882 if (commProperty == None)
|
|
883 commProperty = XInternAtom(dpy, "Comm", False);
|
|
884 if (vimProperty == None)
|
|
885 vimProperty = XInternAtom(dpy, "Vim", False);
|
|
886 if (registryProperty == None)
|
|
887 registryProperty = XInternAtom(dpy, "VimRegistry", False);
|
|
888
|
|
889 if (commWindow == None)
|
|
890 {
|
|
891 commWindow = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy),
|
|
892 getpid(), 0, 10, 10, 0,
|
|
893 WhitePixel(dpy, DefaultScreen(dpy)),
|
|
894 WhitePixel(dpy, DefaultScreen(dpy)));
|
|
895 XSelectInput(dpy, commWindow, PropertyChangeMask);
|
|
896 /* WARNING: Do not step through this while debugging, it will hangup
|
|
897 * the X server! */
|
|
898 XGrabServer(dpy);
|
|
899 DeleteAnyLingerer(dpy, commWindow);
|
|
900 XUngrabServer(dpy);
|
|
901 }
|
|
902
|
|
903 /* Make window recognizable as a vim window */
|
|
904 XChangeProperty(dpy, commWindow, vimProperty, XA_STRING,
|
|
905 8, PropModeReplace, (char_u *)VIM_VERSION_SHORT,
|
|
906 (int)STRLEN(VIM_VERSION_SHORT) + 1);
|
|
907
|
|
908 XSync(dpy, False);
|
|
909 (void)XSetErrorHandler(old_handler);
|
|
910
|
|
911 return got_x_error ? -1 : 0;
|
|
912 }
|
|
913
|
|
914 /*
|
|
915 * Given a server name, see if the name exists in the registry for a
|
|
916 * particular display.
|
|
917 *
|
|
918 * If the given name is registered, return the ID of the window associated
|
|
919 * with the name. If the name isn't registered, then return 0.
|
|
920 *
|
|
921 * Side effects:
|
|
922 * If the registry property is improperly formed, then it is deleted.
|
|
923 * If "delete" is non-zero, then if the named server is found it is
|
|
924 * removed from the registry property.
|
|
925 */
|
|
926 static Window
|
|
927 LookupName(dpy, name, delete, loose)
|
|
928 Display *dpy; /* Display whose registry to check. */
|
|
929 char_u *name; /* Name of a server. */
|
|
930 int delete; /* If non-zero, delete info about name. */
|
|
931 char_u **loose; /* Do another search matching -999 if not found
|
|
932 Return result here if a match is found */
|
|
933 {
|
|
934 char_u *regProp, *entry;
|
|
935 char_u *p;
|
|
936 long_u numItems;
|
|
937 int_u returnValue;
|
|
938
|
|
939 /*
|
|
940 * Read the registry property.
|
|
941 */
|
|
942 if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL)
|
|
943 return 0;
|
|
944
|
|
945 /*
|
|
946 * Scan the property for the desired name.
|
|
947 */
|
|
948 returnValue = (int_u)None;
|
|
949 entry = NULL; /* Not needed, but eliminates compiler warning. */
|
1880
|
950 for (p = regProp; (long_u)(p - regProp) < numItems; )
|
7
|
951 {
|
|
952 entry = p;
|
|
953 while (*p != 0 && !isspace(*p))
|
|
954 p++;
|
|
955 if (*p != 0 && STRICMP(name, p + 1) == 0)
|
|
956 {
|
|
957 sscanf((char *)entry, "%x", &returnValue);
|
|
958 break;
|
|
959 }
|
|
960 while (*p != 0)
|
|
961 p++;
|
|
962 p++;
|
|
963 }
|
|
964
|
|
965 if (loose != NULL && returnValue == (int_u)None && !IsSerialName(name))
|
|
966 {
|
1880
|
967 for (p = regProp; (long_u)(p - regProp) < numItems; )
|
7
|
968 {
|
|
969 entry = p;
|
|
970 while (*p != 0 && !isspace(*p))
|
|
971 p++;
|
|
972 if (*p != 0 && IsSerialName(p + 1)
|
|
973 && STRNICMP(name, p + 1, STRLEN(name)) == 0)
|
|
974 {
|
|
975 sscanf((char *)entry, "%x", &returnValue);
|
|
976 *loose = vim_strsave(p + 1);
|
|
977 break;
|
|
978 }
|
|
979 while (*p != 0)
|
|
980 p++;
|
|
981 p++;
|
|
982 }
|
|
983 }
|
|
984
|
|
985 /*
|
|
986 * Delete the property, if that is desired (copy down the
|
|
987 * remainder of the registry property to overlay the deleted
|
|
988 * info, then rewrite the property).
|
|
989 */
|
|
990 if (delete && returnValue != (int_u)None)
|
|
991 {
|
|
992 int count;
|
|
993
|
|
994 while (*p != 0)
|
|
995 p++;
|
|
996 p++;
|
|
997 count = numItems - (p - regProp);
|
|
998 if (count > 0)
|
1740
|
999 mch_memmove(entry, p, count);
|
7
|
1000 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING,
|
|
1001 8, PropModeReplace, regProp,
|
|
1002 (int)(numItems - (p - entry)));
|
|
1003 XSync(dpy, False);
|
|
1004 }
|
|
1005
|
|
1006 if (regProp != empty_prop)
|
|
1007 XFree(regProp);
|
|
1008 return (Window)returnValue;
|
|
1009 }
|
|
1010
|
|
1011 /*
|
1197
|
1012 * Delete any lingering occurrence of window id. We promise that any
|
|
1013 * occurrence is not ours since it is not yet put into the registry (by us)
|
7
|
1014 *
|
|
1015 * This is necessary in the following scenario:
|
|
1016 * 1. There is an old windowid for an exit'ed vim in the registry
|
|
1017 * 2. We get that id for our commWindow but only want to send, not register.
|
|
1018 * 3. The window will mistakenly be regarded valid because of own commWindow
|
|
1019 */
|
|
1020 static void
|
|
1021 DeleteAnyLingerer(dpy, win)
|
|
1022 Display *dpy; /* Display whose registry to check. */
|
|
1023 Window win; /* Window to remove */
|
|
1024 {
|
|
1025 char_u *regProp, *entry = NULL;
|
|
1026 char_u *p;
|
|
1027 long_u numItems;
|
28
|
1028 int_u wwin;
|
7
|
1029
|
|
1030 /*
|
|
1031 * Read the registry property.
|
|
1032 */
|
|
1033 if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL)
|
|
1034 return;
|
|
1035
|
|
1036 /* Scan the property for the window id. */
|
1880
|
1037 for (p = regProp; (long_u)(p - regProp) < numItems; )
|
7
|
1038 {
|
|
1039 if (*p != 0)
|
|
1040 {
|
28
|
1041 sscanf((char *)p, "%x", &wwin);
|
|
1042 if ((Window)wwin == win)
|
7
|
1043 {
|
|
1044 int lastHalf;
|
|
1045
|
|
1046 /* Copy down the remainder to delete entry */
|
|
1047 entry = p;
|
|
1048 while (*p != 0)
|
|
1049 p++;
|
|
1050 p++;
|
|
1051 lastHalf = numItems - (p - regProp);
|
|
1052 if (lastHalf > 0)
|
1740
|
1053 mch_memmove(entry, p, lastHalf);
|
7
|
1054 numItems = (entry - regProp) + lastHalf;
|
|
1055 p = entry;
|
|
1056 continue;
|
|
1057 }
|
|
1058 }
|
|
1059 while (*p != 0)
|
|
1060 p++;
|
|
1061 p++;
|
|
1062 }
|
|
1063
|
|
1064 if (entry != NULL)
|
|
1065 {
|
|
1066 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty,
|
|
1067 XA_STRING, 8, PropModeReplace, regProp,
|
|
1068 (int)(p - regProp));
|
|
1069 XSync(dpy, False);
|
|
1070 }
|
|
1071
|
|
1072 if (regProp != empty_prop)
|
|
1073 XFree(regProp);
|
|
1074 }
|
|
1075
|
|
1076 /*
|
|
1077 * Read the registry property. Delete it when it's formatted wrong.
|
|
1078 * Return the property in "regPropp". "empty_prop" is used when it doesn't
|
|
1079 * exist yet.
|
|
1080 * Return OK when successful.
|
|
1081 */
|
|
1082 static int
|
|
1083 GetRegProp(dpy, regPropp, numItemsp, domsg)
|
|
1084 Display *dpy;
|
|
1085 char_u **regPropp;
|
|
1086 long_u *numItemsp;
|
|
1087 int domsg; /* When TRUE give error message. */
|
|
1088 {
|
|
1089 int result, actualFormat;
|
|
1090 long_u bytesAfter;
|
|
1091 Atom actualType;
|
819
|
1092 XErrorHandler old_handler;
|
7
|
1093
|
|
1094 *regPropp = NULL;
|
819
|
1095 old_handler = XSetErrorHandler(x_error_check);
|
|
1096 got_x_error = FALSE;
|
|
1097
|
7
|
1098 result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0L,
|
|
1099 (long)MAX_PROP_WORDS, False,
|
|
1100 XA_STRING, &actualType,
|
|
1101 &actualFormat, numItemsp, &bytesAfter,
|
|
1102 regPropp);
|
|
1103
|
819
|
1104 XSync(dpy, FALSE);
|
|
1105 (void)XSetErrorHandler(old_handler);
|
|
1106 if (got_x_error)
|
|
1107 return FAIL;
|
|
1108
|
7
|
1109 if (actualType == None)
|
|
1110 {
|
|
1111 /* No prop yet. Logically equal to the empty list */
|
|
1112 *numItemsp = 0;
|
|
1113 *regPropp = empty_prop;
|
|
1114 return OK;
|
|
1115 }
|
|
1116
|
|
1117 /* If the property is improperly formed, then delete it. */
|
|
1118 if (result != Success || actualFormat != 8 || actualType != XA_STRING)
|
|
1119 {
|
|
1120 if (*regPropp != NULL)
|
|
1121 XFree(*regPropp);
|
|
1122 XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty);
|
|
1123 if (domsg)
|
|
1124 EMSG(_("E251: VIM instance registry property is badly formed. Deleted!"));
|
|
1125 return FAIL;
|
|
1126 }
|
|
1127 return OK;
|
|
1128 }
|
|
1129
|
|
1130 /*
|
1197
|
1131 * This procedure is invoked by the various X event loops throughout Vims when
|
7
|
1132 * a property changes on the communication window. This procedure reads the
|
|
1133 * property and handles command requests and responses.
|
|
1134 */
|
|
1135 void
|
|
1136 serverEventProc(dpy, eventPtr)
|
|
1137 Display *dpy;
|
|
1138 XEvent *eventPtr; /* Information about event. */
|
|
1139 {
|
|
1140 char_u *propInfo;
|
|
1141 char_u *p;
|
|
1142 int result, actualFormat, code;
|
|
1143 long_u numItems, bytesAfter;
|
|
1144 Atom actualType;
|
39
|
1145 char_u *tofree;
|
7
|
1146
|
|
1147 if (eventPtr != NULL)
|
|
1148 {
|
|
1149 if (eventPtr->xproperty.atom != commProperty
|
|
1150 || eventPtr->xproperty.state != PropertyNewValue)
|
|
1151 return;
|
|
1152 }
|
|
1153
|
|
1154 /*
|
|
1155 * Read the comm property and delete it.
|
|
1156 */
|
|
1157 propInfo = NULL;
|
|
1158 result = XGetWindowProperty(dpy, commWindow, commProperty, 0L,
|
|
1159 (long)MAX_PROP_WORDS, True,
|
|
1160 XA_STRING, &actualType,
|
|
1161 &actualFormat, &numItems, &bytesAfter,
|
|
1162 &propInfo);
|
|
1163
|
|
1164 /* If the property doesn't exist or is improperly formed then ignore it. */
|
|
1165 if (result != Success || actualType != XA_STRING || actualFormat != 8)
|
|
1166 {
|
|
1167 if (propInfo != NULL)
|
|
1168 XFree(propInfo);
|
|
1169 return;
|
|
1170 }
|
|
1171
|
|
1172 /*
|
|
1173 * Several commands and results could arrive in the property at
|
|
1174 * one time; each iteration through the outer loop handles a
|
|
1175 * single command or result.
|
|
1176 */
|
1880
|
1177 for (p = propInfo; (long_u)(p - propInfo) < numItems; )
|
7
|
1178 {
|
|
1179 /*
|
|
1180 * Ignore leading NULs; each command or result starts with a
|
|
1181 * NUL so that no matter how badly formed a preceding command
|
|
1182 * is, we'll be able to tell that a new command/result is
|
|
1183 * starting.
|
|
1184 */
|
|
1185 if (*p == 0)
|
|
1186 {
|
|
1187 p++;
|
|
1188 continue;
|
|
1189 }
|
|
1190
|
|
1191 if ((*p == 'c' || *p == 'k') && (p[1] == 0))
|
|
1192 {
|
|
1193 Window resWindow;
|
3668
|
1194 char_u *name, *script, *serial, *end;
|
7
|
1195 Bool asKeys = *p == 'k';
|
39
|
1196 char_u *enc;
|
7
|
1197
|
|
1198 /*
|
|
1199 * This is an incoming command from some other application.
|
|
1200 * Iterate over all of its options. Stop when we reach
|
|
1201 * the end of the property or something that doesn't look
|
|
1202 * like an option.
|
|
1203 */
|
|
1204 p += 2;
|
|
1205 name = NULL;
|
|
1206 resWindow = None;
|
|
1207 serial = (char_u *)"";
|
|
1208 script = NULL;
|
39
|
1209 enc = NULL;
|
1880
|
1210 while ((long_u)(p - propInfo) < numItems && *p == '-')
|
7
|
1211 {
|
|
1212 switch (p[1])
|
|
1213 {
|
|
1214 case 'r':
|
|
1215 end = skipwhite(p + 2);
|
|
1216 resWindow = 0;
|
|
1217 while (vim_isxdigit(*end))
|
|
1218 {
|
|
1219 resWindow = 16 * resWindow + (long_u)hex2nr(*end);
|
|
1220 ++end;
|
|
1221 }
|
|
1222 if (end == p + 2 || *end != ' ')
|
|
1223 resWindow = None;
|
|
1224 else
|
|
1225 {
|
|
1226 p = serial = end + 1;
|
|
1227 clientWindow = resWindow; /* Remember in global */
|
|
1228 }
|
|
1229 break;
|
|
1230 case 'n':
|
|
1231 if (p[2] == ' ')
|
|
1232 name = p + 3;
|
|
1233 break;
|
|
1234 case 's':
|
|
1235 if (p[2] == ' ')
|
|
1236 script = p + 3;
|
|
1237 break;
|
39
|
1238 case 'E':
|
|
1239 if (p[2] == ' ')
|
|
1240 enc = p + 3;
|
|
1241 break;
|
7
|
1242 }
|
|
1243 while (*p != 0)
|
|
1244 p++;
|
|
1245 p++;
|
|
1246 }
|
|
1247
|
|
1248 if (script == NULL || name == NULL)
|
|
1249 continue;
|
|
1250
|
3668
|
1251 if (serverName != NULL && STRICMP(name, serverName) == 0)
|
|
1252 {
|
|
1253 script = serverConvert(enc, script, &tofree);
|
|
1254 if (asKeys)
|
|
1255 server_to_input_buf(script);
|
|
1256 else
|
|
1257 {
|
|
1258 char_u *res;
|
|
1259
|
|
1260 res = eval_client_expr_to_string(script);
|
|
1261 if (resWindow != None)
|
|
1262 {
|
|
1263 garray_T reply;
|
|
1264
|
|
1265 /* Initialize the result property. */
|
|
1266 ga_init2(&reply, 1, 100);
|
39
|
1267 #ifdef FEAT_MBYTE
|
3668
|
1268 ga_grow(&reply, 50 + STRLEN(p_enc));
|
|
1269 sprintf(reply.ga_data, "%cr%c-E %s%c-s %s%c-r ",
|
39
|
1270 0, 0, p_enc, 0, serial, 0);
|
3668
|
1271 reply.ga_len = 14 + STRLEN(p_enc) + STRLEN(serial);
|
39
|
1272 #else
|
3668
|
1273 ga_grow(&reply, 50);
|
|
1274 sprintf(reply.ga_data, "%cr%c-s %s%c-r ",
|
|
1275 0, 0, serial, 0);
|
|
1276 reply.ga_len = 10 + STRLEN(serial);
|
39
|
1277 #endif
|
3668
|
1278
|
|
1279 /* Evaluate the expression and return the result. */
|
|
1280 if (res != NULL)
|
|
1281 ga_concat(&reply, res);
|
|
1282 else
|
|
1283 {
|
|
1284 ga_concat(&reply, (char_u *)_(e_invexprmsg));
|
|
1285 ga_append(&reply, 0);
|
|
1286 ga_concat(&reply, (char_u *)"-c 1");
|
|
1287 }
|
|
1288 ga_append(&reply, NUL);
|
|
1289 (void)AppendPropCarefully(dpy, resWindow, commProperty,
|
|
1290 reply.ga_data, reply.ga_len);
|
|
1291 ga_clear(&reply);
|
|
1292 }
|
|
1293 vim_free(res);
|
|
1294 }
|
|
1295 vim_free(tofree);
|
|
1296 }
|
7
|
1297 }
|
|
1298 else if (*p == 'r' && p[1] == 0)
|
|
1299 {
|
39
|
1300 int serial, gotSerial;
|
|
1301 char_u *res;
|
|
1302 PendingCommand *pcPtr;
|
|
1303 char_u *enc;
|
7
|
1304
|
|
1305 /*
|
|
1306 * This is a reply to some command that we sent out. Iterate
|
|
1307 * over all of its options. Stop when we reach the end of the
|
|
1308 * property or something that doesn't look like an option.
|
|
1309 */
|
|
1310 p += 2;
|
|
1311 gotSerial = 0;
|
|
1312 res = (char_u *)"";
|
|
1313 code = 0;
|
39
|
1314 enc = NULL;
|
1880
|
1315 while ((long_u)(p - propInfo) < numItems && *p == '-')
|
7
|
1316 {
|
|
1317 switch (p[1])
|
|
1318 {
|
|
1319 case 'r':
|
|
1320 if (p[2] == ' ')
|
|
1321 res = p + 3;
|
|
1322 break;
|
39
|
1323 case 'E':
|
|
1324 if (p[2] == ' ')
|
|
1325 enc = p + 3;
|
|
1326 break;
|
7
|
1327 case 's':
|
|
1328 if (sscanf((char *)p + 2, " %d", &serial) == 1)
|
|
1329 gotSerial = 1;
|
|
1330 break;
|
|
1331 case 'c':
|
|
1332 if (sscanf((char *)p + 2, " %d", &code) != 1)
|
|
1333 code = 0;
|
|
1334 break;
|
|
1335 }
|
|
1336 while (*p != 0)
|
|
1337 p++;
|
|
1338 p++;
|
|
1339 }
|
|
1340
|
|
1341 if (!gotSerial)
|
|
1342 continue;
|
|
1343
|
|
1344 /*
|
|
1345 * Give the result information to anyone who's
|
|
1346 * waiting for it.
|
|
1347 */
|
|
1348 for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr)
|
|
1349 {
|
|
1350 if (serial != pcPtr->serial || pcPtr->result != NULL)
|
|
1351 continue;
|
|
1352
|
|
1353 pcPtr->code = code;
|
|
1354 if (res != NULL)
|
39
|
1355 {
|
|
1356 res = serverConvert(enc, res, &tofree);
|
|
1357 if (tofree == NULL)
|
|
1358 res = vim_strsave(res);
|
|
1359 pcPtr->result = res;
|
|
1360 }
|
7
|
1361 else
|
|
1362 pcPtr->result = vim_strsave((char_u *)"");
|
|
1363 break;
|
|
1364 }
|
|
1365 }
|
|
1366 else if (*p == 'n' && p[1] == 0)
|
|
1367 {
|
|
1368 Window win = 0;
|
|
1369 unsigned int u;
|
|
1370 int gotWindow;
|
|
1371 char_u *str;
|
|
1372 struct ServerReply *r;
|
39
|
1373 char_u *enc;
|
7
|
1374
|
|
1375 /*
|
|
1376 * This is a (n)otification. Sent with serverreply_send in VimL.
|
|
1377 * Execute any autocommand and save it for later retrieval
|
|
1378 */
|
|
1379 p += 2;
|
|
1380 gotWindow = 0;
|
|
1381 str = (char_u *)"";
|
39
|
1382 enc = NULL;
|
1880
|
1383 while ((long_u)(p - propInfo) < numItems && *p == '-')
|
7
|
1384 {
|
|
1385 switch (p[1])
|
|
1386 {
|
|
1387 case 'n':
|
|
1388 if (p[2] == ' ')
|
|
1389 str = p + 3;
|
|
1390 break;
|
39
|
1391 case 'E':
|
|
1392 if (p[2] == ' ')
|
|
1393 enc = p + 3;
|
|
1394 break;
|
7
|
1395 case 'w':
|
|
1396 if (sscanf((char *)p + 2, " %x", &u) == 1)
|
|
1397 {
|
|
1398 win = u;
|
|
1399 gotWindow = 1;
|
|
1400 }
|
|
1401 break;
|
|
1402 }
|
|
1403 while (*p != 0)
|
|
1404 p++;
|
|
1405 p++;
|
|
1406 }
|
|
1407
|
|
1408 if (!gotWindow)
|
|
1409 continue;
|
39
|
1410 str = serverConvert(enc, str, &tofree);
|
7
|
1411 if ((r = ServerReplyFind(win, SROP_Add)) != NULL)
|
|
1412 {
|
|
1413 ga_concat(&(r->strings), str);
|
21
|
1414 ga_append(&(r->strings), NUL);
|
7
|
1415 }
|
|
1416 #ifdef FEAT_AUTOCMD
|
1698
|
1417 {
|
|
1418 char_u winstr[30];
|
|
1419
|
|
1420 sprintf((char *)winstr, "0x%x", (unsigned int)win);
|
|
1421 apply_autocmds(EVENT_REMOTEREPLY, winstr, str, TRUE, curbuf);
|
|
1422 }
|
7
|
1423 #endif
|
39
|
1424 vim_free(tofree);
|
7
|
1425 }
|
|
1426 else
|
|
1427 {
|
|
1428 /*
|
|
1429 * Didn't recognize this thing. Just skip through the next
|
|
1430 * null character and try again.
|
|
1431 * Even if we get an 'r'(eply) we will throw it away as we
|
|
1432 * never specify (and thus expect) one
|
|
1433 */
|
|
1434 while (*p != 0)
|
|
1435 p++;
|
|
1436 p++;
|
|
1437 }
|
|
1438 }
|
|
1439 XFree(propInfo);
|
|
1440 }
|
|
1441
|
|
1442 /*
|
|
1443 * Append a given property to a given window, but set up an X error handler so
|
|
1444 * that if the append fails this procedure can return an error code rather
|
|
1445 * than having Xlib panic.
|
|
1446 * Return: 0 for OK, -1 for error
|
|
1447 */
|
|
1448 static int
|
|
1449 AppendPropCarefully(dpy, window, property, value, length)
|
|
1450 Display *dpy; /* Display on which to operate. */
|
|
1451 Window window; /* Window whose property is to be modified. */
|
|
1452 Atom property; /* Name of property. */
|
|
1453 char_u *value; /* Characters to append to property. */
|
|
1454 int length; /* How much to append */
|
|
1455 {
|
|
1456 XErrorHandler old_handler;
|
|
1457
|
|
1458 old_handler = XSetErrorHandler(x_error_check);
|
|
1459 got_x_error = FALSE;
|
|
1460 XChangeProperty(dpy, window, property, XA_STRING, 8,
|
|
1461 PropModeAppend, value, length);
|
|
1462 XSync(dpy, False);
|
|
1463 (void) XSetErrorHandler(old_handler);
|
|
1464 return got_x_error ? -1 : 0;
|
|
1465 }
|
|
1466
|
|
1467
|
|
1468 /*
|
|
1469 * Another X Error handler, just used to check for errors.
|
|
1470 */
|
|
1471 static int
|
|
1472 x_error_check(dpy, error_event)
|
1880
|
1473 Display *dpy UNUSED;
|
|
1474 XErrorEvent *error_event UNUSED;
|
7
|
1475 {
|
|
1476 got_x_error = TRUE;
|
|
1477 return 0;
|
|
1478 }
|
|
1479
|
|
1480 /*
|
|
1481 * Check if "str" looks like it had a serial number appended.
|
|
1482 * Actually just checks if the name ends in a digit.
|
|
1483 */
|
|
1484 static int
|
|
1485 IsSerialName(str)
|
|
1486 char_u *str;
|
|
1487 {
|
|
1488 int len = STRLEN(str);
|
|
1489
|
|
1490 return (len > 1 && vim_isdigit(str[len - 1]));
|
|
1491 }
|
|
1492 #endif /* FEAT_CLIENTSERVER */
|