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