| 1 |
/* |
| 2 |
* select pages from a DVI file. |
| 3 |
* |
| 4 |
* Copyright (c) 1999 |
| 5 |
* WATANABE Takeshi [email protected] |
| 6 |
* Copyright (c) 2002-2004 the xdvik development team |
| 7 |
* |
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
* of this software and associated documentation files (the "Software"), to |
| 10 |
* deal in the Software without restriction, including without limitation the |
| 11 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 12 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 13 |
* furnished to do so, subject to the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be included in |
| 16 |
* all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
* NONINFRINGEMENT. IN NO EVENT SHALL ANY AUTHO OF THIS SOFTWARE BE |
| 22 |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 |
* SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "xdvi-config.h" |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
#include <ctype.h> |
| 32 |
|
| 33 |
#include "dvi.h" |
| 34 |
#include "pagesel.h" |
| 35 |
#include "dvisel.h" |
| 36 |
#include "util.h" |
| 37 |
#include "dvi-init.h" |
| 38 |
#include "events.h" |
| 39 |
#include "dvi-draw.h" |
| 40 |
#include "message-window.h" |
| 41 |
#include "statusline.h" |
| 42 |
#include "print-dialog.h" |
| 43 |
|
| 44 |
/* stack for HTEX / Color stack */ |
| 45 |
typedef enum { IS_A_HREF, IS_A_COLOR } stackElemT; |
| 46 |
|
| 47 |
/* |
| 48 |
* Length of BOP command, without p[4] (previous BOP pointer): |
| 49 |
* bop c_0[4] ... c_9[4] |
| 50 |
*/ |
| 51 |
static const int BOP_PART_LEN = 1 + 10 * 4; |
| 52 |
/* |
| 53 |
* Like above but with p[4]: |
| 54 |
*/ |
| 55 |
static const int BOP_LEN = 1 + 10 * 4 + 4; |
| 56 |
/* |
| 57 |
* Lenght of postamble: |
| 58 |
* post p[4] num[4] den[4] mag[4] l[4] u[4] s[2] t[2] |
| 59 |
*/ |
| 60 |
static const int POSTAMBLE_LEN = 1 + 4 * 6 + 2 + 2; |
| 61 |
/* |
| 62 |
* Postamble without p[4] (final bop offset) and t[2] (total page number): |
| 63 |
* num[4] den[4] mag[4] l[4] u[4] s[2] |
| 64 |
*/ |
| 65 |
static const int POSTAMBLE_PART_LEN = 4 * 5 + 2; |
| 66 |
|
| 67 |
static char *dvips_papersize_special = NULL; |
| 68 |
|
| 69 |
/* |
| 70 |
* Struct to save specials that need to be copied verbatim into the output file |
| 71 |
* (things like `draftcopy'; see dvips manual, `Literal headers') |
| 72 |
*/ |
| 73 |
struct literal_headers { |
| 74 |
size_t size; |
| 75 |
char **headers; |
| 76 |
}; |
| 77 |
static struct literal_headers literal_headers = { 0, NULL }; |
| 78 |
|
| 79 |
static struct specials_stack color_stack = { 0, NULL}; |
| 80 |
static struct specials_stack href_stack = { 0, NULL}; |
| 81 |
|
| 82 |
/* |
| 83 |
stacks saved from the previous page (i.e. active at the |
| 84 |
beginning of the page) |
| 85 |
*/ |
| 86 |
static struct specials_stack color_save_stack = { 0, NULL }; |
| 87 |
static struct specials_stack href_save_stack = { 0, NULL }; |
| 88 |
|
| 89 |
static void |
| 90 |
push_stack(struct specials_stack *stack, const char *content) |
| 91 |
{ |
| 92 |
stack->items = xrealloc(stack->items, (stack->stack_len + 1) * sizeof *(stack->items)); |
| 93 |
stack->items[stack->stack_len].content = xstrdup(content); |
| 94 |
stack->stack_len++; |
| 95 |
} |
| 96 |
|
| 97 |
static Boolean |
| 98 |
stack_contains_item(struct specials_stack *stack, const char *str) |
| 99 |
{ |
| 100 |
size_t i; |
| 101 |
for (i = 0; i < stack->stack_len; i++) { |
| 102 |
if (strcmp(str, stack->items[i].content) == 0) { |
| 103 |
return True; |
| 104 |
} |
| 105 |
} |
| 106 |
return False; |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
pop_stack(struct specials_stack *stack) |
| 111 |
{ |
| 112 |
ASSERT(stack->stack_len >= 1, "Attempt to pop empty stack"); |
| 113 |
free(stack->items[stack->stack_len - 1].content); |
| 114 |
stack->stack_len--; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
empty_stack(struct specials_stack *stack) |
| 119 |
{ |
| 120 |
while(stack->stack_len > 0) { |
| 121 |
pop_stack(stack); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
This is used instead of struct tn, and struct font* from dvi-init.h, |
| 127 |
since it appears that we don't need all the information from there. |
| 128 |
*/ |
| 129 |
static struct fontinfo { |
| 130 |
long TeXnumber; |
| 131 |
unsigned char info[14]; |
| 132 |
char *fontname; |
| 133 |
struct fontinfo *next; |
| 134 |
Boolean used; |
| 135 |
} *fontinfo_head; |
| 136 |
|
| 137 |
#define PutFour(num, fp) { \ |
| 138 |
putc(((num) >> 24) & 0xff, (fp)); \ |
| 139 |
putc(((num) >> 16) & 0xff, (fp)); \ |
| 140 |
putc(((num) >> 8) & 0xff, (fp)); \ |
| 141 |
putc( (num) & 0xff, (fp)); \ |
| 142 |
} |
| 143 |
|
| 144 |
#define CopyNum(fin, fout, num) { \ |
| 145 |
int m = num; \ |
| 146 |
while (--m >= 0) putc(getc(fin), fout); \ |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
FontWrite(FILE *fout, struct fontinfo *fontp, long *fout_pos) |
| 151 |
{ |
| 152 |
if (fontp->TeXnumber > 0xff) { |
| 153 |
if (fontp->TeXnumber > 0xffff) { |
| 154 |
if (fontp->TeXnumber > 0xffffff) { |
| 155 |
putc(FNTDEF4, fout); |
| 156 |
putc((fontp->TeXnumber >> 24) & 0xff, fout); |
| 157 |
(*fout_pos)++; |
| 158 |
} |
| 159 |
else { |
| 160 |
putc(FNTDEF3, fout); |
| 161 |
} |
| 162 |
putc((fontp->TeXnumber >> 16) & 0xff, fout); |
| 163 |
(*fout_pos)++; |
| 164 |
} |
| 165 |
else { |
| 166 |
putc(FNTDEF2, fout); |
| 167 |
} |
| 168 |
putc((fontp->TeXnumber >> 8) & 0xff, fout); |
| 169 |
(*fout_pos)++; |
| 170 |
} |
| 171 |
else { |
| 172 |
putc(FNTDEF1, fout); |
| 173 |
} |
| 174 |
putc(fontp->TeXnumber & 0xff, fout); |
| 175 |
(void)fwrite(fontp->info, sizeof(char), 14, fout); |
| 176 |
(void)fwrite(fontp->fontname, sizeof(char), fontp->info[12] + fontp->info[13], fout); |
| 177 |
(*fout_pos) += 2 + 14 + fontp->info[12] + fontp->info[13]; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
Invoked by spcl_scan; saves file names referenced by special commands |
| 182 |
into the `warn_files' field of `info'. |
| 183 |
*/ |
| 184 |
static Boolean |
| 185 |
scan_for_included_files(char *special, int str_len, void *my_info) |
| 186 |
{ |
| 187 |
struct select_pages_info *info = (struct select_pages_info *)my_info; |
| 188 |
UNUSED(str_len); |
| 189 |
if (info->warn_files.items == NULL) /* no info needed, e.g. when printing */ |
| 190 |
return True; |
| 191 |
|
| 192 |
while (*special == ' ' || *special == '\t') |
| 193 |
++special; |
| 194 |
|
| 195 |
if (memicmp(special, "psfile=", strlen("psfile=")) == 0) { |
| 196 |
char *b_ptr = special + strlen("psfile="); |
| 197 |
char *e_ptr; |
| 198 |
|
| 199 |
while (*b_ptr == '"') |
| 200 |
b_ptr++; |
| 201 |
|
| 202 |
if ((e_ptr = strchr(b_ptr, '"')) != NULL) { |
| 203 |
size_t len = e_ptr - b_ptr; |
| 204 |
char *tmp = xmalloc(len + 1); |
| 205 |
memcpy(tmp, b_ptr, len); |
| 206 |
tmp[len] = '\0'; |
| 207 |
if (!stack_contains_item(&(info->warn_files), tmp)) { |
| 208 |
push_stack(&(info->warn_files), tmp); |
| 209 |
} |
| 210 |
free(tmp); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return True; /* dummy */ |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
Invoked by spcl_scan; saves hyperref and color commands |
| 219 |
into the global stacks `color_stack' and `hyperref_stack'. |
| 220 |
FIXME: In reality char *special is treated as const, but the declaration |
| 221 |
of spcl_scan doesn't allow for this. |
| 222 |
*/ |
| 223 |
static Boolean |
| 224 |
stack_save(char *special, int str_len, void *data) |
| 225 |
{ |
| 226 |
char *ptr; |
| 227 |
size_t len; |
| 228 |
|
| 229 |
UNUSED(data); |
| 230 |
UNUSED(str_len); |
| 231 |
|
| 232 |
while (isspace((int)*special)) |
| 233 |
++special; |
| 234 |
|
| 235 |
ptr = special; |
| 236 |
|
| 237 |
if (memicmp(special, "color ", len = strlen("color ")) == 0) { |
| 238 |
special += len; |
| 239 |
while (*special == ' ' || *special == '\t') |
| 240 |
++special; |
| 241 |
if (memicmp(special, "push ", 5) == 0) { |
| 242 |
push_stack(&color_stack, ptr); |
| 243 |
} |
| 244 |
else if (memicmp(special, "pop", 3) == 0) { |
| 245 |
pop_stack(&color_stack); |
| 246 |
} |
| 247 |
} |
| 248 |
else if (memicmp(special, "html:", len = strlen("html:")) == 0) { |
| 249 |
/* fprintf(stderr, "++++ special: %s; fd: %ld\n", special + 5, ftell(globals.dvi_file.bak_fp)); */ |
| 250 |
special += len; |
| 251 |
if (memicmp(special, "<a href=", 8) == 0 |
| 252 |
|| memicmp(special, "<a name=", 8) == 0) { |
| 253 |
push_stack(&href_stack, ptr); |
| 254 |
} |
| 255 |
else if (memicmp(special, "</a>", 4) == 0) { |
| 256 |
pop_stack(&href_stack); |
| 257 |
} |
| 258 |
} |
| 259 |
else if (memicmp(special, "papersize", len = strlen("papersize")) == 0) { |
| 260 |
free(dvips_papersize_special); |
| 261 |
dvips_papersize_special = xstrdup(special); |
| 262 |
} |
| 263 |
else if (*special == '!' |
| 264 |
|| memcmp(special, "header", strlen("header")) == 0) { |
| 265 |
size_t idx = literal_headers.size++; |
| 266 |
if (globals.debug & DBG_GUI) |
| 267 |
fprintf(stderr, "(literal) header %lu: |%s|\n", (unsigned long)idx, special); |
| 268 |
literal_headers.headers = xrealloc(literal_headers.headers, |
| 269 |
literal_headers.size * sizeof *(literal_headers.headers)); |
| 270 |
literal_headers.headers[idx] = xstrdup(special); |
| 271 |
} |
| 272 |
return True; /* dummy */ |
| 273 |
} |
| 274 |
|
| 275 |
/* write a special to the FILE *fp, updating offset as appropriate. */ |
| 276 |
static void |
| 277 |
write_special(const char *str, FILE *fp, long *offset) |
| 278 |
{ |
| 279 |
unsigned int len = strlen(str); |
| 280 |
if (len < 256) { |
| 281 |
unsigned char c = len; |
| 282 |
putc(XXX1, fp); |
| 283 |
putc(c, fp); |
| 284 |
*offset += 2; |
| 285 |
} |
| 286 |
else { /* how about xxx2, xxx3? It seems that TeX doesn't use them either? */ |
| 287 |
putc(XXX4, fp); |
| 288 |
PutFour(len, fp); |
| 289 |
*offset += 5; |
| 290 |
} |
| 291 |
fputs(str, fp); |
| 292 |
*offset += len; |
| 293 |
} |
| 294 |
|
| 295 |
/* |
| 296 |
* Dump the headers in the global list `literal_headers', |
| 297 |
* and free up resources |
| 298 |
*/ |
| 299 |
static void |
| 300 |
dump_literal_headers(FILE *fp, long *pos) |
| 301 |
{ |
| 302 |
size_t i; |
| 303 |
for (i = 0; i < literal_headers.size; i++) { |
| 304 |
write_special(literal_headers.headers[i], fp, pos); |
| 305 |
free(literal_headers.headers[i]); |
| 306 |
} |
| 307 |
free(literal_headers.headers); |
| 308 |
literal_headers.headers = NULL; |
| 309 |
literal_headers.size = 0; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
static void |
| 314 |
scan_page_for_specials(FILE *fp, int page, Boolean save_stack, |
| 315 |
Boolean (*special_proc)(char *str, int str_len, void *data), |
| 316 |
struct select_pages_info *info) |
| 317 |
{ |
| 318 |
off_t pos_save; |
| 319 |
static ubyte my_scan_buffer[DVI_BUFFER_LEN]; |
| 320 |
|
| 321 |
struct drawinf currinf_save; |
| 322 |
ubyte maxchar_save; |
| 323 |
size_t i; |
| 324 |
TRACE_GUI((stderr, "parsing page: %d", page)); |
| 325 |
if (save_stack) { |
| 326 |
/* |
| 327 |
first, copy the current stacks (which reflect the state on the |
| 328 |
preceding page) to color_save_stack and href_save_stack: |
| 329 |
*/ |
| 330 |
empty_stack(&color_save_stack); |
| 331 |
empty_stack(&href_save_stack); |
| 332 |
for (i = 0; i < color_stack.stack_len; i++) { |
| 333 |
TRACE_GUI((stderr, "saving stack: |%s|", color_stack.items[i].content)); |
| 334 |
push_stack(&color_save_stack, color_stack.items[i].content); |
| 335 |
} |
| 336 |
for (i = 0; i < href_stack.stack_len; i++) { |
| 337 |
TRACE_GUI((stderr, "saving stack: |%s|", href_stack.items[i].content)); |
| 338 |
push_stack(&href_save_stack, href_stack.items[i].content); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/* (void)fseek(fp, pageinfo_get_offset(page), SEEK_SET); */ |
| 343 |
/* |
| 344 |
The datastructures in dvi-draw are rather weird in requiring |
| 345 |
us to provide a buffer, and point the global currinf.pos and end |
| 346 |
pointers to that buffer, for spcl_scan to be able to scan the file. |
| 347 |
First save the contents of the exising currinf, pointing to the main file: |
| 348 |
*/ |
| 349 |
pos_save = save_file_status(fp, &currinf_save, &maxchar_save); |
| 350 |
|
| 351 |
lseek(fileno(fp), pageinfo_get_offset(page), SEEK_SET); |
| 352 |
memset((char *)&currinf.data, 0, sizeof currinf.data); |
| 353 |
currinf.tn_table_len = TNTABLELEN; |
| 354 |
currinf.tn_table = tn_table; |
| 355 |
currinf.tn_head = tn_head; |
| 356 |
|
| 357 |
/* then point currinf to our own buffer: */ |
| 358 |
G_dvi_buf_ptr = my_scan_buffer; |
| 359 |
currinf.pos = currinf.end = G_dvi_buf_ptr; |
| 360 |
lseek(fileno(fp), pageinfo_get_offset(page), SEEK_SET); |
| 361 |
/* |
| 362 |
finally we may invoke spcl_scan(). I hope we can do without |
| 363 |
the |
| 364 |
(!setjmp(some_env)) |
| 365 |
black magic since there shouldn't be any interruptions ... |
| 366 |
*/ |
| 367 |
#if 1 |
| 368 |
spcl_scan(special_proc, info, False, fp); |
| 369 |
#else |
| 370 |
/* this is an attempt at using setjmp(), but it's just too ugly ... */ |
| 371 |
for (;;) { |
| 372 |
int page_bak; |
| 373 |
if (read_events(EV_NOWAIT) & EV_GE_NEWPAGE) |
| 374 |
break; |
| 375 |
page_bak = page; |
| 376 |
if (!setjmp(globals.ev.canit)) { |
| 377 |
fprintf(stderr, "current position: %lu; seeking to: %lu\n", |
| 378 |
(unsigned long)pos_save, |
| 379 |
(unsigned long)pageinfo_get_offset(page)); |
| 380 |
(void) fprintf(stderr, "seeking to offset %ld, page %d of file %d\n", |
| 381 |
pageinfo_get_offset(page), |
| 382 |
page, fileno(globals.dvi_file.bak_fp)); |
| 383 |
fseek(fp, pageinfo_get_offset(page), SEEK_SET); |
| 384 |
spcl_scan(special_proc, info, False); |
| 385 |
} |
| 386 |
else { /* if interrupted */ |
| 387 |
fprintf(stderr, "========= interrupted!\n"); |
| 388 |
if (page >= page_bak) |
| 389 |
page = page_bak; |
| 390 |
break; |
| 391 |
} |
| 392 |
if (page >= current_page) |
| 393 |
break; |
| 394 |
page++; |
| 395 |
} |
| 396 |
#endif |
| 397 |
/* now we restore those pesky globals, hoping that nobody |
| 398 |
has stamped upon them in the meantime. */ |
| 399 |
restore_file_status(fp, currinf_save, maxchar_save, pos_save); |
| 400 |
|
| 401 |
/* reposition file pointer */ |
| 402 |
(void)fseek(fp, pageinfo_get_offset(page), SEEK_SET); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/* dump the `open' commands on the current stack to the FILE *fp */ |
| 407 |
static void |
| 408 |
stack_dump_open_commands(FILE *fp, long *pos) |
| 409 |
{ |
| 410 |
size_t i; |
| 411 |
TRACE_GUI((stderr, "length of stacks: %lu, %lu", |
| 412 |
(unsigned long)color_save_stack.stack_len, |
| 413 |
(unsigned long)href_save_stack.stack_len)); |
| 414 |
for (i = 0; i < color_save_stack.stack_len; i++) { |
| 415 |
TRACE_GUI((stderr, "dumping: |%s|", color_save_stack.items[i].content)); |
| 416 |
write_special(color_save_stack.items[i].content, fp, pos); |
| 417 |
} |
| 418 |
for (i = 0; i < href_save_stack.stack_len; i++) { |
| 419 |
TRACE_GUI((stderr, "dumping: |%s|", href_save_stack.items[i].content)); |
| 420 |
write_special(href_save_stack.items[i].content, fp, pos); |
| 421 |
} |
| 422 |
if (dvips_papersize_special != NULL) |
| 423 |
write_special(dvips_papersize_special, fp, pos); |
| 424 |
TRACE_GUI((stderr, "end of dumping open\n")); |
| 425 |
} |
| 426 |
|
| 427 |
/* dump `close' commands for all the `open' commands on the |
| 428 |
current stack (not the saved one, but the one active at the end of the page) |
| 429 |
to the FILE *fp (closing tags for hyperref anchors, |
| 430 |
`pop' commands for color specials). |
| 431 |
*/ |
| 432 |
static void |
| 433 |
stack_dump_close_commands(FILE *fp, long *pos) |
| 434 |
{ |
| 435 |
size_t i; |
| 436 |
|
| 437 |
for (i = color_stack.stack_len; i > 0; i--) { |
| 438 |
TRACE_GUI((stderr, "===== color pop")); |
| 439 |
write_special("color pop", fp, pos); |
| 440 |
} |
| 441 |
for (i = href_stack.stack_len; i > 0; i--) { |
| 442 |
TRACE_GUI((stderr, "===== html:</a>")); |
| 443 |
write_special("html:</a>", fp, pos); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
static void |
| 449 |
WriteDVI(FILE *fin, FILE *fout, long *fout_pos, int c) |
| 450 |
{ |
| 451 |
int i, n; |
| 452 |
struct fontinfo *fontp; |
| 453 |
|
| 454 |
if (c >= FNTNUM0 && c <= FNT4) { |
| 455 |
if (c >= FNT1) |
| 456 |
n = get_bytes(fin, c - FNT1 + 1); |
| 457 |
else |
| 458 |
n = c - FNTNUM0; |
| 459 |
for (fontp = fontinfo_head; fontp; fontp = fontp->next) |
| 460 |
if (n == fontp->TeXnumber) |
| 461 |
break; |
| 462 |
if (fontp && fontp->used == False) { |
| 463 |
fontp->used = True; |
| 464 |
FontWrite(fout, fontp, fout_pos); |
| 465 |
} |
| 466 |
putc(c, fout); |
| 467 |
(*fout_pos)++; |
| 468 |
switch (c) { |
| 469 |
case FNT4: |
| 470 |
putc((n >> 24) & 0xff, fout); |
| 471 |
(*fout_pos)++; |
| 472 |
case FNT3: |
| 473 |
putc((n >> 16) & 0xff, fout); |
| 474 |
(*fout_pos)++; |
| 475 |
case FNT2: |
| 476 |
putc((n >> 8) & 0xff, fout); |
| 477 |
(*fout_pos)++; |
| 478 |
case FNT1: |
| 479 |
putc(n & 0xff, fout); |
| 480 |
(*fout_pos)++; |
| 481 |
default: |
| 482 |
break; |
| 483 |
} |
| 484 |
} |
| 485 |
else if (c >= FNTDEF1 && c <= FNTDEF4) { |
| 486 |
n = get_bytes(fin, c - FNTDEF1 + 1); |
| 487 |
for (fontp = fontinfo_head; fontp; fontp = fontp->next) |
| 488 |
if (n == fontp->TeXnumber) |
| 489 |
break; |
| 490 |
if (fontp && fontp->used == False) { |
| 491 |
fontp->used = True; |
| 492 |
FontWrite(fout, fontp, fout_pos); |
| 493 |
} |
| 494 |
(void) get_bytes(fin, 12); |
| 495 |
(void) get_bytes(fin, (int)(get_byte(fin) + get_byte(fin))); |
| 496 |
} |
| 497 |
else { |
| 498 |
putc(c, fout); |
| 499 |
(*fout_pos)++; |
| 500 |
n = 0; |
| 501 |
if (c <= XXX4) { |
| 502 |
for (i = 0; i < c - XXX1 + 1; i++) { |
| 503 |
int x = get_byte(fin); |
| 504 |
putc(x, fout); |
| 505 |
(*fout_pos)++; |
| 506 |
n = (n << 8) | x; |
| 507 |
} |
| 508 |
} |
| 509 |
switch (c) { |
| 510 |
case SETRULE: |
| 511 |
case PUTRULE: |
| 512 |
n += 4; |
| 513 |
/* fall through */ |
| 514 |
|
| 515 |
case RIGHT4: |
| 516 |
case W4: |
| 517 |
case X4: |
| 518 |
case DOWN4: |
| 519 |
case Y4: |
| 520 |
case Z4: |
| 521 |
n++; |
| 522 |
/* fall through */ |
| 523 |
|
| 524 |
case RIGHT3: |
| 525 |
case W3: |
| 526 |
case X3: |
| 527 |
case DOWN3: |
| 528 |
case Y3: |
| 529 |
case Z3: |
| 530 |
n++; |
| 531 |
/* fall through */ |
| 532 |
|
| 533 |
case SET2: |
| 534 |
case PUT2: |
| 535 |
if (!resource.omega) { |
| 536 |
dvi_fmt_error("%s:%d: WriteDVI: op-code %d only works with the \"-omega\" option", |
| 537 |
__FILE__, __LINE__, c); |
| 538 |
} |
| 539 |
case RIGHT2: |
| 540 |
case W2: |
| 541 |
case X2: |
| 542 |
case DOWN2: |
| 543 |
case Y2: |
| 544 |
case Z2: |
| 545 |
n++; |
| 546 |
/* fall through */ |
| 547 |
|
| 548 |
case SET1: |
| 549 |
case PUT1: |
| 550 |
case RIGHT1: |
| 551 |
case W1: |
| 552 |
case X1: |
| 553 |
case DOWN1: |
| 554 |
case Y1: |
| 555 |
case Z1: |
| 556 |
#ifdef PTEX |
| 557 |
case TDIR: |
| 558 |
#endif |
| 559 |
n++; |
| 560 |
/* fall through */ |
| 561 |
|
| 562 |
case XXX1: |
| 563 |
case XXX2: |
| 564 |
case XXX3: |
| 565 |
case XXX4: |
| 566 |
CopyNum(fin, fout, n); |
| 567 |
(*fout_pos) += n; |
| 568 |
/* fall through */ |
| 569 |
|
| 570 |
default: |
| 571 |
break; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
/* public functions */ |
| 577 |
|
| 578 |
|
| 579 |
/* callback functions for selecting pages */ |
| 580 |
Boolean |
| 581 |
check_pagerange(struct save_or_print_info *info, int page) |
| 582 |
{ |
| 583 |
return (page >= info->pinfo->from && page <= info->pinfo->to); |
| 584 |
} |
| 585 |
|
| 586 |
Boolean |
| 587 |
check_marked(struct save_or_print_info *info, int page) |
| 588 |
{ |
| 589 |
UNUSED(info); |
| 590 |
return pageinfo_is_marked(page); |
| 591 |
} |
| 592 |
|
| 593 |
void |
| 594 |
select_pages(struct save_or_print_info *info) |
| 595 |
{ |
| 596 |
struct select_pages_info *pinfo = info->pinfo; |
| 597 |
int c, n, page, pagecnt; |
| 598 |
long fout_pos = 0L; |
| 599 |
unsigned long curr_page_offset = 0xffffffff; /* pattern to be overwritten later */ |
| 600 |
unsigned long post_cmd_offset = 0xffffffff; /* pattern to be overwritten later */ |
| 601 |
struct fontinfo *fontp; |
| 602 |
FILE *in_fp = info->finfo->in_fp; |
| 603 |
FILE *out_fp = info->finfo->tmp_dvi_fp; |
| 604 |
|
| 605 |
Boolean headers_dumped = False; |
| 606 |
free(dvips_papersize_special); /* re-initialize */ |
| 607 |
dvips_papersize_special = NULL; |
| 608 |
|
| 609 |
ASSERT(in_fp != NULL, "input file mustn't be NULL in select_pages()!"); |
| 610 |
ASSERT(out_fp != NULL, "output file mustn't be NULL in select_pages()!"); |
| 611 |
|
| 612 |
pinfo->errflag = NO_ERROR; /* reset errflag, in case we're recovering from a previous error */ |
| 613 |
|
| 614 |
/* get font list from postamble; in_fp already has been positioned |
| 615 |
at correct position (start of postamble) in caller. */ |
| 616 |
(void)fseek(in_fp, ftell(in_fp), SEEK_SET); /* paranoia */ |
| 617 |
(void)get_bytes(in_fp, POSTAMBLE_LEN); |
| 618 |
fontinfo_head = NULL; |
| 619 |
for (;;) { |
| 620 |
if ((c = get_byte(in_fp)) < FNTDEF1 || c > FNTDEF4) /* maybe POSTPOST */ |
| 621 |
break; |
| 622 |
fontp = (struct fontinfo *) xmalloc(sizeof(struct fontinfo)); |
| 623 |
fontp->TeXnumber = get_bytes(in_fp, c - FNTDEF1 + 1); |
| 624 |
(void)fread(fontp->info, sizeof(char), 14, in_fp); |
| 625 |
n = fontp->info[12] + fontp->info[13]; |
| 626 |
fontp->fontname = xmalloc(n); |
| 627 |
(void)fread(fontp->fontname, sizeof(char), n, in_fp); |
| 628 |
fontp->next = fontinfo_head; |
| 629 |
fontinfo_head = fontp; |
| 630 |
fontp->used = False; |
| 631 |
} |
| 632 |
|
| 633 |
/* preamble */ |
| 634 |
fseek(in_fp, 0L, SEEK_SET); |
| 635 |
fout_pos = pageinfo_get_offset(0); |
| 636 |
CopyNum(in_fp, out_fp, fout_pos); |
| 637 |
|
| 638 |
/* each page */ |
| 639 |
pagecnt = 0; |
| 640 |
for (page = 0; page < total_pages; page++) { |
| 641 |
scan_page_for_specials(in_fp, page, True, stack_save, NULL); |
| 642 |
|
| 643 |
/* should the current page be selected? */ |
| 644 |
if (pinfo->callback == NULL || pinfo->callback(info, page)) { |
| 645 |
scan_page_for_specials(in_fp, page, False, scan_for_included_files, pinfo); |
| 646 |
|
| 647 |
/* read BOP except for p[4] */ |
| 648 |
CopyNum(in_fp, out_fp, BOP_PART_LEN); |
| 649 |
/* read p[4] (previous bop pointer */ |
| 650 |
(void)get_bytes(in_fp, 4); |
| 651 |
/* write actual value of p[4] */ |
| 652 |
PutFour(curr_page_offset, out_fp); |
| 653 |
/* remember offset of current page, for postamble */ |
| 654 |
curr_page_offset = fout_pos; |
| 655 |
/* update fout_pos to current position */ |
| 656 |
fout_pos += BOP_LEN; |
| 657 |
|
| 658 |
if (!headers_dumped) { |
| 659 |
headers_dumped = True; |
| 660 |
dump_literal_headers(out_fp, &fout_pos); |
| 661 |
} |
| 662 |
|
| 663 |
/* if (!written_pre_cmds) { */ |
| 664 |
stack_dump_open_commands(out_fp, &fout_pos); |
| 665 |
/* written_pre_cmds = True; */ |
| 666 |
/* } */ |
| 667 |
|
| 668 |
while ((c = getc(in_fp)) != EOF) { |
| 669 |
if ((c & 0x80) == 0) { /* ordinary character */ |
| 670 |
putc(c, out_fp); |
| 671 |
fout_pos++; |
| 672 |
} |
| 673 |
else if (c == EOP) { /* End Of Page */ |
| 674 |
/* fprintf(stderr, "EOP at %ld\n", ftell(fin)); */ |
| 675 |
|
| 676 |
/* if (page == to) { */ /* print close stack for last page */ |
| 677 |
stack_dump_close_commands(out_fp, &fout_pos); |
| 678 |
/* } */ |
| 679 |
|
| 680 |
putc(c, out_fp); |
| 681 |
fout_pos++; |
| 682 |
break; |
| 683 |
} |
| 684 |
else { |
| 685 |
/* fprintf(stderr, "WRITE_DVI before: %ld, in_fp before: %ld\n", fout_pos, ftell(in_fp)); */ |
| 686 |
WriteDVI(in_fp, out_fp, &fout_pos, c); |
| 687 |
/* fprintf(stderr, "WRITE_DVI after: %ld; in_fp after: %ld\n", fout_pos, ftell(in_fp)); */ |
| 688 |
} |
| 689 |
/* HACK alert: force synchronization - why is this neccessary? |
| 690 |
Seems to have something do with the fact that two FILE *'s on the same |
| 691 |
file are open. POSIX.1, |
| 692 |
`Interaction of File Descriptors and Standard I/O Streams' seems to |
| 693 |
say the seek()s on one of them also affect the other, but I'm not sure |
| 694 |
about this. |
| 695 |
*/ |
| 696 |
fseek(in_fp, 0L, SEEK_CUR); |
| 697 |
} |
| 698 |
pagecnt++; |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
/* postamble */ |
| 703 |
if (!find_postamble(in_fp, &(pinfo->errflag))) { |
| 704 |
return; |
| 705 |
} |
| 706 |
|
| 707 |
/* read/write POST command */ |
| 708 |
putc(get_byte(in_fp), out_fp); |
| 709 |
/* read over last page offset */ |
| 710 |
get_bytes(in_fp, 4); |
| 711 |
/* write last page offset */ |
| 712 |
PutFour(curr_page_offset, out_fp); |
| 713 |
/* remember start of POST */ |
| 714 |
post_cmd_offset = fout_pos; |
| 715 |
/* copy part of postamble */ |
| 716 |
CopyNum(in_fp, out_fp, POSTAMBLE_PART_LEN); |
| 717 |
/* read t[2], total number of pages */ |
| 718 |
get_bytes(in_fp, 2); |
| 719 |
/* write t[2] (two calls) */ |
| 720 |
putc((pagecnt >> 8) & 0xff, out_fp); |
| 721 |
putc(pagecnt & 0xff, out_fp); |
| 722 |
/* update fout_pos to current pos */ |
| 723 |
fout_pos += POSTAMBLE_LEN; |
| 724 |
|
| 725 |
/* output font list */ |
| 726 |
for (;;) { |
| 727 |
if ((c = get_byte(in_fp)) < FNTDEF1 || c > FNTDEF4) /* maybe POSTPOST */ |
| 728 |
break; |
| 729 |
n = get_bytes(in_fp, c - FNTDEF1 + 1); |
| 730 |
for (fontp = fontinfo_head; fontp; fontp = fontp->next) |
| 731 |
if (n == fontp->TeXnumber) |
| 732 |
break; |
| 733 |
if (fontp && fontp->used == True) |
| 734 |
FontWrite(out_fp, fontp, &fout_pos); |
| 735 |
(void) get_bytes(in_fp, 12); |
| 736 |
(void) get_bytes(in_fp, (int)(get_byte(in_fp) + get_byte(in_fp))); |
| 737 |
} |
| 738 |
|
| 739 |
/* free list */ |
| 740 |
for (fontp = fontinfo_head; fontp;) { |
| 741 |
struct fontinfo *nextp; |
| 742 |
free(fontp->fontname); |
| 743 |
nextp = fontp->next; |
| 744 |
free(fontp); |
| 745 |
fontp = nextp; |
| 746 |
} |
| 747 |
|
| 748 |
/* POSTPOST */ |
| 749 |
putc(c, out_fp); |
| 750 |
get_bytes(in_fp, 4); |
| 751 |
PutFour(post_cmd_offset, out_fp); |
| 752 |
CopyNum(in_fp, out_fp, 1 + 4); |
| 753 |
for (fout_pos += 1 + 4 + 1 + 4; |
| 754 |
fout_pos & 3; |
| 755 |
fout_pos++) { |
| 756 |
putc(TRAILER, out_fp); |
| 757 |
} |
| 758 |
|
| 759 |
/* fclose(in_fp); */ |
| 760 |
fflush(out_fp); |
| 761 |
} |
| 762 |
|