2 * $Xorg: parsedpy.c,v 1.4 2001/02/09 02:05:38 xorgcvs Exp $
5 * parse_displayname - utility routine for splitting up display name strings
8 Copyright 1989, 1998 The Open Group
10 Permission to use, copy, modify, distribute, and sell this software and its
11 documentation for any purpose is hereby granted without fee, provided that
12 the above copyright notice appear in all copies and that both that
13 copyright notice and this permission notice appear in supporting
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 Except as contained in this notice, the name of The Open Group shall not be
27 used in advertising or otherwise to promote the sale, use or other dealings
28 in this Software without prior written authorization from The Open Group.
30 * Author: Jim Fulton, MIT X Consortium
33 /* $XFree86: xc/programs/xauth/parsedpy.c,v 3.7 2003/07/09 15:27:37 tsi Exp $ */
39 #include <stdio.h> /* for NULL */
40 #include <ctype.h> /* for isascii() and isdigit() */
41 #include <X11/Xos.h> /* for strchr() and string routines */
42 #include <X11/Xlib.h> /* for Family contants */
44 #include <sys/utsname.h> /* for struct utsname */
46 #include <X11/Xauth.h> /* for FamilyLocal */
47 #include <X11/Xmu/SysUtil.h>
49 #if defined(UNIXCONN) || defined(LOCALCONN)
50 #define UNIX_CONNECTION "unix"
51 #define UNIX_CONNECTION_LENGTH 4
58 * private utility routines
62 copystring (char *src, int len)
66 if (!src && len != 0) return NULL;
67 cp = malloc (len + 1);
69 if (src) strncpy (cp, src, len);
77 get_local_hostname (char *buf, int maxlen)
80 (void) XmuGetHostname (buf, maxlen);
81 return (buf[0] ? buf : NULL);
90 return (get_local_hostname (buf, sizeof buf) ?
91 copystring (buf, strlen (buf)) : NULL);
96 * parse_displayname - display a display string up into its component parts
99 parse_displayname (char *displayname,
100 int *familyp, /* return */
101 char **hostp, /* return */
102 int *dpynump, /* return */
103 int *scrnump, /* return */
104 char **restp) /* return */
106 char *ptr; /* work variables */
107 int len; /* work variable */
108 int family = -1; /* value to be returned */
109 char *host = NULL; /* must free if set and error return */
110 int dpynum = -1; /* value to be returned */
111 int scrnum = 0; /* value to be returned */
112 char *rest = NULL; /* must free if set and error return */
113 Bool dnet = False; /* if true then using DECnet */
116 if (!displayname || !displayname[0]) return False;
118 /* must have at least :number */
119 ptr = strrchr(displayname, ':');
120 if (!ptr || !ptr[1]) return False;
121 if ((ptr != displayname) && (*(ptr - 1) == ':')) {
128 * get the host string; if none is given, use the most effiecient path
131 len = (ptr - displayname); /* length of host name */
132 if (len == 0) { /* choose most efficient path */
133 #if defined(UNIXCONN) || defined(LOCALCONN)
134 host = copystring (UNIX_CONNECTION, UNIX_CONNECTION_LENGTH);
135 family = FamilyLocal;
138 host = copystring ("0", 1);
139 family = FamilyDECnet;
141 host = copyhostname ();
142 family = FamilyInternet;
145 } else if (!dnet && (*displayname == '[') && (*(ptr - 1) == ']')) {
146 /* Allow RFC2732-like [<IPv6NumericAddress>]:display syntax */
147 family = FamilyInternet6;
148 host = copystring (displayname + 1, len - 2);
150 host = copystring (displayname, len);
154 #if defined(UNIXCONN) || defined(LOCALCONN)
155 if (host && strcmp (host, UNIX_CONNECTION) == 0)
156 family = FamilyLocal;
159 family = FamilyInternet;
163 if (!host) return False;
165 if(strncmp (host, "/tmp/launch", 11) == 0) {
166 family = FamilyLocal;
170 * get the display number; we know that there is something after the
171 * colon (or colons) from above. note that host is now set and must
172 * be freed if there is an error.
175 if (dnet) ptr++; /* skip the extra DECnet colon */
176 ptr++; /* move to start of display num */
180 for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
182 /* check present and valid follow */
183 if (len == 0 || (*cp && *cp != '.')) {
188 dpynum = atoi (ptr); /* it will handle num. as well */
193 * now get screen number if given; ptr may point to nul at this point
199 for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
201 if (len == 0 || (*cp && *cp != '.')) { /* all prop name */
206 scrnum = atoi (ptr); /* it will handle num. as well */
211 * and finally, get any additional stuff that might be following the
212 * the screen number; ptr must point to a period if there is anything
219 rest = copystring (ptr, len);