Debian Codegraph @

texk/dvipsk/header.c

raw
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 declarations:
18 */
19 #include "protos.h"
20
21 static int add_name_general(const char *s, struct header_list **what,
22 char *pre, char *post);
23
24 int
25 add_name(const char *s, struct header_list **what)
26 {
27 return (int) add_name_general (s, what, NULL, NULL);
28 }
29
30 /*
31 * This more general routine adds a name to a list of unique
32 * names.
33 */
34 static int
35 add_name_general(const char *s, struct header_list **what, char *pre, char *post)
36 {
37 struct header_list *p, *q;
38
39 for (p = *what; p != NULL; p = p->next)
40 if (strcmp(p->name, s)==0)
41 return 0;
42 q = (struct header_list *)mymalloc((integer)(sizeof(struct header_list)));
43 q->name = mymalloc(strlen(s) + 1);
44 q->Hname = infont;
45 q->next = NULL;
46 q->precode = pre;
47 q->postcode = post;
48 strcpy(q->name, s);
49 if (*what == NULL)
50 *what = q;
51 else {
52 for (p = *what; p->next != NULL; p = p->next);
53 p->next = q;
54 }
55 return 1;
56 }
57 /*
58 * This function checks the virtual memory usage of a header file.
59 * If we can find a VMusage comment, we use that; otherwise, we use
60 * length of the file.
61 */
62 void
63 checkhmem(const char *s, char *pre, char *post)
64 {
65 FILE *f;
66
67 f = search(headerpath, s, READBIN);
68 if (pre || post) {
69 if (f==NULL)
70 f = search(figpath, s, READBIN);
71 }
72 if (f==0) {
73 char *msg = concat ("! Couldn't find header file: ", s);
74
75 if (secure == 2) {
76 msg = concat (msg,
77 "\nAbsolute and ../relative paths are denied in -R2 mode.");
78 }
79 error(msg);
80 } else {
81 int len, i, j;
82 long mem = -1;
83 char buf[1024];
84
85 len = fread(buf, sizeof(char), 1024, f);
86 for (i=0; i<len-20; i++)
87 if (buf[i]=='%' && strncmp(buf+i, "%%VMusage:", 10)==0) {
88 if (sscanf(buf+i+10, "%d %ld", &j, &mem) != 2)
89 mem = -1;
90 break;
91 }
92 if (mem == -1) {
93 mem = 0;
94 while (len > 0) {
95 mem += len;
96 len = fread(buf, sizeof(char), 1024, f);
97 }
98 }
99 if (mem < 0)
100 mem = DNFONTCOST;
101 (*close_file) (f);
102 #ifdef DEBUG
103 if (dd(D_HEADER))
104 fprintf_str(stderr, "Adding header file \"%s\" %ld\n",
105 s, mem);
106 #endif
107 fontmem -= mem;
108 if (fontmem > 0) /* so we don't count it twice. */
109 swmem -= mem;
110 }
111 }
112 /*
113 * This routine is responsible for adding a header file. We also
114 * calculate the VM usage. If we can find a VMusage comment, we
115 * use that; otherwise, we use the length of the file.
116 */
117 int
118 add_header(const char *s)
119 {
120 return (int) add_header_general (s, NULL, NULL);
121 }
122
123 int
124 add_header_general(const char *s, char *pre, char *post)
125 {
126 int r;
127
128 r = add_name_general(s, &header_head, pre, post);
129 if (r) {
130 if (headersready == 1) {
131 struct header_list *p = header_head;
132
133 while (p) {
134 checkhmem(p->name, p->precode, p->postcode);
135 p = p->next;
136 }
137 headersready = 2;
138 } else if (headersready == 2) {
139 checkhmem(s, pre, post);
140 }
141 }
142 return r;
143 }
144 /*
145 * This routine runs down a list, returning each in order.
146 */
147 static struct header_list *CUR_head = NULL;
148 char *
149 get_name(struct header_list **what)
150 {
151 if (what && *what) {
152 char *p = (*what)->name;
153 infont = (*what)->Hname;
154 CUR_head = *what;
155 *what = (*what)->next;
156 return p;
157 } else
158 return 0;
159 }
160 /*
161 * This routine actually sends the headers.
162 */
163 void
164 send_headers(void) {
165 struct header_list *p = header_head;
166 char *q;
167
168 while (0 != (q=get_name(&p))) {
169 #ifdef DEBUG
170 if (dd(D_HEADER))
171 fprintf_str(stderr, "Sending header file \"%s\"\n", q);
172 #endif
173 #ifdef HPS
174 if (HPS_FLAG) {
175 if (strcmp(q,"target.dct")==0) noprocset = 1;
176 }
177 #endif
178 copyfile_general(q, CUR_head);
179 }
180 infont = NULL;
181 }