| 1 |
/*========================================================================*\ |
| 2 |
|
| 3 |
Copyright (c) 1994-2004 Paul Vojta |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
of this software and associated documentation files (the "Software"), to |
| 7 |
deal in the Software without restriction, including without limitation the |
| 8 |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 |
sell copies of the Software, and to permit persons to whom the Software is |
| 10 |
furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in |
| 13 |
all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 19 |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 |
OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
|
| 23 |
NOTES: |
| 24 |
This code was originally written by Ricardo Telichevesky |
| 25 |
([email protected]) and Luis Miguel Silveira |
| 26 |
([email protected]). |
| 27 |
It was largely influenced by similar code in the SeeTeX/XTeX |
| 28 |
package by Dirk Grunwald ([email protected]). |
| 29 |
|
| 30 |
\*========================================================================*/ |
| 31 |
|
| 32 |
/* ||| To do: |
| 33 |
* ALWAYS_CLOSE_SERVER_CONNECTION? |
| 34 |
* Is there some way of interrupting a process? |
| 35 |
* fork |
| 36 |
* extra bytes on input |
| 37 |
*/ |
| 38 |
|
| 39 |
#ifdef PS_NEWS /* whole file */ |
| 40 |
|
| 41 |
#include "xdvi-config.h" |
| 42 |
#include "xdvi.h" |
| 43 |
#include "events.h" |
| 44 |
#include "dvi-init.h" |
| 45 |
#include "dvi-draw.h" |
| 46 |
|
| 47 |
#include <signal.h> |
| 48 |
#include <sys/file.h> /* this defines FASYNC */ |
| 49 |
#include <X11/X.h> |
| 50 |
#include <X11/Xlib.h> |
| 51 |
#undef SYSV /* To avoid defined SYSV_{WAIT,UCONTEXT} in xview/notify.h. */ |
| 52 |
#include <NeWS/psio.h> |
| 53 |
#include <xvps/pscanvas.h> |
| 54 |
|
| 55 |
/* Condition for retrying a write */ |
| 56 |
#include <errno.h> |
| 57 |
#include <setjmp.h> |
| 58 |
|
| 59 |
/* if POSIX O_NONBLOCK is not available, use O_NDELAY */ |
| 60 |
#if !defined(O_NONBLOCK) && defined(O_NDELAY) |
| 61 |
#define O_NONBLOCK O_NDELAY |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef X_NOT_STDC_ENV |
| 65 |
extern int errno; |
| 66 |
#endif |
| 67 |
|
| 68 |
#ifdef EWOULDBLOCK |
| 69 |
#ifdef EAGAIN |
| 70 |
#define AGAIN_CONDITION (errno == EWOULDBLOCK || errno == EAGAIN) |
| 71 |
#else /* EAGAIN */ |
| 72 |
#define AGAIN_CONDITION (errno == EWOULDBLOCK) |
| 73 |
#endif /* EAGAIN */ |
| 74 |
#else /* EWOULDBLOCK */ |
| 75 |
#ifdef EAGAIN |
| 76 |
#define AGAIN_CONDITION (errno == EAGAIN) |
| 77 |
#endif /* EAGAIN */ |
| 78 |
#endif /* EWOULDBLOCK */ |
| 79 |
|
| 80 |
#if HAVE_POLL |
| 81 |
# include <poll.h> |
| 82 |
# define XIO_IN POLLIN |
| 83 |
# define XIO_OUT POLLOUT |
| 84 |
#else |
| 85 |
# define XIO_IN 1 |
| 86 |
# define XIO_OUT 2 |
| 87 |
#endif |
| 88 |
|
| 89 |
|
| 90 |
#if !defined(FLAKY_SIGPOLL) && !HAVE_STREAMS && !defined(FASYNC) |
| 91 |
# define FLAKY_SIGPOLL 1 |
| 92 |
#endif |
| 93 |
|
| 94 |
char *strtok(char *, const char *); |
| 95 |
|
| 96 |
/* define ALWAYS_CLOSE_SERVER_CONNECTION if you want to close the server |
| 97 |
connection all the time */ |
| 98 |
#undef ALWAYS_CLOSE_SERVER_CONNECTION |
| 99 |
|
| 100 |
|
| 101 |
/* |
| 102 |
* Some setup code. |
| 103 |
*/ |
| 104 |
static const char str0[] = |
| 105 |
"/OW2? version cvi 2 eq def " |
| 106 |
"OW2? " |
| 107 |
"{ /setlinewidth { pop } def} " |
| 108 |
"{ /NeWS 3 0 findpackage beginpackage " |
| 109 |
" /X11 3 0 findpackage beginpackage} " |
| 110 |
"ifelse " |
| 111 |
"currentcanvas /Color get " |
| 112 |
"currentcanvas /Colormap get getcubedescription null eq and " |
| 113 |
" {8 {{currentcanvas /Colormap get 1 index dup dup dup newcube} stopped " |
| 114 |
" {pop pop pop pop pop} {exit} ifelse " |
| 115 |
" 2 div cvi dup 1 eq {exit} if} loop pop} " |
| 116 |
"if\n"; |
| 117 |
/* |
| 118 |
* This string reads chunks (delimited by %%xdvimark). |
| 119 |
* The first character of a chunk tells whether a given chunk |
| 120 |
* is to be done within save/restore or not. |
| 121 |
* The `H' at the end tells it that the first group is a |
| 122 |
* header; i.e., no save/restore. |
| 123 |
*/ |
| 124 |
static const char preamble[] = |
| 125 |
"/xdvi$line 81 string def " |
| 126 |
"/xdvi$run {$error null ne {$error /newerror false put} if " |
| 127 |
" {currentfile cvx stopped " |
| 128 |
" $error null eq {false} {$error /newerror get} ifelse and " |
| 129 |
" {handleerror} if} stopped pop} def " |
| 130 |
"/xdvi$dslen countdictstack def " |
| 131 |
"{currentfile read not {exit} if 72 eq " |
| 132 |
" {xdvi$run} " |
| 133 |
" {/xdvi$sav save def xdvi$run " |
| 134 |
" clear countdictstack xdvi$dslen sub {end} repeat xdvi$sav restore} " |
| 135 |
" ifelse " |
| 136 |
" {(%%xdvimark) currentfile xdvi$line {readline} stopped " |
| 137 |
" {clear} {{eq {false exit} if} {true exit} ifelse} ifelse }loop {exit} if " |
| 138 |
" 58 tagprint flush " |
| 139 |
"}loop\nH"; |
| 140 |
|
| 141 |
extern const char psheader[]; |
| 142 |
extern int psheaderlen; |
| 143 |
|
| 144 |
static const char preamble2[] = " stop\n%%xdvimark\n"; |
| 145 |
#define stopstring preamble2 |
| 146 |
|
| 147 |
/* global procedures (besides initNeWS) */ |
| 148 |
|
| 149 |
static void toggleNeWS(int flag); |
| 150 |
static void destroyNeWS(void); |
| 151 |
static void interruptNeWS(void); |
| 152 |
static void endpageNeWS(void); |
| 153 |
static void drawbeginNeWS(int, int, const char *); |
| 154 |
static void drawbeginNeWS_box(int, int, const char *); |
| 155 |
static void drawrawNeWS(const char *); |
| 156 |
static void drawrawNeWS_box(const char *); |
| 157 |
static void drawfileNeWS(const char *, FILE *); |
| 158 |
static void drawendNeWS(const char *); |
| 159 |
static void beginheaderNeWS(void); |
| 160 |
static void endheaderNeWS(void); |
| 161 |
static void newdocNeWS(void); |
| 162 |
|
| 163 |
static struct psprocs news_procs = { |
| 164 |
/* toggle */ toggleNeWS, |
| 165 |
/* destroy */ destroyNeWS, |
| 166 |
/* interrupt */ interruptNeWS, |
| 167 |
/* endpage */ endpageNeWS, |
| 168 |
/* drawbegin */ drawbeginNeWS, |
| 169 |
/* drawraw */ drawrawNeWS, |
| 170 |
/* drawfile */ drawfileNeWS, |
| 171 |
/* drawend */ drawendNeWS, |
| 172 |
/* beginheader */ beginheaderNeWS, |
| 173 |
/* endheader */ endheaderNeWS, |
| 174 |
/* newdoc */ newdocNeWS |
| 175 |
}; |
| 176 |
|
| 177 |
/* define local static variables */ |
| 178 |
static int NeWS_mag; /* magnification currently in use */ |
| 179 |
static int NeWS_shrink; /* shrink factor currently in use */ |
| 180 |
static unsigned int NeWS_page_w; /* how big our current page is */ |
| 181 |
static unsigned int NeWS_page_h; |
| 182 |
static Boolean NeWS_active; /* if we've started a page yet */ |
| 183 |
static int NeWS_pending; /* number of ack's we're expecting */ |
| 184 |
static const char *NeWS_send_byte; /* next byte to send to NeWS */ |
| 185 |
static const char *NeWS_send_end; /* last + 1 byte to send */ |
| 186 |
static Boolean NeWS_in_header; /* if we're sending a header */ |
| 187 |
static Boolean NeWS_in_doc; /* if we've sent header information */ |
| 188 |
static int NeWS_ev_mask; /* events for which we'll stop */ |
| 189 |
static Boolean NeWS_destroyed = False; |
| 190 |
|
| 191 |
#define NEWS_MASK_NORMAL EV_GE_NEWPAGE |
| 192 |
#define NEWS_MASK_HEADER EV_GE_PS_TOGGLE |
| 193 |
#define NEWS_MASK_INIT (EV_GE_TERM | EV_PS_TOGGLE) |
| 194 |
|
| 195 |
|
| 196 |
/* |
| 197 |
* NeWS I/O code. This should send PS code to NeWS, |
| 198 |
* receive acknowledgements, and receive X events in the meantime. |
| 199 |
* It also checks for SIGPIPE errors. |
| 200 |
*/ |
| 201 |
|
| 202 |
static void read_from_NeWS(void); |
| 203 |
static void write_to_NeWS(void); |
| 204 |
|
| 205 |
static struct xio NeWS_xout = {NULL, 0, XIO_IN, |
| 206 |
#if HAVE_POLL |
| 207 |
NULL, |
| 208 |
#endif |
| 209 |
read_from_NeWS, write_to_NeWS, NULL}; |
| 210 |
|
| 211 |
static struct xio NeWS_xin = {NULL, 0, XIO_IN, |
| 212 |
#if HAVE_POLL |
| 213 |
NULL, |
| 214 |
#endif |
| 215 |
read_from_NeWS, NULL, NULL}; |
| 216 |
|
| 217 |
|
| 218 |
/*---------------------------------------------------------------------------* |
| 219 |
psio_sigpipe_handler () |
| 220 |
|
| 221 |
Arguments: sig, code, scp, addr (see man page for signal) |
| 222 |
Returns: (void) |
| 223 |
Side-Effects: SIGPIPE signal is flagged as sigpipe_error variable is set. |
| 224 |
|
| 225 |
Description: |
| 226 |
Handler for SIGPIPE error generated by a broken pipe in the connection |
| 227 |
to the NeWS server; this may be duer to some abnormal condition, or some |
| 228 |
hairy PostScript code containing commands not implemented by the server. |
| 229 |
|
| 230 |
+----------------------------------------------------------------------------*/ |
| 231 |
|
| 232 |
static Boolean sigpipe_error = False; |
| 233 |
|
| 234 |
static struct sigaction psio_sigpipe_handler_struct; |
| 235 |
/* initialized to {psio_sigpipe_handler, (sigset_t) 0, 0} in initNeWS */ |
| 236 |
|
| 237 |
static RETSIGTYPE |
| 238 |
psio_sigpipe_handler(int sig, int code, struct sigcontext *scp, char *addr) |
| 239 |
{ |
| 240 |
sigpipe_error = True; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
/* |
| 245 |
* read_from_NeWS - This does the actual retrieving of acknowledgements. |
| 246 |
* If other bytes appear on the file - tough. |
| 247 |
*/ |
| 248 |
|
| 249 |
static void |
| 250 |
read_from_NeWS(void) |
| 251 |
{ |
| 252 |
for (;;) { |
| 253 |
int retval; |
| 254 |
|
| 255 |
retval = ps_checkfor(PostScriptInput, PSIO_FIND_TAG, 58); |
| 256 |
if (retval == 0) |
| 257 |
break; |
| 258 |
if (retval < 0) { |
| 259 |
fprintf(stderr, "[xdvik] ps_checkfor: %d\n", retval); |
| 260 |
return; |
| 261 |
} |
| 262 |
(void)ps_checkfor(PostScriptInput, PSIO_WAIT_TAG, 58); |
| 263 |
--NeWS_pending; |
| 264 |
if (NeWS_pending == 0) |
| 265 |
globals.ev.flags |= EV_ACK; |
| 266 |
if (globals.debug & DBG_PS) |
| 267 |
printf("Got NeWS ack; %d pending.\n", NeWS_pending); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
/* |
| 273 |
* write_to_NeWS - Write to the PostScript interpreter. |
| 274 |
*/ |
| 275 |
|
| 276 |
static void |
| 277 |
write_to_NeWS(void) |
| 278 |
{ |
| 279 |
int old_flags; |
| 280 |
int bytes; |
| 281 |
|
| 282 |
old_flags = fcntl(PostScript->file, F_GETFL, 0); |
| 283 |
if (old_flags < 0) return; |
| 284 |
/* set to be non-blocking */ |
| 285 |
if (fcntl(PostScript->file, F_SETFL, old_flags | O_NONBLOCK) < 0) |
| 286 |
return; |
| 287 |
|
| 288 |
for (;;) { |
| 289 |
bytes = write(PostScript->file, NeWS_send_byte, |
| 290 |
NeWS_send_end - NeWS_send_byte); |
| 291 |
if (bytes < 0) { |
| 292 |
if (AGAIN_CONDITION) |
| 293 |
break; |
| 294 |
perror("xdvi NeWS_send"); |
| 295 |
break; |
| 296 |
} |
| 297 |
NeWS_send_byte += bytes; |
| 298 |
if (NeWS_send_byte == NeWS_send_end) { |
| 299 |
NeWS_send_byte = NULL; |
| 300 |
NeWS_xout.xio_events &= ~XIO_OUT; |
| 301 |
#if HAVE_POLL |
| 302 |
/* This check is necessary, since write_to_NeWS can be called |
| 303 |
directly. */ |
| 304 |
if (NeWS_xout.pfd != NULL) |
| 305 |
NeWS_xout.pfd->events &= ~POLLOUT; |
| 306 |
globals.ev.flags |= EV_ACK; |
| 307 |
break; |
| 308 |
#endif |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
fcntl(PostScript->file, F_SETFL, old_flags); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/* |
| 317 |
* Clean up after NeWS_send() |
| 318 |
*/ |
| 319 |
|
| 320 |
static void |
| 321 |
post_send(void) |
| 322 |
{ |
| 323 |
if (sigpipe_error) { |
| 324 |
fputs("NeWS died unexpectedly.\n", stderr); |
| 325 |
destroyNeWS(); |
| 326 |
draw_bbox(); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/* |
| 332 |
* Main routine for writing to the NeWS interpreter. |
| 333 |
*/ |
| 334 |
|
| 335 |
static void |
| 336 |
NeWS_send(const char *cp, size_t len) |
| 337 |
{ |
| 338 |
struct sigaction orig; |
| 339 |
|
| 340 |
if (PostScript == (PSFILE *) NULL || (globals.ev.flags & NeWS_ev_mask)) |
| 341 |
return; |
| 342 |
|
| 343 |
(void) sigaction(SIGPIPE, &psio_sigpipe_handler_struct, &orig); |
| 344 |
sigpipe_error = False; |
| 345 |
|
| 346 |
NeWS_send_byte = cp; |
| 347 |
NeWS_send_end = cp + len; |
| 348 |
NeWS_xout.xio_events |= XIO_OUT; |
| 349 |
#if HAVE_POLL |
| 350 |
if (NeWS_xout.pfd != NULL) |
| 351 |
NeWS_xout.pfd->events |= POLLOUT; |
| 352 |
#endif |
| 353 |
|
| 354 |
write_to_NeWS(); |
| 355 |
(void) read_events(NeWS_ev_mask | EV_ACK); |
| 356 |
|
| 357 |
if (!(globals.ev.flags & EV_ACK)) { /* if interrupted */ |
| 358 |
/* ||| Do somthing more severe here */ |
| 359 |
} |
| 360 |
|
| 361 |
globals.ev.flags &= ~EV_ACK; |
| 362 |
|
| 363 |
/* put back generic handler for SIGPIPE */ |
| 364 |
(void) sigaction(SIGPIPE, &orig, (struct sigaction *) NULL); |
| 365 |
|
| 366 |
if (!NeWS_in_header) |
| 367 |
post_send(); |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
* Wait for acknowledgement from NeWS. With NeWS we have no choice but |
| 372 |
* to wait (||| I think). |
| 373 |
*/ |
| 374 |
|
| 375 |
static void |
| 376 |
waitack(void) |
| 377 |
{ |
| 378 |
if (PostScript == (PSFILE *) NULL) |
| 379 |
return; |
| 380 |
|
| 381 |
if (NeWS_pending > 0) |
| 382 |
(void) read_events(EV_GE_ACK); |
| 383 |
|
| 384 |
if (globals.ev.flags & EV_ACK) { |
| 385 |
globals.ev.flags &= ~EV_ACK; |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
/* ||| Do something more serious here */ |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
/*---------------------------------------------------------------------------* |
| 394 |
initNeWS() |
| 395 |
|
| 396 |
Arguments: None. |
| 397 |
Returns: True if and only if initialization succeeded |
| 398 |
Side-Effects: Static variables may be set. |
| 399 |
|
| 400 |
Description: |
| 401 |
Initializes variables for the application main loop. |
| 402 |
|
| 403 |
+----------------------------------------------------------------------------*/ |
| 404 |
|
| 405 |
Boolean initNeWS(void) |
| 406 |
{ |
| 407 |
static NeWStoken newstoken; |
| 408 |
|
| 409 |
/* now try to open the connection to the NeWS server */ |
| 410 |
if (ps_open_PostScript() == (PSFILE *) NULL) |
| 411 |
return False; |
| 412 |
|
| 413 |
psio_sigpipe_handler_struct.sa_handler = psio_sigpipe_handler; |
| 414 |
sigemptyset(&psio_sigpipe_handler_struct.sa_mask); |
| 415 |
|
| 416 |
#if !FLAKY_SIGPOLL |
| 417 |
if (fcntl(PostScript->file, F_SETOWN, getpid()) == -1) |
| 418 |
perror("xdvi: fcntl F_SETOWN"); |
| 419 |
if (fcntl(PostScript->file, F_SETFL, |
| 420 |
fcntl(PostScript->file, F_GETFL, 0) | FASYNC) == -1) |
| 421 |
perror("xdvi: fcntl F_SETFL"); |
| 422 |
#endif /* not FLAKY_SIGPOLL */ |
| 423 |
if (PostScriptInput->file != PostScript->file) { |
| 424 |
#if !FLAKY_SIGPOLL |
| 425 |
if (fcntl(PostScriptInput->file, F_SETOWN, getpid()) == -1) |
| 426 |
perror("xdvi: fcntl F_SETOWN"); |
| 427 |
if (fcntl(PostScriptInput->file, F_SETFL, |
| 428 |
fcntl(PostScriptInput->file, F_GETFL, 0) | FASYNC) == -1) |
| 429 |
perror("xdvi: fcntl F_SETFL"); |
| 430 |
#endif /* not FLAKY_SIGPOLL */ |
| 431 |
NeWS_xout.xio_events &= ~XIO_IN; |
| 432 |
NeWS_xin.fd = PostScriptInput->file; |
| 433 |
set_io(&NeWS_xin); |
| 434 |
} |
| 435 |
NeWS_xout.fd = PostScript->file; |
| 436 |
set_io(&NeWS_xout); |
| 437 |
|
| 438 |
NeWS_active = False; |
| 439 |
NeWS_in_header = True; |
| 440 |
NeWS_ev_mask = NEWS_MASK_INIT; |
| 441 |
NeWS_pending = 1; |
| 442 |
|
| 443 |
ps_flush_PostScript(); |
| 444 |
NeWS_send(str0, (sizeof str0) - 1); |
| 445 |
/* get xid of window, then make this window the NeWS canvas */ |
| 446 |
(void)ps_token_from_xid(mane.win, &newstoken); |
| 447 |
if (newstoken != -1) { |
| 448 |
ps_setcanvas(newstoken); |
| 449 |
ps_flush_PostScript(); |
| 450 |
|
| 451 |
NeWS_send(preamble, (sizeof preamble) - 1); |
| 452 |
NeWS_send(psheader, psheaderlen); |
| 453 |
NeWS_send(preamble2, (sizeof preamble2) - 1); |
| 454 |
NeWS_in_header = False; |
| 455 |
post_send(); |
| 456 |
waitack(); |
| 457 |
} |
| 458 |
|
| 459 |
if (NeWS_destroyed) |
| 460 |
return False; |
| 461 |
|
| 462 |
/* success */ |
| 463 |
|
| 464 |
NeWS_mag = NeWS_shrink = -1; |
| 465 |
NeWS_page_w = globals.page.w; |
| 466 |
NeWS_page_h = globals.page.h; |
| 467 |
|
| 468 |
psp = news_procs; |
| 469 |
if (!resource.postscript) |
| 470 |
toggleNeWS(0); /* if we got a 'v' already */ |
| 471 |
|
| 472 |
return True; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
/*---------------------------------------------------------------------------* |
| 477 |
toggleNeWS(int flag) |
| 478 |
|
| 479 |
Arguments: flag for toggling PostScript |
| 480 |
Returns: (void) |
| 481 |
Side-Effects: psp.drawbegin is changed |
| 482 |
|
| 483 |
Description: |
| 484 |
Used to toggle the rendering of PostScript by the NeWS server |
| 485 |
Callable from within read_events(). |
| 486 |
|
| 487 |
+----------------------------------------------------------------------------*/ |
| 488 |
|
| 489 |
static void |
| 490 |
toggleNeWS(int flag) |
| 491 |
{ |
| 492 |
if (globals.debug & DBG_PS) |
| 493 |
fprintf(stdout, "Toggling NeWS to %d", flag); |
| 494 |
|
| 495 |
switch (flag) { |
| 496 |
case 0: |
| 497 |
psp.drawbegin = drawbegin_none; |
| 498 |
break; |
| 499 |
case 1: |
| 500 |
psp.drawbegin = drawbeginNeWS; |
| 501 |
break; |
| 502 |
default: |
| 503 |
psp.drawbegin = drawbeginNeWS_box; |
| 504 |
break; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
/*---------------------------------------------------------------------------* |
| 510 |
destroyNeWS() |
| 511 |
|
| 512 |
Arguments: none |
| 513 |
Returns: (void) |
| 514 |
Side-Effects: the pointer to the NeWS file is nulled |
| 515 |
|
| 516 |
Description: |
| 517 |
Close the connection to the NeWS server; used when rendering is terminated |
| 518 |
in any way. |
| 519 |
|
| 520 |
+----------------------------------------------------------------------------*/ |
| 521 |
|
| 522 |
static void |
| 523 |
destroyNeWS(void) |
| 524 |
{ |
| 525 |
psp = no_ps_procs; |
| 526 |
NeWS_destroyed = True; |
| 527 |
scanned_page = scanned_page_ps = scanned_page_reset; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
/*---------------------------------------------------------------------------* |
| 532 |
interruptNeWS() |
| 533 |
|
| 534 |
Arguments: none |
| 535 |
Returns: void |
| 536 |
|
| 537 |
Description: |
| 538 |
Close the connection to the NeWS server; used when rendering is terminated |
| 539 |
because of an interruption in the viewing of the current page. |
| 540 |
||| It would be nice if we could asynchronously ``wake up'' a NeWS process |
| 541 |
(preferably by sending something along the X socket); then we could do |
| 542 |
better than just to wait. |
| 543 |
|
| 544 |
+----------------------------------------------------------------------------*/ |
| 545 |
|
| 546 |
static void |
| 547 |
interruptNeWS(void) |
| 548 |
{ |
| 549 |
if (globals.debug & DBG_PS) |
| 550 |
puts("Running interruptNeWS()"); |
| 551 |
if (NeWS_pending <= 0) return; /* if nothing to do */ |
| 552 |
|
| 553 |
if (NeWS_active) { |
| 554 |
NeWS_send(stopstring, (sizeof stopstring) - 1); |
| 555 |
NeWS_active = False; |
| 556 |
} |
| 557 |
|
| 558 |
for (;;) { |
| 559 |
ps_flush_PostScript(); |
| 560 |
if (NeWS_pending <= 0) break; |
| 561 |
waitack(); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
/*---------------------------------------------------------------------------* |
| 567 |
endpageNeWS() |
| 568 |
|
| 569 |
Arguments: none |
| 570 |
Returns: (void) |
| 571 |
Side-Effects: the NeWS_active variable is cleared. |
| 572 |
|
| 573 |
Description: |
| 574 |
Should be called at the end of a page to end this chunk for the NeWS server. |
| 575 |
|
| 576 |
+----------------------------------------------------------------------------*/ |
| 577 |
|
| 578 |
static void |
| 579 |
endpageNeWS(void) |
| 580 |
{ |
| 581 |
if (globals.debug & DBG_PS) |
| 582 |
puts("endpage sent to NeWS Server"); |
| 583 |
if (NeWS_active) { |
| 584 |
NeWS_send(stopstring, (sizeof stopstring) - 1); |
| 585 |
NeWS_active = False; |
| 586 |
waitack(); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
/*---------------------------------------------------------------------------* |
| 592 |
drawbeginNeWS () |
| 593 |
|
| 594 |
Arguments: xul, yul - coordinates of the upper left corner of the figure |
| 595 |
cp - string with the bounding box line data |
| 596 |
Returns: (void) |
| 597 |
|
| 598 |
Description: |
| 599 |
Opens a connection to the NeWS server and send in the preamble and the |
| 600 |
bounding box information after correctly computing resolution factors. |
| 601 |
In case no rendering is to be done, outlines the figure. An outline is |
| 602 |
also generated whenever the PostScript code is too hairy and generates |
| 603 |
a SIGPIPE signal. |
| 604 |
|
| 605 |
+----------------------------------------------------------------------------*/ |
| 606 |
|
| 607 |
static void |
| 608 |
drawbeginNeWS(int xul, int yul, const char *cp) |
| 609 |
{ |
| 610 |
char buf[100]; |
| 611 |
static const char str[] = " TeXDict begin\n"; |
| 612 |
static const char str2[] = "Hinitgraphics stop\n%%xdvimark\n"; |
| 613 |
|
| 614 |
if (globals.debug & DBG_PS) { |
| 615 |
printf("xul= %d yul= %d\n", xul, yul); |
| 616 |
printf("String = < %s >\n", cp); |
| 617 |
} |
| 618 |
|
| 619 |
/* catch up on the X side */ |
| 620 |
XSync(DISP, False); |
| 621 |
|
| 622 |
if (!NeWS_active) { |
| 623 |
/* send initialization to NeWS server */ |
| 624 |
if (NeWS_page_w < globals.page.w || NeWS_page_h < globals.page.h) { |
| 625 |
if (globals.ev.flags & NEWS_MASK_NORMAL) |
| 626 |
longjmp(globals.ev.canit, 1); |
| 627 |
++NeWS_pending; |
| 628 |
NeWS_page_w = globals.page.w; |
| 629 |
NeWS_page_h = globals.page.h; |
| 630 |
NeWS_send(str2, (sizeof str2) - 1); |
| 631 |
} |
| 632 |
if (magnification != NeWS_mag) { |
| 633 |
if (globals.ev.flags & NEWS_MASK_NORMAL) |
| 634 |
longjmp(globals.ev.canit, 1); |
| 635 |
++NeWS_pending; |
| 636 |
sprintf(buf, "H TeXDict begin /DVImag %d 1000 div def " |
| 637 |
"end stop\n%%%%xdvimark\n", |
| 638 |
NeWS_mag = magnification); |
| 639 |
NeWS_send(buf, strlen(buf)); |
| 640 |
} |
| 641 |
if (mane.shrinkfactor != NeWS_shrink) { |
| 642 |
if (globals.ev.flags & NEWS_MASK_NORMAL) |
| 643 |
longjmp(globals.ev.canit, 1); |
| 644 |
++NeWS_pending; |
| 645 |
sprintf(buf, "H TeXDict begin %d %d div dup " |
| 646 |
"/Resolution X /VResolution X " |
| 647 |
"end stop\n%%%%xdvimark\n", |
| 648 |
resource.pixels_per_inch, |
| 649 |
NeWS_shrink = mane.shrinkfactor); |
| 650 |
NeWS_send(buf, strlen(buf)); |
| 651 |
} |
| 652 |
if (globals.ev.flags & NEWS_MASK_NORMAL) |
| 653 |
longjmp(globals.ev.canit, 1); |
| 654 |
++NeWS_pending; |
| 655 |
NeWS_send(str, (sizeof str) - 1); |
| 656 |
NeWS_active = True; |
| 657 |
} |
| 658 |
|
| 659 |
sprintf(buf, "%d %d moveto\n", xul, yul); |
| 660 |
NeWS_send(buf, strlen(buf)); |
| 661 |
NeWS_send(cp, strlen(cp)); |
| 662 |
} |
| 663 |
|
| 664 |
static void |
| 665 |
drawbeginNeWS_box(int xul, int yul, const char *cp) |
| 666 |
{ |
| 667 |
drawbeginNeWS(xul, yul, cp); |
| 668 |
draw_bbox(); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
/*---------------------------------------------------------------------------* |
| 673 |
drawrawNeWS() |
| 674 |
|
| 675 |
Arguments: origcp - the raw string to be sent to the postscript interpreter |
| 676 |
Returns: (void) |
| 677 |
Side-Effects: (none) |
| 678 |
|
| 679 |
Description: |
| 680 |
If there is a valid connection to the NeWS server, just send the string to |
| 681 |
the interpreter, else leave. |
| 682 |
|
| 683 |
+----------------------------------------------------------------------------*/ |
| 684 |
|
| 685 |
static void |
| 686 |
drawrawNeWS(const char *origcp) |
| 687 |
{ |
| 688 |
const char *pt, *ptm1, *ocp1; |
| 689 |
static char *cp; |
| 690 |
char *cp1; |
| 691 |
static unsigned int cplen = 0; |
| 692 |
unsigned int len; |
| 693 |
double angle; |
| 694 |
Boolean found = False; |
| 695 |
|
| 696 |
if (!NeWS_active) |
| 697 |
return; |
| 698 |
|
| 699 |
if (globals.debug & DBG_PS) |
| 700 |
printf("Raw PS sent to context: <%s>\n", origcp); |
| 701 |
|
| 702 |
/* take a look at the string: NeWS bums on certain rotations */ |
| 703 |
len = strlen(origcp) + 4; |
| 704 |
if (cplen < len) { |
| 705 |
if (cplen != 0) |
| 706 |
free(cp); |
| 707 |
cplen = len; |
| 708 |
cp = xmalloc(cplen); |
| 709 |
} |
| 710 |
ocp1 = origcp; |
| 711 |
pt = origcp; |
| 712 |
while (*pt == ' ' || *pt == '\t') |
| 713 |
++pt; |
| 714 |
cp1 = cp; |
| 715 |
for (;;) { |
| 716 |
ptm1 = pt; |
| 717 |
while (*pt != '\0' && *pt != ' ' && *pt != '\t') |
| 718 |
++pt; |
| 719 |
if (*pt == '\0') |
| 720 |
break; |
| 721 |
while (*pt == ' ' || *pt == '\t') |
| 722 |
++pt; |
| 723 |
if (strncmp(pt, "rotate", 6) == 0 |
| 724 |
&& (pt[6] == '\0' || pt[6] == ' ' || pt[6] == '\t')) { |
| 725 |
/* found rotate; check angle */ |
| 726 |
if (sscanf(ptm1, "%lf", &angle) >= 1) { |
| 727 |
found = True; |
| 728 |
while (angle > 360.0) |
| 729 |
angle -= 360; |
| 730 |
while (angle < -360.0) |
| 731 |
angle += 360; |
| 732 |
if (angle == 90.0) { |
| 733 |
angle = 89.999; |
| 734 |
(void)memcpy(cp1, ocp1, ptm1 - ocp1); |
| 735 |
cp1 += ptm1 - ocp1; |
| 736 |
strcpy(cp1, "89.999 rotate "); |
| 737 |
cp1 += strlen(cp1); |
| 738 |
while (*pt != '\0' && *pt != ' ' && *pt != '\t') |
| 739 |
++pt; |
| 740 |
while (*pt == ' ' || *pt == '\t') |
| 741 |
++pt; |
| 742 |
ocp1 = pt; |
| 743 |
} |
| 744 |
else if (angle == -90.0) { |
| 745 |
angle = -89.999; |
| 746 |
(void)memcpy(cp1, ocp1, ptm1 - ocp1); |
| 747 |
cp1 += ptm1 - ocp1; |
| 748 |
strcpy(cp1, "-89.999 rotate "); |
| 749 |
cp1 += strlen(cp1); |
| 750 |
while (*pt != '\0' && *pt != ' ' && *pt != '\t') |
| 751 |
++pt; |
| 752 |
while (*pt == ' ' || *pt == '\t') |
| 753 |
++pt; |
| 754 |
ocp1 = pt; |
| 755 |
} |
| 756 |
else if (angle == 0.0) { |
| 757 |
(void)memcpy(cp1, ocp1, ptm1 - ocp1); |
| 758 |
cp1 += ptm1 - ocp1; |
| 759 |
while (*pt != '\0' && *pt != ' ' && *pt != '\t') |
| 760 |
++pt; |
| 761 |
while (*pt == ' ' || *pt == '\t') |
| 762 |
++pt; |
| 763 |
ocp1 = pt; |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
strcpy(cp1, ocp1); |
| 769 |
if ((globals.debug & DBG_PS) && found) { |
| 770 |
printf("String is now <%s>\n", cp); |
| 771 |
printf("Found rotate string. Angle is %g degrees.\n", angle); |
| 772 |
} |
| 773 |
|
| 774 |
len = strlen(cp); |
| 775 |
cp[len] = '\n'; |
| 776 |
NeWS_send(cp, len + 1); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
/*---------------------------------------------------------------------------* |
| 781 |
drawfileNeWS() |
| 782 |
|
| 783 |
Arguments: cp - string with the postscript file pathname |
| 784 |
psfile - file, already opened |
| 785 |
Returns: (void) |
| 786 |
Side-Effects: none |
| 787 |
|
| 788 |
Description: |
| 789 |
Postscript file containing the figure is opened and sent to the NeWS server. |
| 790 |
Figure is outlined in case hairy code produces a SIGPIPE signal. |
| 791 |
|
| 792 |
+----------------------------------------------------------------------------*/ |
| 793 |
|
| 794 |
static void |
| 795 |
drawfileNeWS(const char *cp, FILE *psfile) |
| 796 |
{ |
| 797 |
char buffer[1025]; |
| 798 |
int blen; |
| 799 |
struct sigaction orig; |
| 800 |
|
| 801 |
if (!NeWS_active) { |
| 802 |
fclose(psfile); |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
if (globals.debug & DBG_PS) |
| 807 |
printf("printing file %s\n", cp); |
| 808 |
|
| 809 |
sigpipe_error = False; |
| 810 |
for (;;) { |
| 811 |
blen = fread(buffer, sizeof(char), 1024, psfile); |
| 812 |
if (blen == 0) break; |
| 813 |
NeWS_send(buffer, blen); |
| 814 |
if (sigpipe_error || (globals.ev.flags & NeWS_ev_mask)) |
| 815 |
break; |
| 816 |
} |
| 817 |
fclose(psfile); |
| 818 |
|
| 819 |
if (sigpipe_error) { |
| 820 |
fputs("NeWS died unexpectedly.\n", stderr); |
| 821 |
destroyNeWS(); |
| 822 |
draw_bbox(); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
/*---------------------------------------------------------------------------* |
| 828 |
drawendNeWS() |
| 829 |
|
| 830 |
Arguments: cp - string with indication of the end of the special |
| 831 |
Returns: (void) |
| 832 |
|
| 833 |
Description: |
| 834 |
Sends the indication of end of the figure PostScript code. |
| 835 |
|
| 836 |
+----------------------------------------------------------------------------*/ |
| 837 |
|
| 838 |
static void |
| 839 |
drawendNeWS(const char *cp) |
| 840 |
{ |
| 841 |
if (!NeWS_active) |
| 842 |
return; |
| 843 |
|
| 844 |
if (globals.debug & DBG_PS) |
| 845 |
puts("drawend sent to NeWS Server"); |
| 846 |
NeWS_send(cp, strlen(cp)); |
| 847 |
NeWS_send("\n", 1); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
/*---------------------------------------------------------------------------* |
| 852 |
beginheaderNeWS() |
| 853 |
|
| 854 |
Arguments: none |
| 855 |
Returns: (void) |
| 856 |
|
| 857 |
Description: |
| 858 |
Prepares the PostScript interpreter for receipt of header code. |
| 859 |
|
| 860 |
+----------------------------------------------------------------------------*/ |
| 861 |
|
| 862 |
static void |
| 863 |
beginheaderNeWS(void) |
| 864 |
{ |
| 865 |
static const char str[] = "Hsave /xdvi$doc exch def\n"; |
| 866 |
|
| 867 |
if (globals.debug & DBG_PS) |
| 868 |
puts("Running beginheaderNeWS()"); |
| 869 |
|
| 870 |
if (NeWS_active) { |
| 871 |
if (!NeWS_in_header) |
| 872 |
XDVI_FATAL((stderr, "Internal error in beginheaderNeWS().")); |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
if (globals.ev.flags & NEWS_MASK_HEADER) |
| 877 |
longjmp(globals.ev.canit, 1); |
| 878 |
|
| 879 |
NeWS_in_header = True; |
| 880 |
NeWS_ev_mask = NEWS_MASK_HEADER; |
| 881 |
++NeWS_pending; |
| 882 |
if (NeWS_in_doc) |
| 883 |
NeWS_send("H", 1); |
| 884 |
else { |
| 885 |
NeWS_send(str, (sizeof str) - 1); |
| 886 |
NeWS_in_doc = True; |
| 887 |
} |
| 888 |
NeWS_active = True; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
/*---------------------------------------------------------------------------* |
| 893 |
endheaderNeWS() |
| 894 |
|
| 895 |
Arguments: none |
| 896 |
Returns: (void) |
| 897 |
|
| 898 |
Description: |
| 899 |
Prepares the PostScript interpreter for receipt of header code. |
| 900 |
|
| 901 |
+----------------------------------------------------------------------------*/ |
| 902 |
|
| 903 |
static void |
| 904 |
endheaderNeWS(void) |
| 905 |
{ |
| 906 |
static const char str[] = "stop\n%%xdvimark\n"; |
| 907 |
|
| 908 |
if (globals.debug & DBG_PS) |
| 909 |
puts("Running endheaderNeWS()"); |
| 910 |
|
| 911 |
if (NeWS_active) { |
| 912 |
NeWS_send(str, (sizeof str) - 1); |
| 913 |
NeWS_active = False; |
| 914 |
post_send(); |
| 915 |
waitack(); |
| 916 |
NeWS_in_header = False; |
| 917 |
NeWS_ev_mask = NEWS_MASK_NORMAL; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
/*---------------------------------------------------------------------------* |
| 923 |
newdocNeWS() |
| 924 |
|
| 925 |
Arguments: none |
| 926 |
Returns: (void) |
| 927 |
|
| 928 |
Description: |
| 929 |
Clears out headers stored from the previous document. |
| 930 |
|
| 931 |
+----------------------------------------------------------------------------*/ |
| 932 |
|
| 933 |
static void |
| 934 |
newdocNeWS(void) |
| 935 |
{ |
| 936 |
static const char str[] = "H xdvi$doc restore stop\n%%xdvimark\n"; |
| 937 |
|
| 938 |
if (globals.debug & DBG_PS) |
| 939 |
puts("Running newdocNeWS()"); |
| 940 |
|
| 941 |
if (NeWS_in_doc) { |
| 942 |
++NeWS_pending; |
| 943 |
NeWS_send(str, (sizeof str) - 1); |
| 944 |
NeWS_mag = NeWS_shrink = -1; |
| 945 |
NeWS_in_doc = False; |
| 946 |
} |
| 947 |
} |
| 948 |
#else |
| 949 |
/* silence `empty compilation unit' warnings */ |
| 950 |
static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); } |
| 951 |
#endif /* PS_NEWS */ |