home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dvips549.zip / DVIPS / HEADER.C < prev    next >
C/C++ Source or Header  |  1992-10-14  |  4KB  |  139 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 "structures.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. #ifdef DEBUG
  28. extern integer debug_flag ;
  29. #endif
  30. /*
  31.  *   This more general routine adds a name to a list of unique
  32.  *   names.
  33.  */
  34. int
  35. add_name(s, what)
  36.    char *s ;
  37.    struct header_list **what ;
  38. {
  39.    struct header_list *p, *q ;
  40.  
  41.    for (p = *what ; p != NULL; p = p->next)
  42.       if (strcmp(p->name, s)==0)
  43.          return 0 ;
  44.    q = (struct header_list *)mymalloc((integer)(sizeof(struct header_list)
  45.                                           + strlen(s))) ;
  46.    q->next = NULL ;
  47.    strcpy(q->name, s) ;
  48.    if (*what == NULL)
  49.       *what = q ;
  50.    else {
  51.       for (p = *what; p->next != NULL; p = p->next) ;
  52.       p->next = q ;
  53.    }
  54.    return 1 ;
  55. }
  56. /*
  57.  *   This routine is responsible for adding a header file.  We also
  58.  *   calculate the VM usage.  If we can find a VMusage comment, we
  59.  *   use that; otherwise, we use the length of the file.
  60.  */
  61. int
  62. add_header(s)
  63. char *s ;
  64. {
  65.    int r ;
  66.  
  67.    r = add_name(s, &header_head) ;
  68.    if (r) {
  69.       FILE *f = search(headerpath, s, READ) ;
  70.  
  71.       if (f==0) {
  72.          (void)sprintf(errbuf, "! Couldn't find header file %s", s) ;
  73.          error(errbuf) ;
  74.       } else {
  75.          int len, i, j ;
  76.          long mem = -1 ;
  77.          char buf[1024] ;
  78.  
  79.          len = fread(buf, sizeof(char), 1024, f) ;
  80.          for (i=0; i<len-20; i++)
  81.             if (buf[i]=='%' && strncmp(buf+i, "%%VMusage:", 10)==0) {
  82.                if (sscanf(buf+i+10, "%d %ld", &j, &mem) != 2)
  83.                   mem = -1 ;
  84.                break ;
  85.             }
  86.          if (mem == -1) {
  87.             mem = 0 ;
  88.             while (len > 0) {
  89.                mem += len ;
  90.                len = fread(buf, sizeof(char), 1024, f) ;
  91.             }
  92.          }
  93.          if (mem < 0)
  94.             mem = DNFONTCOST ;
  95.          fclose(f) ;
  96.          lastheadermem = mem ;
  97. #ifdef DEBUG
  98.          if (dd(D_HEADER))
  99.             (void)fprintf(stderr, "Adding header file \"%s\" %ld\n",
  100.                                    s, mem) ;
  101. #endif
  102.          fontmem -= mem ;
  103.          if (fontmem > 0) /* so we don't count it twice. */
  104.             swmem -= mem ;
  105.       }
  106.    }
  107.    return r ;
  108. }
  109. /*
  110.  *   This routine runs down a list, returning each in order.
  111.  */
  112. char *
  113. get_name(what)
  114.    struct header_list **what ;
  115. {
  116.    if (what && *what) {
  117.       char *p = (*what)->name ;
  118.       *what =  (*what)->next ;
  119.       return p ;
  120.    } else
  121.       return 0 ;
  122. }
  123. /*
  124.  *   This routine actually sends the headers.
  125.  */
  126. void
  127. send_headers() {
  128.    struct header_list *p = header_head ;
  129.    char *q ;
  130.  
  131.    while (q=get_name(&p)) {
  132. #ifdef DEBUG
  133.       if (dd(D_HEADER))
  134.          (void)fprintf(stderr, "Sending header file \"%s\"\n", q) ;
  135. #endif
  136.       copyfile(q) ;
  137.    }
  138. }
  139.