Merge branch 'vim'
[MacVim.git] / src / if_xcmdsrv.c
blob6a7ceb6ca8fad60fad078001120be9149c00233f
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * X command server by Flemming Madsen
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.
10 * if_xcmdsrv.c: Functions for passing commands through an X11 display.
14 #include "vim.h"
15 #include "version.h"
17 #if (defined(FEAT_CLIENTSERVER) || defined(PROTO)) \
18 && !defined(MAC_CLIENTSERVER)
20 # ifdef FEAT_X11
21 # include <X11/Intrinsic.h>
22 # include <X11/Xatom.h>
23 # endif
26 * This file provides procedures that implement the command server
27 * functionality of Vim when in contact with an X11 server.
29 * Adapted from TCL/TK's send command in tkSend.c of the tk 3.6 distribution.
30 * Adapted for use in Vim by Flemming Madsen. Protocol changed to that of tk 4
34 * Copyright (c) 1989-1993 The Regents of the University of California.
35 * All rights reserved.
37 * Permission is hereby granted, without written agreement and without
38 * license or royalty fees, to use, copy, modify, and distribute this
39 * software and its documentation for any purpose, provided that the
40 * above copyright notice and the following two paragraphs appear in
41 * all copies of this software.
43 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
44 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
45 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
46 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
49 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
50 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
51 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
52 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
57 * When a result is being awaited from a sent command, one of
58 * the following structures is present on a list of all outstanding
59 * sent commands. The information in the structure is used to
60 * process the result when it arrives. You're probably wondering
61 * how there could ever be multiple outstanding sent commands.
62 * This could happen if Vim instances invoke each other recursively.
63 * It's unlikely, but possible.
66 typedef struct PendingCommand
68 int serial; /* Serial number expected in result. */
69 int code; /* Result Code. 0 is OK */
70 char_u *result; /* String result for command (malloc'ed).
71 * NULL means command still pending. */
72 struct PendingCommand *nextPtr;
73 /* Next in list of all outstanding commands.
74 * NULL means end of list. */
75 } PendingCommand;
77 static PendingCommand *pendingCommands = NULL;
78 /* List of all commands currently
79 * being waited for. */
82 * The information below is used for communication between processes
83 * during "send" commands. Each process keeps a private window, never
84 * even mapped, with one property, "Comm". When a command is sent to
85 * an interpreter, the command is appended to the comm property of the
86 * communication window associated with the interp's process. Similarly,
87 * when a result is returned from a sent command, it is also appended
88 * to the comm property.
90 * Each command and each result takes the form of ASCII text. For a
91 * command, the text consists of a nul character followed by several
92 * nul-terminated ASCII strings. The first string consists of a
93 * single letter:
94 * "c" for an expression
95 * "k" for keystrokes
96 * "r" for reply
97 * "n" for notification.
98 * Subsequent strings have the form "option value" where the following options
99 * are supported:
101 * -r commWindow serial
103 * This option means that a response should be sent to the window
104 * whose X identifier is "commWindow" (in hex), and the response should
105 * be identified with the serial number given by "serial" (in decimal).
106 * If this option isn't specified then the send is asynchronous and
107 * no response is sent.
109 * -n name
110 * "Name" gives the name of the application for which the command is
111 * intended. This option must be present.
113 * -E encoding
114 * Encoding name used for the text. This is the 'encoding' of the
115 * sender. The receiver may want to do conversion to his 'encoding'.
117 * -s script
118 * "Script" is the script to be executed. This option must be
119 * present. Taken as a series of keystrokes in a "k" command where
120 * <Key>'s are expanded
122 * The options may appear in any order. The -n and -s options must be
123 * present, but -r may be omitted for asynchronous RPCs. For compatibility
124 * with future releases that may add new features, there may be additional
125 * options present; as long as they start with a "-" character, they will
126 * be ignored.
128 * A result also consists of a zero character followed by several null-
129 * terminated ASCII strings. The first string consists of the single
130 * letter "r". Subsequent strings have the form "option value" where
131 * the following options are supported:
133 * -s serial
134 * Identifies the command for which this is the result. It is the
135 * same as the "serial" field from the -s option in the command. This
136 * option must be present.
138 * -r result
139 * "Result" is the result string for the script, which may be either
140 * a result or an error message. If this field is omitted then it
141 * defaults to an empty string.
143 * -c code
144 * 0: for OK. This is the default.
145 * 1: for error: Result is the last error
147 * -i errorInfo
148 * -e errorCode
149 * Not applicable for Vim
151 * Options may appear in any order, and only the -s option must be
152 * present. As with commands, there may be additional options besides
153 * these; unknown options are ignored.
157 * Maximum size property that can be read at one time by
158 * this module:
161 #define MAX_PROP_WORDS 100000
163 struct ServerReply
165 Window id;
166 garray_T strings;
168 static garray_T serverReply = { 0, 0, 0, 0, 0 };
169 enum ServerReplyOp { SROP_Find, SROP_Add, SROP_Delete };
171 typedef int (*EndCond) __ARGS((void *));
174 * Forward declarations for procedures defined later in this file:
177 static Window LookupName __ARGS((Display *dpy, char_u *name, int delete, char_u **loose));
178 static int SendInit __ARGS((Display *dpy));
179 static int DoRegisterName __ARGS((Display *dpy, char_u *name));
180 static void DeleteAnyLingerer __ARGS((Display *dpy, Window w));
181 static int GetRegProp __ARGS((Display *dpy, char_u **regPropp, long_u *numItemsp, int domsg));
182 static int WaitForPend __ARGS((void *p));
183 static int WaitForReply __ARGS((void *p));
184 static int WindowValid __ARGS((Display *dpy, Window w));
185 static void ServerWait __ARGS((Display *dpy, Window w, EndCond endCond, void *endData, int localLoop, int seconds));
186 static struct ServerReply *ServerReplyFind __ARGS((Window w, enum ServerReplyOp op));
187 static int AppendPropCarefully __ARGS((Display *display, Window window, Atom property, char_u *value, int length));
188 static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
189 static int IsSerialName __ARGS((char_u *name));
191 /* Private variables for the "server" functionality */
192 static Atom registryProperty = None;
193 static Atom vimProperty = None;
194 static int got_x_error = FALSE;
196 static char_u *empty_prop = (char_u *)""; /* empty GetRegProp() result */
199 * Associate an ASCII name with Vim. Try real hard to get a unique one.
200 * Returns FAIL or OK.
203 serverRegisterName(dpy, name)
204 Display *dpy; /* display to register with */
205 char_u *name; /* the name that will be used as a base */
207 int i;
208 int res;
209 char_u *p = NULL;
211 res = DoRegisterName(dpy, name);
212 if (res < 0)
214 i = 1;
217 if (res < -1 || i >= 1000)
219 MSG_ATTR(_("Unable to register a command server name"),
220 hl_attr(HLF_W));
221 return FAIL;
223 if (p == NULL)
224 p = alloc(STRLEN(name) + 10);
225 if (p == NULL)
227 res = -10;
228 continue;
230 sprintf((char *)p, "%s%d", name, i++);
231 res = DoRegisterName(dpy, p);
233 while (res < 0)
235 vim_free(p);
237 return OK;
240 static int
241 DoRegisterName(dpy, name)
242 Display *dpy;
243 char_u *name;
245 Window w;
246 XErrorHandler old_handler;
247 #define MAX_NAME_LENGTH 100
248 char_u propInfo[MAX_NAME_LENGTH + 20];
250 if (commProperty == None)
252 if (SendInit(dpy) < 0)
253 return -2;
257 * Make sure the name is unique, and append info about it to
258 * the registry property. It's important to lock the server
259 * here to prevent conflicting changes to the registry property.
260 * WARNING: Do not step through this while debugging, it will hangup the X
261 * server!
263 XGrabServer(dpy);
264 w = LookupName(dpy, name, FALSE, NULL);
265 if (w != (Window)0)
267 Status status;
268 int dummyInt;
269 unsigned int dummyUns;
270 Window dummyWin;
273 * The name is currently registered. See if the commWindow
274 * associated with the name exists. If not, or if the commWindow
275 * is *our* commWindow, then just unregister the old name (this
276 * could happen if an application dies without cleaning up the
277 * registry).
279 old_handler = XSetErrorHandler(x_error_check);
280 status = XGetGeometry(dpy, w, &dummyWin, &dummyInt, &dummyInt,
281 &dummyUns, &dummyUns, &dummyUns, &dummyUns);
282 (void)XSetErrorHandler(old_handler);
283 if (status != Success && w != commWindow)
285 XUngrabServer(dpy);
286 XFlush(dpy);
287 return -1;
289 (void)LookupName(dpy, name, /*delete=*/TRUE, NULL);
291 sprintf((char *)propInfo, "%x %.*s", (int_u)commWindow,
292 MAX_NAME_LENGTH, name);
293 old_handler = XSetErrorHandler(x_error_check);
294 got_x_error = FALSE;
295 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8,
296 PropModeAppend, propInfo, STRLEN(propInfo) + 1);
297 XUngrabServer(dpy);
298 XSync(dpy, False);
299 (void)XSetErrorHandler(old_handler);
301 if (!got_x_error)
303 #ifdef FEAT_EVAL
304 set_vim_var_string(VV_SEND_SERVER, name, -1);
305 #endif
306 serverName = vim_strsave(name);
307 #ifdef FEAT_TITLE
308 need_maketitle = TRUE;
309 #endif
310 return 0;
312 return -2;
315 #if defined(FEAT_GUI) || defined(PROTO)
317 * Clean out new ID from registry and set it as comm win.
318 * Change any registered window ID.
320 void
321 serverChangeRegisteredWindow(dpy, newwin)
322 Display *dpy; /* Display to register with */
323 Window newwin; /* Re-register to this ID */
325 char_u propInfo[MAX_NAME_LENGTH + 20];
327 commWindow = newwin;
329 /* Always call SendInit() here, to make sure commWindow is marked as a Vim
330 * window. */
331 if (SendInit(dpy) < 0)
332 return;
334 /* WARNING: Do not step through this while debugging, it will hangup the X
335 * server! */
336 XGrabServer(dpy);
337 DeleteAnyLingerer(dpy, newwin);
338 if (serverName != NULL)
340 /* Reinsert name if we was already registered */
341 (void)LookupName(dpy, serverName, /*delete=*/TRUE, NULL);
342 sprintf((char *)propInfo, "%x %.*s",
343 (int_u)newwin, MAX_NAME_LENGTH, serverName);
344 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8,
345 PropModeAppend, (char_u *)propInfo,
346 STRLEN(propInfo) + 1);
348 XUngrabServer(dpy);
350 #endif
353 * Send to an instance of Vim via the X display.
354 * Returns 0 for OK, negative for an error.
357 serverSendToVim(dpy, name, cmd, result, server, asExpr, localLoop, silent)
358 Display *dpy; /* Where to send. */
359 char_u *name; /* Where to send. */
360 char_u *cmd; /* What to send. */
361 char_u **result; /* Result of eval'ed expression */
362 Window *server; /* Actual ID of receiving app */
363 Bool asExpr; /* Interpret as keystrokes or expr ? */
364 Bool localLoop; /* Throw away everything but result */
365 int silent; /* don't complain about no server */
367 Window w;
368 char_u *property;
369 int length;
370 int res;
371 static int serial = 0; /* Running count of sent commands.
372 * Used to give each command a
373 * different serial number. */
374 PendingCommand pending;
375 char_u *loosename = NULL;
377 if (result != NULL)
378 *result = NULL;
379 if (name == NULL || *name == NUL)
380 name = (char_u *)"GVIM"; /* use a default name */
382 if (commProperty == None && dpy != NULL)
384 if (SendInit(dpy) < 0)
385 return -1;
388 /* Execute locally if no display or target is ourselves */
389 if (dpy == NULL || (serverName != NULL && STRICMP(name, serverName) == 0))
391 if (asExpr)
393 char_u *ret;
395 ret = eval_client_expr_to_string(cmd);
396 if (result != NULL)
398 if (ret == NULL)
399 *result = vim_strsave((char_u *)_(e_invexprmsg));
400 else
401 *result = ret;
403 else
404 vim_free(ret);
405 return ret == NULL ? -1 : 0;
407 else
408 server_to_input_buf(cmd);
409 return 0;
413 * Bind the server name to a communication window.
415 * Find any survivor with a serialno attached to the name if the
416 * original registrant of the wanted name is no longer present.
418 * Delete any lingering names from dead editors.
420 while (TRUE)
422 w = LookupName(dpy, name, FALSE, &loosename);
423 /* Check that the window is hot */
424 if (w != None)
426 if (!WindowValid(dpy, w))
428 LookupName(dpy, loosename ? loosename : name,
429 /*DELETE=*/TRUE, NULL);
430 continue;
433 break;
435 if (w == None)
437 if (!silent)
438 EMSG2(_(e_noserver), name);
439 return -1;
441 else if (loosename != NULL)
442 name = loosename;
443 if (server != NULL)
444 *server = w;
447 * Send the command to target interpreter by appending it to the
448 * comm window in the communication window.
449 * Length must be computed exactly!
451 #ifdef FEAT_MBYTE
452 length = STRLEN(name) + STRLEN(p_enc) + STRLEN(cmd) + 14;
453 #else
454 length = STRLEN(name) + STRLEN(cmd) + 10;
455 #endif
456 property = (char_u *)alloc((unsigned)length + 30);
458 #ifdef FEAT_MBYTE
459 sprintf((char *)property, "%c%c%c-n %s%c-E %s%c-s %s",
460 0, asExpr ? 'c' : 'k', 0, name, 0, p_enc, 0, cmd);
461 #else
462 sprintf((char *)property, "%c%c%c-n %s%c-s %s",
463 0, asExpr ? 'c' : 'k', 0, name, 0, cmd);
464 #endif
465 if (name == loosename)
466 vim_free(loosename);
467 /* Add a back reference to our comm window */
468 serial++;
469 sprintf((char *)property + length, "%c-r %x %d",
470 0, (int_u)commWindow, serial);
471 /* Add length of what "-r %x %d" resulted in, skipping the NUL. */
472 length += STRLEN(property + length + 1) + 1;
474 res = AppendPropCarefully(dpy, w, commProperty, property, length + 1);
475 vim_free(property);
476 if (res < 0)
478 EMSG(_("E248: Failed to send command to the destination program"));
479 return -1;
482 if (!asExpr) /* There is no answer for this - Keys are sent async */
483 return 0;
486 * Register the fact that we're waiting for a command to
487 * complete (this is needed by SendEventProc and by
488 * AppendErrorProc to pass back the command's results).
490 pending.serial = serial;
491 pending.code = 0;
492 pending.result = NULL;
493 pending.nextPtr = pendingCommands;
494 pendingCommands = &pending;
496 ServerWait(dpy, w, WaitForPend, &pending, localLoop, 600);
499 * Unregister the information about the pending command
500 * and return the result.
502 if (pendingCommands == &pending)
503 pendingCommands = pending.nextPtr;
504 else
506 PendingCommand *pcPtr;
508 for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr)
509 if (pcPtr->nextPtr == &pending)
511 pcPtr->nextPtr = pending.nextPtr;
512 break;
515 if (result != NULL)
516 *result = pending.result;
517 else
518 vim_free(pending.result);
520 return pending.code == 0 ? 0 : -1;
523 static int
524 WaitForPend(p)
525 void *p;
527 PendingCommand *pending = (PendingCommand *) p;
528 return pending->result != NULL;
532 * Return TRUE if window "w" exists and has a "Vim" property on it.
534 static int
535 WindowValid(dpy, w)
536 Display *dpy;
537 Window w;
539 XErrorHandler old_handler;
540 Atom *plist;
541 int numProp;
542 int i;
544 old_handler = XSetErrorHandler(x_error_check);
545 got_x_error = 0;
546 plist = XListProperties(dpy, w, &numProp);
547 XSync(dpy, False);
548 XSetErrorHandler(old_handler);
549 if (plist == NULL || got_x_error)
550 return FALSE;
552 for (i = 0; i < numProp; i++)
553 if (plist[i] == vimProperty)
555 XFree(plist);
556 return TRUE;
558 XFree(plist);
559 return FALSE;
563 * Enter a loop processing X events & polling chars until we see a result
565 static void
566 ServerWait(dpy, w, endCond, endData, localLoop, seconds)
567 Display *dpy;
568 Window w;
569 EndCond endCond;
570 void *endData;
571 int localLoop;
572 int seconds;
574 time_t start;
575 time_t now;
576 time_t lastChk = 0;
577 XEvent event;
578 XPropertyEvent *e = (XPropertyEvent *)&event;
579 # define SEND_MSEC_POLL 50
581 time(&start);
582 while (endCond(endData) == 0)
584 time(&now);
585 if (seconds >= 0 && (now - start) >= seconds)
586 break;
587 if (now != lastChk)
589 lastChk = now;
590 if (!WindowValid(dpy, w))
591 break;
593 * Sometimes the PropertyChange event doesn't come.
594 * This can be seen in eg: vim -c 'echo remote_expr("gvim", "3+2")'
596 serverEventProc(dpy, NULL);
598 if (localLoop)
600 /* Just look out for the answer without calling back into Vim */
601 #ifndef HAVE_SELECT
602 struct pollfd fds;
604 fds.fd = ConnectionNumber(dpy);
605 fds.events = POLLIN;
606 if (poll(&fds, 1, SEND_MSEC_POLL) < 0)
607 break;
608 #else
609 fd_set fds;
610 struct timeval tv;
612 tv.tv_sec = 0;
613 tv.tv_usec = SEND_MSEC_POLL * 1000;
614 FD_ZERO(&fds);
615 FD_SET(ConnectionNumber(dpy), &fds);
616 if (select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &tv) < 0)
617 break;
618 #endif
619 while (XEventsQueued(dpy, QueuedAfterReading) > 0)
621 XNextEvent(dpy, &event);
622 if (event.type == PropertyNotify && e->window == commWindow)
623 serverEventProc(dpy, &event);
626 else
628 if (got_int)
629 break;
630 ui_delay((long)SEND_MSEC_POLL, TRUE);
631 ui_breakcheck();
638 * Fetch a list of all the Vim instance names currently registered for the
639 * display.
641 * Returns a newline separated list in allocated memory or NULL.
643 char_u *
644 serverGetVimNames(dpy)
645 Display *dpy;
647 char_u *regProp;
648 char_u *entry;
649 char_u *p;
650 long_u numItems;
651 int_u w;
652 garray_T ga;
654 if (registryProperty == None)
656 if (SendInit(dpy) < 0)
657 return NULL;
659 ga_init2(&ga, 1, 100);
662 * Read the registry property.
664 if (GetRegProp(dpy, &regProp, &numItems, TRUE) == FAIL)
665 return NULL;
668 * Scan all of the names out of the property.
670 ga_init2(&ga, 1, 100);
671 for (p = regProp; (long_u)(p - regProp) < numItems; p++)
673 entry = p;
674 while (*p != 0 && !isspace(*p))
675 p++;
676 if (*p != 0)
678 w = None;
679 sscanf((char *)entry, "%x", &w);
680 if (WindowValid(dpy, (Window)w))
682 ga_concat(&ga, p + 1);
683 ga_concat(&ga, (char_u *)"\n");
685 while (*p != 0)
686 p++;
689 if (regProp != empty_prop)
690 XFree(regProp);
691 ga_append(&ga, NUL);
692 return ga.ga_data;
695 /* ----------------------------------------------------------
696 * Reply stuff
699 static struct ServerReply *
700 ServerReplyFind(w, op)
701 Window w;
702 enum ServerReplyOp op;
704 struct ServerReply *p;
705 struct ServerReply e;
706 int i;
708 p = (struct ServerReply *) serverReply.ga_data;
709 for (i = 0; i < serverReply.ga_len; i++, p++)
710 if (p->id == w)
711 break;
712 if (i >= serverReply.ga_len)
713 p = NULL;
715 if (p == NULL && op == SROP_Add)
717 if (serverReply.ga_growsize == 0)
718 ga_init2(&serverReply, sizeof(struct ServerReply), 1);
719 if (ga_grow(&serverReply, 1) == OK)
721 p = ((struct ServerReply *) serverReply.ga_data)
722 + serverReply.ga_len;
723 e.id = w;
724 ga_init2(&e.strings, 1, 100);
725 mch_memmove(p, &e, sizeof(e));
726 serverReply.ga_len++;
729 else if (p != NULL && op == SROP_Delete)
731 ga_clear(&p->strings);
732 mch_memmove(p, p + 1, (serverReply.ga_len - i - 1) * sizeof(*p));
733 serverReply.ga_len--;
736 return p;
740 * Convert string to windowid.
741 * Issue an error if the id is invalid.
743 Window
744 serverStrToWin(str)
745 char_u *str;
747 unsigned id = None;
749 sscanf((char *)str, "0x%x", &id);
750 if (id == None)
751 EMSG2(_("E573: Invalid server id used: %s"), str);
753 return (Window)id;
757 * Send a reply string (notification) to client with id "name".
758 * Return -1 if the window is invalid.
761 serverSendReply(name, str)
762 char_u *name;
763 char_u *str;
765 char_u *property;
766 int length;
767 int res;
768 Display *dpy = X_DISPLAY;
769 Window win = serverStrToWin(name);
771 if (commProperty == None)
773 if (SendInit(dpy) < 0)
774 return -2;
776 if (!WindowValid(dpy, win))
777 return -1;
779 #ifdef FEAT_MBYTE
780 length = STRLEN(p_enc) + STRLEN(str) + 14;
781 #else
782 length = STRLEN(str) + 10;
783 #endif
784 if ((property = (char_u *)alloc((unsigned)length + 30)) != NULL)
786 #ifdef FEAT_MBYTE
787 sprintf((char *)property, "%cn%c-E %s%c-n %s%c-w %x",
788 0, 0, p_enc, 0, str, 0, (unsigned int)commWindow);
789 #else
790 sprintf((char *)property, "%cn%c-n %s%c-w %x",
791 0, 0, str, 0, (unsigned int)commWindow);
792 #endif
793 /* Add length of what "%x" resulted in. */
794 length += STRLEN(property + length);
795 res = AppendPropCarefully(dpy, win, commProperty, property, length + 1);
796 vim_free(property);
797 return res;
799 return -1;
802 static int
803 WaitForReply(p)
804 void *p;
806 Window *w = (Window *) p;
807 return ServerReplyFind(*w, SROP_Find) != NULL;
811 * Wait for replies from id (win)
812 * Return 0 and the malloc'ed string when a reply is available.
813 * Return -1 if the window becomes invalid while waiting.
816 serverReadReply(dpy, win, str, localLoop)
817 Display *dpy;
818 Window win;
819 char_u **str;
820 int localLoop;
822 int len;
823 char_u *s;
824 struct ServerReply *p;
826 ServerWait(dpy, win, WaitForReply, &win, localLoop, -1);
828 if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0)
830 *str = vim_strsave(p->strings.ga_data);
831 len = STRLEN(*str) + 1;
832 if (len < p->strings.ga_len)
834 s = (char_u *) p->strings.ga_data;
835 mch_memmove(s, s + len, p->strings.ga_len - len);
836 p->strings.ga_len -= len;
838 else
840 /* Last string read. Remove from list */
841 ga_clear(&p->strings);
842 ServerReplyFind(win, SROP_Delete);
844 return 0;
846 return -1;
850 * Check for replies from id (win).
851 * Return TRUE and a non-malloc'ed string if there is. Else return FALSE.
854 serverPeekReply(dpy, win, str)
855 Display *dpy;
856 Window win;
857 char_u **str;
859 struct ServerReply *p;
861 if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0)
863 if (str != NULL)
864 *str = p->strings.ga_data;
865 return 1;
867 if (!WindowValid(dpy, win))
868 return -1;
869 return 0;
874 * Initialize the communication channels for sending commands and receiving
875 * results.
877 static int
878 SendInit(dpy)
879 Display *dpy;
881 XErrorHandler old_handler;
884 * Create the window used for communication, and set up an
885 * event handler for it.
887 old_handler = XSetErrorHandler(x_error_check);
888 got_x_error = FALSE;
890 if (commProperty == None)
891 commProperty = XInternAtom(dpy, "Comm", False);
892 if (vimProperty == None)
893 vimProperty = XInternAtom(dpy, "Vim", False);
894 if (registryProperty == None)
895 registryProperty = XInternAtom(dpy, "VimRegistry", False);
897 if (commWindow == None)
899 commWindow = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy),
900 getpid(), 0, 10, 10, 0,
901 WhitePixel(dpy, DefaultScreen(dpy)),
902 WhitePixel(dpy, DefaultScreen(dpy)));
903 XSelectInput(dpy, commWindow, PropertyChangeMask);
904 /* WARNING: Do not step through this while debugging, it will hangup
905 * the X server! */
906 XGrabServer(dpy);
907 DeleteAnyLingerer(dpy, commWindow);
908 XUngrabServer(dpy);
911 /* Make window recognizable as a vim window */
912 XChangeProperty(dpy, commWindow, vimProperty, XA_STRING,
913 8, PropModeReplace, (char_u *)VIM_VERSION_SHORT,
914 (int)STRLEN(VIM_VERSION_SHORT) + 1);
916 XSync(dpy, False);
917 (void)XSetErrorHandler(old_handler);
919 return got_x_error ? -1 : 0;
923 * Given a server name, see if the name exists in the registry for a
924 * particular display.
926 * If the given name is registered, return the ID of the window associated
927 * with the name. If the name isn't registered, then return 0.
929 * Side effects:
930 * If the registry property is improperly formed, then it is deleted.
931 * If "delete" is non-zero, then if the named server is found it is
932 * removed from the registry property.
934 static Window
935 LookupName(dpy, name, delete, loose)
936 Display *dpy; /* Display whose registry to check. */
937 char_u *name; /* Name of a server. */
938 int delete; /* If non-zero, delete info about name. */
939 char_u **loose; /* Do another search matching -999 if not found
940 Return result here if a match is found */
942 char_u *regProp, *entry;
943 char_u *p;
944 long_u numItems;
945 int_u returnValue;
948 * Read the registry property.
950 if (GetRegProp(dpy, &regProp, &numItems, FALSE) == FAIL)
951 return 0;
954 * Scan the property for the desired name.
956 returnValue = (int_u)None;
957 entry = NULL; /* Not needed, but eliminates compiler warning. */
958 for (p = regProp; (long_u)(p - regProp) < numItems; )
960 entry = p;
961 while (*p != 0 && !isspace(*p))
962 p++;
963 if (*p != 0 && STRICMP(name, p + 1) == 0)
965 sscanf((char *)entry, "%x", &returnValue);
966 break;
968 while (*p != 0)
969 p++;
970 p++;
973 if (loose != NULL && returnValue == (int_u)None && !IsSerialName(name))
975 for (p = regProp; (long_u)(p - regProp) < numItems; )
977 entry = p;
978 while (*p != 0 && !isspace(*p))
979 p++;
980 if (*p != 0 && IsSerialName(p + 1)
981 && STRNICMP(name, p + 1, STRLEN(name)) == 0)
983 sscanf((char *)entry, "%x", &returnValue);
984 *loose = vim_strsave(p + 1);
985 break;
987 while (*p != 0)
988 p++;
989 p++;
994 * Delete the property, if that is desired (copy down the
995 * remainder of the registry property to overlay the deleted
996 * info, then rewrite the property).
998 if (delete && returnValue != (int_u)None)
1000 int count;
1002 while (*p != 0)
1003 p++;
1004 p++;
1005 count = numItems - (p - regProp);
1006 if (count > 0)
1007 mch_memmove(entry, p, count);
1008 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING,
1009 8, PropModeReplace, regProp,
1010 (int)(numItems - (p - entry)));
1011 XSync(dpy, False);
1014 if (regProp != empty_prop)
1015 XFree(regProp);
1016 return (Window)returnValue;
1020 * Delete any lingering occurrence of window id. We promise that any
1021 * occurrence is not ours since it is not yet put into the registry (by us)
1023 * This is necessary in the following scenario:
1024 * 1. There is an old windowid for an exit'ed vim in the registry
1025 * 2. We get that id for our commWindow but only want to send, not register.
1026 * 3. The window will mistakenly be regarded valid because of own commWindow
1028 static void
1029 DeleteAnyLingerer(dpy, win)
1030 Display *dpy; /* Display whose registry to check. */
1031 Window win; /* Window to remove */
1033 char_u *regProp, *entry = NULL;
1034 char_u *p;
1035 long_u numItems;
1036 int_u wwin;
1039 * Read the registry property.
1041 if (GetRegProp(dpy, &regProp, &numItems, FALSE) == FAIL)
1042 return;
1044 /* Scan the property for the window id. */
1045 for (p = regProp; (long_u)(p - regProp) < numItems; )
1047 if (*p != 0)
1049 sscanf((char *)p, "%x", &wwin);
1050 if ((Window)wwin == win)
1052 int lastHalf;
1054 /* Copy down the remainder to delete entry */
1055 entry = p;
1056 while (*p != 0)
1057 p++;
1058 p++;
1059 lastHalf = numItems - (p - regProp);
1060 if (lastHalf > 0)
1061 mch_memmove(entry, p, lastHalf);
1062 numItems = (entry - regProp) + lastHalf;
1063 p = entry;
1064 continue;
1067 while (*p != 0)
1068 p++;
1069 p++;
1072 if (entry != NULL)
1074 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty,
1075 XA_STRING, 8, PropModeReplace, regProp,
1076 (int)(p - regProp));
1077 XSync(dpy, False);
1080 if (regProp != empty_prop)
1081 XFree(regProp);
1085 * Read the registry property. Delete it when it's formatted wrong.
1086 * Return the property in "regPropp". "empty_prop" is used when it doesn't
1087 * exist yet.
1088 * Return OK when successful.
1090 static int
1091 GetRegProp(dpy, regPropp, numItemsp, domsg)
1092 Display *dpy;
1093 char_u **regPropp;
1094 long_u *numItemsp;
1095 int domsg; /* When TRUE give error message. */
1097 int result, actualFormat;
1098 long_u bytesAfter;
1099 Atom actualType;
1100 XErrorHandler old_handler;
1102 *regPropp = NULL;
1103 old_handler = XSetErrorHandler(x_error_check);
1104 got_x_error = FALSE;
1106 result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0L,
1107 (long)MAX_PROP_WORDS, False,
1108 XA_STRING, &actualType,
1109 &actualFormat, numItemsp, &bytesAfter,
1110 regPropp);
1112 XSync(dpy, FALSE);
1113 (void)XSetErrorHandler(old_handler);
1114 if (got_x_error)
1115 return FAIL;
1117 if (actualType == None)
1119 /* No prop yet. Logically equal to the empty list */
1120 *numItemsp = 0;
1121 *regPropp = empty_prop;
1122 return OK;
1125 /* If the property is improperly formed, then delete it. */
1126 if (result != Success || actualFormat != 8 || actualType != XA_STRING)
1128 if (*regPropp != NULL)
1129 XFree(*regPropp);
1130 XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty);
1131 if (domsg)
1132 EMSG(_("E251: VIM instance registry property is badly formed. Deleted!"));
1133 return FAIL;
1135 return OK;
1139 * This procedure is invoked by the various X event loops throughout Vims when
1140 * a property changes on the communication window. This procedure reads the
1141 * property and handles command requests and responses.
1143 void
1144 serverEventProc(dpy, eventPtr)
1145 Display *dpy;
1146 XEvent *eventPtr; /* Information about event. */
1148 char_u *propInfo;
1149 char_u *p;
1150 int result, actualFormat, code;
1151 long_u numItems, bytesAfter;
1152 Atom actualType;
1153 char_u *tofree;
1155 if (eventPtr != NULL)
1157 if (eventPtr->xproperty.atom != commProperty
1158 || eventPtr->xproperty.state != PropertyNewValue)
1159 return;
1163 * Read the comm property and delete it.
1165 propInfo = NULL;
1166 result = XGetWindowProperty(dpy, commWindow, commProperty, 0L,
1167 (long)MAX_PROP_WORDS, True,
1168 XA_STRING, &actualType,
1169 &actualFormat, &numItems, &bytesAfter,
1170 &propInfo);
1172 /* If the property doesn't exist or is improperly formed then ignore it. */
1173 if (result != Success || actualType != XA_STRING || actualFormat != 8)
1175 if (propInfo != NULL)
1176 XFree(propInfo);
1177 return;
1181 * Several commands and results could arrive in the property at
1182 * one time; each iteration through the outer loop handles a
1183 * single command or result.
1185 for (p = propInfo; (long_u)(p - propInfo) < numItems; )
1188 * Ignore leading NULs; each command or result starts with a
1189 * NUL so that no matter how badly formed a preceding command
1190 * is, we'll be able to tell that a new command/result is
1191 * starting.
1193 if (*p == 0)
1195 p++;
1196 continue;
1199 if ((*p == 'c' || *p == 'k') && (p[1] == 0))
1201 Window resWindow;
1202 char_u *name, *script, *serial, *end, *res;
1203 Bool asKeys = *p == 'k';
1204 garray_T reply;
1205 char_u *enc;
1208 * This is an incoming command from some other application.
1209 * Iterate over all of its options. Stop when we reach
1210 * the end of the property or something that doesn't look
1211 * like an option.
1213 p += 2;
1214 name = NULL;
1215 resWindow = None;
1216 serial = (char_u *)"";
1217 script = NULL;
1218 enc = NULL;
1219 while ((long_u)(p - propInfo) < numItems && *p == '-')
1221 switch (p[1])
1223 case 'r':
1224 end = skipwhite(p + 2);
1225 resWindow = 0;
1226 while (vim_isxdigit(*end))
1228 resWindow = 16 * resWindow + (long_u)hex2nr(*end);
1229 ++end;
1231 if (end == p + 2 || *end != ' ')
1232 resWindow = None;
1233 else
1235 p = serial = end + 1;
1236 clientWindow = resWindow; /* Remember in global */
1238 break;
1239 case 'n':
1240 if (p[2] == ' ')
1241 name = p + 3;
1242 break;
1243 case 's':
1244 if (p[2] == ' ')
1245 script = p + 3;
1246 break;
1247 case 'E':
1248 if (p[2] == ' ')
1249 enc = p + 3;
1250 break;
1252 while (*p != 0)
1253 p++;
1254 p++;
1257 if (script == NULL || name == NULL)
1258 continue;
1261 * Initialize the result property, so that we're ready at any
1262 * time if we need to return an error.
1264 if (resWindow != None)
1266 ga_init2(&reply, 1, 100);
1267 #ifdef FEAT_MBYTE
1268 ga_grow(&reply, 50 + STRLEN(p_enc));
1269 sprintf(reply.ga_data, "%cr%c-E %s%c-s %s%c-r ",
1270 0, 0, p_enc, 0, serial, 0);
1271 reply.ga_len = 14 + STRLEN(p_enc) + STRLEN(serial);
1272 #else
1273 ga_grow(&reply, 50);
1274 sprintf(reply.ga_data, "%cr%c-s %s%c-r ", 0, 0, serial, 0);
1275 reply.ga_len = 10 + STRLEN(serial);
1276 #endif
1278 res = NULL;
1279 if (serverName != NULL && STRICMP(name, serverName) == 0)
1281 script = serverConvert(enc, script, &tofree);
1282 if (asKeys)
1283 server_to_input_buf(script);
1284 else
1285 res = eval_client_expr_to_string(script);
1286 vim_free(tofree);
1288 if (resWindow != None)
1290 if (res != NULL)
1291 ga_concat(&reply, res);
1292 else if (asKeys == 0)
1294 ga_concat(&reply, (char_u *)_(e_invexprmsg));
1295 ga_append(&reply, 0);
1296 ga_concat(&reply, (char_u *)"-c 1");
1298 ga_append(&reply, NUL);
1299 (void)AppendPropCarefully(dpy, resWindow, commProperty,
1300 reply.ga_data, reply.ga_len);
1301 ga_clear(&reply);
1303 vim_free(res);
1305 else if (*p == 'r' && p[1] == 0)
1307 int serial, gotSerial;
1308 char_u *res;
1309 PendingCommand *pcPtr;
1310 char_u *enc;
1313 * This is a reply to some command that we sent out. Iterate
1314 * over all of its options. Stop when we reach the end of the
1315 * property or something that doesn't look like an option.
1317 p += 2;
1318 gotSerial = 0;
1319 res = (char_u *)"";
1320 code = 0;
1321 enc = NULL;
1322 while ((long_u)(p - propInfo) < numItems && *p == '-')
1324 switch (p[1])
1326 case 'r':
1327 if (p[2] == ' ')
1328 res = p + 3;
1329 break;
1330 case 'E':
1331 if (p[2] == ' ')
1332 enc = p + 3;
1333 break;
1334 case 's':
1335 if (sscanf((char *)p + 2, " %d", &serial) == 1)
1336 gotSerial = 1;
1337 break;
1338 case 'c':
1339 if (sscanf((char *)p + 2, " %d", &code) != 1)
1340 code = 0;
1341 break;
1343 while (*p != 0)
1344 p++;
1345 p++;
1348 if (!gotSerial)
1349 continue;
1352 * Give the result information to anyone who's
1353 * waiting for it.
1355 for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr)
1357 if (serial != pcPtr->serial || pcPtr->result != NULL)
1358 continue;
1360 pcPtr->code = code;
1361 if (res != NULL)
1363 res = serverConvert(enc, res, &tofree);
1364 if (tofree == NULL)
1365 res = vim_strsave(res);
1366 pcPtr->result = res;
1368 else
1369 pcPtr->result = vim_strsave((char_u *)"");
1370 break;
1373 else if (*p == 'n' && p[1] == 0)
1375 Window win = 0;
1376 unsigned int u;
1377 int gotWindow;
1378 char_u *str;
1379 struct ServerReply *r;
1380 char_u *enc;
1383 * This is a (n)otification. Sent with serverreply_send in VimL.
1384 * Execute any autocommand and save it for later retrieval
1386 p += 2;
1387 gotWindow = 0;
1388 str = (char_u *)"";
1389 enc = NULL;
1390 while ((long_u)(p - propInfo) < numItems && *p == '-')
1392 switch (p[1])
1394 case 'n':
1395 if (p[2] == ' ')
1396 str = p + 3;
1397 break;
1398 case 'E':
1399 if (p[2] == ' ')
1400 enc = p + 3;
1401 break;
1402 case 'w':
1403 if (sscanf((char *)p + 2, " %x", &u) == 1)
1405 win = u;
1406 gotWindow = 1;
1408 break;
1410 while (*p != 0)
1411 p++;
1412 p++;
1415 if (!gotWindow)
1416 continue;
1417 str = serverConvert(enc, str, &tofree);
1418 if ((r = ServerReplyFind(win, SROP_Add)) != NULL)
1420 ga_concat(&(r->strings), str);
1421 ga_append(&(r->strings), NUL);
1423 #ifdef FEAT_AUTOCMD
1425 char_u winstr[30];
1427 sprintf((char *)winstr, "0x%x", (unsigned int)win);
1428 apply_autocmds(EVENT_REMOTEREPLY, winstr, str, TRUE, curbuf);
1430 #endif
1431 vim_free(tofree);
1433 else
1436 * Didn't recognize this thing. Just skip through the next
1437 * null character and try again.
1438 * Even if we get an 'r'(eply) we will throw it away as we
1439 * never specify (and thus expect) one
1441 while (*p != 0)
1442 p++;
1443 p++;
1446 XFree(propInfo);
1450 * Append a given property to a given window, but set up an X error handler so
1451 * that if the append fails this procedure can return an error code rather
1452 * than having Xlib panic.
1453 * Return: 0 for OK, -1 for error
1455 static int
1456 AppendPropCarefully(dpy, window, property, value, length)
1457 Display *dpy; /* Display on which to operate. */
1458 Window window; /* Window whose property is to be modified. */
1459 Atom property; /* Name of property. */
1460 char_u *value; /* Characters to append to property. */
1461 int length; /* How much to append */
1463 XErrorHandler old_handler;
1465 old_handler = XSetErrorHandler(x_error_check);
1466 got_x_error = FALSE;
1467 XChangeProperty(dpy, window, property, XA_STRING, 8,
1468 PropModeAppend, value, length);
1469 XSync(dpy, False);
1470 (void) XSetErrorHandler(old_handler);
1471 return got_x_error ? -1 : 0;
1476 * Another X Error handler, just used to check for errors.
1478 static int
1479 x_error_check(dpy, error_event)
1480 Display *dpy UNUSED;
1481 XErrorEvent *error_event UNUSED;
1483 got_x_error = TRUE;
1484 return 0;
1488 * Check if "str" looks like it had a serial number appended.
1489 * Actually just checks if the name ends in a digit.
1491 static int
1492 IsSerialName(str)
1493 char_u *str;
1495 int len = STRLEN(str);
1497 return (len > 1 && vim_isdigit(str[len - 1]));
1499 #endif /* FEAT_CLIENTSERVER */