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