#include "vim.h"
#include "version.h"
#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
# ifdef FEAT_X11
# include <X11/Intrinsic.h>
# include <X11/Xatom.h>
# endif
# if defined(HAVE_SYS_SELECT_H) && \
(!defined(HAVE_SYS_TIME_H) || defined(SYS_SELECT_WITH_SYS_TIME))
# include <sys/select.h>
# endif
# ifndef HAVE_SELECT
# ifdef HAVE_SYS_POLL_H
# include <sys/poll.h>
# else
# ifdef HAVE_POLL_H
# include <poll.h>
# endif
# endif
# endif
typedef struct PendingCommand
{
int serial;
int code;
char_u *result;
struct PendingCommand *nextPtr;
} PendingCommand;
static PendingCommand *pendingCommands = NULL;
#define MAX_PROP_WORDS 100000
struct ServerReply
{
Window id;
garray_T strings;
};
static garray_T serverReply = { 0, 0, 0, 0, 0 };
enum ServerReplyOp { SROP_Find, SROP_Add, SROP_Delete };
typedef int (*EndCond) __ARGS((void *));
static Window LookupName __ARGS((Display *dpy, char_u *name, int delete, char_u **loose));
static int SendInit __ARGS((Display *dpy));
static int DoRegisterName __ARGS((Display *dpy, char_u *name));
static void DeleteAnyLingerer __ARGS((Display *dpy, Window w));
static int GetRegProp __ARGS((Display *dpy, char_u **regPropp, long_u *numItemsp, int domsg));
static int WaitForPend __ARGS((void *p));
static int WaitForReply __ARGS((void *p));
static int WindowValid __ARGS((Display *dpy, Window w));
static void ServerWait __ARGS((Display *dpy, Window w, EndCond endCond, void *endData, int localLoop, int seconds));
static struct ServerReply *ServerReplyFind __ARGS((Window w, enum ServerReplyOp op));
static int AppendPropCarefully __ARGS((Display *display, Window window, Atom property, char_u *value, int length));
static int x_error_check __ARGS((Display *dpy, XErrorEvent *error_event));
static int IsSerialName __ARGS((char_u *name));
static Atom registryProperty = None;
static Atom vimProperty = None;
static int got_x_error = FALSE;
static char_u *empty_prop = (char_u *)"";
int
serverRegisterName(dpy, name)
Display *dpy;
char_u *name;
{
int i;
int res;
char_u *p = NULL;
res = DoRegisterName(dpy, name);
if (res < 0)
{
i = 1;
do
{
if (res < -1 || i >= 1000)
{
MSG_ATTR(_("Unable to register a command server name"),
hl_attr(HLF_W));
return FAIL;
}
if (p == NULL)
p = alloc(STRLEN(name) + 10);
if (p == NULL)
{
res = -10;
continue;
}
sprintf((char *)p, "%s%d", name, i++);
res = DoRegisterName(dpy, p);
}
while (res < 0)
;
vim_free(p);
}
return OK;
}
static int
DoRegisterName(dpy, name)
Display *dpy;
char_u *name;
{
Window w;
XErrorHandler old_handler;
#define MAX_NAME_LENGTH 100
char_u propInfo[MAX_NAME_LENGTH + 20];
if (commProperty == None)
{
if (SendInit(dpy) < 0)
return -2;
}
XGrabServer(dpy);
w = LookupName(dpy, name, FALSE, NULL);
if (w != (Window)0)
{
Status status;
int dummyInt;
unsigned int dummyUns;
Window dummyWin;
old_handler = XSetErrorHandler(x_error_check);
status = XGetGeometry(dpy, w, &dummyWin, &dummyInt, &dummyInt,
&dummyUns, &dummyUns, &dummyUns, &dummyUns);
(void)XSetErrorHandler(old_handler);
if (status != Success && w != commWindow)
{
XUngrabServer(dpy);
XFlush(dpy);
return -1;
}
(void)LookupName(dpy, name, TRUE, NULL);
}
sprintf((char *)propInfo, "%x %.*s", (int_u)commWindow,
MAX_NAME_LENGTH, name);
old_handler = XSetErrorHandler(x_error_check);
got_x_error = FALSE;
XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8,
PropModeAppend, propInfo, STRLEN(propInfo) + 1);
XUngrabServer(dpy);
XSync(dpy, False);
(void)XSetErrorHandler(old_handler);
if (!got_x_error)
{
#ifdef FEAT_EVAL
set_vim_var_string(VV_SEND_SERVER, name, -1);
#endif
serverName = vim_strsave(name);
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
return 0;
}
return -2;
}
#if defined(FEAT_GUI) || defined(PROTO)
void
serverChangeRegisteredWindow(dpy, newwin)
Display *dpy;
Window newwin;
{
char_u propInfo[MAX_NAME_LENGTH + 20];
commWindow = newwin;
if (SendInit(dpy) < 0)
return;
XGrabServer(dpy);
DeleteAnyLingerer(dpy, newwin);
if (serverName != NULL)
{
(void)LookupName(dpy, serverName, TRUE, NULL);
sprintf((char *)propInfo, "%x %.*s",
(int_u)newwin, MAX_NAME_LENGTH, serverName);
XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8,
PropModeAppend, (char_u *)propInfo,
STRLEN(propInfo) + 1);
}
XUngrabServer(dpy);
}
#endif
int
serverSendToVim(dpy, name, cmd, result, server, asExpr, localLoop, silent)
Display *dpy;
char_u *name;
char_u *cmd;
char_u **result;
Window *server;
Bool asExpr;
Bool localLoop;
int silent;
{
Window w;
char_u *property;
int length;
int res;
static int serial = 0;
PendingCommand pending;
char_u *loosename = NULL;
if (result != NULL)
*result = NULL;
if (name == NULL || *name == NUL)
name = (char_u *)"GVIM";
if (commProperty == None && dpy != NULL)
{
if (SendInit(dpy) < 0)
return -1;
}
if (dpy == NULL || (serverName != NULL && STRICMP(name, serverName) == 0))
{
if (asExpr)
{
char_u *ret;
ret = eval_client_expr_to_string(cmd);
if (result != NULL)
{
if (ret == NULL)
*result = vim_strsave((char_u *)_(e_invexprmsg));
else
*result = ret;
}
else
vim_free(ret);
return ret == NULL ? -1 : 0;
}
else
server_to_input_buf(cmd);
return 0;
}
while (TRUE)
{
w = LookupName(dpy, name, FALSE, &loosename);
if (w != None)
{
if (!WindowValid(dpy, w))
{
LookupName(dpy, loosename ? loosename : name,
TRUE, NULL);
continue;
}
}
break;
}
if (w == None)
{
if (!silent)
EMSG2(_(e_noserver), name);
return -1;
}
else if (loosename != NULL)
name = loosename;
if (server != NULL)
*server = w;
#ifdef FEAT_MBYTE
length = STRLEN(name) + STRLEN(p_enc) + STRLEN(cmd) + 14;
#else
length = STRLEN(name) + STRLEN(cmd) + 10;
#endif
property = (char_u *)alloc((unsigned)length + 30);
#ifdef FEAT_MBYTE
sprintf((char *)property, "%c%c%c-n %s%c-E %s%c-s %s",
0, asExpr ? 'c' : 'k', 0, name, 0, p_enc, 0, cmd);
#else
sprintf((char *)property, "%c%c%c-n %s%c-s %s",
0, asExpr ? 'c' : 'k', 0, name, 0, cmd);
#endif
if (name == loosename)
vim_free(loosename);
serial++;
sprintf((char *)property + length, "%c-r %x %d",
0, (int_u)commWindow, serial);
length += STRLEN(property + length + 1) + 1;
res = AppendPropCarefully(dpy, w, commProperty, property, length + 1);
vim_free(property);
if (res < 0)
{
EMSG(_("E248: Failed to send command to the destination program"));
return -1;
}
if (!asExpr)
return 0;
pending.serial = serial;
pending.code = 0;
pending.result = NULL;
pending.nextPtr = pendingCommands;
pendingCommands = &pending;
ServerWait(dpy, w, WaitForPend, &pending, localLoop, 600);
if (pendingCommands == &pending)
pendingCommands = pending.nextPtr;
else
{
PendingCommand *pcPtr;
for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr)
if (pcPtr->nextPtr == &pending)
{
pcPtr->nextPtr = pending.nextPtr;
break;
}
}
if (result != NULL)
*result = pending.result;
else
vim_free(pending.result);
return pending.code == 0 ? 0 : -1;
}
static int
WaitForPend(p)
void *p;
{
PendingCommand *pending = (PendingCommand *) p;
return pending->result != NULL;
}
static int
WindowValid(dpy, w)
Display *dpy;
Window w;
{
XErrorHandler old_handler;
Atom *plist;
int numProp;
int i;
old_handler = XSetErrorHandler(x_error_check);
got_x_error = 0;
plist = XListProperties(dpy, w, &numProp);
XSync(dpy, False);
XSetErrorHandler(old_handler);
if (plist == NULL || got_x_error)
return FALSE;
for (i = 0; i < numProp; i++)
if (plist[i] == vimProperty)
{
XFree(plist);
return TRUE;
}
XFree(plist);
return FALSE;
}
static void
ServerWait(dpy, w, endCond, endData, localLoop, seconds)
Display *dpy;
Window w;
EndCond endCond;
void *endData;
int localLoop;
int seconds;
{
time_t start;
time_t now;
time_t lastChk = 0;
XEvent event;
XPropertyEvent *e = (XPropertyEvent *)&event;
# define SEND_MSEC_POLL 50
time(&start);
while (endCond(endData) == 0)
{
time(&now);
if (seconds >= 0 && (now - start) >= seconds)
break;
if (now != lastChk)
{
lastChk = now;
if (!WindowValid(dpy, w))
break;
serverEventProc(dpy, NULL);
}
if (localLoop)
{
#ifndef HAVE_SELECT
struct pollfd fds;
fds.fd = ConnectionNumber(dpy);
fds.events = POLLIN;
if (poll(&fds, 1, SEND_MSEC_POLL) < 0)
break;
#else
fd_set fds;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = SEND_MSEC_POLL * 1000;
FD_ZERO(&fds);
FD_SET(ConnectionNumber(dpy), &fds);
if (select(ConnectionNumber(dpy) + 1, &fds, NULL, NULL, &tv) < 0)
break;
#endif
while (XEventsQueued(dpy, QueuedAfterReading) > 0)
{
XNextEvent(dpy, &event);
if (event.type == PropertyNotify && e->window == commWindow)
serverEventProc(dpy, &event);
}
}
else
{
if (got_int)
break;
ui_delay((long)SEND_MSEC_POLL, TRUE);
ui_breakcheck();
}
}
}
char_u *
serverGetVimNames(dpy)
Display *dpy;
{
char_u *regProp;
char_u *entry;
char_u *p;
long_u numItems;
int_u w;
garray_T ga;
if (registryProperty == None)
{
if (SendInit(dpy) < 0)
return NULL;
}
ga_init2(&ga, 1, 100);
if (GetRegProp(dpy, ®Prop, &numItems, TRUE) == FAIL)
return NULL;
ga_init2(&ga, 1, 100);
for (p = regProp; (p - regProp) < numItems; p++)
{
entry = p;
while (*p != 0 && !isspace(*p))
p++;
if (*p != 0)
{
w = None;
sscanf((char *)entry, "%x", &w);
if (WindowValid(dpy, (Window)w))
{
ga_concat(&ga, p + 1);
ga_concat(&ga, (char_u *)"\n");
}
while (*p != 0)
p++;
}
}
if (regProp != empty_prop)
XFree(regProp);
ga_append(&ga, NUL);
return ga.ga_data;
}
static struct ServerReply *
ServerReplyFind(w, op)
Window w;
enum ServerReplyOp op;
{
struct ServerReply *p;
struct ServerReply e;
int i;
p = (struct ServerReply *) serverReply.ga_data;
for (i = 0; i < serverReply.ga_len; i++, p++)
if (p->id == w)
break;
if (i >= serverReply.ga_len)
p = NULL;
if (p == NULL && op == SROP_Add)
{
if (serverReply.ga_growsize == 0)
ga_init2(&serverReply, sizeof(struct ServerReply), 1);
if (ga_grow(&serverReply, 1) == OK)
{
p = ((struct ServerReply *) serverReply.ga_data)
+ serverReply.ga_len;
e.id = w;
ga_init2(&e.strings, 1, 100);
mch_memmove(p, &e, sizeof(e));
serverReply.ga_len++;
}
}
else if (p != NULL && op == SROP_Delete)
{
ga_clear(&p->strings);
mch_memmove(p, p + 1, (serverReply.ga_len - i - 1) * sizeof(*p));
serverReply.ga_len--;
}
return p;
}
Window
serverStrToWin(str)
char_u *str;
{
unsigned id = None;
sscanf((char *)str, "0x%x", &id);
if (id == None)
EMSG2(_("E573: Invalid server id used: %s"), str);
return (Window)id;
}
int
serverSendReply(name, str)
char_u *name;
char_u *str;
{
char_u *property;
int length;
int res;
Display *dpy = X_DISPLAY;
Window win = serverStrToWin(name);
if (commProperty == None)
{
if (SendInit(dpy) < 0)
return -2;
}
if (!WindowValid(dpy, win))
return -1;
#ifdef FEAT_MBYTE
length = STRLEN(p_enc) + STRLEN(str) + 14;
#else
length = STRLEN(str) + 10;
#endif
if ((property = (char_u *)alloc((unsigned)length + 30)) != NULL)
{
#ifdef FEAT_MBYTE
sprintf((char *)property, "%cn%c-E %s%c-n %s%c-w %x",
0, 0, p_enc, 0, str, 0, (unsigned int)commWindow);
#else
sprintf((char *)property, "%cn%c-n %s%c-w %x",
0, 0, str, 0, (unsigned int)commWindow);
#endif
length += STRLEN(property + length);
res = AppendPropCarefully(dpy, win, commProperty, property, length + 1);
vim_free(property);
return res;
}
return -1;
}
static int
WaitForReply(p)
void *p;
{
Window *w = (Window *) p;
return ServerReplyFind(*w, SROP_Find) != NULL;
}
int
serverReadReply(dpy, win, str, localLoop)
Display *dpy;
Window win;
char_u **str;
int localLoop;
{
int len;
char_u *s;
struct ServerReply *p;
ServerWait(dpy, win, WaitForReply, &win, localLoop, -1);
if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0)
{
*str = vim_strsave(p->strings.ga_data);
len = STRLEN(*str) + 1;
if (len < p->strings.ga_len)
{
s = (char_u *) p->strings.ga_data;
mch_memmove(s, s + len, p->strings.ga_len - len);
p->strings.ga_len -= len;
}
else
{
ga_clear(&p->strings);
ServerReplyFind(win, SROP_Delete);
}
return 0;
}
return -1;
}
int
serverPeekReply(dpy, win, str)
Display *dpy;
Window win;
char_u **str;
{
struct ServerReply *p;
if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0)
{
if (str != NULL)
*str = p->strings.ga_data;
return 1;
}
if (!WindowValid(dpy, win))
return -1;
return 0;
}
static int
SendInit(dpy)
Display *dpy;
{
XErrorHandler old_handler;
old_handler = XSetErrorHandler(x_error_check);
got_x_error = FALSE;
if (commProperty == None)
commProperty = XInternAtom(dpy, "Comm", False);
if (vimProperty == None)
vimProperty = XInternAtom(dpy, "Vim", False);
if (registryProperty == None)
registryProperty = XInternAtom(dpy, "VimRegistry", False);
if (commWindow == None)
{
commWindow = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy),
getpid(), 0, 10, 10, 0,
WhitePixel(dpy, DefaultScreen(dpy)),
WhitePixel(dpy, DefaultScreen(dpy)));
XSelectInput(dpy, commWindow, PropertyChangeMask);
XGrabServer(dpy);
DeleteAnyLingerer(dpy, commWindow);
XUngrabServer(dpy);
}
XChangeProperty(dpy, commWindow, vimProperty, XA_STRING,
8, PropModeReplace, (char_u *)VIM_VERSION_SHORT,
(int)STRLEN(VIM_VERSION_SHORT) + 1);
XSync(dpy, False);
(void)XSetErrorHandler(old_handler);
return got_x_error ? -1 : 0;
}
static Window
LookupName(dpy, name, delete, loose)
Display *dpy;
char_u *name;
int delete;
char_u **loose;
{
char_u *regProp, *entry;
char_u *p;
long_u numItems;
int_u returnValue;
if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL)
return 0;
returnValue = (int_u)None;
entry = NULL;
for (p = regProp; (p - regProp) < numItems; )
{
entry = p;
while (*p != 0 && !isspace(*p))
p++;
if (*p != 0 && STRICMP(name, p + 1) == 0)
{
sscanf((char *)entry, "%x", &returnValue);
break;
}
while (*p != 0)
p++;
p++;
}
if (loose != NULL && returnValue == (int_u)None && !IsSerialName(name))
{
for (p = regProp; (p - regProp) < numItems; )
{
entry = p;
while (*p != 0 && !isspace(*p))
p++;
if (*p != 0 && IsSerialName(p + 1)
&& STRNICMP(name, p + 1, STRLEN(name)) == 0)
{
sscanf((char *)entry, "%x", &returnValue);
*loose = vim_strsave(p + 1);
break;
}
while (*p != 0)
p++;
p++;
}
}
if (delete && returnValue != (int_u)None)
{
int count;
while (*p != 0)
p++;
p++;
count = numItems - (p - regProp);
if (count > 0)
mch_memmove(entry, p, count);
XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING,
8, PropModeReplace, regProp,
(int)(numItems - (p - entry)));
XSync(dpy, False);
}
if (regProp != empty_prop)
XFree(regProp);
return (Window)returnValue;
}
static void
DeleteAnyLingerer(dpy, win)
Display *dpy;
Window win;
{
char_u *regProp, *entry = NULL;
char_u *p;
long_u numItems;
int_u wwin;
if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL)
return;
for (p = regProp; (p - regProp) < numItems; )
{
if (*p != 0)
{
sscanf((char *)p, "%x", &wwin);
if ((Window)wwin == win)
{
int lastHalf;
entry = p;
while (*p != 0)
p++;
p++;
lastHalf = numItems - (p - regProp);
if (lastHalf > 0)
mch_memmove(entry, p, lastHalf);
numItems = (entry - regProp) + lastHalf;
p = entry;
continue;
}
}
while (*p != 0)
p++;
p++;
}
if (entry != NULL)
{
XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty,
XA_STRING, 8, PropModeReplace, regProp,
(int)(p - regProp));
XSync(dpy, False);
}
if (regProp != empty_prop)
XFree(regProp);
}
static int
GetRegProp(dpy, regPropp, numItemsp, domsg)
Display *dpy;
char_u **regPropp;
long_u *numItemsp;
int domsg;
{
int result, actualFormat;
long_u bytesAfter;
Atom actualType;
XErrorHandler old_handler;
*regPropp = NULL;
old_handler = XSetErrorHandler(x_error_check);
got_x_error = FALSE;
result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0L,
(long)MAX_PROP_WORDS, False,
XA_STRING, &actualType,
&actualFormat, numItemsp, &bytesAfter,
regPropp);
XSync(dpy, FALSE);
(void)XSetErrorHandler(old_handler);
if (got_x_error)
return FAIL;
if (actualType == None)
{
*numItemsp = 0;
*regPropp = empty_prop;
return OK;
}
if (result != Success || actualFormat != 8 || actualType != XA_STRING)
{
if (*regPropp != NULL)
XFree(*regPropp);
XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty);
if (domsg)
EMSG(_("E251: VIM instance registry property is badly formed. Deleted!"));
return FAIL;
}
return OK;
}
void
serverEventProc(dpy, eventPtr)
Display *dpy;
XEvent *eventPtr;
{
char_u *propInfo;
char_u *p;
int result, actualFormat, code;
long_u numItems, bytesAfter;
Atom actualType;
char_u *tofree;
if (eventPtr != NULL)
{
if (eventPtr->xproperty.atom != commProperty
|| eventPtr->xproperty.state != PropertyNewValue)
return;
}
propInfo = NULL;
result = XGetWindowProperty(dpy, commWindow, commProperty, 0L,
(long)MAX_PROP_WORDS, True,
XA_STRING, &actualType,
&actualFormat, &numItems, &bytesAfter,
&propInfo);
if (result != Success || actualType != XA_STRING || actualFormat != 8)
{
if (propInfo != NULL)
XFree(propInfo);
return;
}
for (p = propInfo; (p - propInfo) < numItems; )
{
if (*p == 0)
{
p++;
continue;
}
if ((*p == 'c' || *p == 'k') && (p[1] == 0))
{
Window resWindow;
char_u *name, *script, *serial, *end, *res;
Bool asKeys = *p == 'k';
garray_T reply;
char_u *enc;
p += 2;
name = NULL;
resWindow = None;
serial = (char_u *)"";
script = NULL;
enc = NULL;
while (p - propInfo < numItems && *p == '-')
{
switch (p[1])
{
case 'r':
end = skipwhite(p + 2);
resWindow = 0;
while (vim_isxdigit(*end))
{
resWindow = 16 * resWindow + (long_u)hex2nr(*end);
++end;
}
if (end == p + 2 || *end != ' ')
resWindow = None;
else
{
p = serial = end + 1;
clientWindow = resWindow;
}
break;
case 'n':
if (p[2] == ' ')
name = p + 3;
break;
case 's':
if (p[2] == ' ')
script = p + 3;
break;
case 'E':
if (p[2] == ' ')
enc = p + 3;
break;
}
while (*p != 0)
p++;
p++;
}
if (script == NULL || name == NULL)
continue;
if (resWindow != None)
{
ga_init2(&reply, 1, 100);
#ifdef FEAT_MBYTE
ga_grow(&reply, 50 + STRLEN(p_enc));
sprintf(reply.ga_data, "%cr%c-E %s%c-s %s%c-r ",
0, 0, p_enc, 0, serial, 0);
reply.ga_len = 14 + STRLEN(p_enc) + STRLEN(serial);
#else
ga_grow(&reply, 50);
sprintf(reply.ga_data, "%cr%c-s %s%c-r ", 0, 0, serial, 0);
reply.ga_len = 10 + STRLEN(serial);
#endif
}
res = NULL;
if (serverName != NULL && STRICMP(name, serverName) == 0)
{
script = serverConvert(enc, script, &tofree);
if (asKeys)
server_to_input_buf(script);
else
res = eval_client_expr_to_string(script);
vim_free(tofree);
}
if (resWindow != None)
{
if (res != NULL)
ga_concat(&reply, res);
else if (asKeys == 0)
{
ga_concat(&reply, (char_u *)_(e_invexprmsg));
ga_append(&reply, 0);
ga_concat(&reply, (char_u *)"-c 1");
}
ga_append(&reply, NUL);
(void)AppendPropCarefully(dpy, resWindow, commProperty,
reply.ga_data, reply.ga_len);
ga_clear(&reply);
}
vim_free(res);
}
else if (*p == 'r' && p[1] == 0)
{
int serial, gotSerial;
char_u *res;
PendingCommand *pcPtr;
char_u *enc;
p += 2;
gotSerial = 0;
res = (char_u *)"";
code = 0;
enc = NULL;
while ((p-propInfo) < numItems && *p == '-')
{
switch (p[1])
{
case 'r':
if (p[2] == ' ')
res = p + 3;
break;
case 'E':
if (p[2] == ' ')
enc = p + 3;
break;
case 's':
if (sscanf((char *)p + 2, " %d", &serial) == 1)
gotSerial = 1;
break;
case 'c':
if (sscanf((char *)p + 2, " %d", &code) != 1)
code = 0;
break;
}
while (*p != 0)
p++;
p++;
}
if (!gotSerial)
continue;
for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr)
{
if (serial != pcPtr->serial || pcPtr->result != NULL)
continue;
pcPtr->code = code;
if (res != NULL)
{
res = serverConvert(enc, res, &tofree);
if (tofree == NULL)
res = vim_strsave(res);
pcPtr->result = res;
}
else
pcPtr->result = vim_strsave((char_u *)"");
break;
}
}
else if (*p == 'n' && p[1] == 0)
{
Window win = 0;
unsigned int u;
int gotWindow;
char_u *str;
struct ServerReply *r;
char_u *enc;
p += 2;
gotWindow = 0;
str = (char_u *)"";
enc = NULL;
while ((p-propInfo) < numItems && *p == '-')
{
switch (p[1])
{
case 'n':
if (p[2] == ' ')
str = p + 3;
break;
case 'E':
if (p[2] == ' ')
enc = p + 3;
break;
case 'w':
if (sscanf((char *)p + 2, " %x", &u) == 1)
{
win = u;
gotWindow = 1;
}
break;
}
while (*p != 0)
p++;
p++;
}
if (!gotWindow)
continue;
str = serverConvert(enc, str, &tofree);
if ((r = ServerReplyFind(win, SROP_Add)) != NULL)
{
ga_concat(&(r->strings), str);
ga_append(&(r->strings), NUL);
}
#ifdef FEAT_AUTOCMD
{
char_u winstr[30];
sprintf((char *)winstr, "0x%x", (unsigned int)win);
apply_autocmds(EVENT_REMOTEREPLY, winstr, str, TRUE, curbuf);
}
#endif
vim_free(tofree);
}
else
{
while (*p != 0)
p++;
p++;
}
}
XFree(propInfo);
}
static int
AppendPropCarefully(dpy, window, property, value, length)
Display *dpy;
Window window;
Atom property;
char_u *value;
int length;
{
XErrorHandler old_handler;
old_handler = XSetErrorHandler(x_error_check);
got_x_error = FALSE;
XChangeProperty(dpy, window, property, XA_STRING, 8,
PropModeAppend, value, length);
XSync(dpy, False);
(void) XSetErrorHandler(old_handler);
return got_x_error ? -1 : 0;
}
static int
x_error_check(dpy, error_event)
Display *dpy;
XErrorEvent *error_event;
{
got_x_error = TRUE;
return 0;
}
static int
IsSerialName(str)
char_u *str;
{
int len = STRLEN(str);
return (len > 1 && vim_isdigit(str[len - 1]));
}
#endif