home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xauth / parsedpy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-10  |  5.6 KB  |  222 lines

  1. /*
  2.  * $XConsortium: parsedpy.c,v 1.7 89/12/10 17:00:56 rws Exp $
  3.  *
  4.  * parse_displayname - utility routine for splitting up display name strings
  5.  *
  6.  * Copyright 1989 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising
  13.  * or publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  20.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  21.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  22.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * Author:  Jim Fulton, MIT X Consortium
  26.  */
  27.  
  28. #include <stdio.h>            /* for NULL */
  29. #include <ctype.h>            /* for isascii() and isdigit() */
  30. #include <X11/Xos.h>            /* for index() and string routines */
  31. #include <X11/Xlib.h>            /* for Family contants */
  32. #ifdef hpux
  33. #include <sys/utsname.h>        /* for struct utsname */
  34. #endif
  35. #include <X11/Xauth.h>            /* for FamilyLocal */
  36. #include <X11/Xmu/SysUtil.h>
  37.  
  38. #ifdef UNIXCONN
  39. #define UNIX_CONNECTION "unix"
  40. #define UNIX_CONNECTION_LENGTH 4
  41. #endif
  42.  
  43. extern char *malloc();
  44.  
  45.  
  46. /*
  47.  * private utility routines
  48.  */
  49.  
  50. /*static*/ char *copystring (src, len)
  51.     char *src;
  52.     int len;
  53. {
  54.     char *cp;
  55.  
  56.     if (!src && len != 0) return NULL;
  57.     cp = malloc (len + 1);
  58.     if (cp) {
  59.     if (src) strncpy (cp, src, len);
  60.     cp[len] = '\0';
  61.     }
  62.     return cp;
  63. }
  64.  
  65.  
  66. char *get_local_hostname (buf, maxlen)
  67.     char *buf;
  68.     int maxlen;
  69. {
  70.     buf[0] = '\0';
  71.     (void) XmuGetHostname (buf, maxlen);
  72.     return (buf[0] ? buf : NULL);
  73. }
  74.  
  75. #ifndef UNIXCONN
  76. static char *copyhostname ()
  77. {
  78.     char buf[256];
  79.  
  80.     return (get_local_hostname (buf, sizeof buf) ? 
  81.         copystring (buf, strlen (buf)) : NULL);
  82. }
  83. #endif
  84.  
  85. /*
  86.  * parse_displayname - display a display string up into its component parts
  87.  */
  88. Bool parse_displayname (displayname, familyp, hostp, dpynump, scrnump, restp)
  89.     char *displayname;
  90.     int *familyp;            /* return */
  91.     char **hostp;            /* return */
  92.     int *dpynump, *scrnump;        /* return */
  93.     char **restp;            /* return */
  94. {
  95.     char *ptr;                /* work variables */
  96.     int len;                /* work variable */
  97.     int family = -1;            /* value to be returned */
  98.     char *host = NULL;            /* must free if set and error return */
  99.     int dpynum = -1;            /* value to be returned */
  100.     int scrnum = 0;            /* value to be returned */
  101.     char *rest = NULL;            /* must free if set and error return */
  102.     Bool dnet = False;            /* if true then using DECnet */
  103.  
  104.                     /* check the name */
  105.     if (!displayname || !displayname[0]) return False;
  106.  
  107.                     /* must have at least :number */
  108.     ptr = index (displayname, ':');
  109.     if (!ptr || !ptr[1]) return False;
  110.     if (ptr[1] == ':') {
  111.     if (ptr[2] == '\0') return False;
  112.     dnet = True;
  113.     }
  114.  
  115.  
  116.     /*
  117.      * get the host string; if none is given, use the most effiecient path
  118.      */
  119.  
  120.     len = (ptr - displayname);    /* length of host name */
  121.     if (len == 0) {            /* choose most efficient path */
  122. #ifdef UNIXCONN
  123.     host = copystring (UNIX_CONNECTION, UNIX_CONNECTION_LENGTH);
  124.     family = FamilyLocal;
  125. #else
  126.     if (dnet) {
  127.         host = copystring ("0", 1);
  128.         family = FamilyDECnet;
  129.     } else {
  130.         host = copyhostname ();
  131.         family = FamilyInternet;
  132.     }
  133. #endif
  134.     } else {
  135.     host = copystring (displayname, len);
  136.     if (dnet) {
  137.         family = dnet;
  138.     } else {
  139. #ifdef UNIXCONN
  140.         if (host && strcmp (host, UNIX_CONNECTION) == 0)
  141.           family = FamilyLocal;
  142.         else
  143. #endif
  144.           family = FamilyInternet;
  145.     }
  146.     }
  147.  
  148.     if (!host) return False;
  149.  
  150.  
  151.     /*
  152.      * get the display number; we know that there is something after the
  153.      * colon (or colons) from above.  note that host is now set and must
  154.      * be freed if there is an error.
  155.      */
  156.  
  157.     if (dnet) ptr++;            /* skip the extra DECnet colon */
  158.     ptr++;                /* move to start of display num */
  159.     {
  160.     register char *cp;
  161.  
  162.     for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
  163.     len = (cp - ptr);
  164.                     /* check present and valid follow */
  165.     if (len == 0 || (*cp && *cp != '.')) {
  166.         free (host);
  167.         return False;
  168.     }
  169.     
  170.     dpynum = atoi (ptr);        /* it will handle num. as well */
  171.     ptr = cp;
  172.     }
  173.  
  174.     /*
  175.      * now get screen number if given; ptr may point to nul at this point
  176.      */
  177.     if (ptr[0] == '.') {
  178.     register char *cp;
  179.  
  180.     ptr++;
  181.     for (cp = ptr; *cp && isascii(*cp) && isdigit(*cp); cp++) ;
  182.     len = (cp - ptr);
  183.     if (len == 0 || (*cp && *cp != '.')) {    /* all prop name */
  184.         free (host);
  185.         return False;
  186.     }
  187.  
  188.     scrnum = atoi (ptr);        /* it will handle num. as well */
  189.     ptr = cp;
  190.     }
  191.  
  192.     /*
  193.      * and finally, get any additional stuff that might be following the
  194.      * the screen number; ptr must point to a period if there is anything
  195.      */
  196.  
  197.     if (ptr[0] == '.') {
  198.     ptr++;
  199.     len = strlen (ptr);
  200.     if (len > 0) {
  201.         rest = copystring (ptr, len);
  202.         if (!rest) {
  203.         free (host);
  204.         return False;
  205.         }
  206.     }
  207.     }
  208.  
  209.     /*
  210.      * and we are done!
  211.      */
  212.  
  213.     *familyp = family;
  214.     *hostp = host;
  215.     *dpynump = dpynum;
  216.     *scrnump = scrnum;
  217.     *restp = rest;
  218.     return True;
  219. }
  220.  
  221.         
  222.