home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 3.3J / os33j.iso / NextLibrary / TeX / tex / src / dvips / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-27  |  9.3 KB  |  336 lines

  1. /*
  2.  *   The search routine takes a directory list, separated by PATHSEP, and
  3.  *   tries to open a file.  Null directory components indicate current
  4.  *   directory. if the file SUBDIR exists and the file is a font file,
  5.  *   it checks for the file in a subdirectory named the same as the font name.
  6.  *   Returns the open file descriptor if ok, else NULL.
  7.  */
  8. #include "dvips.h" /* The copyright notice in that file is included too! */
  9. #include <ctype.h>
  10.  
  11. #if defined(SYSV) || defined(VMS) || defined(__THINK__) || defined(MSDOS)
  12. #define MAXPATHLEN (256)
  13. #else
  14. #include <sys/param.h>          /* for MAXPATHLEN */
  15. #endif
  16. #ifndef MSDOS
  17. #ifndef VMS
  18. #ifndef VMCMS /* IBM: VM/CMS */
  19. #ifndef __THINK__
  20. #include <pwd.h>
  21. #endif
  22. #endif  /* IBM: VM/CMS */
  23. #endif
  24. #endif
  25. /*
  26.  *
  27.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  28.  */
  29.  
  30. #ifdef DEBUG
  31. extern integer debug_flag;
  32. #endif  /* DEBUG */
  33. extern char *mfmode ;
  34. extern int actualdpi ;
  35. char realnameoffile[MAXPATHLEN] ;
  36. FILE *
  37. search(path, file, mode)
  38.         char *path, *file, *mode ;
  39. {
  40.    extern char *getenv(), *newstring() ;
  41.    register char *nam ;                 /* index into fname */
  42.    register FILE *fd ;                  /* file desc of file */
  43.    char fname[MAXPATHLEN] ;             /* to store file name */
  44.    static char *home = 0 ;              /* home is where the heart is */
  45. #ifdef VMCMS /* IBM: VM/CMS - we don't have paths or dirsep's but we strip off
  46.                              filename if there is a Unix path dirsep    */
  47.    register char *lastdirsep ;
  48.    lastdirsep = strrchr(file, '/') ;
  49.    if ( NULL != lastdirsep ) file = lastdirsep + 1 ;
  50.    if ((fd=fopen(file,mode)) != NULL) {
  51.       return(fd) ;
  52.    } else {
  53.       return(NULL) ;
  54.    }
  55. #else
  56.    if (*file == DIRSEP) {               /* if full path name */
  57.       if ((fd=fopen(file,mode)) != NULL) {
  58.          strcpy(realnameoffile, file) ;
  59.          return(fd) ;
  60.       } else
  61.          return(NULL) ;
  62.    }
  63. #endif   /* IBM: VM/CMS */
  64.  
  65. #ifdef MSDOS
  66.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  67.       if ((fd=fopen(file,mode)) != NULL)
  68.          return(fd) ;
  69.       else
  70.          return(NULL) ;
  71.    }
  72. #endif
  73.  
  74.    do {
  75.       /* copy the current directory into fname */
  76.       nam = fname;
  77.       /* copy till PATHSEP */
  78.       if (*path == '~') {
  79.          char *p = nam ;
  80.          path++ ;
  81.          while (*path && *path != PATHSEP && *path != DIRSEP)
  82.             *p++ = *path++ ;
  83.          *p = 0 ;
  84.          if (*nam == 0) {
  85.             if (home == 0) {
  86.                if (home = getenv("HOME"))
  87.                   home = newstring(home) ;
  88.                else
  89.                   home = "." ;
  90.             }
  91.             strcpy(fname, home) ;
  92.          } else {
  93. #ifdef MSDOS
  94.             error("! ~username in path???") ;
  95. #else
  96. #ifdef VMS
  97.             error("! ~username in path???") ;
  98. #else
  99. #ifdef VMCMS  /* IBM: VM/CMS */
  100.             error("! ~username in path???") ;
  101. #else
  102. #ifdef MVSXA  /* IBM: MVS/XA */
  103.             error("! ~username in path???") ;
  104. #else
  105. #ifdef __THINK__
  106.             error("! ~username in path???") ;
  107. #else
  108.             struct passwd *pw = getpwnam(fname) ;
  109.             if (pw)
  110.                strcpy(fname, pw->pw_dir) ;
  111.             else
  112.                error("no such user") ;
  113. #endif
  114. #endif  /* IBM: VM/CMS */
  115. #endif
  116. #endif
  117. #endif
  118.          }
  119.          nam = fname + strlen(fname) ;
  120.       }
  121.       while (*path != PATHSEP && *path) *nam++ = *path++;
  122.       *nam = 0 ;
  123. #ifndef VMS
  124. #ifndef __THINK__
  125.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  126.  
  127.       if (*file != '\0') {
  128.          if ((nam != fname) && *(nam-1) != DIRSEP) /* GNW 1992.07.09 */
  129.             *nam++ = DIRSEP;                  /* add separator */
  130.          (void)strcpy(nam,file);                   /* tack the file on */
  131.       }
  132.       else
  133.          *nam = '\0' ;
  134. #else
  135.       (void)strcpy(nam,file);                   /* tack the file on */
  136. #endif
  137. #else
  138.       (void)strcpy(nam,file);                   /* tack the file on */
  139. #endif
  140. #ifdef MVSXA
  141.       if (fname[0] == '/') {
  142.          fname[0] = '\'';
  143.          strcat(&fname[strlen(fname)],"\'");
  144.       }
  145.       if (fname[0] == '.') fname[0] = ' ';
  146.       if (fname[1] == '.') fname[1] = ' ';
  147. #endif
  148.  
  149.       /* belated check -- bah! */
  150.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  151.          error("! overran allocated storage in search()");
  152.  
  153. #ifdef DEBUG
  154.       if (dd(D_PATHS))
  155.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  156. #endif
  157.       if ((fd=fopen(fname,mode)) != NULL) {
  158.          strcpy(realnameoffile, fname) ;
  159.          return(fd);
  160.       }
  161.  
  162.    /* skip over PATHSEP and try again */
  163.    } while (*(path++));
  164.  
  165.    return(NULL);
  166.  
  167. }               /* end search */
  168.  
  169. FILE *
  170. pksearch(path, file, mode, n, dpi, vdpi)
  171.         char *path, *file, *mode ;
  172.     char *n ;
  173.     halfword dpi, vdpi ;
  174. {
  175.    extern char *getenv(), *newstring() ;
  176.    register char *nam ;                 /* index into fname */
  177.    register FILE *fd ;                  /* file desc of file */
  178.    char fname[MAXPATHLEN] ;             /* to store file name */
  179.    static char *home = 0 ;              /* home is where the heart is */
  180.    int sub ;
  181.  
  182.    if (*file == DIRSEP) {               /* if full path name */
  183.       if ((fd=fopen(file,mode)) != NULL)
  184.          return(fd) ;
  185.       else
  186.          return(NULL) ;
  187.    }
  188. #ifdef MSDOS
  189.    if ( isalpha(file[0]) && file[1]==':' ) {  /* if full path name */
  190.       if ((fd=fopen(file,mode)) != NULL)
  191.          return(fd) ;
  192.       else
  193.          return(NULL) ;
  194.    }
  195. #endif
  196.    do {
  197.       /* copy the current directory into fname */
  198.       nam = fname;
  199.       sub = 0 ;
  200.       /* copy till PATHSEP */
  201.       if (*path == '~') {
  202.          char *p = nam ;
  203.          path++ ;
  204.          while (*path && *path != PATHSEP && *path != DIRSEP)
  205.             *p++ = *path++ ;
  206.          *p = 0 ;
  207.          if (*nam == 0) {
  208.             if (home == 0) {
  209.                if (home = getenv("HOME"))
  210.                   home = newstring(home) ;
  211.                else
  212.                   home = "." ;
  213.             }
  214.             strcpy(fname, home) ;
  215.          } else {
  216. #ifdef MSDOS
  217.             error("! ~username in path???") ;
  218. #else
  219. #ifdef VMS
  220.             error("! ~username in path???") ;
  221. #else
  222. #ifdef VMCMS  /* IBM: VM/CMS */
  223.             error("! ~username in path???") ;
  224. #else
  225. #ifdef MVSXA  /* IBM: MVS/XA */
  226.             error("! ~username in path???") ;
  227. #else
  228. #ifdef __THINK__
  229.             error("! ~username in path???") ;
  230. #else
  231.             struct passwd *pw = getpwnam(fname) ;
  232.             if (pw)
  233.                strcpy(fname, pw->pw_dir) ;
  234.             else
  235.                error("no such user") ;
  236. #endif
  237. #endif /* IBM: VM/CMS */
  238. #endif
  239. #endif
  240. #endif
  241.          }
  242.          nam = fname + strlen(fname) ;
  243.       }
  244.       /* copy till PATHSEP */
  245.       while (*path != PATHSEP && *path) {
  246.          if (*path == '%') {
  247.             sub = 1 ;
  248.             path++ ;
  249.             switch(*path) {
  250.                case 'b': sprintf(nam, "%d", actualdpi) ; break ;
  251.                case 'd': sprintf(nam, "%d", dpi) ; break ;
  252.                case 'f': strcpy(nam, n) ; break ;
  253.                case 'm': if (mfmode == 0)
  254.                             if (actualdpi == 300) mfmode = "imagen" ;
  255.                             else if (actualdpi == 400) mfmode = "nexthi" ;
  256.                             else if (actualdpi == 635) mfmode = "linolo" ;
  257.                             else if (actualdpi == 1270) mfmode = "linohi" ;
  258.                             else if (actualdpi == 2540) mfmode = "linosuper" ;
  259.                          if (mfmode == 0)
  260.                             error("! MF mode not set, but used in pk path") ;
  261.                          strcpy(nam, mfmode) ;
  262.                          break ;
  263.                case 'p': strcpy(nam, "pk") ; break ;
  264.                case '%': strcpy(nam, "%") ; break ;
  265.                default: error("! bad format character in pk path") ;
  266.             }
  267.             nam = fname + strlen(fname) ;
  268.             if (*path)
  269.                path++ ;
  270.          } else
  271.             *nam++ = *path++;
  272.       }
  273. #ifndef VMS
  274. #ifndef __THINK__
  275.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  276. #endif
  277. #endif /* VMS */
  278.       if (sub == 0 && *file) {
  279.          if ((nam != fname) && *(nam-1) != DIRSEP) /* GNW 1992.07.09 */
  280.             *nam++ = DIRSEP ;
  281.          strcpy(nam, file) ;
  282.       } else
  283.          *nam = 0 ;
  284.  
  285. #ifdef MVSXA   /* IBM: MVS/XA */
  286.       if (fname[0] == '/') {
  287.          fname[0] = '\'';
  288.          strcat(&fname[strlen(fname)],"\'");
  289.       }
  290.       if (fname[0] == '.') fname[0] = ' ';
  291.       if (fname[1] == '.') fname[1] = ' ';
  292. #endif         /* IBM: MVS/XA */
  293.       /* belated check -- bah! */
  294.       if (strlen(fname) + 1 > MAXPATHLEN)
  295.          error("! overran allocated storage in search()");
  296.  
  297. #ifdef DEBUG
  298.       if (dd(D_PATHS))
  299.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  300. #endif
  301.       if ((fd=fopen(fname,mode)) != NULL)
  302.          return(fd);
  303.  
  304.    /* skip over PATHSEP and try again */
  305.    } while (*(path++));
  306.  
  307.    return(NULL);
  308.  
  309. }               /* end search */
  310.  
  311. /* do we report file openings? */
  312.  
  313. #ifdef DEBUG
  314. #  ifdef fopen
  315. #    undef fopen
  316. #  endif
  317. #  ifdef VMCMS  /* IBM: VM/CMS */
  318. #    define fopen cmsfopen
  319. #  endif /* IBM: VM/CMS */
  320. FILE *my_real_fopen(n, t)
  321. register char *n, *t ;
  322. {
  323.    FILE *tf ;
  324.    if (dd(D_FILES)) {
  325.       fprintf(stderr, "<%s(%s)> ", n, t) ;
  326.       tf = fopen(n, t) ;
  327.       if (tf == 0)
  328.          fprintf(stderr, "failed\n") ;
  329.       else
  330.          fprintf(stderr, "succeeded\n") ;
  331.    } else
  332.       tf = fopen(n, t) ;
  333.    return tf ;
  334. }
  335. #endif
  336.