home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / dvips / resident.c < prev    next >
C/C++ Source or Header  |  1993-01-27  |  24KB  |  917 lines

  1. /*
  2.  *   This code reads in and handles the defaults for the program from the
  3.  *   file config.sw.  This entire file is a bit kludgy, sorry.
  4.  */
  5. #include "dvips.h" /* The copyright notice in that file is included too! */
  6. #include "paths.h"
  7. /*
  8.  *   This is the structure definition for resident fonts.  We use
  9.  *   a small and simple hash table to handle these.  We don't need
  10.  *   a big hash table.
  11.  */
  12. struct resfont *reshash[RESHASHPRIME] ;
  13. /*
  14.  *   These are the external routines we use.
  15.  */
  16. extern void error() ;
  17. extern integer scalewidth() ;
  18. extern int tfmload() ;
  19. extern FILE *search() ;
  20. extern shalfword pkbyte() ;
  21. extern integer pkquad() ;
  22. extern integer pktrio() ;
  23. extern Boolean pkopen() ;
  24. extern char *getenv() ;
  25. extern char *newstring() ;
  26. extern int add_header() ;
  27. extern int add_name() ;
  28. extern char *get_name() ;
  29. extern int system() ;
  30. extern void handlepapersize() ;
  31. void getpsinfo() ;
  32. extern void *revlist() ;
  33. /*
  34.  *   These are the external variables we use.
  35.  */
  36. #ifdef DEBUG
  37. extern integer debug_flag;
  38. #endif  /* DEBUG */
  39. extern integer pagecopies ;
  40. extern int overridemag ;
  41. extern long bytesleft ;
  42. extern quarterword *raster ;
  43. extern FILE *pkfile ;
  44. extern char *oname ;
  45. extern integer swmem, fontmem ;
  46. extern char *tfmpath, *pictpath ;
  47. extern char *pkpath ;
  48. extern char *vfpath ;
  49. extern char *figpath ;
  50. extern char *configpath ;
  51. extern Boolean noenv ;
  52. #ifdef SEARCH_SUBDIRECTORIES
  53. extern char *fontsubdirpath ;
  54. #endif
  55. #ifdef FONTLIB
  56. extern char *flipath, *fliname ;
  57. #endif
  58. extern char *headerpath ;
  59. extern char *paperfmt ; 
  60. extern char *nextstring ;
  61. extern char *maxstring ;
  62. extern char *warningmsg ;
  63. extern Boolean disablecomments ;
  64. extern Boolean compressed ;
  65. extern int quiet ;
  66. extern int filter ;
  67. extern Boolean reverse ;
  68. extern Boolean usesPSfonts ;
  69. extern Boolean nosmallchars ;
  70. extern Boolean removecomments ;
  71. extern Boolean safetyenclose ;
  72. extern Boolean dopprescan ;
  73. extern integer maxsecsize ;
  74. extern integer mag ;
  75. extern Boolean sepfiles ;
  76. extern int actualdpi ;
  77. extern int vactualdpi ;
  78. extern int maxdrift ;
  79. extern int vmaxdrift ;
  80. extern char *printer ;
  81. extern char *mfmode ;
  82. extern Boolean sendcontrolD ;
  83. extern int lastresortsizes[] ;
  84. extern integer hoff, voff ;
  85. extern struct papsiz *papsizes ;
  86. /*
  87.  *   To maintain a list of document fonts, we use the following
  88.  *   pointer.
  89.  */
  90. struct header_list *ps_fonts_used ;
  91. /*
  92.  *   Our hash routine.
  93.  */
  94. int
  95. hash(s)
  96.    char *s ;
  97. {
  98.    int h = 12 ;
  99.  
  100.    while (*s != 0)
  101.       h = (h + h + *s++) % RESHASHPRIME ;
  102.    return(h) ;
  103. }
  104. /*
  105.  *   Reverse the hash chains.
  106.  */
  107. void
  108. revpslists() {
  109.    register int i ;
  110.    for (i=0; i<RESHASHPRIME; i++)
  111.       reshash[i] = (struct resfont *)revlist(reshash[i]) ;
  112. }
  113. /*
  114.  *   cleanres() marks all resident fonts as not being yet sent.
  115.  */
  116. void
  117. cleanres() {
  118.    register int i ;
  119.    register struct resfont *p ;
  120.    for (i=0; i<RESHASHPRIME; i++)
  121.       for (p=reshash[i]; p; p=p->next)
  122.          p->sent = 0 ;
  123. }
  124. /*
  125.  *   The routine that looks up a font name.
  126.  */
  127. struct resfont *
  128. lookup(name)
  129.    char *name ;
  130. {
  131.    struct resfont *p ;
  132.  
  133.    for (p=reshash[hash(name)]; p!=NULL; p=p->next)
  134.       if (strcmp(p->Keyname, name)==0)
  135.          return(p) ;
  136.    return(NULL) ;
  137. }
  138. /*
  139.  *   This routine adds an entry.
  140.  */
  141. void
  142. add_entry(TeXname, PSname, specinfo, downloadinfo)
  143.    char *TeXname, *PSname, *specinfo, *downloadinfo ;
  144. {
  145.    struct resfont *p ;
  146.    int h ;
  147.  
  148.    if (PSname == NULL)
  149.       PSname = TeXname ;
  150.    p = (struct resfont *)mymalloc((integer)sizeof(struct resfont)) ;
  151.    p->Keyname = TeXname ;
  152.    p->PSname = PSname ;
  153.    p->TeXname = TeXname ;
  154.    p->specialinstructions = specinfo ;
  155.    if (downloadinfo && *downloadinfo)
  156.       p->downloadheader = downloadinfo ;
  157.    else
  158.       p->downloadheader = 0 ;
  159.    h = hash(TeXname) ;
  160.    p->next = reshash[h] ;
  161.    p->sent = 0 ;
  162.    reshash[h] = p ;
  163. }
  164. /*
  165.  *   Now our residentfont routine.  Returns the number of characters in
  166.  *   this font, based on the TFM file.
  167.  */
  168. extern char *infont ;
  169. int
  170. residentfont(curfnt)
  171.         register fontdesctype *curfnt ;
  172. {
  173.    register shalfword i ;
  174.    struct resfont *p ;
  175.  
  176. /*
  177.  *   First we determine if we can find this font in the resident list.
  178.  */
  179.    if (*curfnt->area)
  180.       return 0 ; /* resident fonts never have a nonstandard font area */
  181.    if ((p=lookup(curfnt->name))==NULL)
  182.       return 0 ;
  183. /*
  184.  *   We clear out some pointers:
  185.  */
  186. #ifdef DEBUG
  187.    if (dd(D_FONTS))
  188.         (void)fprintf(stderr,"Font %s <%s> is resident.\n",
  189.                                      curfnt->name, p->PSname) ;
  190. #endif  /* DEBUG */
  191.    curfnt->resfont = p ;
  192.    curfnt->name = p->TeXname ;
  193.    for (i=0; i<256; i++) {
  194.       curfnt->chardesc[i].TFMwidth = 0 ;
  195.       curfnt->chardesc[i].packptr = NULL ;
  196.       curfnt->chardesc[i].pixelwidth = 0 ;
  197.       curfnt->chardesc[i].flags = 0 ;
  198.    }
  199.    add_name(p->PSname, &ps_fonts_used) ;
  200. /*
  201.  *   We include the font here.  But we only should need to include the
  202.  *   font if we have a stupid spooler; smart spoolers should be able
  203.  *   to supply it automatically.
  204.  */
  205.    if (p->downloadheader) {
  206.       char *cp = p->downloadheader ;
  207.       char *q ;
  208.  
  209.       infont = p->PSname ;
  210.       while (1) {
  211.          q = cp ;
  212.          while (*cp && *cp != ' ')
  213.             cp++ ;
  214.          if (*cp) {
  215.             *cp = 0 ;
  216.             add_header(q) ;
  217.             *cp++ = ' ' ;
  218.          } else {
  219.             add_header(q) ;
  220.             break ;
  221.          }
  222.          infont = 0 ;
  223.       }
  224.       infont = 0 ;
  225.    }
  226.    i = tfmload(curfnt) ;
  227.    if (i < 0)
  228.       i = 1 ;
  229.    usesPSfonts = 1 ;
  230.    return(i) ;
  231. }
  232. #define INLINE_SIZE (500)
  233. static char was_inline[INLINE_SIZE] ;
  234. void
  235. bad_config() {
  236.    extern void exit() ;
  237.  
  238.    error("Error in config file:") ;
  239.    (void)fprintf(stderr, "%s\n", was_inline) ;
  240.    exit(1) ;
  241. }
  242. /*
  243.  *   Get environment variables! These override entries in ./config.h.
  244.  *   We substitute everything of the form ::, ^: or :$ with default,
  245.  *   so a user can easily build on to the existing paths.
  246.  */
  247. static char *getpath(who, what)
  248. char *who, *what ;
  249. {
  250.    if (who) {
  251.       register char *pp, *qq ;
  252.       int lastsep = 1 ;
  253.  
  254.       for (pp=nextstring, qq=who; *qq;) {
  255.          if (*qq == PATHSEP) {
  256.             if (lastsep) {
  257.                strcpy(pp, what) ;
  258.                pp = pp + strlen(pp) ;
  259.             }
  260.             lastsep = 1 ;
  261.          } else
  262.             lastsep = 0 ;
  263.          *pp++ = *qq++ ;
  264.       }
  265.       if (lastsep) {
  266.          strcpy(pp, what) ;
  267.          pp = pp + strlen(pp) ;
  268.       }
  269.       *pp = 0 ;
  270.       qq = nextstring ;
  271.       nextstring = pp + 1 ;
  272.       return qq ;
  273.    } else
  274.       return what ;
  275. }
  276. /*
  277.  *   We use this function so we can support strings delimited by
  278.  *   double quotes with spaces in them.
  279.  */
  280. char *configstring(s, nullok)
  281. char *s ;
  282. int nullok ;
  283. {
  284.    char tstr[300] ;
  285.    char *p = tstr ;
  286.  
  287.    while (*s && *s <= ' ')
  288.       s++ ;
  289.    if (*s == '"') {
  290.       s++ ;
  291.       while (*s != 10 && *s != 0 && *s != '"' && p < tstr+290)
  292.          *p++ = *s++ ;
  293.    } else {
  294.       while (*s > ' ' && p < tstr+290)
  295.          *p++ = *s++ ;
  296.    }
  297.    *p = 0 ;
  298.    if (p == tstr && ! nullok)
  299.       bad_config() ;
  300.    return newstring(tstr) ;
  301. }
  302. /*
  303.  *   Now we have the getdefaults routine.
  304.  */
  305. static char *psmapfile = PSMAPFILE ;
  306. void
  307. getdefaults(s)
  308. char *s ;
  309. {
  310.    FILE *deffile ;
  311.    char PSname[300] ;
  312.    register char *p ;
  313.    int i, j ;
  314.    integer hsiz, vsiz ;
  315.    char *d = configpath ;
  316.    int canaddtopaper = 0 ;
  317.  
  318.    if (printer == NULL) {
  319.       if (s) {
  320.          strcpy(PSname, s) ;
  321.       } else {
  322. #ifndef VMCMS  /* IBM: VM/CMS - don't have home directory on VMCMS */
  323.          d = "~" ;
  324. #endif  /* IBM: VM/CMS */
  325.          strcpy(PSname, DVIPSRC) ;
  326.       }
  327.    } else {
  328.       strcpy(PSname, "config.") ;
  329.       strcat(PSname, printer) ;
  330.    }
  331.    if ((deffile=search(d,PSname,READ))!=NULL) {
  332.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  333. /*
  334.  *   We need to get rid of the newline.
  335.  */
  336.        for (p=was_inline; *p; p++) ;
  337.        if (p > was_inline) *(p-1) = 0 ;
  338.        if (was_inline[0] != '@')
  339.           canaddtopaper = 0 ;
  340.        switch (was_inline[0]) {
  341. /*
  342.  *   Handling paper size information:
  343.  *
  344.  *      If line is empty, then we clear out the paper size information
  345.  *      we have so far.
  346.  *
  347.  *      If it is `@+', then we add to the current paper size info.
  348.  *
  349.  *      If it is `name hsize vsize', then we start a new definition.
  350.  */
  351. case '@' :
  352.          p = was_inline + 1 ;
  353.          while (*p && *p <= ' ') p++ ;
  354.          if (*p == 0) {
  355.             papsizes = 0 ; /* throw away memory */
  356.          } else if (*p == '+') {
  357.             if (canaddtopaper == 0)
  358.                error(
  359.       "can't have @+ in config file not immediately following a non-@ line") ;
  360.             else {
  361.                *(nextstring-1) = '\n' ;/* IBM: VM/CMS - changed 10 to "\n" */
  362.                p++ ;
  363.                while (*p && *p == ' ') p++ ;
  364.                strcpy(nextstring, p) ;
  365.                nextstring += strlen(p) + 1 ;
  366.             }
  367.          } else {
  368.             struct papsiz *ps ;
  369.             
  370.             ps = (struct papsiz *)mymalloc((integer)sizeof(struct papsiz)) ;
  371.             ps->next = papsizes ;
  372.             papsizes = ps ;
  373.             ps->name = p ;
  374.             while (*p && *p > ' ')
  375.                p++ ;
  376.             *p++ = 0 ;
  377.             ps->name = newstring(ps->name) ;
  378.             while (*p && *p <= ' ') p++ ;
  379.             handlepapersize(p, &hsiz, &vsiz) ;
  380.             ps->xsize = hsiz ;
  381.             ps->ysize = vsiz ;
  382.             ps->specdat = nextstring++ ;
  383.             canaddtopaper = 1 ;
  384.          }
  385.          break ;
  386. case 'a' :
  387.          dopprescan = (was_inline[1] != '0') ;
  388.          break ;
  389. case 'b':
  390. #ifdef SHORTINT
  391.          if (sscanf(was_inline+1, "%ld", &pagecopies) != 1) bad_config() ;
  392. #else
  393.          if (sscanf(was_inline+1, "%d", &pagecopies) != 1) bad_config() ;
  394. #endif
  395.          if (pagecopies < 1 || pagecopies > 1000)
  396.             bad_config() ;
  397.          break ;
  398. case 'm' :
  399. #ifdef SHORTINT
  400.          if (sscanf(was_inline+1, "%ld", &swmem) != 1) bad_config() ;
  401. #else   /* ~SHORTINT */
  402.          if (sscanf(was_inline+1, "%d", &swmem) != 1) bad_config() ;
  403. #endif  /* ~SHORTINT */
  404.          swmem += fontmem ; /* grab headers we've seen already */
  405.          break ;
  406. case 'M' :
  407.          mfmode = configstring(was_inline+1, 0) ;
  408.          break ;
  409. case 'o' :
  410.          oname = configstring(was_inline+1, 1) ;
  411.          if (*oname && oname[strlen(oname)-1] == ':')
  412.             sendcontrolD = 1 ; /* if we send to a device, *we* are spooler */
  413.          break ;
  414. case 'O' :
  415.          p = was_inline + 1 ;
  416.          handlepapersize(p, &hoff, &voff) ;
  417.          break ;
  418. #ifdef FONTLIB
  419. case 'L' : 
  420.          {
  421.             char tempname[300] ;
  422.             extern char *fliparse() ;
  423.             if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  424.             else {
  425.                flipath = getpath(fliparse(PSname,tempname), flipath);
  426.                fliname = newstring(tempname) ;
  427.             }
  428.      }
  429.          break ;
  430. #endif
  431. case 'T' : 
  432.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  433.          else tfmpath = getpath(PSname, tfmpath) ;
  434.          break ;
  435. case 'P' :
  436.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  437.          else pkpath = getpath(PSname, pkpath) ;
  438.          break ;
  439. case 'p' :
  440.          p = was_inline + 1 ;
  441.          while (*p && *p <= ' ')
  442.             p++ ;
  443.          if (*p == '+') {
  444.             if (sscanf(p+1, "%s", PSname) != 1) bad_config() ;
  445.             getpsinfo(PSname) ;
  446.          } else {
  447.             psmapfile = configstring(was_inline+1, 0) ;
  448.          }
  449.          break ;
  450. case 'V' : case 'v' :
  451.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  452.          else vfpath = getpath(PSname, vfpath) ;
  453.          break ;
  454. case 'S' :
  455.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  456.          else figpath = getpath(PSname, figpath) ;
  457.          break ;
  458. case 's':
  459.          safetyenclose = 1 ;
  460.          break ;
  461. case 'H' : 
  462.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  463.          else headerpath = getpath(PSname, headerpath) ;
  464.          break ;
  465. case ' ' : case '*' : case '#' : case ';' : case '=' : case 0 : case '\n' :
  466.          break ;
  467. case 'r' :
  468.          reverse = (was_inline[1] != '0') ;
  469.          break ;
  470. /*
  471.  *   This case is for last resort font scaling; I hate this, but enough
  472.  *   people have in no uncertain terms demanded it that I'll go ahead and
  473.  *   add it.
  474.  *
  475.  *   This line must have numbers on it, resolutions, to search for the
  476.  *   font as a last resort, and then the font will be scaled.  These
  477.  *   resolutions should be in increasing order.
  478.  *
  479.  *   For most machines, just `300' is sufficient here; on the NeXT,
  480.  *   `300 400' may be more appropriate.
  481.  */
  482. case 'R':
  483.          i = 0 ;
  484.          p = was_inline + 1 ;
  485.          while (*p) {
  486.             while (*p && *p <= ' ')
  487.                p++ ;
  488.             if ('0' <= *p && *p <= '9') {
  489.                j = 0 ;
  490.                while ('0' <= *p && *p <= '9')
  491.                   j = 10 * j + (*p++ - '0') ;
  492.                if (i > 0)
  493.                   if (lastresortsizes[i-1] > j) {
  494.                      error("last resort sizes (R) must be sorted") ;
  495.                      bad_config() ;
  496.                   }
  497.                lastresortsizes[i++] = j ;
  498.             } else {
  499.                if (*p == 0)
  500.                   break ;
  501.                error("! only numbers expected on `R' line in config!") ;
  502.             }
  503.          }
  504.          lastresortsizes[i] = 32000 ;
  505.          break ;
  506. case 'D' :
  507.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  508.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  509.      vactualdpi = actualdpi;
  510.          break ;
  511. /*
  512.  *   Execute a command.  This can be dangerous, but can also be very useful.
  513.  */
  514. case 'E' :
  515. #ifdef SECURE
  516.          error("dvips was compiled with SECURE, which disables E in config") ;
  517. #else
  518.          (void)system(was_inline+1) ;
  519. #endif
  520.          break ;
  521. case 'K':
  522.          removecomments = (was_inline[1] != '0') ;
  523.          break ;
  524. case 'U':
  525.          nosmallchars = (was_inline[1] != '0') ;
  526.          break ;
  527. case 'W':
  528.          for (p=was_inline+1; *p && *p <= ' '; p++) ;
  529.          if (*p)
  530.             warningmsg = newstring(p) ;
  531.          else
  532.             warningmsg = 0 ;
  533.          break ;
  534. case 'X' :
  535.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  536.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  537.          break ;
  538. case 'Y' :
  539.          if (sscanf(was_inline+1, "%d", &vactualdpi) != 1) bad_config() ;
  540.          if (vactualdpi < 10 || vactualdpi > 10000) bad_config() ;
  541.          break ;
  542. case 'x': case 'y':
  543.          if (sscanf(was_inline+1, "%d", &mag) != 1) bad_config() ;
  544.          overridemag = (was_inline[0] == 'x') ? 1 : -1 ;
  545.          break ;
  546. case 'e' :
  547.          if (sscanf(was_inline+1, "%d", &maxdrift) != 1) bad_config() ;
  548.          if (maxdrift < 0) bad_config() ;
  549.      vmaxdrift = maxdrift;
  550.          break ;
  551. case 'q' : case 'Q' :
  552.          quiet = (was_inline[1] != '0') ;
  553.          break ;
  554. case 'f' : case 'F' :
  555.          filter = (was_inline[1] != '0') ;
  556.          break ;
  557. case 'h' : 
  558.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  559.          else (void)add_header(PSname) ;
  560.          break ;
  561. case 'i' :
  562.          if (sscanf(was_inline+1, "%d", &maxsecsize) != 1)
  563.             maxsecsize = 0 ;
  564.          sepfiles = 1 ;
  565.          break ;
  566. case 'I':
  567.          noenv = (was_inline[1] != '0') ;
  568.          break ;
  569. case 'N' :
  570.          disablecomments = (was_inline[1] != '0') ;
  571.          break ;
  572. case 'Z' :
  573.          compressed = (was_inline[1] != '0') ;
  574.          break ;
  575. default:
  576.          bad_config() ;
  577.       }
  578.      }
  579.      (void)fclose(deffile) ;
  580.    } else {
  581.       if (printer)
  582.          error("! no such printer (can't find corresponding config file)") ;
  583.    }
  584. }
  585.  
  586. /*
  587.  *   If a character pointer is passed in, use that name; else, use the
  588.  *   default (possibly set) name.
  589.  */
  590. void getpsinfo(name)
  591. char *name ;
  592. {
  593.    FILE *deffile ;
  594.    register char *p ;
  595.    char *specinfo, *downloadinfo ;
  596.    char downbuf[200] ;
  597.  
  598.    if (name == 0)
  599.       name = psmapfile ;
  600.    if ((deffile=search(configpath, name, READ))!=NULL) {
  601.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  602.          p = was_inline ;
  603.          if (*p > ' ' && *p != '*' && *p != '#' && *p != ';' && *p != '%') {
  604.             char *TeXname = NULL ;
  605.             char *PSname = NULL ;
  606.             specinfo = NULL ;
  607.             downloadinfo = NULL ;
  608.             downbuf[0] = 0 ;
  609.             while (*p) {
  610.                while (*p && *p <= ' ')
  611.                   p++ ;
  612.                if (*p) {
  613.                   if (*p == '"')
  614.                      specinfo = p + 1 ;
  615.                   else if (*p == '<') {
  616.                      if (downloadinfo) {
  617.                         strcat(downbuf, downloadinfo) ;
  618.                         strcat(downbuf, " ") ;
  619.                      }
  620.                      downloadinfo = p + 1 ;
  621.                   } else if (TeXname)
  622.                      PSname = p ;
  623.                   else
  624.                      TeXname = p ;
  625.                   if (*p == '"') {
  626.                      p++ ;
  627.                      while (*p != '"' && *p)
  628.                         p++ ;
  629.                   } else
  630.                      while (*p > ' ')
  631.                         p++ ;
  632.                   if (*p)
  633.                      *p++ = 0 ;
  634.                }
  635.             }
  636.             if (downloadinfo)
  637.                strcat(downbuf, downloadinfo) ;
  638.             if (TeXname) {
  639.                TeXname = newstring(TeXname) ;
  640.                specinfo = newstring(specinfo) ;
  641.                PSname = newstring(PSname) ;
  642.                downloadinfo = newstring(downbuf) ;
  643.                add_entry(TeXname, PSname, specinfo, downloadinfo) ;
  644.             }
  645.         }
  646.       }
  647.       (void)fclose(deffile) ;
  648.    }
  649. }
  650. /*
  651.  *   Get environment variables! These override entries in ./config.h.
  652.  *   We substitute everything of the form ::, ^: or :$ with default,
  653.  *   so a user can easily build on to the existing paths.
  654.  */
  655. static char *getenvup(who, what)
  656. char *who, *what ;
  657. {
  658.    return getpath(getenv(who), what) ;
  659. }
  660. #ifdef SEARCH_SUBDIRECTORIES
  661. static char *concat3();
  662. #endif
  663. void checkenv(which)
  664. int which ;
  665. {
  666.    if (which) {
  667.       tfmpath = getenvup("TEXFONTS", tfmpath) ;
  668.       vfpath = getenvup("VFFONTS", vfpath) ;
  669.       pictpath = getenvup("TEXPICTS", pictpath) ;
  670.       figpath = getenvup("TEXINPUTS", figpath) ;
  671.       headerpath = getenvup("DVIPSHEADERS", headerpath) ;
  672.       if (getenv("TEXPKS"))
  673.          pkpath = getenvup("TEXPKS", pkpath) ;
  674.       else if (getenv("TEXPACKED"))
  675.          pkpath = getenvup("TEXPACKED", pkpath) ;
  676.       else if (getenv("PKFONTS"))
  677.          pkpath = getenvup("PKFONTS", pkpath) ;
  678. #ifdef SEARCH_SUBDIRECTORIES
  679.       else if (getenv("TEXFONTS"))
  680.          pkpath = getenvup("TEXFONTS", pkpath) ;
  681.       if (getenv ("TEXFONTS_SUBDIR"))
  682.          fontsubdirpath = getenvup ("TEXFONTS_SUBDIR", fontsubdirpath);
  683.       {
  684.          char pathsep[2] ;
  685.          char *do_subdir_path();
  686.          char *dirs = do_subdir_path (fontsubdirpath);
  687.          /* If the paths were in dynamic storage before, that memory is
  688.             wasted now.  */
  689.          pathsep[0] = PATHSEP ;
  690.          pathsep[1] = '\0' ;
  691.          tfmpath = concat3 (tfmpath, pathsep, dirs);
  692.          pkpath = concat3 (pkpath, pathsep, dirs);
  693.       }
  694. #endif
  695.    } else
  696.       configpath = getenvup("TEXCONFIG", configpath) ;
  697. }
  698.  
  699. #ifdef SEARCH_SUBDIRECTORIES
  700.  
  701. #include <sys/types.h>
  702. #include <sys/stat.h>
  703. #include <errno.h>
  704.  
  705. #ifdef SYSV
  706. #include <dirent.h>
  707. typedef struct dirent *directory_entry_type;
  708. #else
  709. #include <sys/dir.h>
  710. typedef struct direct *directory_entry_type;
  711. #endif
  712.  
  713. /* Declare the routine to get the current working directory.  */
  714.  
  715. #ifndef HAVE_GETCWD
  716. extern char *getwd ();
  717. #define getcwd(b, len)  ((b) ? getwd (b) : getwd (xmalloc (len)))
  718. #else
  719. #ifdef ANSI
  720. extern char *getcwd (char *, int);
  721. #else
  722. extern char *getcwd ();
  723. #endif /* not ANSI */
  724. #endif /* not HAVE_GETWD */
  725.  
  726. #if defined(SYSV) || defined(VMS) || defined(MSDOS)
  727. #define MAXPATHLEN (256)
  728. #else   /* ~SYSV */
  729. #include <sys/param.h>          /* for MAXPATHLEN */
  730. #endif
  731.  
  732. extern void exit() ;
  733. extern int chdir() ;
  734.  
  735. /* Memory operations: variants of malloc(3) and realloc(3) that just
  736.    give up the ghost when they fail.  */
  737.  
  738. extern char *realloc ();
  739.  
  740. char *
  741. xmalloc (size)
  742.   unsigned size;
  743. {
  744.   char *mem = malloc (size);
  745.   
  746.   if (mem == NULL)
  747.     {
  748.       fprintf (stderr, "! Cannot allocate %u bytes.\n", size);
  749.       exit (10);
  750.     }
  751.   
  752.   return mem;
  753. }
  754.  
  755.  
  756. char *
  757. xrealloc (ptr, size)
  758.   char *ptr;
  759.   unsigned size;
  760. {
  761.   char *mem = realloc (ptr, size);
  762.   
  763.   if (mem == NULL)
  764.     {
  765.       fprintf (stderr, "! Cannot reallocate %u bytes at %x.\n", size, ptr);
  766.       exit (10);
  767.     }
  768.     
  769.   return mem;
  770. }
  771.  
  772.  
  773. /* Return, in NAME, the next component of PATH, i.e., the characters up
  774.    to the next PATHSEP.  */
  775.    
  776. static void
  777. next_component (name, path)
  778.   char name[];
  779.   char **path;
  780. {
  781.   unsigned count = 0;
  782.   
  783.   while (**path != 0 && **path != PATHSEP)
  784.     {
  785.       name[count++] = **path;
  786.       (*path)++; /* Move further along, even between calls.  */
  787.     }
  788.   
  789.   name[count] = 0;
  790.   if (**path == PATHSEP)
  791.     (*path)++; /* Move past the delimiter.  */
  792. }
  793.  
  794.  
  795. #ifndef _POSIX_SOURCE
  796. #define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
  797. #endif
  798.  
  799. /* Return true if FN is a directory or a symlink to a directory,
  800.    false if not. */
  801.  
  802. int
  803. is_dir (fn)
  804.   char *fn;
  805. {
  806.   struct stat stats;
  807.  
  808.   return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode);
  809. }
  810.  
  811.  
  812. static char *
  813. concat3 (s1, s2, s3)
  814.   char *s1, *s2, *s3;
  815. {
  816.   char *r = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  817.   strcpy (r, s1);
  818.   strcat (r, s2);
  819.   strcat (r, s3);
  820.   return r;
  821. }
  822.  
  823.  
  824. /* DIR_LIST is the default list of directories (colon-separated) to
  825.    search.  We want to add all the subdirectories directly below each of
  826.    the directories in the path.
  827.      
  828.    We return the list of directories found.  */
  829.  
  830. char *
  831. do_subdir_path (dir_list)
  832.   char *dir_list;
  833. {
  834.   char *cwd;
  835.   unsigned len;
  836.   char *result = xmalloc ((unsigned)1);
  837.   char *temp = dir_list;
  838.   char dirsep[2] ;
  839.  
  840.   dirsep[0] = DIRSEP ;
  841.   dirsep[1] = '\0' ;
  842.  
  843.   /* Make a copy in writable memory.  */
  844.   dir_list = xmalloc (strlen (temp) + 1);
  845.   strcpy (dir_list, temp);
  846.   
  847.   *result = 0;
  848.  
  849.   /* Unfortunately, we can't look in the environment for the current
  850.      directory, because if we are running under a program (let's say
  851.      Emacs), the PWD variable might have been set by Emacs' parent
  852.      to the current directory at the time Emacs was invoked.  This
  853.      is not necessarily the same directory the user expects to be
  854.      in.  So, we must always call getcwd(3) or getwd(3), even though
  855.      they are slow and prone to hang in networked installations.  */
  856.   cwd = getcwd (NULL, MAXPATHLEN + 2);
  857.   if (cwd == NULL)
  858.     {
  859.       perror ("getcwd");
  860.       exit (errno);
  861.     }
  862.  
  863.   do
  864.     {
  865.       DIR *dir;
  866.       directory_entry_type e;
  867.       char dirname[MAXPATHLEN];
  868.  
  869.       next_component (dirname, &dir_list);
  870.  
  871.       /* All the `::'s should be gone by now, but we may as well make
  872.          sure `chdir' doesn't crash.  */
  873.       if (*dirname == 0) continue;
  874.  
  875.       /* By changing directories, we save a bunch of string
  876.          concatenations (and make the pathnames the kernel looks up
  877.          shorter).  */
  878.       if (chdir (dirname) != 0) continue;
  879.  
  880.       dir = opendir (".");
  881.       if (dir == NULL) continue;
  882.  
  883.       while ((e = readdir (dir)) != NULL)
  884.         {
  885.           if (is_dir (e->d_name) && strcmp (e->d_name, ".") != 0
  886.               && strcmp (e->d_name, "..") != 0)
  887.             {
  888.               char *found = concat3 (dirname, dirsep, e->d_name);
  889.  
  890.               result = xrealloc (result, strlen (result) + strlen (found) + 2);
  891.  
  892.               len = strlen (result);
  893.               if (len > 0)
  894.                 {
  895.                   result[len] = PATHSEP;
  896.                   result[len + 1] = 0;
  897.                 }
  898.               strcat (result, found);
  899.               free (found);
  900.             }
  901.         }
  902.       closedir (dir);
  903.  
  904.       /* Change back to the current directory, in case the path
  905.          contains relative directory names.  */
  906.       if (chdir (cwd) != 0)
  907.         {
  908.           perror (cwd);
  909.           exit (errno);
  910.         }
  911.     }
  912.   while (*dir_list != 0);
  913.   
  914.   return result;
  915. }
  916. #endif /* SEARCH_SUBDIRECTORIES */
  917.