#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <ctype.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#ifdef hpux
#include <sys/utsname.h>
#endif
#include <X11/Xauth.h>
#include <X11/Xmu/SysUtil.h>
#if defined(UNIXCONN) || defined(LOCALCONN)
#define UNIX_CONNECTION "unix"
#define UNIX_CONNECTION_LENGTH 4
#endif
#include <stdlib.h>
#include "xauth.h"
char *
copystring (char *src, int len)
{
char *cp;
if (!src && len != 0) return NULL;
cp = malloc (len + 1);
if (cp) {
if (src) strncpy (cp, src, len);
cp[len] = '\0';
}
return cp;
}
char *
get_local_hostname (char *buf, int maxlen)
{
buf[0] = '\0';
(void) XmuGetHostname (buf, maxlen);
return (buf[0] ? buf : NULL);
}
#ifndef UNIXCONN
static char *
copyhostname (void)
{
char buf[256];
return (get_local_hostname (buf, sizeof buf) ?
copystring (buf, strlen (buf)) : NULL);
}
#endif
Bool
parse_displayname (char *displayname,
int *familyp,
char **hostp,
int *dpynump,
int *scrnump,
char **restp)
{
char *ptr;
int len;
int family = -1;
char *host = NULL;
int dpynum = -1;
int scrnum = 0;
char *rest = NULL;
Bool dnet = False;
if (!displayname || !displayname[0]) return False;
ptr = strrchr(displayname, ':');
if (!ptr || !ptr[1]) return False;
if ((ptr != displayname) && (*(ptr - 1) == ':')) {
ptr--;
dnet = True;
}
len = (ptr - displayname);
if (len == 0) {
#if defined(UNIXCONN) || defined(LOCALCONN)
host = copystring (UNIX_CONNECTION, UNIX_CONNECTION_LENGTH);
family = FamilyLocal;
#else
if (dnet) {
host = copystring ("0", 1);
family = FamilyDECnet;
} else {
host = copyhostname ();
family = FamilyInternet;
}
#endif
} else if (!dnet && (*displayname == '[') && (*(ptr - 1) == ']')) {
family = FamilyInternet6;
host = copystring (displayname + 1, len - 2);
} else {
host = copystring (displayname, len);
if (dnet) {
family = dnet;
} else {
#if defined(UNIXCONN) || defined(LOCALCONN)
if (host && strcmp (host, UNIX_CONNECTION) == 0)
family = FamilyLocal;
else
#endif
family = FamilyInternet;
}
}
if (!host) return False;
if(strncmp (host, "/tmp/launch", 11) == 0) {
family = FamilyLocal;
}
if (dnet) ptr++;
ptr++;
{
register char *cp;
for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
len = (cp - ptr);
if (len == 0 || (*cp && *cp != '.')) {
free (host);
return False;
}
dpynum = atoi (ptr);
ptr = cp;
}
if (ptr[0] == '.') {
register char *cp;
ptr++;
for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
len = (cp - ptr);
if (len == 0 || (*cp && *cp != '.')) {
free (host);
return False;
}
scrnum = atoi (ptr);
ptr = cp;
}
if (ptr[0] == '.') {
ptr++;
len = strlen (ptr);
if (len > 0) {
rest = copystring (ptr, len);
if (!rest) {
free (host);
return False;
}
}
}
*familyp = family;
*hostp = host;
*dpynump = dpynum;
*scrnump = scrnum;
*restp = rest;
return True;
}