| 1 |
/* dvipos-20070107 |
| 2 |
|
| 3 |
Copyright (C) 2003 by Jin-Hwan <[email protected]> |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef HAVE_CONFIG_H |
| 21 |
#include <config.h> |
| 22 |
#else |
| 23 |
#define PACKAGE_BUGREPORT "[email protected]" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "utils.h" |
| 27 |
#include "dvicore.h" |
| 28 |
|
| 29 |
#include <limits.h> |
| 30 |
#ifdef MIKTEX |
| 31 |
#include <getopt.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
/* Global variables */ |
| 35 |
int verbose = M_FAIL, do_smashchars = 0; /* output message only if failed */ |
| 36 |
FILE *infp = NULL, *outfp = NULL, *bbxfp = NULL, *frmfp = NULL; |
| 37 |
char *infname = NULL, *bbxfname = NULL, *frmfname = NULL; |
| 38 |
SIGNED_QUAD dbg_location = 0, rule_width = 6554; /* .1pt */ |
| 39 |
|
| 40 |
/* Internal variables */ |
| 41 |
static char *outfname = NULL; |
| 42 |
static char deffname[] = "default"; |
| 43 |
static int nmag = 0, resol = 300, bpage = 1, epage = INT_MAX; |
| 44 |
|
| 45 |
#ifdef MIKTEX |
| 46 |
#define PLATFORM "(MiKTeX 2.2)" |
| 47 |
#else |
| 48 |
#define PLATFORM "(KPATHSEA)" |
| 49 |
#endif |
| 50 |
|
| 51 |
static void title (void) |
| 52 |
{ |
| 53 |
msg_out(M_VERBOSE, "This is DVIpos, Version %s %s\nby Jin-Hwan Cho <[email protected]>\n\n", VERSION, PLATFORM); |
| 54 |
} |
| 55 |
|
| 56 |
static void usage (void) |
| 57 |
{ |
| 58 |
fprintf(stdout, "Usage: dvipos [OPTION...] infile[.dvi]\n\n" |
| 59 |
"-h, --help print usage\n" |
| 60 |
"-W, --warning print warnings\n" |
| 61 |
"-v, --verbose print verbose output\n" |
| 62 |
"-d, --debug print 'dvitype' debugging data\n" |
| 63 |
"-s, --smashchars regard height and depth as zero\n" |
| 64 |
"-o, --output=FILE send all output to FILE\n" |
| 65 |
"-b, --bbox[=FILE] send bounding box to FILE (default FILE is infile.pos)\n" |
| 66 |
"-f, --framed[=BASE] request copy of DVI file, BASE.dvi, with bounding boxes\n" |
| 67 |
" *framed* (default FILE is infile_frames.dvi)\n" |
| 68 |
"-m, --mag=INT override TeX magnification by INT\n" |
| 69 |
// "-r, --resolution=INT set resolution by INT\n" |
| 70 |
"-w, --framesize=INT set frame rule size by INT (default 6554 = .1pt)\n" |
| 71 |
"-p, --pages=X:Y set page ranges from X to Y\n" |
| 72 |
"\nEmail bug reports to %s.\n", PACKAGE_BUGREPORT); |
| 73 |
} |
| 74 |
|
| 75 |
static struct option long_options[] = { |
| 76 |
/* Options without arguments: */ |
| 77 |
{ "debug", no_argument, NULL, 'd' }, |
| 78 |
{ "help", no_argument, NULL, 'h' }, |
| 79 |
{ "verbose", no_argument, NULL, 'v' }, |
| 80 |
{ "warning", no_argument, NULL, 'W' }, |
| 81 |
{ "smashchars", no_argument, NULL, 's' }, |
| 82 |
/* Options accepting an argument: */ |
| 83 |
{ "mag", required_argument, NULL, 'm' }, |
| 84 |
{ "framesize", required_argument, NULL, 'w' }, |
| 85 |
{ "pages", required_argument, NULL, 'p' }, |
| 86 |
{ "resolution", required_argument, NULL, 'r' }, |
| 87 |
{ "output", optional_argument, NULL, 'o' }, |
| 88 |
/* Options accepting an optional argument: */ |
| 89 |
{ "bbox", optional_argument, NULL, 'b' }, |
| 90 |
{ "framed", optional_argument, NULL, 'f' }, |
| 91 |
{ 0, 0, 0, 0 } |
| 92 |
}; |
| 93 |
|
| 94 |
static void read_options (int argc, char **argv) |
| 95 |
{ |
| 96 |
int op; |
| 97 |
|
| 98 |
while ((op = getopt_long(argc, argv, "dhvwm:s:r:p:o:b::f::", long_options, (int *)0)) != EOF) { |
| 99 |
char *p, *q; |
| 100 |
switch (op) { |
| 101 |
case 'd': /* debug */ |
| 102 |
verbose |= M_DEBUG; |
| 103 |
break; |
| 104 |
case 'h': /* help */ |
| 105 |
verbose |= M_VERBOSE; title(); usage(); exit(1); |
| 106 |
break; |
| 107 |
case 'v': /* verbose */ |
| 108 |
verbose |= M_VERBOSE; |
| 109 |
break; |
| 110 |
case 'W': /* warning */ |
| 111 |
verbose |= M_WARNING; |
| 112 |
break; |
| 113 |
case 's': /* smashchars */ |
| 114 |
do_smashchars = 1; |
| 115 |
break; |
| 116 |
case 'm': /* mag */ |
| 117 |
if (*(q = optarg) == '=') q++; |
| 118 |
nmag = (int)strtol(q, &p, 0); |
| 119 |
if (p == q || nmag < 0) |
| 120 |
msg_out(M_FAIL, "Missing a number for magnification.\n"); |
| 121 |
break; |
| 122 |
case 'r': /* resolution */ |
| 123 |
if (*(q = optarg) == '=') q++; |
| 124 |
resol = (int)strtol(q, &p, 0); |
| 125 |
if (p == q || resol < 0) |
| 126 |
msg_out(M_FAIL, "Missing a number for resolution.\n"); |
| 127 |
break; |
| 128 |
case 'w': /* framesize */ |
| 129 |
if (*(q = optarg) == '=') q++; |
| 130 |
rule_width = (int)strtol(q, &p, 0); |
| 131 |
if (p == q || rule_width < 0) |
| 132 |
msg_out(M_FAIL, "Missing a number for frame rule size.\n"); |
| 133 |
break; |
| 134 |
case 'p': /* pages */ |
| 135 |
if (*(q = optarg) == '=') q++; |
| 136 |
bpage = (int)strtol(q, &p, 0); |
| 137 |
if (p == q) msg_out(M_FAIL, "Missing a page range.\n"); |
| 138 |
if (*p == ':') { |
| 139 |
epage = (int)strtol(q = p + 1, &p, 0); |
| 140 |
if (p == q) msg_out(M_FAIL, "Missing a 2nd page number.\n"); |
| 141 |
} |
| 142 |
if (bpage > epage) { |
| 143 |
int sw = bpage; bpage = epage; epage = sw; |
| 144 |
} |
| 145 |
break; |
| 146 |
case 'o': /* output */ |
| 147 |
if (*(q = optarg) == '=') q++; |
| 148 |
if (strlen(q) > 0) outfname = xstrdup(q); |
| 149 |
else msg_out(M_FAIL, "Missing an output file name.\n"); |
| 150 |
break; |
| 151 |
case 'b': /* bbox */ |
| 152 |
bbxfname = deffname; |
| 153 |
if (optarg) { |
| 154 |
if (*(q = optarg) == '=') q++; |
| 155 |
if (strlen(q) > 0) bbxfname = xstrdup(q); |
| 156 |
} |
| 157 |
break; |
| 158 |
case 'f': /* framed */ |
| 159 |
frmfname = deffname; |
| 160 |
if (optarg) { |
| 161 |
if (*(q = optarg) == '=') q++; |
| 162 |
if (strlen(q) > 0) frmfname = xstrdup(q); |
| 163 |
} |
| 164 |
break; |
| 165 |
default: |
| 166 |
msg_out(M_FAIL, "Unparsed option (%d)\n", op); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/* The first non-optional argument is to be used as the DVI file name */ |
| 171 |
if (optind < argc) { |
| 172 |
infname = make_suffix(argv[optind], "dvi"); |
| 173 |
optind++; |
| 174 |
while (optind < argc) |
| 175 |
msg_out(M_WARNING, "Warning: Ignore unparsed argument (%s).\n", argv[optind++]); |
| 176 |
} |
| 177 |
|
| 178 |
/* set up all the options */ |
| 179 |
msg_out(M_DEBUG, "Options selected:\n"); |
| 180 |
msg_out(M_DEBUG, " Starting page = * \n"); |
| 181 |
msg_out(M_DEBUG, " Maximum number of pages = 1000000\n"); |
| 182 |
msg_out(M_DEBUG, " Output level = 4 (the works)\n"); |
| 183 |
msg_out(M_DEBUG, " Resolution = %d.00000000 pixels per inch\n", resol); |
| 184 |
if (nmag > 0) |
| 185 |
msg_out(M_DEBUG, " New magnification factor = %-8.03f\n", nmag / 1000.); |
| 186 |
} |
| 187 |
|
| 188 |
int main (int argc, char *argv[]) |
| 189 |
{ |
| 190 |
int i; |
| 191 |
|
| 192 |
if (argc > 1) |
| 193 |
read_options(argc, argv); |
| 194 |
|
| 195 |
title(); |
| 196 |
|
| 197 |
if (infname == NULL) |
| 198 |
msg_out(M_FAIL, "No DVI filename specified.\n"); |
| 199 |
|
| 200 |
// if (outfname == NULL) |
| 201 |
// outfname = make_suffix(infname, "tuo"); |
| 202 |
|
| 203 |
if (bbxfname == deffname) |
| 204 |
bbxfname = make_suffix(infname, "pos"); |
| 205 |
|
| 206 |
if (frmfname) { |
| 207 |
if (frmfname == deffname) { |
| 208 |
frmfname = make_suffix(infname, "frames.dvi"); |
| 209 |
*(frmfname+strlen(frmfname)-11) = '_'; |
| 210 |
} else { |
| 211 |
char *p = frmfname; |
| 212 |
frmfname = make_suffix(p, "dvi"); |
| 213 |
free(p); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/* Open the input DVI file */ |
| 218 |
if ((infp = fopen(infname, "rb")) == NULL) |
| 219 |
msg_out(M_FAIL, "Could not open the input file %s.", infname); |
| 220 |
|
| 221 |
/* Open the output file */ |
| 222 |
if (outfname == NULL) { |
| 223 |
outfp = stdout; |
| 224 |
#ifdef WIN32 |
| 225 |
setmode(fileno(stdout), _O_BINARY); |
| 226 |
#endif |
| 227 |
} else if ((outfp = fopen(outfname, "ab")) == NULL) |
| 228 |
msg_out(M_FAIL, "Could not open the output file %s.\n", outfname); |
| 229 |
|
| 230 |
/* Open the boundingbox file */ |
| 231 |
if (bbxfname && (bbxfp = fopen(bbxfname, "wb")) == NULL) |
| 232 |
msg_out(M_FAIL, "Could not open the boundingbox file %s.\n", bbxfname); |
| 233 |
|
| 234 |
/* Open the framed DVI file */ |
| 235 |
if (frmfname && (frmfp = fopen(frmfname, "wb")) == NULL) |
| 236 |
msg_out(M_FAIL, "Could not open the framed DVI file %s.\n", frmfname); |
| 237 |
|
| 238 |
#ifdef KPATHSEA |
| 239 |
kpse_set_program_name(argv[0], "dvipos"); |
| 240 |
#elif MIKTEX |
| 241 |
miktex_initialize(); |
| 242 |
#endif |
| 243 |
|
| 244 |
dvi_init(nmag, resol); |
| 245 |
|
| 246 |
if (bpage <= 0) bpage = 1; |
| 247 |
if (epage >= dvi_pages) epage = dvi_pages; |
| 248 |
|
| 249 |
for (i = bpage; i <= epage; i++) |
| 250 |
dvi_do_page(i); |
| 251 |
|
| 252 |
dvi_close(); |
| 253 |
|
| 254 |
if (infname) free(infname); |
| 255 |
if (outfname) free(outfname); |
| 256 |
if (bbxfname) free(frmfname); |
| 257 |
if (frmfname) free(frmfname); |
| 258 |
|
| 259 |
if (outfp) fclose(outfp); |
| 260 |
if (bbxfp) fclose(bbxfp); |
| 261 |
if (frmfp) fclose(frmfp); |
| 262 |
|
| 263 |
#ifdef MIKTEX |
| 264 |
miktex_uninitialize(); |
| 265 |
#endif |
| 266 |
|
| 267 |
exit(0); |
| 268 |
} |