home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / applications / wp / dvi2lj.lha / findfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-14  |  6.4 KB  |  337 lines

  1. #include "config.h"
  2.  
  3. #ifndef KPATHSEA
  4.  
  5. #include <string.h>
  6. #include <stdio.h>
  7. #ifndef vms
  8. # include <sys/types.h>
  9. # include <sys/stat.h>
  10. #else
  11. # include <types.h>
  12. # include <stat.h>
  13. #endif
  14.  
  15. int stat();
  16.  
  17. char *path_segment();
  18. char *pprint();
  19. extern void  Warning();
  20. extern bool  G_quiet;
  21. extern int   Debug;
  22.  
  23. #ifdef MAKETEXPK
  24. extern bool    makeTexPK ;
  25. #endif
  26.  
  27. #ifdef LJ
  28. # ifdef LJ4
  29. extern int     RESOLUTION;
  30. extern char   *MFMODE;
  31. # else
  32. #  define RESOLUTION 300
  33. # endif
  34. #endif
  35.  
  36. #ifdef unix
  37. # ifdef DO_SUBDIRECTORIES
  38. # include <glob.h>
  39. # endif
  40. #endif
  41.  
  42. #ifdef unix
  43.   static char* raster_table[] = {
  44. #ifdef USEPXL
  45.     "%P/%N.%Rpk",
  46.     "%P/%N.%Rpxl",
  47.     "%P/dpi%R/%N.pk",
  48.     "%P/%R/%N.pxl",
  49.     "%P/dpi%R/%N.pxl",
  50.     "%P/pxl%M/%N.pk",
  51.     "%P/pxl%M/%N.pxl",
  52.     "%P/%N.%R",
  53. #else
  54.     "%P/%N.%Rgf",
  55.     "%P/%N.%Mgf",
  56. #endif
  57.     NULL
  58.   };
  59. #endif
  60.  
  61. #ifdef vms
  62.   static char* raster_table[] = {
  63. #ifdef USEPXL
  64.     "%P[%R]%N.pk;",
  65.     "%P%N.%Rpk;",
  66.     "%P:[%R]%N.pk;",
  67.     "%P:%N.%Rpk;",
  68.     "%P[%R]%N.pk;",
  69.     "%P%N.%Mpxl;",
  70.     "%P:[%M]%N.pxl;",
  71.     "%P:%N.%Mpxl;",
  72. #else
  73.     "%P%N.%Rgf;",
  74.     "%P:%N.%Rgf;",
  75.     "%P%N.%Mgf;",
  76.     "%P:%N.%Mgf;",
  77. #endif
  78.     NULL
  79.   };
  80. #endif
  81.  
  82. #ifdef MSDOS
  83.   static char* raster_table[] = {
  84. #ifdef USEPXL
  85.     "%P/dpi%R/%N.pk",
  86.     "%P/%R/%N.pk",
  87.     "%P/dpi%R/%N.pxl",
  88.     "%P/%R/%N.pxl",
  89.     "%P/pxl%M/%N.pk",
  90.     "%P/pxl%M/%N.pxl",
  91.     "%P/%N.%R",
  92. #else
  93.     "%P/%R/%N.gf",
  94.     "%P/dpi%R/%N.gf",
  95.     "%P/pxl%M/%N.gf",
  96. #endif
  97.     NULL
  98.   };
  99. #endif
  100.  
  101. #ifdef _AMIGA
  102.   static char* raster_table[] = {
  103. #ifdef USEPXL
  104.     "%P/%N.%Rpk",
  105.     "%P/%N.%Rpxl",
  106.     "%P/dpi%R/%N.pk",
  107.     "%P/%R/%N.pxl",
  108.     "%P/dpi%R/%N.pxl",
  109.     "%P/pxl%M/%N.pk",
  110.     "%P/pxl%M/%N.pxl",
  111.     "%P/%R/%N.%Rpk",
  112.     "%P/%N.%R",
  113. #else
  114.     "%P/%N.%Rgf",
  115.     "%P/%N.%Mgf",
  116. #endif
  117.     NULL
  118.   };
  119. #endif
  120.  
  121.  
  122.  
  123.  
  124. bool
  125. findfile(path,n,fontmag,name,tfm,level)
  126. char path[STRSIZE];   /* PIXEL path */
  127. char n[STRSIZE];      /* name of font */
  128. long fontmag;         /* magnification */
  129. char name[STRSIZE];   /* full name of PXL file  (returned) */
  130. bool tfm;             /* are we searching for a tfm file? */
  131. int level;            /* recursion level to prevent calling of MakeTeXPK */ 
  132. {
  133.   char local_path[STRSIZE];
  134. #ifdef MAKETEXPK
  135.   char MakePKCommand[STRSIZE];
  136. #endif
  137.   char *pathpt;
  138.   struct stat s;
  139.   int resolution, i;
  140.   char **pattern;
  141.  
  142.   if (tfm) {
  143.     resolution = 0; /* dont'care */
  144. #ifdef DEBUG
  145.     if (Debug) 
  146.       fprintf(stderr, 
  147.           "locating tfm %s, path %s, depth=%d\n",
  148.           n, path, level);
  149. #endif
  150.   } else {
  151.     resolution = (int)(fontmag/5.0 + 0.5);
  152. #ifdef DEBUG
  153.     if (Debug) 
  154.       fprintf(stderr, 
  155.           "locating raster %s, resolution %d on path %s, depth=%d\n",
  156.           n, resolution, path, level);
  157. #endif
  158.   }
  159.  
  160.  
  161. #ifndef vms
  162.     for( i=0; (pathpt = path_segment(i, path, local_path)) != NULL; i++) {
  163.       if (tfm) {
  164.     if (!stat( pprint(name,"%P/%N.tfm",pathpt,n,resolution,fontmag), &s))
  165.       return(_TRUE);
  166.       } else {
  167.     for (pattern = raster_table; *pattern; pattern++) {
  168.       if (!stat( pprint(name,*pattern,pathpt,n,resolution,fontmag), &s))
  169.         return(_TRUE);
  170.     }
  171.       }
  172.     }
  173. #else
  174.   if (tfm) {
  175.     if ( !stat(pprint(name,"%P/%N.tfm",pathpt,n,resolution,fontmag), &s))
  176.       return(_TRUE);
  177.   } else {
  178.     for (pattern = raster_table; *pattern; pattern++) {
  179.       if (!stat( pprint(name,*pattern,path,n,resolution,fontmag), &s))
  180.     return(_TRUE);
  181.     }
  182.   }
  183. #endif
  184.  
  185. #ifdef FUTURE
  186.     for(i=0; (pathpt=path_segment((bool)(i==0),VFPATH,local_path))!=NULL;i++) {
  187.       sprintf(name,"%s/%s.vfm",pathpt,n);
  188.       printf("searching virtual font <%s>\n",name);
  189.       if (stat(name,&s) == 0) return(_TRUE);
  190.     }
  191. #endif
  192.  
  193. #ifdef DO_SUBDIRECTORIES
  194.   if (level < MAX_SUBDIR_SEARCH_DEPTH) {
  195.     for( i=0; (pathpt = path_segment(i, path, local_path)) != NULL; i++) {
  196.       glob_t globbuf;
  197.       char   glob_path[STRSIZE + 3];
  198.       int    i;
  199.       sprintf( glob_path, "%s/*", pathpt );
  200.       if (glob( glob_path, 0, NULL, &globbuf ) == 0) {
  201.     for (i = 0; i < globbuf.gl_pathc; i++) {
  202.       if ((stat( globbuf.gl_pathv[i], &s) == 0) && S_ISDIR(s.st_mode)) {
  203.         if (findfile(globbuf.gl_pathv[i],
  204.              n,
  205.              fontmag,
  206.              name,
  207.              tfm,
  208.              level+1 ) == _TRUE) {
  209.           globfree( &globbuf );
  210.           return(_TRUE);
  211.         }
  212.       }
  213.     }
  214.     globfree( &globbuf );
  215.       }
  216.     }
  217.   } else {
  218.     Warning("maximal searchdepth (%d) exceeded, path=%s",
  219.         MAX_SUBDIR_SEARCH_DEPTH, path);
  220.   }
  221. #endif
  222.  
  223. #ifdef MAKETEXPK
  224.   if (makeTexPK && level == 0 && !tfm) {
  225.     sprintf(MakePKCommand,"%s %s %d %d %.4f %s",MAKETEXPK,
  226.         n,resolution,RESOLUTION,
  227.         (float)((float)resolution/RESOLUTION),MFMODE);
  228.     if (!G_quiet) 
  229.       fprintf(stderr,"calling: %s\n",MakePKCommand);
  230.     system(MakePKCommand);
  231.     if (findfile(path,n,fontmag,name,tfm,1)) 
  232.       return(_TRUE);
  233.   }
  234. #endif
  235.  
  236.   if (level == 0) {
  237.     /* return error message */
  238.     if (tfm) {
  239.       sprintf(name, "tfm file not found: path=%s, name=%s", path,n);
  240.     } else {
  241.       sprintf(name,
  242. #ifdef USEPXL
  243.      "pk or pxl font not found: path=%s, name=%s, resolution=%d, magn=%ld",
  244. #else
  245.      "gf font not found: path=%s, name=%s, resolution=%d, magn=%ld",
  246. #endif
  247.       path,n,resolution,fontmag);
  248.     }
  249.   }
  250.   return(_FALSE);
  251. }
  252.  
  253.  
  254. char *
  255. pprint (buffer, input, path, name, resolution, fontmag)
  256. char *buffer;
  257. char *input;
  258. char *path;
  259. char *name;
  260. int  resolution;
  261. long fontmag;
  262. {
  263.   char *output = buffer;
  264.   
  265.   /*  fprintf(stderr, "pprint called with <%s>\n", input);*/
  266.   for (; *input != '\0'; input++)
  267.     {
  268.       if (*input == '%') {
  269.     switch (*(++input)) 
  270.       {
  271.       case 'P':
  272.         strcpy(output, path);
  273.         output += strlen(output);
  274.         break;
  275.  
  276.       case 'N':
  277.         strcpy(output, name);
  278.         output += strlen(output);
  279.         break;
  280.  
  281.       case 'R':
  282.         sprintf(output, "%d", resolution);
  283.         output += strlen(output);
  284.         break;
  285.  
  286.       case 'M':
  287. #ifdef vms
  288.         sprintf(output, "%d", fontmag);
  289. #else
  290.         sprintf(output, "%ld", fontmag);
  291. #endif
  292.         output += strlen(output);
  293.         break;
  294.  
  295.       default:
  296.         *output++ = '%';
  297.         *output++ = *input;
  298.         break;
  299.       }
  300.       } else {
  301.     *output++ = *input;
  302.       }
  303.     }
  304.   *output = '\0';
  305.   /* fprintf(stderr, "pprint returns <%s>\n", buffer); */
  306.   return buffer;
  307. }
  308.  
  309.  
  310. char *
  311. path_segment(index,full_path,local_path)
  312. int index;
  313. char *full_path, *local_path;
  314. {
  315.   static char *pppt;
  316.   char *pathpt;
  317.  
  318.   if (index == 0) pathpt = strcpy(local_path,full_path);
  319.   else pathpt = pppt;
  320.   if (pathpt != NULL) {
  321. #ifdef unix
  322.     pppt = strchr(pathpt , ':' );
  323. #else
  324.     pppt = strchr(pathpt , ';' );
  325. #endif
  326.     if (pppt != NULL) {
  327.       *pppt = '\0';
  328.       pppt++;
  329.     }
  330.   }
  331.   return pathpt;
  332. }
  333.  
  334. #endif
  335.  
  336.  
  337.