| 1 |
/* |
| 2 |
* Copyright (c) 2002-2004 Paul Vojta and the xdvik development team |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 18 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* SU: Printing/saving backend code (all the non-GUI stuff), |
| 25 |
* adapted from non-k xdvi's popups.c. |
| 26 |
* |
| 27 |
* Implementation notes: |
| 28 |
* All inter-process communication happens in an asynchronous |
| 29 |
* fasion, so that the GUI remains responsive. This means that if |
| 30 |
* there are several processes in sequence (e.g. dvips invocation, |
| 31 |
* then ps2pdf invocation), the latter must be called by the child |
| 32 |
* watching procedure of the former (e.g. ps2pdf_exited by the |
| 33 |
* fork_process() callback in call_ps2pdf()), else we can't catch its |
| 34 |
* return status, do cleanup afterwards etc., because the code inside |
| 35 |
* the caller may be executed *before* the child has finished. (Note |
| 36 |
* e.g. how unlink()ing the temporary PS file is done inside |
| 37 |
* ps2pdf_exited() and not in the callers). This is the reason why we |
| 38 |
* often need to pass information to the subprocess via a data pointer. |
| 39 |
*/ |
| 40 |
|
| 41 |
#include "xdvi-config.h" |
| 42 |
#include "xdvi.h" |
| 43 |
#include "dvisel.h" /* for select_pages_info struct */ |
| 44 |
#include "print-internal.h" |
| 45 |
#include "print-log.h" |
| 46 |
#include "search-internal.h" |
| 47 |
#include "events.h" |
| 48 |
#include "message-window.h" |
| 49 |
#include "util.h" |
| 50 |
#include "x_util.h" |
| 51 |
#include "string-utils.h" |
| 52 |
#include "my-snprintf.h" |
| 53 |
|
| 54 |
#include <errno.h> |
| 55 |
#include <signal.h> |
| 56 |
#include <sys/stat.h> |
| 57 |
|
| 58 |
/* if POSIX O_NONBLOCK is not available, use O_NDELAY */ |
| 59 |
#if !defined(O_NONBLOCK) && defined(O_NDELAY) |
| 60 |
# define O_NONBLOCK O_NDELAY |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifdef EWOULDBLOCK |
| 64 |
# ifdef EAGAIN |
| 65 |
# define AGAIN_CONDITION (errno == EWOULDBLOCK || errno == EAGAIN) |
| 66 |
# else |
| 67 |
# define AGAIN_CONDITION (errno == EWOULDBLOCK) |
| 68 |
# endif |
| 69 |
#else /* EWOULDBLOCK */ |
| 70 |
# ifdef EAGAIN |
| 71 |
# define AGAIN_CONDITION (errno == EAGAIN) |
| 72 |
# endif |
| 73 |
#endif /* EWOULDBLOCK */ |
| 74 |
|
| 75 |
#if HAVE_POLL |
| 76 |
# include <poll.h> |
| 77 |
# define XIO_IN POLLIN |
| 78 |
# define XIO_OUT POLLOUT |
| 79 |
#else |
| 80 |
# define XIO_IN 1 |
| 81 |
# define XIO_OUT 2 |
| 82 |
#endif /* HAVE_POLL */ |
| 83 |
|
| 84 |
static struct xchild print_child = {NULL, 0, "dvips", NULL, NULL, SIGKILL, NULL |
| 85 |
}; |
| 86 |
|
| 87 |
static char *read_from_dvips(int, void *); |
| 88 |
|
| 89 |
static struct xio print_xio = { NULL, 0, XIO_IN, |
| 90 |
#if HAVE_POLL |
| 91 |
NULL, |
| 92 |
#endif |
| 93 |
read_from_dvips, |
| 94 |
NULL, /* write proc - not needed */ |
| 95 |
NULL /* data */ |
| 96 |
}; |
| 97 |
|
| 98 |
static void dvips_alarm(struct xtimer *this, void *data); |
| 99 |
|
| 100 |
static struct xtimer dvips_timer = {NULL, {0, 0}, XTM_DEFAULT, dvips_alarm, NULL |
| 101 |
#if XDVI_XT_TIMER_HACK |
| 102 |
, NULL, NULL |
| 103 |
#endif |
| 104 |
}; |
| 105 |
|
| 106 |
static int dvips_sig; /* SIGINT or SIGKILL */ |
| 107 |
|
| 108 |
static int dvips_status; |
| 109 |
static const int DVIPS_STAT_NONE = 0; |
| 110 |
static const int DVIPS_STAT_RUN = 1; |
| 111 |
static const int DVIPS_STAT_WAIT = 2; |
| 112 |
|
| 113 |
/* |
| 114 |
FIXME: implement this in terms of fork_process()! |
| 115 |
Currently it doesn't work since the DVI file and the |
| 116 |
output file are realized via redirecting stdin/stdout. |
| 117 |
This also obscures the log output (the I/O redirection |
| 118 |
isn't visible to users). |
| 119 |
|
| 120 |
TODO: Remove the detailed log output, and implement |
| 121 |
a progress window that only shows the page numbers. |
| 122 |
The detailed log should go to an `xdvi log output' page. |
| 123 |
*/ |
| 124 |
|
| 125 |
static void |
| 126 |
fork_dvips(char **argv, struct save_or_print_info *info, childProcT proc) |
| 127 |
{ |
| 128 |
int print_io[2]; |
| 129 |
int i; |
| 130 |
struct file_info *finfo = info->finfo; |
| 131 |
/* printlog_append(argv[0], strlen(argv[0])); */ |
| 132 |
FILE *fout = NULL; |
| 133 |
|
| 134 |
if (info->act == FILE_SAVE || info->print_target == TO_FILE) { /* printing to PS file, open file for writing */ |
| 135 |
const char *out_file; |
| 136 |
|
| 137 |
if (info->print_target == TO_FILE || info->fmt == FMT_PS) |
| 138 |
out_file = finfo->out_file; |
| 139 |
else |
| 140 |
out_file = finfo->tmp_ps_file; |
| 141 |
|
| 142 |
if ((fout = XFOPEN(out_file, "w")) == NULL) { |
| 143 |
popup_message(globals.widgets.top_level, |
| 144 |
MSG_ERR, |
| 145 |
NULL, "Could not open %s for writing: %s.", |
| 146 |
out_file, |
| 147 |
strerror(errno)); |
| 148 |
return; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
printlog_popup(info); |
| 153 |
|
| 154 |
printlog_append_str(info, "Calling: `"); |
| 155 |
printlog_append_str(info, argv[0]); |
| 156 |
|
| 157 |
for (i = 1; argv[i] != NULL; i++) { |
| 158 |
printlog_append_str(info, " "); |
| 159 |
printlog_append_str(info, argv[i]); |
| 160 |
} |
| 161 |
printlog_append_str(info, "'\n"); |
| 162 |
|
| 163 |
if (xpipe(print_io) != 0) { |
| 164 |
perror("[xdvi] pipe"); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
/* Fork process */ |
| 169 |
|
| 170 |
/* flush output buffers to avoid double buffering (i.e. data |
| 171 |
waiting in the output buffer being written twice, by the parent |
| 172 |
and the child) */ |
| 173 |
fflush(stderr); |
| 174 |
fflush(stdout); |
| 175 |
|
| 176 |
print_child.name = xstrdup(argv[0]); |
| 177 |
print_child.proc = proc; |
| 178 |
print_child.data = info; |
| 179 |
print_child.pid = fork(); |
| 180 |
if (print_child.pid == 0) { /* if child */ |
| 181 |
/* change into dir of DVI file so that included image files etc. are found */ |
| 182 |
(void)chdir(globals.dvi_file.dirname); |
| 183 |
|
| 184 |
/* make the input file pointer the STDIN of the dvips process */ |
| 185 |
if (info->page_selection == PAGE_MARKED) { /* printing selected pages from temporary file */ |
| 186 |
ASSERT(finfo->tmp_dvi_fp != NULL, "tmp fp mustn't be NULL!"); |
| 187 |
(void)dup2(fileno(finfo->tmp_dvi_fp), STDIN_FILENO); |
| 188 |
} |
| 189 |
else { /* printing from main or backup file */ |
| 190 |
(void)dup2(fileno(finfo->in_fp), STDIN_FILENO); |
| 191 |
} |
| 192 |
(void)lseek(0, 0, SEEK_SET); |
| 193 |
|
| 194 |
if (fout != NULL) { /* printing to file, make stdout of child go to fout */ |
| 195 |
(void)dup2(fileno(fout), STDOUT_FILENO); |
| 196 |
(void)close(fileno(fout)); |
| 197 |
} |
| 198 |
else { /* printing to printer, make stdout of child go to print_io[1] */ |
| 199 |
(void)dup2(print_io[1], STDOUT_FILENO); |
| 200 |
} |
| 201 |
|
| 202 |
/* make stderr of child go to print_io[1] */ |
| 203 |
(void)dup2(print_io[1], STDERR_FILENO); |
| 204 |
(void)close(print_io[1]); |
| 205 |
(void)close(print_io[0]); |
| 206 |
|
| 207 |
if (setsid() == -1) { /* so we can kill the process group */ |
| 208 |
perror("setsid"); |
| 209 |
fflush(stderr); |
| 210 |
_exit(1); |
| 211 |
} |
| 212 |
(void)execvp(*argv, argv); |
| 213 |
popup_message(globals.widgets.top_level, |
| 214 |
MSG_ERR, |
| 215 |
NULL, |
| 216 |
"Execution of \"%s\" failed: %s.\n", *argv, strerror(errno)); |
| 217 |
fflush(stderr); |
| 218 |
_exit(1); |
| 219 |
} |
| 220 |
|
| 221 |
if (fout != NULL) |
| 222 |
fclose(fout); |
| 223 |
|
| 224 |
if (print_child.pid == -1) { /* error */ |
| 225 |
perror("[xdvi] vfork"); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
set_chld(&print_child); |
| 230 |
dvips_sig = SIGINT; |
| 231 |
|
| 232 |
(void)close(print_io[1]); |
| 233 |
|
| 234 |
/* Set up file descriptor for non-blocking I/O */ |
| 235 |
prep_fd(print_io[0], True); |
| 236 |
print_xio.fd = print_io[0]; |
| 237 |
print_xio.data = info; |
| 238 |
set_io(&print_xio); |
| 239 |
|
| 240 |
dvips_status = DVIPS_STAT_RUN; /* running */ |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* Create an argument list for dvips, using information from info |
| 245 |
* and X resources. Returns the result list in freshly allocated |
| 246 |
* memory. |
| 247 |
*/ |
| 248 |
static char ** |
| 249 |
create_dvips_argv(const struct save_or_print_info *info, Boolean do_pdf) |
| 250 |
{ |
| 251 |
const struct select_pages_info *pinfo = info->pinfo; |
| 252 |
|
| 253 |
size_t argv_len = 128; /* should be ample ... */ |
| 254 |
char **argv = xmalloc(argv_len * sizeof *argv); |
| 255 |
size_t idx = 0; |
| 256 |
const char *printer_options = info->printer_options; |
| 257 |
const char *dvips_options = info->dvips_options; |
| 258 |
char from_page[LENGTH_OF_INT]; |
| 259 |
char to_page[LENGTH_OF_INT]; |
| 260 |
|
| 261 |
TRACE_GUI((stderr, "dvips options: |%s|", dvips_options)); |
| 262 |
|
| 263 |
argv[idx++] = xstrdup(resource.dvips_path); |
| 264 |
|
| 265 |
if (dvips_options != NULL) { |
| 266 |
char **dvips_args = get_separated_list(dvips_options, " \t", True); /* this allocates dvips_args */ |
| 267 |
int i; |
| 268 |
for (i = 0; dvips_args[i] != NULL; i++) { |
| 269 |
argv[idx++] = dvips_args[i]; |
| 270 |
} |
| 271 |
free(dvips_args); |
| 272 |
} |
| 273 |
|
| 274 |
if (do_pdf) |
| 275 |
argv[idx++] = xstrdup("-Ppdf"); |
| 276 |
|
| 277 |
argv[idx++] = xstrdup("-f"); |
| 278 |
|
| 279 |
if (info->print_target == TO_PRINTER && printer_options != NULL) { /* printing to printer */ |
| 280 |
char **printer_args = get_separated_list(printer_options, " \t", True); /* this allocates printer_args */ |
| 281 |
int i; |
| 282 |
ASSERT(*printer_args != NULL, "args should contain at least the string \"lpr\""); |
| 283 |
argv[idx] = xstrdup("-o!"); |
| 284 |
argv[idx] = xstrcat(argv[idx], printer_args[0]); |
| 285 |
free(printer_args[0]); |
| 286 |
/* now append everything to the printer pipe command, so that dvips doesn't see it as an option */ |
| 287 |
for (i = 1; printer_args[i] != NULL; i++) { |
| 288 |
argv[idx] = xstrcat(argv[idx], " "); |
| 289 |
argv[idx] = xstrcat(argv[idx], printer_args[i]); |
| 290 |
free(printer_args[i]); |
| 291 |
} |
| 292 |
free(printer_args); |
| 293 |
idx++; |
| 294 |
} |
| 295 |
|
| 296 |
if (info->page_selection == PAGE_RANGE) { |
| 297 |
/* convert back from 0-based to 1-based, also taking globals.pageno_correct into accout |
| 298 |
* (which is 1 by default, so we need to add 2) */ |
| 299 |
argv[idx] = xstrdup("-p="); |
| 300 |
SNPRINTF(from_page, LENGTH_OF_INT, "%d", pinfo->from - globals.pageno_correct + 2); |
| 301 |
argv[idx] = xstrcat(argv[idx], from_page); |
| 302 |
idx++; |
| 303 |
argv[idx] = xstrdup("-l="); |
| 304 |
SNPRINTF(to_page, LENGTH_OF_INT, "%d", pinfo->to - globals.pageno_correct + 2); |
| 305 |
argv[idx] = xstrcat(argv[idx], to_page); |
| 306 |
idx++; |
| 307 |
} |
| 308 |
|
| 309 |
argv[idx] = NULL; /* terminate it */ |
| 310 |
ASSERT(idx <= argv_len, "Too many elements"); |
| 311 |
return argv; |
| 312 |
} |
| 313 |
|
| 314 |
/* Check if selecting pages worked, report error else */ |
| 315 |
static Boolean |
| 316 |
select_pages_report_error(const struct save_or_print_info *info) |
| 317 |
{ |
| 318 |
if (info->pinfo->errflag == NO_ERROR) |
| 319 |
return False; |
| 320 |
|
| 321 |
popup_message(globals.widgets.top_level, |
| 322 |
MSG_INFO, |
| 323 |
NULL, |
| 324 |
"Could not save DVI file to %s: %s.", |
| 325 |
info->finfo->tmp_dvi_file, |
| 326 |
get_dvi_error(info->pinfo->errflag)); |
| 327 |
return True; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/* |
| 332 |
* Collect dvips output, appending to print log. |
| 333 |
* Currently return value is a dummy (NULL); TODO: return |
| 334 |
* the output, for later filtering. |
| 335 |
*/ |
| 336 |
static char * |
| 337 |
read_from_dvips(int ignored, void *data) |
| 338 |
{ |
| 339 |
int bytes; |
| 340 |
char line[80]; |
| 341 |
struct save_or_print_info *info = data; |
| 342 |
|
| 343 |
UNUSED(ignored); |
| 344 |
|
| 345 |
for (;;) { |
| 346 |
#ifndef MOTIF |
| 347 |
bytes = read(print_xio.fd, line, sizeof line); |
| 348 |
#else |
| 349 |
bytes = read(print_xio.fd, line, sizeof line - 1); |
| 350 |
#endif |
| 351 |
if (bytes < 0) { |
| 352 |
if (AGAIN_CONDITION) |
| 353 |
break; |
| 354 |
perror("xdvi: read_from_dvips"); |
| 355 |
break; |
| 356 |
} |
| 357 |
|
| 358 |
if (bytes == 0) |
| 359 |
break; |
| 360 |
else { |
| 361 |
#ifdef MOTIF |
| 362 |
line[bytes] = '\0'; |
| 363 |
#endif |
| 364 |
printlog_append(info, line, bytes); |
| 365 |
} |
| 366 |
} |
| 367 |
return NULL; /* TODO */ |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
* Report dvips exit status, and remove temporary DVI file if needed. |
| 372 |
*/ |
| 373 |
static void |
| 374 |
dvips_exited(int exitval, struct xchild *child) |
| 375 |
{ |
| 376 |
char str[128] = ""; |
| 377 |
int ms; |
| 378 |
|
| 379 |
struct save_or_print_info *info = (struct save_or_print_info *)child->data; |
| 380 |
|
| 381 |
read_from_dvips(0, info); |
| 382 |
clear_io(&print_xio); |
| 383 |
(void)close(print_xio.fd); |
| 384 |
|
| 385 |
if (WIFEXITED(exitval)) { |
| 386 |
if (WEXITSTATUS(exitval) == 0) { |
| 387 |
if (info->act == FILE_SAVE || info->print_target == TO_FILE) { |
| 388 |
printlog_append_str(info, "\nCreated Postscript file "); |
| 389 |
printlog_append_str(info, info->finfo->out_file); |
| 390 |
printlog_append_str(info, ".\n"); |
| 391 |
} |
| 392 |
else { |
| 393 |
printlog_append_str(info, "Done.\n"); |
| 394 |
} |
| 395 |
|
| 396 |
/* remove temporary DVI file if it exists */ |
| 397 |
if (info->finfo->tmp_dvi_file != NULL) { |
| 398 |
if (globals.debug & DBG_GUI) |
| 399 |
TRACE_GUI((stderr, "NOT removing temporary DVI file: |%s|", info->finfo->tmp_dvi_file)); |
| 400 |
else { |
| 401 |
if (info->finfo->tmp_dvi_fp != NULL) { |
| 402 |
fclose(info->finfo->tmp_dvi_fp); |
| 403 |
info->finfo->tmp_dvi_fp = NULL; |
| 404 |
} |
| 405 |
unlink(info->finfo->tmp_dvi_file); |
| 406 |
info->finfo->tmp_dvi_file = NULL; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
else |
| 411 |
sprintf(str, "\nDvips returned exit code %d.\n", WEXITSTATUS(exitval)); |
| 412 |
} |
| 413 |
else if (WIFSIGNALED(exitval)) |
| 414 |
sprintf(str, "\nDvips terminated by signal %d.\n", WTERMSIG(exitval)); |
| 415 |
else |
| 416 |
sprintf(str, "\nDvips returned unknown status 0x%x.\n", exitval); |
| 417 |
|
| 418 |
ms = resource.dvips_hang; |
| 419 |
if (str[0] != '\0') { |
| 420 |
xdvi_bell(); |
| 421 |
ms = resource.dvips_fail_hang; |
| 422 |
printlog_append_str(info, str); |
| 423 |
} |
| 424 |
|
| 425 |
if (ms > 0) { |
| 426 |
dvips_timer.data = info; |
| 427 |
set_timer(&dvips_timer, ms); |
| 428 |
dvips_status = DVIPS_STAT_WAIT; |
| 429 |
} |
| 430 |
else { |
| 431 |
dvips_status = DVIPS_STAT_NONE; |
| 432 |
} |
| 433 |
|
| 434 |
printlog_enable_closebutton(info); |
| 435 |
} |
| 436 |
|
| 437 |
/* |
| 438 |
* Report pd2pdf exit status, and remove temporary PS file if needed. |
| 439 |
*/ |
| 440 |
static void |
| 441 |
ps2pdf_exited(int status, struct xchild *this) |
| 442 |
{ |
| 443 |
char *err_msg = NULL; |
| 444 |
int ms = resource.dvips_hang; |
| 445 |
struct save_or_print_info *info = (struct save_or_print_info *)this->data; |
| 446 |
|
| 447 |
/* if child exited with error and xio struct is available for child, |
| 448 |
print error text */ |
| 449 |
if (this->io != NULL && (WIFEXITED(status) != 0)) { |
| 450 |
if ((WEXITSTATUS(status) != 0) |
| 451 |
&& (err_msg = (this->io->read_proc)(this->io->fd, NULL)) != NULL) { |
| 452 |
char buf[LENGTH_OF_INT]; |
| 453 |
SNPRINTF(buf, LENGTH_OF_INT, "%d", WEXITSTATUS(status)); |
| 454 |
ms = resource.dvips_fail_hang; |
| 455 |
xdvi_bell(); |
| 456 |
printlog_append_str(info, "\n\nError calling "); |
| 457 |
if (this->name != NULL) { |
| 458 |
printlog_append_str(info, "\""); |
| 459 |
printlog_append_str(info, this->name); |
| 460 |
printlog_append_str(info, "\" "); |
| 461 |
} |
| 462 |
printlog_append_str(info, "\nCommand exited with error code "); |
| 463 |
printlog_append_str(info, buf); |
| 464 |
printlog_append_str(info, ":\n"); |
| 465 |
printlog_append_str(info, err_msg); |
| 466 |
free(err_msg); |
| 467 |
} |
| 468 |
else { |
| 469 |
int retval; |
| 470 |
printlog_append_str(info, "\nCreated PDF file "); |
| 471 |
printlog_append_str(info, info->finfo->out_file); |
| 472 |
printlog_append_str(info, ".\n"); |
| 473 |
TRACE_FILES((stderr, "Removing temporary PS file: `%s'", |
| 474 |
info->finfo->tmp_ps_file)); |
| 475 |
retval = unlink(info->finfo->tmp_ps_file); |
| 476 |
if (retval != 0) { |
| 477 |
fprintf(stderr, "Could not unlink `%s': %s.\n", |
| 478 |
info->finfo->tmp_ps_file, strerror(errno)); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
else { |
| 483 |
popup_message(globals.widgets.top_level, |
| 484 |
MSG_ERR, REPORT_XDVI_BUG_TEMPLATE, |
| 485 |
"Internal error: ps2pdf_exited() called while child still running?"); |
| 486 |
} |
| 487 |
if (ms > 0) { |
| 488 |
dvips_timer.data = info; |
| 489 |
set_timer(&dvips_timer, ms); |
| 490 |
dvips_status = DVIPS_STAT_WAIT; |
| 491 |
} |
| 492 |
else { |
| 493 |
dvips_status = DVIPS_STAT_NONE; |
| 494 |
} |
| 495 |
|
| 496 |
printlog_enable_closebutton(info); |
| 497 |
|
| 498 |
free(this->name); |
| 499 |
free(this->io); |
| 500 |
free(this); |
| 501 |
} |
| 502 |
|
| 503 |
static void |
| 504 |
call_ps2pdf(const char *path, const struct save_or_print_info *info) |
| 505 |
{ |
| 506 |
const char *argv[5]; |
| 507 |
size_t idx = 0; |
| 508 |
|
| 509 |
argv[idx++] = path; |
| 510 |
argv[idx++] = info->finfo->tmp_ps_file; |
| 511 |
argv[idx++] = info->finfo->out_file; |
| 512 |
argv[idx++] = NULL; |
| 513 |
|
| 514 |
/* need to run this in globals.xdvi_dir again, since the dvips conversion directory |
| 515 |
globals.dvi_file.dirname may not be writable! */ |
| 516 |
if (!fork_process("ps2pdf", True, globals.cwd, ps2pdf_exited, (void *)info, |
| 517 |
0, (char **)argv)) { |
| 518 |
popup_message(globals.widgets.top_level, |
| 519 |
MSG_ERR, |
| 520 |
NULL, "Couldn't fork %s process: %s\n", argv[0], strerror(errno)); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
static void |
| 525 |
dvips_ps2pdf(int exitval, struct xchild *child) |
| 526 |
{ |
| 527 |
char str[128] = ""; |
| 528 |
int ms; |
| 529 |
struct save_or_print_info *info = (struct save_or_print_info *)child->data; |
| 530 |
|
| 531 |
read_from_dvips(0, info); |
| 532 |
clear_io(&print_xio); |
| 533 |
(void)close(print_xio.fd); |
| 534 |
|
| 535 |
if (WIFEXITED(exitval)) { |
| 536 |
if (WEXITSTATUS(exitval) == 0) { |
| 537 |
/* dvips ended OK; call ps2pdf: */ |
| 538 |
TRACE_GUI((stderr, "Created temporary PS file |%s|", info->finfo->tmp_ps_file)); |
| 539 |
printlog_append_str(info, "\nCalling "); |
| 540 |
printlog_append_str(info, resource.ps2pdf_path); |
| 541 |
printlog_append_str(info, " ..."); |
| 542 |
|
| 543 |
/* remove temporary DVI file if it exists */ |
| 544 |
if (info->finfo->tmp_dvi_file != NULL) { |
| 545 |
if (globals.debug & DBG_GUI) |
| 546 |
TRACE_GUI((stderr, "NOT removing temporary DVI file: |%s|", info->finfo->tmp_dvi_file)); |
| 547 |
else { |
| 548 |
if (info->finfo->tmp_dvi_fp != NULL) { |
| 549 |
fclose(info->finfo->tmp_dvi_fp); |
| 550 |
info->finfo->tmp_dvi_fp = NULL; |
| 551 |
} |
| 552 |
unlink(info->finfo->tmp_dvi_file); |
| 553 |
info->finfo->tmp_dvi_file = NULL; |
| 554 |
} |
| 555 |
} |
| 556 |
/* invoke ps2pdf conversion */ |
| 557 |
call_ps2pdf(resource.ps2pdf_path, info); |
| 558 |
} |
| 559 |
else |
| 560 |
sprintf(str, "\nPrint process returned exit code %d.\n", |
| 561 |
WEXITSTATUS(exitval)); |
| 562 |
} |
| 563 |
else if (WIFSIGNALED(exitval)) |
| 564 |
sprintf(str, "\nPrint process terminated by signal %d.\n", |
| 565 |
WTERMSIG(exitval)); |
| 566 |
else |
| 567 |
sprintf(str, "\nPrint process returned unknown status 0x%x.\n", |
| 568 |
exitval); |
| 569 |
|
| 570 |
/* enable close button only if dvips conversion already failed */ |
| 571 |
ms = resource.dvips_hang; |
| 572 |
if (str[0] != '\0') { |
| 573 |
xdvi_bell(); |
| 574 |
ms = resource.dvips_fail_hang; |
| 575 |
printlog_append_str(info, str); |
| 576 |
|
| 577 |
if (ms > 0) { |
| 578 |
dvips_timer.data = info; |
| 579 |
set_timer(&dvips_timer, ms); |
| 580 |
dvips_status = DVIPS_STAT_WAIT; |
| 581 |
} |
| 582 |
else { |
| 583 |
dvips_status = DVIPS_STAT_NONE; |
| 584 |
} |
| 585 |
|
| 586 |
printlog_enable_closebutton(info); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
static void |
| 592 |
dvips_alarm(struct xtimer *this, void *data) |
| 593 |
{ |
| 594 |
struct save_or_print_info *info = (struct save_or_print_info *)data; |
| 595 |
UNUSED(this); |
| 596 |
|
| 597 |
printlog_popdown(info, False); |
| 598 |
|
| 599 |
dvips_status = DVIPS_STAT_NONE; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
static void |
| 605 |
cb_dvips_keep(Widget w, XtPointer client_data, XtPointer call_data) |
| 606 |
{ |
| 607 |
|
| 608 |
UNUSED(w); |
| 609 |
UNUSED(client_data); |
| 610 |
UNUSED(call_data); |
| 611 |
|
| 612 |
/* use negative value so that we don't loose the value itself */ |
| 613 |
TRACE_GUI((stderr, "toggling resource from %d to %d, %d to %d", |
| 614 |
resource.dvips_hang, -resource.dvips_hang, |
| 615 |
resource.dvips_fail_hang, -resource.dvips_fail_hang)); |
| 616 |
resource.dvips_hang = -resource.dvips_hang; |
| 617 |
resource.dvips_fail_hang = -resource.dvips_fail_hang; |
| 618 |
|
| 619 |
store_preference(NULL, "dvipsHangTime", "%d", resource.dvips_hang); |
| 620 |
store_preference(NULL, "dvipsFailHangTime", "%d", resource.dvips_fail_hang); |
| 621 |
|
| 622 |
|
| 623 |
if (dvips_status == DVIPS_STAT_WAIT) { |
| 624 |
dvips_status = DVIPS_STAT_NONE; |
| 625 |
cancel_timer(&dvips_timer); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
/* if user selects the window manager destroy button */ |
| 630 |
static void |
| 631 |
cb_dvips_destroy(Widget w, XtPointer client_data, XtPointer call_data) |
| 632 |
{ |
| 633 |
struct save_or_print_info *info = (struct save_or_print_info *)client_data; |
| 634 |
|
| 635 |
UNUSED(w); |
| 636 |
UNUSED(call_data); |
| 637 |
|
| 638 |
if (dvips_status == DVIPS_STAT_RUN) { |
| 639 |
kill(print_child.pid, dvips_sig); |
| 640 |
dvips_sig = SIGKILL; |
| 641 |
printlog_append_str(info, "^C"); |
| 642 |
} |
| 643 |
|
| 644 |
if (dvips_status == DVIPS_STAT_WAIT) { |
| 645 |
dvips_status = DVIPS_STAT_NONE; |
| 646 |
cancel_timer(&dvips_timer); |
| 647 |
} |
| 648 |
|
| 649 |
printlog_reset(info); |
| 650 |
printlog_popdown(info, True); |
| 651 |
} |
| 652 |
|
| 653 |
static void |
| 654 |
cb_dvips_cancel(Widget w, XtPointer client_data, XtPointer call_data) |
| 655 |
{ |
| 656 |
struct save_or_print_info *info = (struct save_or_print_info *)client_data; |
| 657 |
|
| 658 |
UNUSED(w); |
| 659 |
UNUSED(call_data); |
| 660 |
|
| 661 |
if (dvips_status != DVIPS_STAT_RUN) |
| 662 |
return; /* How did we get here? */ |
| 663 |
|
| 664 |
kill(print_child.pid, dvips_sig); |
| 665 |
dvips_sig = SIGKILL; |
| 666 |
printlog_append_str(info, "^C"); |
| 667 |
} |
| 668 |
|
| 669 |
static void |
| 670 |
cb_dvips_close(Widget w, XtPointer client_data, XtPointer call_data) |
| 671 |
{ |
| 672 |
struct save_or_print_info *info = (struct save_or_print_info *)client_data; |
| 673 |
|
| 674 |
UNUSED(w); |
| 675 |
UNUSED(call_data); |
| 676 |
|
| 677 |
if (dvips_status == DVIPS_STAT_RUN) |
| 678 |
return; /* How did we get here? */ |
| 679 |
|
| 680 |
if (dvips_status == DVIPS_STAT_WAIT) { |
| 681 |
dvips_status = DVIPS_STAT_NONE; |
| 682 |
cancel_timer(&dvips_timer); |
| 683 |
} |
| 684 |
|
| 685 |
printlog_popdown(info, True); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
void |
| 690 |
internal_save(struct save_or_print_info *info) |
| 691 |
{ |
| 692 |
struct file_info *finfo = info->finfo; |
| 693 |
static struct callback_info cinfo = { |
| 694 |
cb_dvips_close, cb_dvips_cancel, cb_dvips_destroy, cb_dvips_keep |
| 695 |
}; |
| 696 |
int tmp_fd; |
| 697 |
char **argv = NULL; |
| 698 |
int i; |
| 699 |
childProcT dvips_exit_proc = dvips_exited; /* default procedure to call after fork_dvips() */ |
| 700 |
Boolean do_pdf = False; /* force `-Ppdf' for dvips? (used for ps2pdf conversion) */ |
| 701 |
|
| 702 |
ASSERT(info->fmt != FMT_NONE, "No valid output format selected!"); |
| 703 |
|
| 704 |
/* fprintf(stderr, "INTERNAL SAVE - format: %d; selection: %d\n", info->fmt, info->page_selection); */ |
| 705 |
|
| 706 |
info->callbacks = &cinfo; |
| 707 |
|
| 708 |
switch (info->fmt) { |
| 709 |
|
| 710 |
case FMT_DVI: |
| 711 |
/* here we first create a temporary file, and if that succeeded, |
| 712 |
move it to the final position */ |
| 713 |
select_pages(info); |
| 714 |
|
| 715 |
if (!select_pages_report_error(info)) { |
| 716 |
/* |
| 717 |
* ... else, move the temporary file to its final destination. For that, we |
| 718 |
* try the more efficient rename() first; if this fails, try copying the file |
| 719 |
* (rename() will e.g. fail if the tmp directory is on a different file |
| 720 |
* system). We could perhaps examine errno after the renaming attempt to |
| 721 |
* find out whether we need to do the copy attempt in the first place, but |
| 722 |
* that'd be rather error-prone ... */ |
| 723 |
ASSERT(finfo->tmp_dvi_file != NULL, "filename mustn't be NULL"); |
| 724 |
if (rename(finfo->tmp_dvi_file, finfo->out_file) != 0 |
| 725 |
&& !copy_file(finfo->tmp_dvi_file, finfo->out_file)) { |
| 726 |
popup_message(globals.widgets.top_level, |
| 727 |
MSG_ERR, NULL, "Creating %s failed: %s", |
| 728 |
finfo->out_file, strerror(errno)); |
| 729 |
} |
| 730 |
else { /* moving worked */ |
| 731 |
TRACE_GUI((stderr, "renamed %s to %s\n", finfo->tmp_dvi_file, finfo->out_file)); |
| 732 |
|
| 733 |
if (info->pinfo->warn_files.stack_len > 0) { /* do we need to warn user about referenced files? */ |
| 734 |
char *warn_files = xstrdup(""); |
| 735 |
size_t i; |
| 736 |
for (i = 0; i < info->pinfo->warn_files.stack_len; i++) { |
| 737 |
warn_files = xstrcat(warn_files, info->pinfo->warn_files.items[i].content); |
| 738 |
warn_files = xstrcat(warn_files, "\n"); |
| 739 |
} |
| 740 |
popup_message(globals.widgets.top_level, |
| 741 |
MSG_INFO, |
| 742 |
"The files listed in the message (probably images) are not part " |
| 743 |
"of the newly created DVI file; they are only referenced by that file. " |
| 744 |
"Therefore, if you want to distribute this file or use it on a different computer," |
| 745 |
"you will need to distribute all the referenced files, too.\n\n" |
| 746 |
"Tip: A better format for distributing files is Postscript or PDF. " |
| 747 |
"To create a Postscript file, use the `File->Print' menu, " |
| 748 |
"then select `Print to file' in the dialog window.", |
| 749 |
"Created %s.\n" |
| 750 |
"Please note that the following files are referenced by this file, " |
| 751 |
"and are needed for displaying or printing it correctly:\n%s\n", |
| 752 |
finfo->out_file, warn_files); |
| 753 |
free(warn_files); |
| 754 |
} |
| 755 |
else { |
| 756 |
/* char *ptr = strrchr(finfo->out_file, '/'); */ |
| 757 |
/* if (ptr == NULL) */ |
| 758 |
/* ptr = finfo->out_file; */ |
| 759 |
/* else */ |
| 760 |
/* ptr++; */ |
| 761 |
popup_message(globals.widgets.top_level, |
| 762 |
MSG_INFO, |
| 763 |
NULL, |
| 764 |
"Created DVI file:\n%s.", finfo->out_file); |
| 765 |
} |
| 766 |
if (info->finfo->tmp_dvi_fp != NULL) { |
| 767 |
fclose(info->finfo->tmp_dvi_fp); |
| 768 |
info->finfo->tmp_dvi_fp = NULL; |
| 769 |
} |
| 770 |
unlink(finfo->tmp_dvi_file); |
| 771 |
info->finfo->tmp_dvi_file = NULL; |
| 772 |
} |
| 773 |
} |
| 774 |
break; |
| 775 |
|
| 776 |
case FMT_PS2PDF: |
| 777 |
free(finfo->tmp_ps_file); |
| 778 |
finfo->tmp_ps_file = NULL; |
| 779 |
tmp_fd = xdvi_temp_fd(&(finfo->tmp_ps_file)); /* this allocates finfo->tmp_ps_file */ |
| 780 |
if (tmp_fd == -1) { |
| 781 |
popup_message(globals.widgets.top_level, |
| 782 |
MSG_ERR, NULL, |
| 783 |
"Couldn't create temporary PS file for conversion to PDF: %s", |
| 784 |
strerror(errno)); |
| 785 |
return; |
| 786 |
} |
| 787 |
else { |
| 788 |
TRACE_GUI((stderr, "name of temporary PS file: |%s|", finfo->tmp_ps_file)); |
| 789 |
} |
| 790 |
dvips_exit_proc = dvips_ps2pdf; /* call ps2pdf conversion after dvips exited */ |
| 791 |
do_pdf = True; |
| 792 |
/* fall through */ |
| 793 |
case FMT_PS: |
| 794 |
if (info->page_selection == PAGE_MARKED) { /* want to save selected pages? */ |
| 795 |
ASSERT(info->pinfo->callback != NULL, "Callback musn't be NULL!"); |
| 796 |
ASSERT(finfo->tmp_dvi_fp != NULL, "Temporary file pointer musn't be NULL!"); |
| 797 |
ASSERT(finfo->tmp_dvi_file != NULL, "Temporary filename musn't be NULL!"); |
| 798 |
select_pages(info); |
| 799 |
} |
| 800 |
if (info->printlog == NULL) |
| 801 |
printlog_create(info, "Xdvik: Saving", "Automatically close this window after file has been saved"); |
| 802 |
else |
| 803 |
printlog_enable_cancelbutton(info); |
| 804 |
|
| 805 |
if ((argv = create_dvips_argv(info, do_pdf)) == NULL) { |
| 806 |
/* something went *really* wrong; assume user has already been warned about it */ |
| 807 |
return; |
| 808 |
} |
| 809 |
fork_dvips(argv, info, dvips_exit_proc); |
| 810 |
|
| 811 |
/* dellocate argv */ |
| 812 |
for (i = 0; argv[i] != NULL; i++) { |
| 813 |
free(argv[i]); |
| 814 |
} |
| 815 |
free(argv); |
| 816 |
|
| 817 |
break; |
| 818 |
case FMT_ISO_8859_1: |
| 819 |
case FMT_UTF8: |
| 820 |
if (search_extract_text(info)) { |
| 821 |
popup_message(globals.widgets.top_level, |
| 822 |
MSG_INFO, |
| 823 |
NULL, |
| 824 |
"Created text file %s.", finfo->out_file); |
| 825 |
} |
| 826 |
else { |
| 827 |
popup_message(globals.widgets.top_level, |
| 828 |
MSG_INFO, |
| 829 |
NULL, |
| 830 |
"Extracting text from DVI file failed!"); |
| 831 |
} |
| 832 |
break; |
| 833 |
case FMT_NONE: |
| 834 |
break; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
void |
| 839 |
internal_print(struct save_or_print_info *info) |
| 840 |
{ |
| 841 |
static struct callback_info cinfo = { |
| 842 |
cb_dvips_close, cb_dvips_cancel, cb_dvips_destroy, cb_dvips_keep |
| 843 |
}; |
| 844 |
char **argv = NULL; |
| 845 |
int i; |
| 846 |
|
| 847 |
info->callbacks = &cinfo; |
| 848 |
|
| 849 |
if (info->page_selection == PAGE_MARKED) { /* want to print selected pages? */ |
| 850 |
ASSERT(info->pinfo->callback != NULL, "Callback musn't be NULL!"); |
| 851 |
ASSERT(info->finfo->tmp_dvi_fp != NULL, "Temporary file pointer musn't be NULL!"); |
| 852 |
ASSERT(info->finfo->tmp_dvi_file != NULL, "Temporary filename musn't be NULL!"); |
| 853 |
select_pages(info); |
| 854 |
} |
| 855 |
if (info->printlog == NULL) |
| 856 |
printlog_create(info, "Xdvik: Printing", "Automatically close this window when printing finishes"); |
| 857 |
else |
| 858 |
printlog_enable_cancelbutton(info); |
| 859 |
|
| 860 |
if ((argv = create_dvips_argv(info, False)) == NULL) { |
| 861 |
/* something went *really* wrong; assume user has already been warned about it */ |
| 862 |
return; |
| 863 |
} |
| 864 |
|
| 865 |
fork_dvips(argv, info, dvips_exited); |
| 866 |
|
| 867 |
/* dellocate argv */ |
| 868 |
for (i = 0; argv[i] != NULL; i++) { |
| 869 |
free(argv[i]); |
| 870 |
} |
| 871 |
free(argv); |
| 872 |
} |