| 1 |
/* dvihp.c: emulate the dvihp shell script |
| 2 |
|
| 3 |
Converted to C by Eli Zaretskii <[email protected]> |
| 4 |
Adapted to Win32 by Fabrice POPINEAU |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with this library; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <kpathsea/config.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#ifdef _WIN32 |
| 24 |
#include <io.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <kpathsea/kpathsea.h> |
| 28 |
#include "mktex.h" |
| 29 |
#include "stackenv.h" |
| 30 |
|
| 31 |
static const char rcs_id[] = |
| 32 |
"$Id: dvihp.c,v 1.7 2002/10/06 14:02:09 olaf Exp $"; |
| 33 |
static char rcs_revision[] = "$Revision: 1.7 $"; |
| 34 |
|
| 35 |
static void usage (void) |
| 36 |
{ |
| 37 |
printf ("Usage: %s [OPTIONS] [DVIFILE[.dvi]].\n\ |
| 38 |
Translate the given DVIFILE to Hewlett-Packard PCL by calling dvicopy\n\ |
| 39 |
and then $DVILJ (dvilj4 by default).\n\ |
| 40 |
In the absence of other options, pipe the PCL to $SPOOL (lpr by default).\n\ |
| 41 |
\n\ |
| 42 |
Options are recognized from dvips where possible:\n\ |
| 43 |
-A print odd pages\n\ |
| 44 |
-B print even pages\n\ |
| 45 |
-d # set debug bits to # (see documentation)\n\ |
| 46 |
-D # set resolution to #\n\ |
| 47 |
-f run as filter\n\ |
| 48 |
-l # don't print pages after #\n\ |
| 49 |
-m manual feed\n\ |
| 50 |
-n # print # pages\n\ |
| 51 |
-O #,# set/change paper offset to #,# mm\n\ |
| 52 |
-o s output to s instead of spooling\n\ |
| 53 |
-p # don't print pages before #\n\ |
| 54 |
-Ps pass directly to lpr\n\ |
| 55 |
-v verbose operation\n\ |
| 56 |
-x # set magnification to #\n\ |
| 57 |
\n\ |
| 58 |
Other options are passed to the dvilj program.\n\ |
| 59 |
\n\ |
| 60 |
Email bug reports to [email protected].\n", kpse_invocation_name); |
| 61 |
fflush (stdout); |
| 62 |
} |
| 63 |
|
| 64 |
/* Emulate `rm -f prefix*' */ |
| 65 |
|
| 66 |
|
| 67 |
static void rmfiles (char *prefix) |
| 68 |
{ |
| 69 |
#ifdef _WIN32 |
| 70 |
/* Win32 doesn't have POSIX dirent functions. */ |
| 71 |
WIN32_FIND_DATA ffd; |
| 72 |
HANDLE hnd; |
| 73 |
int go_on; |
| 74 |
string temp = concat (prefix, "*"); |
| 75 |
string directory = xdirname (prefix); |
| 76 |
|
| 77 |
pushd (directory); |
| 78 |
hnd = FindFirstFile(temp, &ffd); |
| 79 |
go_on = (hnd != INVALID_HANDLE_VALUE); |
| 80 |
while (go_on) { |
| 81 |
/* FIXME: can this remove read-only files also, like rm -f does? */ |
| 82 |
DeleteFile(ffd.cFileName); |
| 83 |
go_on = (FindNextFile(hnd, &ffd) == TRUE); |
| 84 |
} |
| 85 |
FindClose(hnd); |
| 86 |
free(temp); |
| 87 |
popd (); |
| 88 |
#else /* not _WIN32 */ |
| 89 |
DIR *dp; |
| 90 |
struct dirent *de; |
| 91 |
int temp_len = strlen (prefix); |
| 92 |
string directory = "./"; |
| 93 |
const_string base = xbasename (prefix); |
| 94 |
|
| 95 |
/* Copy the directory part of PREFIX with the trailing slash, if any. */ |
| 96 |
if (base != prefix) |
| 97 |
{ |
| 98 |
directory = (string) xmalloc (base - prefix + 1); |
| 99 |
directory[0] = '\0'; |
| 100 |
strncat (directory, prefix, base - prefix); |
| 101 |
} |
| 102 |
|
| 103 |
/* Find matching files and delete them. */ |
| 104 |
if ((dp = opendir (directory)) != 0) { |
| 105 |
while ((de = readdir (dp)) != 0) |
| 106 |
{ |
| 107 |
string found = concat (directory, de->d_name); |
| 108 |
|
| 109 |
if (FILESTRNCASEEQ (found, prefix, temp_len)) |
| 110 |
/* On POSIX-compliant systems, including DJGPP, this will also |
| 111 |
remove read-only files and empty directories, like rm -f does. */ |
| 112 |
if (remove (found)) |
| 113 |
perror (found); |
| 114 |
free (found); |
| 115 |
} |
| 116 |
} |
| 117 |
#endif /* not _WIN32 */ |
| 118 |
} |
| 119 |
|
| 120 |
static void __cdecl output_and_cleanup(int code) |
| 121 |
{ |
| 122 |
/* FIXME : what cleanup should be done ? */ |
| 123 |
} |
| 124 |
|
| 125 |
char *progname; |
| 126 |
|
| 127 |
int |
| 128 |
main (int argc, char *argv[]) |
| 129 |
{ |
| 130 |
string opt = (string) xmalloc (1); |
| 131 |
const_string output = "", infile = ""; |
| 132 |
const_string output_opt = " -e"; |
| 133 |
const_string dvilj; |
| 134 |
const_string spool; |
| 135 |
const_string tmpdir; |
| 136 |
const_string spool_opt = ""; |
| 137 |
char template[PATH_MAX]; |
| 138 |
const_string vfless_dvi; |
| 139 |
int verbose = 0; |
| 140 |
string cmdline; |
| 141 |
const_string maybe_spool_cmd = ""; |
| 142 |
const_string space = " "; |
| 143 |
|
| 144 |
output_and_cleanup_function = output_and_cleanup; |
| 145 |
|
| 146 |
progname = argv[0]; |
| 147 |
kpse_set_program_name (argv[0], "dvihp"); |
| 148 |
#ifdef _WIN32 |
| 149 |
setmode(fileno(stdin), _O_BINARY); |
| 150 |
setmode(fileno(stdout), _O_BINARY); |
| 151 |
SetConsoleCtrlHandler((PHANDLER_ROUTINE)mt_exit, TRUE); |
| 152 |
#else |
| 153 |
# ifdef SIGINT |
| 154 |
signal (SIGINT, sigint_handler); |
| 155 |
# endif |
| 156 |
# ifdef SIGQUIT |
| 157 |
signal (SIGQUIT, sigint_handler); |
| 158 |
# endif |
| 159 |
# ifdef SIGHUP |
| 160 |
signal (SIGHUP, sigint_handler); |
| 161 |
# endif |
| 162 |
# ifdef SIGTERM |
| 163 |
signal (SIGTERM, sigint_handler); |
| 164 |
# endif |
| 165 |
#endif |
| 166 |
|
| 167 |
dvilj = kpse_var_value ("DVILJ"); |
| 168 |
spool = kpse_var_value ("SPOOL"); |
| 169 |
tmpdir = kpse_var_value ("TMPDIR"); |
| 170 |
opt[0] = '\0'; |
| 171 |
|
| 172 |
if(!tmpdir) |
| 173 |
tmpdir = kpse_var_value ("TMP"); |
| 174 |
if(!tmpdir) |
| 175 |
tmpdir = kpse_var_value ("TEMP"); |
| 176 |
|
| 177 |
/* Set default values for variables: |
| 178 |
|
| 179 |
${DVILJ=dvilj4} the dvilj variant to run |
| 180 |
${SPOOL=lpr} used to print an LJ file |
| 181 |
${TMPDIR=/tmp} for the dvicopy output */ |
| 182 |
if (!dvilj || !*dvilj) |
| 183 |
dvilj = "dvilj4"; |
| 184 |
if (!spool || !*spool) |
| 185 |
{ |
| 186 |
#ifdef __DJGPP__ |
| 187 |
/* Feature: if they don't have LPR installed, make the |
| 188 |
default be to print to the local printer device. */ |
| 189 |
char lpr_path[PATH_MAX]; |
| 190 |
extern char *__dosexec_find_on_path (const char *, char **, char *); |
| 191 |
extern char **environ; |
| 192 |
|
| 193 |
if (!__dosexec_find_on_path ("lpr", (char **)0, lpr_path) |
| 194 |
&& !__dosexec_find_on_path ("lpr", environ, lpr_path)) |
| 195 |
spool = "> PRN"; |
| 196 |
else |
| 197 |
#endif |
| 198 |
spool = "| lpr"; |
| 199 |
} |
| 200 |
|
| 201 |
if (!tmpdir || !*tmpdir) |
| 202 |
{ |
| 203 |
#ifdef _WIN32 |
| 204 |
tmpdir = "c:/tmp"; |
| 205 |
#else |
| 206 |
tmpdir = "/tmp"; |
| 207 |
#endif |
| 208 |
if (access (tmpdir, R_OK|W_OK) != 0) |
| 209 |
{ |
| 210 |
mkdir (tmpdir, S_IRWXU); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (argc <= 1) |
| 215 |
{ |
| 216 |
fprintf (stderr, "%s: Missing argument(s).\nTry `%s --help' for more information.\n", |
| 217 |
kpse_invocation_name, kpse_invocation_name); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
while (--argc) |
| 222 |
{ |
| 223 |
char *arg = *++argv; |
| 224 |
|
| 225 |
if (STREQ (arg, "-help") || STREQ (arg, "--help")) |
| 226 |
{ |
| 227 |
usage (); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
else if (STREQ (arg, "-version") || STREQ (arg, "--version")) |
| 231 |
{ |
| 232 |
printf ("%s: (Dviljk 2.6) %s\nThere is NO warranty. This program is public domain.\n", |
| 233 |
kpse_invocation_name, rcs_revision); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
else if (arg[0] == '-') |
| 237 |
{ |
| 238 |
string newopt = NULL; |
| 239 |
|
| 240 |
switch (arg[1]) |
| 241 |
{ |
| 242 |
case 'A': /* -A => -D1 (odd pages) */ |
| 243 |
newopt = concat (opt, " -D1"); |
| 244 |
break; |
| 245 |
case 'B': /* -B -> -D2 (even pages) */ |
| 246 |
newopt = concat (opt, " -D2"); |
| 247 |
break; |
| 248 |
case 'd': /* -d => --D (debug) */ |
| 249 |
if (arg[2] == '\0') |
| 250 |
{ |
| 251 |
argc--; |
| 252 |
newopt = concat3 (opt, " --D", *++argv); |
| 253 |
} |
| 254 |
else |
| 255 |
newopt = concat3 (opt, " --D", arg + 2); |
| 256 |
break; |
| 257 |
case 'D': /* -D => -R (resolution) */ |
| 258 |
argc--; |
| 259 |
newopt = concat3 (opt, " -R", *++argv); |
| 260 |
break; |
| 261 |
case 'f': /* -f (run as filter) */ |
| 262 |
infile = ""; |
| 263 |
output = "-"; |
| 264 |
break; |
| 265 |
case 'l': /* -l => -t (ending page) */ |
| 266 |
if (arg[2] == '\0') |
| 267 |
{ |
| 268 |
argc--; |
| 269 |
newopt = concat3 (opt, " -t", *++argv); |
| 270 |
} |
| 271 |
else |
| 272 |
newopt = concat3 (opt, " -t", arg + 2); |
| 273 |
break; |
| 274 |
case 'm': /* -m => -A (manual feed) */ |
| 275 |
newopt = concat (opt, " -A"); |
| 276 |
break; |
| 277 |
case 'n': /* -n => -p (page count) */ |
| 278 |
if (arg[2] == '\0') |
| 279 |
{ |
| 280 |
argc--; |
| 281 |
newopt = concat3 (opt, " -t", *++argv); |
| 282 |
} |
| 283 |
else |
| 284 |
newopt = concat3 (opt, " -t", arg + 2); |
| 285 |
break; |
| 286 |
case 'o': /* -o (output file) */ |
| 287 |
{ |
| 288 |
string inbase = xstrdup (xbasename (infile)); |
| 289 |
if (argc == 1 && *inbase) |
| 290 |
{ |
| 291 |
/* No remaining args, output to foo.lj. */ |
| 292 |
int inbaselen = strlen (inbase); |
| 293 |
|
| 294 |
if (FILESTRCASEEQ (inbase + inbaselen - 4, ".dvi")) |
| 295 |
output = strcpy (inbase + inbaselen - 4, ".lj"); |
| 296 |
else |
| 297 |
{ |
| 298 |
#if defined (MSDOS) && defined _PC_NAME_MAX |
| 299 |
if (pathconf (infile, _PC_NAME_MAX) <= 12) |
| 300 |
{ |
| 301 |
/* Plain MS-DOS (not Windows 95), can't have |
| 302 |
more than a single dot in a filename. */ |
| 303 |
char *dot = strchr (inbase, '.'); |
| 304 |
|
| 305 |
if (dot) |
| 306 |
*dot = '\0'; |
| 307 |
} |
| 308 |
#endif |
| 309 |
output = concat (inbase, ".lj"); |
| 310 |
free (inbase); |
| 311 |
} |
| 312 |
} |
| 313 |
else if (arg[2] == '\0') |
| 314 |
{ |
| 315 |
argc--; |
| 316 |
output = *++argv; |
| 317 |
} |
| 318 |
else |
| 319 |
output = arg + 2; |
| 320 |
break; |
| 321 |
} |
| 322 |
case 'O': /* -O => -x, -y (page offsets) */ |
| 323 |
{ |
| 324 |
int x_len; |
| 325 |
char *params = arg + 2; |
| 326 |
char *x; |
| 327 |
|
| 328 |
if (arg[2] == '\0') |
| 329 |
{ |
| 330 |
argc--; |
| 331 |
params = *++argv; |
| 332 |
} |
| 333 |
|
| 334 |
x_len = strcspn (params, ","); |
| 335 |
x = (char *)xmalloc (x_len + 1); |
| 336 |
x[0] = '\0'; |
| 337 |
strncat (x, params, x_len); |
| 338 |
if (params[x_len] == ',') |
| 339 |
x_len++; /* get past the comma, if any */ |
| 340 |
newopt = concatn (opt, " -x", x, " y", params + x_len, NULL); |
| 341 |
break; |
| 342 |
} |
| 343 |
case 'p': /* -p => -f (starting page) */ |
| 344 |
if (arg[2] == '\0') |
| 345 |
{ |
| 346 |
argc--; |
| 347 |
newopt = concat3 (opt, " -f", *++argv); |
| 348 |
} |
| 349 |
else |
| 350 |
newopt = concat3 (opt, " -f", arg + 2); |
| 351 |
break; |
| 352 |
case 'P': /* -Pprinter */ |
| 353 |
output = ""; |
| 354 |
if (spool[0] == '>') /* direct printing: replace device */ |
| 355 |
spool = concat ("> ", arg[2] ? arg + 2 : *++argv); |
| 356 |
else |
| 357 |
spool_opt = concat ("-P", arg[2] ? arg + 2 : *++argv); |
| 358 |
if (arg[2] == '\0') |
| 359 |
argc--; |
| 360 |
break; |
| 361 |
case 'v': /* verbose */ |
| 362 |
verbose = 1; |
| 363 |
newopt = concat (opt, " -v"); |
| 364 |
break; |
| 365 |
case 'x': /* -x => -m (magnification) */ |
| 366 |
if (arg[2] == '\0') |
| 367 |
{ |
| 368 |
argc--; |
| 369 |
newopt = concat3 (opt, " -m", *++argv); |
| 370 |
} |
| 371 |
else |
| 372 |
newopt = concat3 (opt, " -m", arg + 2); |
| 373 |
break; |
| 374 |
case '-': /* -- => end of options */ |
| 375 |
argc--; |
| 376 |
infile = *++argv; |
| 377 |
break; |
| 378 |
default: /* pass other options through */ |
| 379 |
newopt = concat3 (opt, space, arg); |
| 380 |
break; |
| 381 |
} |
| 382 |
|
| 383 |
if (newopt) |
| 384 |
{ |
| 385 |
free (opt); |
| 386 |
opt = newopt; |
| 387 |
} |
| 388 |
} |
| 389 |
else |
| 390 |
infile = arg; |
| 391 |
} |
| 392 |
|
| 393 |
/* Generate temporary filenames. Assumes PID is 5 digits. */ |
| 394 |
sprintf (template, "%s/dvi%5.5d.", tmpdir, getpid ()); |
| 395 |
vfless_dvi = concat (template, "-vf"); |
| 396 |
|
| 397 |
/* Expand VF references. |
| 398 |
If INFILE is null, this will read standard input. |
| 399 |
dvilj can't read from a pipe, so always write to a file. */ |
| 400 |
if (verbose) |
| 401 |
fprintf (stderr, "Running dvicopy %s >%s\n", infile, vfless_dvi); |
| 402 |
|
| 403 |
/* FIXME: special characters in filenames aren't protected from the shell. */ |
| 404 |
cmdline = concatn ("dvicopy ", infile, " >", vfless_dvi, NULL); |
| 405 |
if (system (cmdline)) |
| 406 |
{ |
| 407 |
fprintf (stderr, "%s: dvicopy %s failed.\n", progname, infile); |
| 408 |
return 1; |
| 409 |
} |
| 410 |
free (cmdline); |
| 411 |
|
| 412 |
if (verbose) |
| 413 |
{ |
| 414 |
struct stat st_buf; |
| 415 |
|
| 416 |
if (stat (vfless_dvi, &st_buf)) |
| 417 |
perror (vfless_dvi); |
| 418 |
else /* assume they only want to see the size and timestamp */ |
| 419 |
fprintf (stderr, " %ld %s %s\n", |
| 420 |
(long)st_buf.st_size, ctime (&st_buf.st_mtime), vfless_dvi); |
| 421 |
} |
| 422 |
if (!*output) |
| 423 |
{ |
| 424 |
output = "-"; /* output to stdout */ |
| 425 |
maybe_spool_cmd = concat (spool, spool_opt); |
| 426 |
} |
| 427 |
|
| 428 |
/* Translate DVI to LJ. */ |
| 429 |
cmdline = concatn (dvilj, space, opt, output_opt, output, |
| 430 |
space, vfless_dvi, space, maybe_spool_cmd, NULL); |
| 431 |
if (verbose) |
| 432 |
fprintf (stderr, "Running %s\n", cmdline); |
| 433 |
if (system (cmdline)) |
| 434 |
{ |
| 435 |
fprintf (stderr, "%s: %s failed.\n", progname, dvilj); |
| 436 |
return 2; |
| 437 |
} |
| 438 |
|
| 439 |
/* rm -f $TMPDIR/dvi$$.* */ |
| 440 |
rmfiles (template); |
| 441 |
|
| 442 |
return 0; |
| 443 |
} |