home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / resident.c < prev    next >
C/C++ Source or Header  |  1993-04-26  |  25KB  |  936 lines

  1. /*   For use with emTeX set FONTPATH to "TEXTFM"
  2.  */
  3. #ifndef FONTPATH
  4. #define FONTPATH "TEXFONTS"
  5. #endif
  6.  
  7. /*
  8.  *   This code reads in and handles the defaults for the program from the
  9.  *   file config.sw.  This entire file is a bit kludgy, sorry.
  10.  */
  11. #include "dvips.h" /* The copyright notice in that file is included too! */
  12. #include "paths.h"
  13. /*
  14.  *   This is the structure definition for resident fonts.  We use
  15.  *   a small and simple hash table to handle these.  We don't need
  16.  *   a big hash table.
  17.  */
  18. struct resfont *reshash[RESHASHPRIME] ;
  19. /*
  20.  *   These are the external routines we use.
  21.  */
  22. extern void error() ;
  23. extern integer scalewidth() ;
  24. extern int tfmload() ;
  25. extern FILE *search() ;
  26. extern shalfword pkbyte() ;
  27. extern integer pkquad() ;
  28. extern integer pktrio() ;
  29. extern Boolean pkopen() ;
  30. extern char *getenv() ;
  31. extern char *newstring() ;
  32. extern int add_header() ;
  33. extern int add_name() ;
  34. extern char *get_name() ;
  35. extern int system() ;
  36. extern void handlepapersize() ;
  37. extern void checkstrings() ;
  38. void getpsinfo() ;
  39. extern void *revlist() ;
  40. /*
  41.  *   These are the external variables we use.
  42.  */
  43. #ifdef DEBUG
  44. extern integer debug_flag;
  45. #endif  /* DEBUG */
  46. extern integer pagecopies ;
  47. extern int overridemag ;
  48. extern long bytesleft ;
  49. extern quarterword *raster ;
  50. extern FILE *pkfile ;
  51. extern char *oname ;
  52. extern integer swmem, fontmem ;
  53. extern char *tfmpath, *pictpath ;
  54. extern char *pkpath ;
  55. extern char *vfpath ;
  56. extern char *figpath ;
  57. extern char *configpath ;
  58. extern Boolean noenv ;
  59. #ifdef SEARCH_SUBDIRECTORIES
  60. extern char *fontsubdirpath ;
  61. #endif
  62. #ifdef FONTLIB
  63. extern char *flipath, *fliname ;
  64. #endif
  65. extern char *headerpath ;
  66. extern char *paperfmt ; 
  67. extern char *nextstring ;
  68. extern char *maxstring ;
  69. extern char *warningmsg ;
  70. extern Boolean disablecomments ;
  71. extern Boolean compressed ;
  72. extern int quiet ;
  73. extern int filter ;
  74. extern Boolean reverse ;
  75. extern Boolean usesPSfonts ;
  76. extern Boolean nosmallchars ;
  77. extern Boolean removecomments ;
  78. extern Boolean safetyenclose ;
  79. extern Boolean dopprescan ;
  80. extern integer maxsecsize ;
  81. extern integer mag ;
  82. extern Boolean sepfiles ;
  83. extern int actualdpi ;
  84. extern int vactualdpi ;
  85. extern int maxdrift ;
  86. extern int vmaxdrift ;
  87. extern char *printer ;
  88. extern char *mfmode ;
  89. extern Boolean sendcontrolD ;
  90. extern int lastresortsizes[] ;
  91. extern integer hoff, voff ;
  92. extern struct papsiz *papsizes ;
  93. /*
  94.  *   To maintain a list of document fonts, we use the following
  95.  *   pointer.
  96.  */
  97. struct header_list *ps_fonts_used ;
  98. /*
  99.  *   Our hash routine.
  100.  */
  101. int
  102. hash(s)
  103.    char *s ;
  104. {
  105.    int h = 12 ;
  106.  
  107.    while (*s != 0)
  108.       h = (h + h + *s++) % RESHASHPRIME ;
  109.    return(h) ;
  110. }
  111. /*
  112.  *   Reverse the hash chains.
  113.  */
  114. void
  115. revpslists() {
  116.    register int i ;
  117.    for (i=0; i<RESHASHPRIME; i++)
  118.       reshash[i] = (struct resfont *)revlist(reshash[i]) ;
  119. }
  120. /*
  121.  *   cleanres() marks all resident fonts as not being yet sent.
  122.  */
  123. void
  124. cleanres() {
  125.    register int i ;
  126.    register struct resfont *p ;
  127.    for (i=0; i<RESHASHPRIME; i++)
  128.       for (p=reshash[i]; p; p=p->next)
  129.          p->sent = 0 ;
  130. }
  131. /*
  132.  *   The routine that looks up a font name.
  133.  */
  134. struct resfont *
  135. lookup(name)
  136.    char *name ;
  137. {
  138.    struct resfont *p ;
  139.  
  140.    for (p=reshash[hash(name)]; p!=NULL; p=p->next)
  141.       if (strcmp(p->Keyname, name)==0)
  142.          return(p) ;
  143.    return(NULL) ;
  144. }
  145. /*
  146.  *   This routine adds an entry.
  147.  */
  148. void
  149. add_entry(TeXname, PSname, specinfo, downloadinfo)
  150.    char *TeXname, *PSname, *specinfo, *downloadinfo ;
  151. {
  152.    struct resfont *p ;
  153.    int h ;
  154.  
  155.    if (PSname == NULL)
  156.       PSname = TeXname ;
  157.    p = (struct resfont *)mymalloc((integer)sizeof(struct resfont)) ;
  158.    p->Keyname = TeXname ;
  159.    p->PSname = PSname ;
  160.    p->TeXname = TeXname ;
  161.    p->specialinstructions = specinfo ;
  162.    if (downloadinfo && *downloadinfo)
  163.       p->downloadheader = downloadinfo ;
  164.    else
  165.       p->downloadheader = 0 ;
  166.    h = hash(TeXname) ;
  167.    p->next = reshash[h] ;
  168.    p->sent = 0 ;
  169.    reshash[h] = p ;
  170. }
  171. /*
  172.  *   Now our residentfont routine.  Returns the number of characters in
  173.  *   this font, based on the TFM file.
  174.  */
  175. extern char *infont ;
  176. int
  177. residentfont(curfnt)
  178.         register fontdesctype *curfnt ;
  179. {
  180.    register shalfword i ;
  181.    struct resfont *p ;
  182.  
  183. /*
  184.  *   First we determine if we can find this font in the resident list.
  185.  */
  186.    if (*curfnt->area)
  187.       return 0 ; /* resident fonts never have a nonstandard font area */
  188.    if ((p=lookup(curfnt->name))==NULL)
  189.       return 0 ;
  190. /*
  191.  *   We clear out some pointers:
  192.  */
  193. #ifdef DEBUG
  194.    if (dd(D_FONTS))
  195.         (void)fprintf(stderr,"Font %s <%s> is resident.\n",
  196.                                      curfnt->name, p->PSname) ;
  197. #endif  /* DEBUG */
  198.    curfnt->resfont = p ;
  199.    curfnt->name = p->TeXname ;
  200.    for (i=0; i<256; i++) {
  201.       curfnt->chardesc[i].TFMwidth = 0 ;
  202.       curfnt->chardesc[i].packptr = NULL ;
  203.       curfnt->chardesc[i].pixelwidth = 0 ;
  204.       curfnt->chardesc[i].flags = 0 ;
  205.    }
  206.    add_name(p->PSname, &ps_fonts_used) ;
  207. /*
  208.  *   We include the font here.  But we only should need to include the
  209.  *   font if we have a stupid spooler; smart spoolers should be able
  210.  *   to supply it automatically.
  211.  */
  212.    if (p->downloadheader) {
  213.       char *cp = p->downloadheader ;
  214.       char *q ;
  215.  
  216.       infont = p->PSname ;
  217.       while (1) {
  218.          q = cp ;
  219.          while (*cp && *cp != ' ')
  220.             cp++ ;
  221.          if (*cp) {
  222.             *cp = 0 ;
  223.             add_header(q) ;
  224.             *cp++ = ' ' ;
  225.          } else {
  226.             add_header(q) ;
  227.             break ;
  228.          }
  229.          infont = 0 ;
  230.       }
  231.       infont = 0 ;
  232.    }
  233.    i = tfmload(curfnt) ;
  234.    if (i < 0)
  235.       i = 1 ;
  236.    usesPSfonts = 1 ;
  237.    return(i) ;
  238. }
  239. #define INLINE_SIZE (500)
  240. static char was_inline[INLINE_SIZE] ;
  241. void
  242. bad_config() {
  243.    extern void exit() ;
  244.  
  245.    error("Error in config file:") ;
  246.    (void)fprintf(stderr, "%s\n", was_inline) ;
  247.    exit(1) ;
  248. }
  249. /*
  250.  *   Get environment variables! These override entries in ./config.h.
  251.  *   We substitute everything of the form ::, ^: or :$ with default,
  252.  *   so a user can easily build on to the existing paths.
  253.  */
  254. static char *getpath(who, what)
  255. char *who, *what ;
  256. {
  257.    if (who) {
  258.       register char *pp, *qq ;
  259.       int lastsep = 1 ;
  260.  
  261.       for (pp=nextstring, qq=who; *qq;) {
  262.          if (*qq == PATHSEP) {
  263.             if (lastsep) {
  264.                strcpy(pp, what) ;
  265.                pp = pp + strlen(pp) ;
  266.             }
  267.             lastsep = 1 ;
  268.          } else
  269.             lastsep = 0 ;
  270.          *pp++ = *qq++ ;
  271.       }
  272.       if (lastsep) {
  273.          strcpy(pp, what) ;
  274.          pp = pp + strlen(pp) ;
  275.       }
  276.       *pp = 0 ;
  277.       qq = nextstring ;
  278.       nextstring = pp + 1 ;
  279.       return qq ;
  280.    } else
  281.       return what ;
  282. }
  283. /*
  284.  *   We use this function so we can support strings delimited by
  285.  *   double quotes with spaces in them.  We also accept strings
  286.  *   with spaces in them, but kill off any spaces at the end.
  287.  */
  288. char *configstring(s, nullok)
  289. char *s ;
  290. int nullok ;
  291. {
  292.    char tstr[300] ;
  293.    char *p = tstr ;
  294.  
  295.    while (*s && *s <= ' ')
  296.       s++ ;
  297.    if (*s == '"') {
  298.       s++ ;
  299.       while (*s != 10 && *s != 0 && *s != '"' && p < tstr+290)
  300.          *p++ = *s++ ;
  301.    } else {
  302.       while (*s && p < tstr+290)
  303.          *p++ = *s++ ;
  304.       while (*(p-1) <= ' ' && p > tstr)
  305.          p-- ;
  306.    }
  307.    *p = 0 ;
  308.    if (p == tstr && ! nullok)
  309.       bad_config() ;
  310.    return newstring(tstr) ;
  311. }
  312. /*
  313.  *   Now we have the getdefaults routine.
  314.  */
  315. static char *psmapfile = PSMAPFILE ;
  316. void
  317. getdefaults(s)
  318. char *s ;
  319. {
  320.    FILE *deffile ;
  321.    char PSname[300] ;
  322.    register char *p ;
  323.    int i, j ;
  324.    integer hsiz, vsiz ;
  325.    char *d = configpath ;
  326.    int canaddtopaper = 0 ;
  327.  
  328.    if (printer == NULL) {
  329.       if (s) {
  330.          strcpy(PSname, s) ;
  331.       } else {
  332. #ifndef VMCMS  /* IBM: VM/CMS - don't have home directory on VMCMS */
  333. #ifndef MVSXA
  334.          d = "~" ;
  335. #endif
  336. #endif  /* IBM: VM/CMS */
  337.          strcpy(PSname, DVIPSRC) ;
  338.       }
  339.    } else {
  340. #ifdef OS2
  341.       strcpy(PSname, printer) ;
  342.       strcat(PSname, ".cfg") ;
  343. #else
  344.       strcpy(PSname, "config.") ;
  345.       strcat(PSname, printer) ;
  346. #endif
  347.    }
  348.    if ((deffile=search(d,PSname,READ))!=NULL) {
  349.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  350. /*
  351.  *   We need to get rid of the newline.
  352.  */
  353.        for