summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipsk/header.c
blob: c3b5539b65cb3dfd2f21589c41cdb1e5a94c0947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 *   This routine handles the PostScript prologs that might
 *   be included through:
 *
 *      - Default
 *      - Use of PostScript fonts
 *      - Specific inclusion through specials, etc.
 *      - Use of graphic specials that require them.
 *
 *   Things are real simple.  We build a linked list of headers to
 *   include.  Then, when the time comes, we simply copy those
 *   headers down.
 */
#include "dvips.h" /* The copyright notice in that file is included too! */
struct header_list *header_head ;
/*
 *   The external routines we use.
 */
#include "protos.h"
extern char errbuf[] ;
extern integer fontmem, swmem ;
#ifndef KPATHSEA
extern char *headerpath ;
#endif
extern char *infont ;
extern int headersready ;
#ifdef DEBUG
extern integer debug_flag ;
#endif
#ifdef HPS
extern Boolean noprocset ;
extern Boolean HPS_FLAG ;
#endif

int
add_name P2C(char *, s, struct header_list **, what)
{
  return (int) add_name_general (s, what, NULL, NULL);
}

/*
 *   This more general routine adds a name to a list of unique
 *   names.
 */
int
add_name_general P4C(char *, s, struct header_list **, what, char *, pre, char *, post)
{
   struct header_list *p, *q ;

   for (p = *what ; p != NULL; p = p->next)
      if (strcmp(p->name, s)==0)
         return 0 ;
   q = (struct header_list *)mymalloc((integer)(sizeof(struct header_list)
                                          + strlen(s))) ;
   q->Hname = infont ;
   q->next = NULL ;
   q->precode = pre ;
   q->postcode = post ;
   strcpy(q->name, s) ;
   if (*what == NULL)
      *what = q ;
   else {
      for (p = *what; p->next != NULL; p = p->next) ;
      p->next = q ;
   }
   return 1 ;
}
/*
 *   This function checks the virtual memory usage of a header file.
 *   If we can find a VMusage comment, we use that; otherwise, we use
 *   length of the file.
 */
void checkhmem P3C(char *, s, char *, pre, char *, post)
{
   FILE *f ;

   f = search(headerpath, s, READBIN) ;
   if (pre || post) {
      if (f==NULL)
	 f = search(figpath, s, READBIN) ;
   }
   if (f==0) {
      (void)sprintf(errbuf, "! Couldn't find header file %s.\nNote that an absolute path or a relative path with .. are denied in -R2 mode.", s) ;
      error(errbuf) ;
   } else {
      int len, i, j ;
      long mem = -1 ;
      char buf[1024] ;

      len = fread(buf, sizeof(char), 1024, f) ;
      for (i=0; i<len-20; i++)
         if (buf[i]=='%' && strncmp(buf+i, "%%VMusage:", 10)==0) {
            if (sscanf(buf+i+10, "%d %ld", &j, &mem) != 2)
               mem = -1 ;
            break ;
         }
      if (mem == -1) {
         mem = 0 ;
         while (len > 0) {
            mem += len ;
            len = fread(buf, sizeof(char), 1024, f) ;
         }
      }
      if (mem < 0)
         mem = DNFONTCOST ;
      (*close_file) (f) ;
#ifdef DEBUG
      if (dd(D_HEADER))
         (void)fprintf(stderr, "Adding header file \"%s\" %ld\n",
                                s, mem) ;
#endif
      fontmem -= mem ;
      if (fontmem > 0) /* so we don't count it twice. */
         swmem -= mem ;
   }
}
/*
 *   This routine is responsible for adding a header file.  We also
 *   calculate the VM usage.  If we can find a VMusage comment, we
 *   use that; otherwise, we use the length of the file.
 */
int
add_header P1C(char *, s)
{
  return (int) add_header_general (s, NULL, NULL);
}

int
add_header_general P3C(char *, s, char *, pre, char *, post)
{
   int r ;

   r = add_name_general(s, &header_head, pre, post) ;
   if (r) {
      if (headersready == 1) {
         struct header_list *p = header_head ;

         while (p) {
            checkhmem(p->name, p->precode, p->postcode) ;
            p = p->next ;
         }
         headersready = 2 ;
      } else if (headersready == 2) {
         checkhmem(s, pre, post) ;
      }
   }
   return r ;
}
/*
 *   This routine runs down a list, returning each in order.
 */
static struct header_list *CUR_head = NULL ;
char *
get_name P1C(struct header_list **, what)
{
   if (what && *what) {
      char *p = (*what)->name ;
      infont = (*what)->Hname ;
      CUR_head = *what ;
      *what =  (*what)->next ;
      return p ;
   } else
      return 0 ;
}
/*
 *   This routine actually sends the headers.
 */
void
send_headers P1H(void) {
   struct header_list *p = header_head ;
   char *q ;

   while (0 != (q=get_name(&p))) {
#ifdef DEBUG
      if (dd(D_HEADER))
         (void)fprintf(stderr, "Sending header file \"%s\"\n", q) ;
#endif
#ifdef HPS
     if (HPS_FLAG) {
 	     if (strcmp(q,"target.dct")==0) noprocset = 1 ;
       }
#endif
      copyfile_general(q, CUR_head) ;
   }
   infont = 0 ;
}