Upload Tizen:Main source
[pkgs/xorg/util/xorg-x11-xauth.git] / parsedpy.c
1 /*
2  * $Xorg: parsedpy.c,v 1.4 2001/02/09 02:05:38 xorgcvs Exp $
3  * $XdotOrg: $
4  *
5  * parse_displayname - utility routine for splitting up display name strings
6  *
7  * 
8 Copyright 1989, 1998  The Open Group
9
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
14 documentation.
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
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.
25
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.
29  * *
30  * Author:  Jim Fulton, MIT X Consortium
31  */
32
33 /* $XFree86: xc/programs/xauth/parsedpy.c,v 3.7 2003/07/09 15:27:37 tsi Exp $ */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
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 */
43 #ifdef hpux
44 #include <sys/utsname.h>                /* for struct utsname */
45 #endif
46 #include <X11/Xauth.h>                  /* for FamilyLocal */
47 #include <X11/Xmu/SysUtil.h>
48
49 #if defined(UNIXCONN) || defined(LOCALCONN)
50 #define UNIX_CONNECTION "unix"
51 #define UNIX_CONNECTION_LENGTH 4
52 #endif
53
54 #include <stdlib.h>
55 #include "xauth.h"
56
57 /*
58  * private utility routines
59  */
60
61 char *
62 copystring (char *src, int len)
63 {
64     char *cp;
65
66     if (!src && len != 0) return NULL;
67     cp = malloc (len + 1);
68     if (cp) {
69         if (src) strncpy (cp, src, len);
70         cp[len] = '\0';
71     }
72     return cp;
73 }
74
75
76 char *
77 get_local_hostname (char *buf, int maxlen)
78 {
79     buf[0] = '\0';
80     (void) XmuGetHostname (buf, maxlen);
81     return (buf[0] ? buf : NULL);
82 }
83
84 #ifndef UNIXCONN
85 static char *
86 copyhostname (void)
87 {
88     char buf[256];
89
90     return (get_local_hostname (buf, sizeof buf) ? 
91             copystring (buf, strlen (buf)) : NULL);
92 }
93 #endif
94
95 /*
96  * parse_displayname - display a display string up into its component parts
97  */
98 Bool 
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 */
105 {
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 */
114
115                                         /* check the name */
116     if (!displayname || !displayname[0]) return False;
117
118                                         /* must have at least :number */
119     ptr = strrchr(displayname, ':');
120     if (!ptr || !ptr[1]) return False;
121     if ((ptr != displayname) && (*(ptr - 1) == ':')) {
122         ptr--;
123         dnet = True;
124     }
125
126
127     /*
128      * get the host string; if none is given, use the most effiecient path
129      */
130
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;
136 #else
137         if (dnet) {
138             host = copystring ("0", 1);
139             family = FamilyDECnet;
140         } else {
141             host = copyhostname ();
142             family = FamilyInternet;
143         }
144 #endif
145     } else if (!dnet && (*displayname == '[') && (*(ptr - 1) == ']')) {
146         /* Allow RFC2732-like [<IPv6NumericAddress>]:display syntax */
147         family = FamilyInternet6;
148         host = copystring (displayname + 1, len - 2);
149     } else {
150         host = copystring (displayname, len);
151         if (dnet) {
152             family = dnet;
153         } else {
154 #if defined(UNIXCONN) || defined(LOCALCONN)
155             if (host && strcmp (host, UNIX_CONNECTION) == 0)
156               family = FamilyLocal;
157             else
158 #endif
159               family = FamilyInternet;
160         }
161     }
162
163     if (!host) return False;
164
165     if(strncmp (host, "/tmp/launch", 11) == 0) {
166         family = FamilyLocal;
167     }
168
169     /*
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.
173      */
174
175     if (dnet) ptr++;                    /* skip the extra DECnet colon */
176     ptr++;                              /* move to start of display num */
177     {
178         register char *cp;
179
180         for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
181         len = (cp - ptr);
182                                         /* check present and valid follow */
183         if (len == 0 || (*cp && *cp != '.')) {
184             free (host);
185             return False;
186         }
187         
188         dpynum = atoi (ptr);            /* it will handle num. as well */
189         ptr = cp;
190     }
191
192     /*
193      * now get screen number if given; ptr may point to nul at this point
194      */
195     if (ptr[0] == '.') {
196         register char *cp;
197
198         ptr++;
199         for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
200         len = (cp - ptr);
201         if (len == 0 || (*cp && *cp != '.')) {  /* all prop name */
202             free (host);
203             return False;
204         }
205
206         scrnum = atoi (ptr);            /* it will handle num. as well */
207         ptr = cp;
208     }
209
210     /*
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
213      */
214
215     if (ptr[0] == '.') {
216         ptr++;
217         len = strlen (ptr);
218         if (len > 0) {
219             rest = copystring (ptr, len);
220             if (!rest) {
221                 free (host);
222                 return False;
223             }
224         }
225     }
226
227     /*
228      * and we are done!
229      */
230
231     *familyp = family;
232     *hostp = host;
233     *dpynump = dpynum;
234     *scrnump = scrnum;
235     *restp = rest;
236     return True;
237 }
238
239