Back to index

tetex-bin  3.0
header.c
Go to the documentation of this file.
00001 /*
00002  *   This routine handles the PostScript prologs that might
00003  *   be included through:
00004  *
00005  *      - Default
00006  *      - Use of PostScript fonts
00007  *      - Specific inclusion through specials, etc.
00008  *      - Use of graphic specials that require them.
00009  *
00010  *   Things are real simple.  We build a linked list of headers to
00011  *   include.  Then, when the time comes, we simply copy those
00012  *   headers down.
00013  */
00014 #include "dvips.h" /* The copyright notice in that file is included too! */
00015 struct header_list *header_head ;
00016 /*
00017  *   The external routines we use.
00018  */
00019 #include "protos.h"
00020 extern char errbuf[] ;
00021 extern integer fontmem, swmem ;
00022 #ifndef KPATHSEA
00023 extern char *headerpath ;
00024 #endif
00025 extern char *infont ;
00026 extern int headersready ;
00027 #ifdef DEBUG
00028 extern integer debug_flag ;
00029 #endif
00030 #ifdef HPS
00031 extern Boolean noprocset ;
00032 extern Boolean HPS_FLAG ;
00033 #endif
00034 /*
00035  *   This more general routine adds a name to a list of unique
00036  *   names.
00037  */
00038 int
00039 add_name P2C(char *, s, struct header_list **, what)
00040 {
00041    struct header_list *p, *q ;
00042 
00043    for (p = *what ; p != NULL; p = p->next)
00044       if (strcmp(p->name, s)==0)
00045          return 0 ;
00046    q = (struct header_list *)mymalloc((integer)(sizeof(struct header_list)
00047                                           + strlen(s))) ;
00048    q->Hname = infont ;
00049    q->next = NULL ;
00050    strcpy(q->name, s) ;
00051    if (*what == NULL)
00052       *what = q ;
00053    else {
00054       for (p = *what; p->next != NULL; p = p->next) ;
00055       p->next = q ;
00056    }
00057    return 1 ;
00058 }
00059 /*
00060  *   This function checks the virtual memory usage of a header file.
00061  *   If we can find a VMusage comment, we use that; otherwise, we use
00062  *   length of the file.
00063  */
00064 void checkhmem P1C(char *, s)
00065 {
00066    FILE *f = search(headerpath, s, READBIN) ;
00067 
00068    if (f==0) {
00069       (void)sprintf(errbuf, "! Couldn't find header file %s", s) ;
00070       error(errbuf) ;
00071    } else {
00072       int len, i, j ;
00073       long mem = -1 ;
00074       char buf[1024] ;
00075 
00076       len = fread(buf, sizeof(char), 1024, f) ;
00077       for (i=0; i<len-20; i++)
00078          if (buf[i]=='%' && strncmp(buf+i, "%%VMusage:", 10)==0) {
00079             if (sscanf(buf+i+10, "%d %ld", &j, &mem) != 2)
00080                mem = -1 ;
00081             break ;
00082          }
00083       if (mem == -1) {
00084          mem = 0 ;
00085          while (len > 0) {
00086             mem += len ;
00087             len = fread(buf, sizeof(char), 1024, f) ;
00088          }
00089       }
00090       if (mem < 0)
00091          mem = DNFONTCOST ;
00092       (*close_file) (f) ;
00093 #ifdef DEBUG
00094       if (dd(D_HEADER))
00095          (void)fprintf(stderr, "Adding header file \"%s\" %ld\n",
00096                                 s, mem) ;
00097 #endif
00098       fontmem -= mem ;
00099       if (fontmem > 0) /* so we don't count it twice. */
00100          swmem -= mem ;
00101    }
00102 }
00103 /*
00104  *   This routine is responsible for adding a header file.  We also
00105  *   calculate the VM usage.  If we can find a VMusage comment, we
00106  *   use that; otherwise, we use the length of the file.
00107  */
00108 int
00109 add_header P1C(char *, s)
00110 {
00111    int r ;
00112 
00113    r = add_name(s, &header_head) ;
00114    if (r) {
00115       if (headersready == 1) {
00116          struct header_list *p = header_head ;
00117 
00118          while (p) {
00119             checkhmem(p->name) ;
00120             p = p->next ;
00121          }
00122          headersready = 2 ;
00123       } else if (headersready == 2) {
00124          checkhmem(s) ;
00125       }
00126    }
00127    return r ;
00128 }
00129 /*
00130  *   This routine runs down a list, returning each in order.
00131  */
00132 char *
00133 get_name P1C(struct header_list **, what)
00134 {
00135    if (what && *what) {
00136       char *p = (*what)->name ;
00137       infont = (*what)->Hname ;
00138       *what =  (*what)->next ;
00139       return p ;
00140    } else
00141       return 0 ;
00142 }
00143 /*
00144  *   This routine actually sends the headers.
00145  */
00146 void
00147 send_headers P1H(void) {
00148    struct header_list *p = header_head ;
00149    char *q ;
00150 
00151    while (0 != (q=get_name(&p))) {
00152 #ifdef DEBUG
00153       if (dd(D_HEADER))
00154          (void)fprintf(stderr, "Sending header file \"%s\"\n", q) ;
00155 #endif
00156 #ifdef HPS
00157      if (HPS_FLAG) {
00158             if (strcmp(q,"target.dct")==0) noprocset = 1 ;
00159        }
00160 #endif
00161       copyfile(q) ;
00162    }
00163    infont = 0 ;
00164 }