home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / header.c < prev    next >
C/C++ Source or Header  |  1993-01-31  |  4KB  |  164 lines

  1. /*
  2.  *   This routine handles the PostScript prologs that might
  3.  *   be included through:
  4.  *
  5.  *      - Default
  6.  *      - Use of PostScript fonts
  7.  *      - Specific inclusion through specials, etc.
  8.  *      - Use of graphic specials that require them.
  9.  *
  10.  *   Things are real simple.  We build a linked list of headers to
  11.  *   include.  Then, when the time comes, we simply copy those
  12.  *   headers down.
  13.  */
  14. #include "dvips.h" /* The copyright notice in that file is included too! */
  15. struct header_list *header_head ;
  16. /*
  17.  *   The external routines we use.
  18.  */
  19. extern char *newstring() ;
  20. extern void error() ;
  21. extern void copyfile() ;
  22. extern FILE *search() ;
  23. extern long lastheadermem ;
  24. extern char errbuf[] ;
  25. extern integer fontmem, swmem ;
  26. extern char *headerpath ;
  27. extern char *infont ;
  28. extern int headersready ;
  29. #ifdef DEBUG
  30. extern integer debug_flag ;
  31. #endif
  32. /*
  33.  *   This more general routine adds a name to a list of unique
  34.  *   names.
  35.  */
  36. int
  37. add_name(s, what)
  38.    char *s ;
  39.    struct header_list **what ;
  40. {
  41.    struct header_list *p, *q ;
  42.  
  43.    for (p = *what ; p != NULL; p = p->next)
  44.       if (strcmp(p->name, s)==0)
  45.          return 0 ;
  46.    q = (struct header_list *)mymalloc((integer)(sizeof(struct header_list)
  47.                                           + strlen(s))) ;
  48.    q->Hname = infont ;
  49.    q->next = NULL ;
  50.    strcpy(q->name, s) ;
  51.    if (*what == NULL)
  52.       *what = q ;
  53.    else {
  54.       for (p = *what; p->next != NULL; p = p->next) ;
  55.       p->next = q ;
  56.    }
  57.    return 1 ;
  58. }
  59. /*
  60.  *   This function checks the virtual memory usage of a header file.
  61.  *   If we can find a VMusage comment, we use that; otherwise, we use
  62.  *   length of the file.
  63.  */
  64. void checkhmem(s)
  65. char *s ;
  66. {
  67.    FILE *f = search(headerpath, s, READ) ;
  68.  
  69.    if (f==0) {
  70.       (void)sprintf(errbuf, "! Couldn't find header file %s", s) ;
  71.       error(errbuf) ;
  72.    } else {
  73.       int len, i, j ;
  74.       long mem = -1 ;
  75.       char buf[1024] ;
  76.  
  77.       len = fread(buf, sizeof(char), 1024, f) ;
  78.       for (i=0; i<len-20; i++)
  79.          if (buf[i]=='%' && strncmp(buf+i, "%%VMusage:", 10)==0) {
  80.             if (sscanf(buf+i+10, "%d %ld", &j, &mem) != 2)
  81.                mem = -1 ;
  82.             break ;
  83.          }
  84.       if (mem == -1) {
  85.          mem = 0 ;
  86.          while (len > 0) {
  87.             mem += len ;
  88.             len = fread(buf, sizeof(char), 1024, f) ;
  89.          }
  90.       }
  91.       if (mem < 0)
  92.          mem = DNFONTCOST ;
  93.       fclose(f) ;
  94.       lastheadermem = mem ;
  95. #ifdef DEBUG
  96.       if (dd(D_HEADER))
  97.          (void)fprintf(stderr, "Adding header file \"%s\" %ld\n",
  98.                                 s, mem) ;
  99. #endif
  100.       fontmem -= mem ;
  101.       if (fontmem > 0) /* so we don't count it twice. */
  102.          swmem -= mem ;
  103.    }
  104. }
  105. /*
  106.  *   This routine is responsible for adding a header file.  We also
  107.  *   calculate the VM usage.  If we can find a VMusage comment, we
  108.  *   use that; otherwise, we use the length of the file.
  109.  */
  110. int
  111. add_header(s)
  112. char *s ;
  113. {
  114.    int r ;
  115.  
  116.    r = add_name(s, &header_head) ;
  117.    if (r) {
  118.       if (headersready == 1) {
  119.          struct header_list *p = header_head ;
  120.  
  121.          while (p) {
  122.             checkhmem(p->name) ;
  123.             p = p->next ;
  124.          }
  125.          headersready = 2 ;
  126.       } else if (headersready == 2) {
  127.          checkhmem(s) ;
  128.       }
  129.    }
  130.    return r ;
  131. }
  132. /*
  133.  *   This routine runs down a list, returning each in order.
  134.  */
  135. char *
  136. get_name(what)
  137.    struct header_list **what ;
  138. {
  139.    if (what && *what) {
  140.       char *p = (*what)->name ;
  141.       infont = (*what)->Hname ;
  142.       *what =  (*what)->next ;
  143.       return p ;
  144.    } else
  145.       return 0 ;
  146. }
  147. /*
  148.  *   This routine actually sends the headers.
  149.  */
  150. void
  151. send_headers() {
  152.    struct header_list *p = header_head ;
  153.    char *q ;
  154.  
  155.    while (0 != (q=get_name(&p))) {
  156. #ifdef DEBUG
  157.       if (dd(D_HEADER))
  158.          (void)fprintf(stderr, "Sending header file \"%s\"\n", q) ;
  159. #endif
  160.       copyfile(q) ;
  161.    }
  162.    infont = 0 ;
  163. }
  164.