home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xdm / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  5.0 KB  |  246 lines

  1. /*
  2.  * xdm - display manager daemon
  3.  *
  4.  * $XConsortium: file.c,v 1.15 91/02/13 19:13:21 rws Exp $
  5.  *
  6.  * Copyright 1988 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 or
  13.  * 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.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. /*
  22.  * file.c
  23.  */
  24.  
  25. # include    "dm.h"
  26. # include    <ctype.h>
  27.  
  28. DisplayTypeMatch (d1, d2)
  29. DisplayType    d1, d2;
  30. {
  31.     return d1.location == d2.location &&
  32.            d1.lifetime == d2.lifetime &&
  33.            d1.origin == d2.origin;
  34. }
  35.  
  36. static void
  37. freeArgs (args)
  38.     char    **args;
  39. {
  40.     char    **a;
  41.  
  42.     for (a = args; *a; a++)
  43.     free (*a);
  44.     free ((char *) args);
  45. }
  46.  
  47. static char **
  48. splitIntoWords (s)
  49.     char    *s;
  50. {
  51.     char    **args, **newargs;
  52.     char    *wordStart;
  53.     int        nargs;
  54.  
  55.     args = 0;
  56.     nargs = 0;
  57.     while (*s)
  58.     {
  59.     while (*s && isspace (*s))
  60.         ++s;
  61.     if (!*s || *s == '#')
  62.         break;
  63.     wordStart = s;
  64.     while (*s && *s != '#' && !isspace (*s))
  65.         ++s;
  66.     if (!args)
  67.     {
  68.             args = (char **) malloc (2 * sizeof (char *));
  69.             if (!args)
  70.             return NULL;
  71.     }
  72.     else
  73.     {
  74.         newargs = (char **) realloc ((char *) args,
  75.                      (nargs+2)*sizeof (char *));
  76.         if (!newargs)
  77.         {
  78.             freeArgs (args);
  79.             return NULL;
  80.         }
  81.         args = newargs;
  82.     }
  83.     args[nargs] = malloc (s - wordStart + 1);
  84.     if (!args[nargs])
  85.     {
  86.         freeArgs (args);
  87.         return NULL;
  88.     }
  89.     strncpy (args[nargs], wordStart, s - wordStart);
  90.     args[nargs][s-wordStart] = '\0';
  91.     ++nargs;
  92.     args[nargs] = NULL;
  93.     }
  94.     return args;
  95. }
  96.  
  97. static char **
  98. copyArgs (args)
  99.     char    **args;
  100. {
  101.     char    **a, **new, **n;
  102.  
  103.     for (a = args; *a; a++)
  104.     /* SUPPRESS 530 */
  105.     ;
  106.     new = (char **) malloc ((a - args + 1) * sizeof (char *));
  107.     if (!new)
  108.     return NULL;
  109.     n = new;
  110.     a = args;
  111.     /* SUPPRESS 560 */
  112.     while (*n++ = *a++)
  113.     /* SUPPRESS 530 */
  114.     ;
  115.     return new;
  116. }
  117.  
  118. freeSomeArgs (args, n)
  119.     char    **args;
  120.     int        n;
  121. {
  122.     char    **a;
  123.  
  124.     a = args;
  125.     while (n--)
  126.     free (*a++);
  127.     free ((char *) args);
  128. }
  129.  
  130. ParseDisplay (source, acceptableTypes, numAcceptable)
  131. char        *source;
  132. DisplayType    *acceptableTypes;
  133. int        numAcceptable;
  134. {
  135.     char        **args, **argv, **a;
  136.     char        *name, *class, *type;
  137.     struct display    *d;
  138.     int            usedDefault;
  139.     DisplayType        displayType;
  140.  
  141.     args = splitIntoWords (source);
  142.     if (!args)
  143.     return;
  144.     if (!args[0])
  145.     {
  146.     LogError ("Missing display name in servers file\n");
  147.     freeArgs (args);
  148.     return;
  149.     }
  150.     name = args[0];
  151.     if (!args[1])
  152.     {
  153.     LogError ("Missing display type for %s\n", args[0]);
  154.     freeArgs (args);
  155.     return;
  156.     }
  157.     displayType = parseDisplayType (args[1], &usedDefault);
  158.     class = NULL;
  159.     type = args[1];
  160.     argv = args + 2;
  161.     /*
  162.      * extended syntax; if the second argument doesn't
  163.      * exactly match a legal display type and the third
  164.      * argument does, use the second argument as the
  165.      * display class string
  166.      */
  167.     if (usedDefault && args[2])
  168.     {
  169.     displayType = parseDisplayType (args[2], &usedDefault);
  170.     if (!usedDefault)
  171.     {
  172.         class = args[1];
  173.         type = args[2];
  174.         argv = args + 3;
  175.     }
  176.     }
  177.     while (numAcceptable)
  178.     {
  179.     if (DisplayTypeMatch (*acceptableTypes, displayType))
  180.         break;
  181.     --numAcceptable;
  182.     ++acceptableTypes;
  183.     }
  184.     if (!numAcceptable)
  185.     {
  186.     LogError ("Unacceptable display type %s for display %s\n",
  187.           type, name);
  188.     }
  189.     d = FindDisplayByName (name);
  190.     if (d)
  191.     {
  192.     d->state = OldEntry;
  193.     if (class && strcmp (d->class, class))
  194.     {
  195.         char    *newclass;
  196.  
  197.         newclass = malloc ((unsigned) (strlen (class) + 1));
  198.         if (newclass)
  199.         {
  200.         free (d->class);
  201.         strcpy (newclass, class);
  202.         d->class = newclass;
  203.         }
  204.     }
  205.     Debug ("Found existing display:  %s %s %s", d->name, d->class, type);
  206.     freeArgs (d->argv);
  207.     }
  208.     else
  209.     {
  210.     d = NewDisplay (name, class);
  211.     Debug ("Found new display:  %s %s %s", d->name, d->class, type);
  212.     }
  213.     d->displayType = displayType;
  214.     d->argv = copyArgs (argv);
  215.     for (a = d->argv; a && *a; a++)
  216.     Debug (" %s", *a);
  217.     Debug ("\n");
  218.     freeSomeArgs (args, argv - args);
  219. }
  220.  
  221. static struct displayMatch {
  222.     char        *name;
  223.     DisplayType    type;
  224. } displayTypes[] = {
  225.     "local",        { Local, Permanent, FromFile },
  226.     "foreign",        { Foreign, Permanent, FromFile },
  227.     0,            { Local, Permanent, FromFile },
  228. };
  229.  
  230. DisplayType
  231. parseDisplayType (string, usedDefault)
  232.     char    *string;
  233.     int    *usedDefault;
  234. {
  235.     struct displayMatch    *d;
  236.  
  237.     for (d = displayTypes; d->name; d++)
  238.         if (!strcmp (d->name, string))
  239.         {
  240.             *usedDefault = 0;
  241.             return d->type;
  242.         }
  243.     *usedDefault = 1;
  244.     return d->type;
  245. }
  246.