home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / stex2-18.zip / SeeTeX / Xtex / fontpath.c < prev    next >
C/C++ Source or Header  |  1992-06-23  |  3KB  |  137 lines

  1. #include <stdio.h>
  2. #include <sys/param.h>
  3. #include <ctype.h>
  4. #include <X11/Xlib.h>
  5.  
  6. /* X toolkit header files */
  7.  
  8. #include <X11/Intrinsic.h>
  9. #include <X11/StringDefs.h>
  10. #include <X11/Shell.h>
  11.  
  12. /* Widget header files. */
  13.  
  14. /*
  15.  * Insure that a single directory is in the font path
  16.  *
  17.  * Made changes to take care of duplication of directories caused by :
  18.  *    (a) Presence/absence of ending slahes in path components
  19.  *    (b) Confusion due to links
  20.  * Shankar Ramamoorthy 04 / 07 / 92
  21.  */
  22.  
  23. #ifdef __hpux
  24. #define getwd(d) getcwd(d, MAXPATHLEN)
  25. #endif
  26.  
  27. extern char *ProgName;
  28.  
  29. static int
  30. fontPathInsureSingleDirectory(dpy, path)
  31.      Display *dpy;
  32.      char *path;
  33. {
  34.   int ncurrent;
  35.   int i;
  36.   char currentDir[MAXPATHLEN], absPath[MAXPATHLEN], 
  37.       fpCompAbsPath[MAXPATHLEN];
  38.   char **currentList = XGetFontPath (dpy, &ncurrent);
  39.   if (!currentList) {
  40.     fprintf (stderr, "%s:  unable to get old font path.\n",
  41.          ProgName);
  42.     return(0);
  43.   }
  44.   
  45.   /* Get the current working directory and save it */
  46.   (void) getwd (currentDir);
  47.  
  48.   /* Get the absolute pathname of directory to check */
  49.   if (chdir (path) == 0)
  50.     (void) getwd (absPath);
  51.   else
  52.     {
  53.     /* Unable to change to directory */
  54.     XFreeFontPath (currentList);
  55.     return (0);
  56.     }
  57.  
  58.   for ( i= 0; i < ncurrent; i++ ) {
  59.     /* Change to next component on fontpath */
  60.     if (chdir (currentList[i]) == 0) {
  61.       (void) getwd (fpCompAbsPath);
  62.       if (strcmp (fpCompAbsPath, absPath) == 0) {
  63.     /* Its there.
  64.      * Restore the current working directory and return.
  65.      */
  66.         chdir (currentDir);
  67.         XFreeFontPath (currentList);
  68.         return (1);
  69.       }
  70.     }
  71.   }
  72.  
  73.   /* Restore current working directory */
  74.   chdir (currentDir);
  75.   
  76.   /* Wasn't found, so install it at the beginning */
  77.   
  78.   {
  79.     char **newList = (char **) malloc( sizeof( char *) * (ncurrent + 1) );
  80.  
  81.     for (i = 0 ; i < ncurrent; i++ ) {
  82.       newList[i] = currentList[i];
  83.     }
  84.     newList[ncurrent] = path;
  85.  
  86.     XSetFontPath(dpy, newList, ncurrent + 1);
  87.     free( newList );
  88.     XFreeFontPath( currentList );
  89.   }
  90. }
  91.  
  92. fontPathInsurePaths(dpy, stringOfPaths)
  93.      Display *dpy;
  94.      char *stringOfPaths;
  95. {
  96.  
  97.   /* make a copy of stringOfPaths, because we modify it */
  98.  
  99.   int len = strlen( stringOfPaths );
  100.  
  101.   if (len > 0) {
  102.     char *newString = (char *) malloc( sizeof(char ) * (len + 1) );
  103.     char *start, *end;
  104.     int done = FALSE;
  105.  
  106.     strcpy(newString, stringOfPaths);
  107.  
  108.     start = end = newString;
  109.  
  110.     done = (start == 0 || *start == 0);
  111.  
  112.     while ( !done ) {
  113.  
  114.       int atEnd;
  115.  
  116.       while ( start && isspace(*start) ) start++;
  117.       end = start;
  118.       while ( end && *end != 0 && ! (*end == ',' || *end == ':') ) end++;
  119.       done = (*end == 0);
  120.  
  121.       /* terminate string, check it's non-null and shove to X server */
  122.  
  123.       *end = 0;
  124.       if ( strlen(start) >  0) {
  125.     fontPathInsureSingleDirectory( dpy, start );
  126.       }
  127.  
  128.       start = end + 1;
  129.  
  130.     }
  131.  
  132.     /* delete copy we allocated */
  133.  
  134.     free( newString );
  135.   }
  136. }
  137.