Debian Codegraph @

texk/xdvipsk/xdvips.c

raw
1 /*
2 * This is the main routine.
3 */
4 #ifndef XDVIPSK
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6 #else
7 #include "xdvips.h" /* The copyright notice in that file is included too! */
8 #define VERSION "2026.1"
9 #define VTEX_VERSION "2026.02.21"
10 #endif /* XDVIPSK */
11 #ifdef KPATHSEA
12 #include <kpathsea/c-pathch.h>
13 #include <kpathsea/proginit.h>
14 #include <kpathsea/progname.h>
15 #include <kpathsea/tex-hush.h>
16 #include <kpathsea/tex-make.h>
17 #include <kpathsea/variable.h>
18 #include <kpathsea/version.h>
19 #include <kpathsea/lib.h>
20 #ifdef strdup
21 #undef strdup
22 #endif
23 #define strdup xstrdup
24 #include "paths.h"
25 #else /* ! KPATHSEA */
26 #include <stdlib.h> /* for malloc, etc. */
27 #if !defined(SYSV) && !defined(WIN32)
28 extern char *strtok(); /* some systems don't have this in strings.h */
29 #endif
30 #if defined(WIN32)
31 #include <io.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #define O_BINARY _O_BINARY
35 #endif
36 #define DIR_SEP DIRSEP
37 #endif /* KPATHSEA */
38 #ifdef VMS
39 #define GLOBAL globaldef
40 #ifdef __GNUC__
41 #include "climsgdef.h" /* created by hand, extracted from STARLET.MLB */
42 /* and put in GNU_CC:[INCLUDE.LOCAL] */
43 #include "ctype.h"
44 #include "descrip.h"
45 #else
46 #include climsgdef
47 #include ctype
48 #include descrip
49 #endif
50 #endif
51 #ifdef __APPLE__ /* for Mac OS X, T. Uchiyama */
52 #include <sys/types.h>
53 #include <sys/time.h>
54 #include <sys/resource.h>
55 #endif
56
57 #if defined(WIN32) && defined(KPATHSEA)
58 FILE *generic_fsyscp_fopen(const char *filename, const char *mode)
59 {
60 FILE *f;
61
62 f = fsyscp_fopen (filename, mode);
63
64 if (f == NULL && file_system_codepage != win32_codepage) {
65 int tmpcp = file_system_codepage;
66 file_system_codepage = win32_codepage;
67 f = fsyscp_fopen (filename, mode);
68 file_system_codepage = tmpcp;
69 }
70
71 return f;
72 }
73 #undef fopen
74 #define fopen(file, fmode) generic_fsyscp_fopen(file, fmode)
75 #endif
76
77 #ifndef DEFRES
78 #define DEFRES (600)
79 #endif
80
81 #ifdef XDVIPSK
82 #ifndef WIN32
83 #define stricmp strcasecmp
84 #define strnicmp strncasecmp
85 #endif
86 #include "lua.h"
87 #include "lauxlib.h"
88 #include "lualib.h"
89 lua_State *L = NULL;
90 #include "glyphlist_table.h"
91 #include "font_glyph_maps.h"
92 #include "xdvipsk_tounicode.h"
93 Boolean lua_prescan_specials = 0;
94 Boolean lua_scan_specials = 0;
95 Boolean lua_after_prescan = 0;
96 Boolean lua_after_drawchar = 0;
97 Boolean lua_after_drawrule = 0;
98 Boolean lua_process_stack = 0;
99 Boolean lua_dvips_exit = 0;
100 #ifdef _MSC_VER
101 #undef timezone
102 #endif
103 #include <time.h> /* For `struct tm'. */
104 #if defined (HAVE_SYS_TIME_H)
105 # include <sys/time.h>
106 #elif defined (HAVE_SYS_TIMEB_H)
107 # include <sys/timeb.h>
108 #endif
109 #endif /* XDVIPSK */
110
111 /*
112 * The external declarations:
113 */
114 #include "protos.h"
115
116 /*
117 * First we define some globals.
118 */
119 #ifdef VMS
120 static char ofnme[252],infnme[252],pap[40],thh[20];
121 #endif
122
123 /* PS fonts fully downloaded as headers */
124 char *downloadedpsnames[DOWNLOADEDPSSIZE];
125
126 int found_problems = 0; /* should we exit successfully? */
127 int unused_top_of_psnames; /* unused top number of downloadedpsnames[#] */
128 fontdesctype *fonthead; /* list of all fonts mentioned so far */
129 fontdesctype *curfnt; /* the currently selected font */
130 sectiontype *sections; /* sections to process document in */
131 #ifndef XDVIPSK
132 Boolean partialdownload = 1; /* turn on partial downloading */
133 #else
134 Boolean t1_partialdownload = 1; /* turn on Type1 font partial downloading */
135 Boolean cid_partialdownload = 1; /* turn on OpenType font partial downloading */
136 #endif /* XDVIPSK */
137 Boolean manualfeed; /* manual feed? */
138 Boolean landscaperotate = 0; /* when picking paper sizes allow rotated media */
139 Boolean compressed; /* compressed? */
140 Boolean downloadpspk; /* use PK for downloaded PS fonts? */
141 Boolean safetyenclose; /* enclose in save/restore for stupid spoolers? */
142 Boolean removecomments = 0; /* remove comments from included PS? */
143 Boolean nosmallchars; /* disable small char optimization for X4045? */
144 Boolean cropmarks; /* add cropmarks? */
145 Boolean abspage = 0; /* are page numbers absolute? */
146 Boolean tryepsf = 0; /* should we try to make it espf? */
147 int secure = 1; /* make safe for suid */
148 int secure_option = 0; /* set by -R */
149 int collatedcopies = 1; /* how many collated copies? */
150 int sectioncopies = 1; /* how many times to repeat each section? */
151 integer pagecopies = 1; /* how many times to repeat each page? */
152 shalfword linepos = 0; /* where are we on the line being output? */
153 integer maxpages; /* the maximum number of pages */
154 Boolean notfirst, notlast; /* true if a first page was specified */
155 Boolean evenpages, oddpages; /* true if doing only even/only odd pages */
156 Boolean pagelist; /* true if using page ranges */
157 Boolean sendcontrolD; /* should we send a control D at end? */
158 Boolean shiftlowchars; /* shift [0-32, 127] characters higher? */
159 integer firstpage; /* the number of the first page if specified */
160 integer lastpage;
161 integer firstseq;
162 integer lastseq;
163 integer hpapersize, vpapersize; /* horizontal and vertical paper size */
164 integer hoff, voff; /* horizontal and vertical offsets */
165 integer maxsecsize = 0; /* the maximum size of a section */
166 integer firstboploc; /* where the first bop is */
167 Boolean sepfiles; /* each section in its own file? */
168 int numcopies; /* number of copies of each page to print */
169 char *titlename=""; /* if given, used for %%Title */
170 const char *oname; /* output file name */
171 char *iname; /* dvi file name */
172 char *fulliname; /* same, with current working directory */
173 char *strings; /* strings for program */
174 char *nextstring, *maxstring; /* string pointers */
175 FILE *dvifile, *bitfile; /* dvi and output files */
176 quarterword *curpos; /* current position in virtual character packet */
177 quarterword *curlim; /* final byte in virtual character packet */
178 fontmaptype *ffont; /* first font in current frame */
179 real conv; /* conversion ratio, pixels per DVI unit */
180 real vconv; /* conversion ratio, pixels per DVI unit */
181 real alpha; /* conversion ratio, DVI unit per TFM unit */
182 double mag; /* the magnification of this document */
183 integer num, den; /* the numerator and denominator */
184 int overridemag; /* substitute for mag value in DVI file? */
185 int actualdpi = DEFRES; /* the actual resolution of the printer */
186 int vactualdpi = DEFRES; /* the actual resolution of the printer */
187 int maxdrift; /* max pixels away from true rounded position */
188 int vmaxdrift; /* max pixels away from true rounded position */
189 char *paperfmt; /* command-line paper format */
190 int landscape = 0; /* landscape mode */
191 integer fontmem; /* memory remaining in printer */
192 integer pagecount; /* page counter for the sections */
193 integer pagenum; /* the page number we currently look at */
194 long bytesleft; /* number of bytes left in raster */
195 quarterword *raster; /* area for raster manipulations */
196 integer hh, vv; /* horizontal and vertical pixel positions */
197 Boolean noomega = 0; /* Omega extensions are enabled */
198 Boolean noptex = 0; /* pTeX extensions are enabled */
199 Boolean lastpsizwins = 1; /* if 1, last \special{papersize=w,h} wins */
200 #ifdef XDVIPSK
201 Boolean noluatex = 0; /* LuaTeX extensions are enabled */
202 Boolean noToUnicode = FALSE; /* ToUnicode Cmap file for OpenType font generation are enabled */
203 enc_type Otf_Enc_Type = enc_charcode;
204 Boolean charcode_otf_g2u =
205 #ifdef CHARCODE_G2U
206 TRUE;
207 #else
208 FALSE;
209 #endif
210 Boolean TURBO_MODE = 0; /* Write direct EPS files to PS stream */
211 Boolean RESIZE_MODE = 0; /* Enable emTeX graphics to rescale */
212 #if defined(WIN32)
213 char RESIZE_FILTER_BW = 'w'; /* Default rescale filter for BW emTeX graphics */
214 char RESIZE_FILTER_Gray = 'w';/* Default rescale filter for Gray emTeX graphics */
215 char RESIZE_FILTER_RGB = 'w'; /* Default rescale filter for RGB emTeX graphics */
216 #else
217 char RESIZE_FILTER_BW = 'r'; /* Default rescale filter for BW emTeX graphics */
218 char RESIZE_FILTER_Gray = 'r';/* Default rescale filter for Gray emTeX graphics */
219 char RESIZE_FILTER_RGB = 'r'; /* Default rescale filter for RGB emTeX graphics */
220 #endif
221 char RESIZE_FILTER_CMYK = 'r';/* Default rescale filter for CMYK emTeX graphics */
222 int GDI_STRETCH_MODE_BW = 1; /* WinGDI stretch mode for BW rescale (default = BLACKONWHITE) */
223 int GDI_STRETCH_MODE_Gray = 3;/* WinGDI stretch mode for Gray rescale (default = COLORONCOLOR) */
224 int GDI_STRETCH_MODE_RGB = 3; /* WinGDI stretch mode for RGB rescale (default = COLORONCOLOR) */
225 int GDI_STRETCH_MODE_CMYK = 1;/* WinGDI stretch mode for CMYK rescale (not used) */
226 char RESIZE_FILTER_BWName[128];
227 char RESIZE_FILTER_GrayName[128];
228 char RESIZE_FILTER_RGBName[128];
229 char RESIZE_FILTER_CMYKName[128];
230 Boolean EMSEARCH_MODE = 0; /* Enable emTeX graphics to be searched for alternatives */
231 char *Exe_Log_Name = NULL; /* Program logfile name (dvi_name.xdvips.log) */
232 FILE *Exe_Log = NULL; /* Program logfile*/
233 int log_records_count = 0; /* Log records count */
234 Boolean Exe_Log_Mode = 1; /* Log records writing mode */
235 Boolean VTEX_SPEC_MODE = 0; /* Skip specials with prefixes mt:, vtex:, MC:, BMC: and EMC: */
236 integer dvipos; /* currently read dvi position pointer */
237 int epochseconds = 0;
238 int microseconds = 0;
239 #endif /* XDVIPSK */
240
241 /*-----------------------------------------------------------------------*
242 * The PATH definitions cannot be defined on the command line because so many
243 * DEFINE's overflow the DCL buffer when using the GNU CC compiler.
244 *-----------------------------------------------------------------------*/
245 #if defined(VMS) && defined(__GNUC__)
246 #include "vms_gcc_paths.h"
247 #endif
248
249 const char *infont; /* is the file we are downloading a font? */
250 #ifdef XDVIPSK
251 quarterword /* otftype_enum */ inotftype;
252 #endif /* XDVIPSK */
253 #ifndef KPATHSEA
254 #ifdef ATARIST
255 # define TFMPATH "."
256 # define PKPATH "."
257 # define VFPATH "."
258 # define FIGPATH "."
259 # define HEADERPATH "."
260 # define CONFIGPATH "."
261 #endif
262 char *tfmpath = TFMPATH; /* pointer to directories for tfm files */
263 char *pkpath = PKPATH; /* pointer to directories for pk files */
264 char *vfpath = VFPATH; /* pointer to directories for vf files */
265 char *figpath = FIGPATH; /* pointer to directories for figure files */
266 char *headerpath = HEADERPATH; /* pointer to directories for header files */
267 char *configpath = CONFIGPATH; /* where to find config files */
268 #ifndef PICTPATH
269 #ifndef __THINK__
270 #define PICTPATH "."
271 #else
272 #define PICTPATH ":"
273 #endif
274 #endif
275 char *pictpath = PICTPATH; /* where IFF/etc. pictures are found */
276 #ifdef SEARCH_SUBDIRECTORIES
277 char *fontsubdirpath = FONTSUBDIRPATH;
278 #endif
279 #endif /* ! KPATHSEA */
280 #ifdef FONTLIB
281 char *flipath = FLIPATH; /* pointer to directories for fli files */
282 char *fliname = FLINAME; /* pointer to names of fli files */
283 #endif
284 integer swmem; /* font memory in the PostScript printer */
285 int quiet; /* should we only print errors to stderr? */
286 int filter; /* act as filter default output to stdout,
287 default input to stdin? */
288 int dvips_debug_flag; /* output config and map files to stderr if 1 */
289 int prettycolumn; /* the column we are at when running pretty */
290 int gargc; /* global argument count */
291 char **gargv; /* global argument vector */
292 int totalpages = 0; /* total number of pages */
293 Boolean reverse; /* are we going reverse? */
294 Boolean usesPSfonts; /* do we use local PostScript fonts? */
295 #ifdef XDVIPSK
296 Boolean usesOTFfonts; /* do we use local OpenType fonts? */
297 otfcmaptype *OTF_list = NULL;/* relation between OpenType fonts and ToUnicode cmaps*/
298 FT_Library ft_lib = NULL;
299 #endif /* XDVIPSK */
300 Boolean usesspecial; /* do we use \special? */
301 Boolean headers_off; /* do we send headers or not? */
302 Boolean usescolor; /* IBM: color - do we use colors? */
303 const char *headerfile; /* default header file */
304 char *warningmsg; /* a message to write, if set in config file */
305 Boolean multiplesects; /* more than one section? */
306 Boolean disablecomments; /* should we suppress any EPSF comments? */
307 char *printer; /* what printer to send this to? */
308 char *mfmode; /* default MF mode */
309 char *mflandmode; /* allow an optional landscape mode def */
310 int mfmode_option; /* set by -mode command-line option */
311 int oname_option; /* set by -o option */
312 frametype frames[MAXFRAME]; /* stack for virtual fonts */
313 integer pagecost; /* memory used on the page being prescanned */
314 int delchar; /* characters to delete from prescanned page */
315 integer fsizetol; /* max dvi units error for psfile font sizes */
316 Boolean includesfonts; /* are fonts used in included psfiles? */
317 fontdesctype *fonthd[MAXFONTHD];/* list headers for included fonts of 1 name */
318 int nextfonthd; /* next unused fonthd[] index */
319 char xdig[256]; /* table for reading hexadecimal digits */
320 char banner[] = BANNER; /* our startup message */
321 char banner2[] = BANNER2; /* our second startup message */
322 #ifdef XDVIPSK
323 char banner3[] = BANNER3; /* our third startup message */
324 const char *xdvipsk_bug_address =
325 "Email dvips related bug reports to [email protected].\nEmail xdvipsk related bug reports to [email protected].\n";
326 #endif /* XDVIPSK */
327 Boolean noenv = 0; /* ignore PRINTER envir variable? */
328 Boolean dopprescan = 0; /* do we do a scan before the prescan? */
329 struct papsiz *papsizes; /* all available paper size */
330 int headersready; /* ready to check headers? */
331 #if defined(MSDOS) || defined(OS2) || defined(ATARIST)
332 char *mfjobname = NULL; /* name of mfjob file if given */
333 FILE *mfjobfile = NULL; /* mfjob file for font making instructions */
334 #endif
335 #ifdef DEBUG
336 integer debug_flag = 0;
337 #endif /* DEBUG */
338 char queryline[256]; /* interactive query of options */
339 int qargc;
340 char *qargv[32];
341 char queryoptions;
342 #ifdef HPS
343 Boolean HPS_FLAG = 0;
344 #endif
345
346 /* Declare the routine to get the current working directory. */
347
348 /* The getwd/getcwd stuff here is not portable, and I don't think it's
349 worth it to include xgetcwd.d just so we can print the full pathname
350 of the DVI file in the output. --karl */
351 #define IGNORE_CWD
352
353 #ifndef IGNORE_CWD
354 #ifndef HAVE_GETCWD
355 extern char *getwd (); /* said to be faster than getcwd (SunOS man page) */
356 #define getcwd(b, len) getwd(b) /* used here only when b nonnull */
357 #else
358 #ifdef ANSI
359 extern char *getcwd (char *, int);
360 #else
361 extern char *getcwd ();
362 #endif /* not ANSI */
363 #endif /* not HAVE_GETWD */
364 #if defined(SYSV) || defined(VMS) || (defined(MSDOS) && !defined(__DJGPP__)) || defined(OS2) || defined(ATARIST)
365 #define MAXPATHLEN (256)
366 #else
367 #include <sys/param.h> /* for MAXPATHLEN */
368 #endif
369 #endif /* not IGNORE_CWD */
370
371 static const char *helparr[] = {
372 #ifndef VMCMS
373 #ifndef XDVIPSK
374 "Usage: dvips [OPTION]... FILENAME[.dvi]",
375 #else
376 "Usage: xdvipsk [OPTION]... FILENAME[.dvi]",
377 #endif
378 #else
379 " VM/CMS Usage:",
380 #ifndef XDVIPSK
381 " dvips fname [ftype [fmode]] [options]",
382 #else
383 " xdvipsk fname [ftype [fmode]] [options]",
384 #endif
385 "or",
386 #ifndef XDVIPSK
387 " dvips fname[.ftype[.fmode]] [options]",
388 #else
389 " xdvipsk fname [ftype [fmode]] [options]",
390 #endif
391 #endif
392 "Convert DVI input files to PostScript.",
393 "See http://tug.org/dvips/ for the full manual and other information.",
394 #ifdef XDVIPSK
395 "See https://github.com/vtex-soft/xdvipsk for the specific xdvipsk ",
396 "information and source.",
397 #endif
398 "",
399 "Options:",
400 "-a* Conserve memory, not time -A Print only odd (TeX) pages",
401 "-b # Page copies, for posters e.g. -B Print only even (TeX) pages",
402 "-bitmapfontenc [on,off,strict] control bitmap font encoding",
403 "-c # Uncollated copies -C # Collated copies",
404 "-d # Debugging -D # Resolution",
405 "-e # Maxdrift value -E* Try to create EPSF",
406 "-f* Run as filter -F* Send control-D at end",
407 #ifdef SHIFTLOWCHARS
408 #ifndef XDVIPSK
409 " -G* Shift low chars to higher pos.",
410 #else
411 "-g* write log file -G* Shift low chars to higher pos.",
412 #endif /* XDVIPSK */
413 #else
414 #ifdef XDVIPSK
415 "-g* write log file",
416 #endif /* XDVIPSK */
417 #endif
418 #ifndef XDVIPSK
419 "-h f Add header file",
420 "-i* Separate file per section",
421 "-j* Download fonts partially",
422 #else
423 "-h f Add header file -H* Turbo mode for PS graphics",
424 "-i* Separate file per section -I p Resize mode for emTeX graphics",
425 "-j* Download T1 fonts partially -J* Download OpenType fonts partially",
426 #endif /* XDVIPSK */
427 "-k* Print crop marks -K* Pull comments from inclusions",
428 "-l # Last page -L* Last special papersize wins",
429 "-landscaperotate* Allow landscape to print rotated on portrait papersizes",
430 #ifdef XDVIPSK
431 "-lua s Lua script file name",
432 #endif /* XDVIPSK */
433 "-m* Manual feed -M* Don't make fonts",
434 "-mode s Metafont device name",
435 "-n # Maximum number of pages -N* No structured comments",
436 "-noomega Disable Omega extensions",
437 "-noptex Disable pTeX extensions",
438 #ifdef XDVIPSK
439 "-noluatex Disable LuaTeX extensions",
440 "-noToUnicode Disable ToUnicode CMap file generation for OpenType fonts",
441 #endif /* XDVIPSK */
442 "-o f Output file -O c Set/change paper offset",
443 #if defined(MSDOS) || defined(OS2)
444 "-p # First page -P s Load $s.cfg",
445 #else
446 "-p # First page -P s Load config.$s",
447 #endif
448 "-pp l Print only pages listed",
449 #ifndef XDVIPSK
450 "-q* Run quietly",
451 #else
452 "-q* Run quietly -Q* Skip VTeX private specials",
453 #endif /* XDVIPSK */
454 "-r* Reverse order of pages -R* Run securely",
455 "-s* Enclose output in save/restore -S # Max section size in pages",
456 "-t s Paper format -T c Specify desired page size",
457 "-title s Title in comment",
458 "-u s PS mapfile -U* Disable string param trick",
459 "-v Print version number and quit -V* Send downloadable PS fonts as PK",
460 #ifdef XDVIPSK
461 " -W* Extended search for emTeX graphics",
462 #endif /* XDVIPSK */
463 "-x # Override dvi magnification -X # Horizontal resolution",
464 "-y # Multiply by dvi magnification -Y # Vertical resolution",
465 #ifdef HPS
466 "-z* Hyper PS -Z* Compress bitmap fonts",
467 #else
468 " -Z* Compress bitmap fonts",
469 #endif
470 /*"- Interactive query of options", */
471 " # = number f = file s = string * = suffix, `0' to turn off",
472 " c = comma-separated dimension pair (e.g., 3.2in,-32.1cm)",
473 #ifndef XDVIPSK
474 " l = comma-separated list of page ranges (e.g., 1-4,7-9)", 0};
475 #else
476 " l = comma-separated list of page ranges (e.g., 1-4,7-9)",
477 " p = comma-separated pixel-form:filter pairs (e.g. RGB:t,CMYK:r),",
478 " `0' to turn off", 0};
479 #endif /* XDVIPSK */
480
481 void
482 help(int status)
483 {
484 const char **p;
485 FILE *f = status == 0 ? stdout : stderr;
486 for (p=helparr; *p; p++)
487 fprintf (f, "%s\n", *p);
488
489 putc ('\n', f);
490 #ifdef KPATHSEA
491 #ifndef XDVIPSK
492 fputs (kpse_bug_address, f);
493 #else
494 fputs (xdvipsk_bug_address, f);
495 #endif /* XDVIPSK */
496 #endif
497 }
498
499 static void
500 freememforpsnames(void)
501 {
502 int i;
503
504 for (i = 0; i < unused_top_of_psnames && downloadedpsnames[i]; i++)
505 free (downloadedpsnames[i]);
506 }
507
508 /*
509 * This error routine prints an error message; if the first
510 * character is !, it aborts the job.
511 */
512
513 static char *progname;
514
515 #ifdef XDVIPSK
516 void openlogfile(int argc, char **argv)
517 {
518 char *p;
519
520 if (argc > 1)
521 {
522 p = argv[argc - 1];
523 if (*p != '-')
524 {
525 Exe_Log_Name = mymalloc(strlen(p) + 30);
526 strcpy(Exe_Log_Name, p);
527
528 p = strrchr(Exe_Log_Name, '.');
529 if (p)
530 if (stricmp(p, ".dvi") == 0)
531 *p = 0;
532 strcat(Exe_Log_Name, ".xdvips.log");
533
534 Exe_Log = fopen(Exe_Log_Name, "w");
535 }
536 }
537 }
538
539 void
540 writelogrecord(const char *s)
541 {
542 char *str, *s1;
543
544 if (Exe_Log && s) {
545 str = mymalloc(strlen(s) + 30);
546 if (*s == '!') {
547 strcpy(str, "Error: ");
548 s1 = (char *)s;
549 s1++;
550 while (*s1 == ' ')
551 s1++;
552 strcat(str, s1);
553 }
554 else {
555 if (strnicmp(s, "warn", 4) == 0)
556 strcpy(str, s);
557 else {
558 strcpy(str, "Warning: ");
559 strcat(str, s);
560 }
561 }
562 if (str[strlen(str) - 1] != '\n') {
563 strcat(str, "\n");
564 }
565 fputs(str, Exe_Log);
566 fflush(Exe_Log);
567 log_records_count++;
568 free(str);
569 }
570 }
571
572 void
573 dvips_exit(int code)
574 {
575 int ii;
576 int sec = 0;
577 int microsec = 0;
578
579 for (ii = 0; ii < RESHASHPRIME; ii++)
580 {
581 struct resfont *pt, *next;
582 resfont_ref *pt_ref, *next_ref;
583 charusetype_ref *cu_ref, *next_cu_ref;
584 charusetype_entry *cu_entry, *next_cu_entry;
585
586 pt = reshash[ii];
587 while (pt)
588 {
589 next = pt->next;
590 free(pt);
591 pt = next;
592 }
593 reshash[ii] = NULL;
594
595 pt_ref = reshash_ps[ii];
596 while (pt_ref)
597 {
598 next_ref = pt_ref->next;
599 free(pt_ref);
600 pt_ref = next_ref;
601 }
602 reshash_ps[ii] = NULL;
603
604 pt_ref = reshash_v[ii];
605 while (pt_ref)
606 {
607 next_ref = pt_ref->next;
608 free(pt_ref);
609 pt_ref = next_ref;
610 }
611 reshash_v[ii] = NULL;
612
613 cu_ref = charused_hash[ii];
614 while (cu_ref)
615 {
616 cu_entry = cu_ref->head;
617 while (cu_entry)
618 {
619 next_cu_entry = cu_entry->next;
620 free(cu_entry);
621 cu_entry = next_cu_entry;
622 }
623
624 next_cu_ref = cu_ref->next;
625 free(cu_ref);
626 cu_ref = next_cu_ref;
627 }
628 charused_hash[ii] = NULL;
629 }
630
631 if (ft_lib)
632 FT_Done_FreeType(ft_lib);
633 ft_lib = NULL;
634
635 if (Exe_Log) {
636 if ((log_records_count == 0) && (code == 0)) {
637 fputs("!!!Success!!!\n", Exe_Log);
638 }
639 fclose(Exe_Log);
640 }
641
642 if (L) {
643 if (lua_dvips_exit)
644 run_lua_dvips_exit(L, code, log_records_count);
645 lua_close(L);
646 }
647 L = NULL;
648
649 get_seconds_and_micros(&sec, &microsec);
650 sec = sec - epochseconds;
651 if (microsec < microseconds) {
652 microsec = microsec + 1000000;
653 sec = sec - 1;
654 }
655 microsec = microsec - microseconds;
656 fprintf (stderr, "Processing time: %d.%ds\n", sec, microsec);
657 exit(code);
658 }
659
660 void get_seconds_and_micros(int *seconds, int *micros)
661 {
662 #if defined (HAVE_GETTIMEOFDAY)
663 struct timeval tv;
664 gettimeofday(&tv, NULL);
665 *seconds = (int)tv.tv_sec;
666 *micros = (int)tv.tv_usec;
667 #elif defined (HAVE_FTIME)
668 struct timeb tb;
669 ftime(&tb);
670 *seconds = tb.time;
671 *micros = tb.millitm * 1000;
672 #else
673 time_t myclock = time((time_t *) NULL);
674 *seconds = (int) myclock;
675 *micros = 0;
676 #endif
677 }
678 #endif /* XDVIPSK */
679
680 void
681 error_with_perror(const char *s, const char *fname)
682 {
683 if (prettycolumn > 0)
684 fprintf(stderr,"\n");
685 prettycolumn = 0;
686 fprintf_str(stderr, "%s: %s", progname, s);
687 if (fname) {
688 putc (' ', stderr);
689 perror (fname);
690 } else {
691 putc ('\n', stderr);
692 }
693 #ifdef XDVIPSK
694 writelogrecord(s);
695 #endif /* XDVIPSK */
696
697 if (*s=='!') {
698 freememforpsnames();
699 if (bitfile != NULL) {
700 cleanprinter();
701 }
702 #ifndef XDVIPSK
703 exit(1); /* fatal */
704 #else
705 (void)printf("\nRemoving output file...");
706 if (remove(oname) != 0)
707 (void)printf("unable to remove unfinished output file!");
708 dvips_exit(1);
709 #endif /* XDVIPSK */
710 }
711 }
712
713 /*
714 * This error routine prints an error message; if the first
715 * character is !, it aborts the job.
716 */
717 void
718 error(const char *s)
719 {
720 error_with_perror (s, NULL);
721 }
722
723 #ifndef KPATHSEA
724 char *
725 concat(char *s1, char *s2)
726 {
727 char *s = malloc(strlen(s1)+strlen(s2)+1);
728 if (s == NULL) {
729 fprintf(stderr, "Malloc failed to give %d bytes.\nAborting\n",
730 strlen(s1)+strlen(s2)+1);
731 exit(1);
732 }
733 strcpy(s, s1);
734 strcat(s, s2);
735 }
736 #endif
737
738 /* Report a warning if both checksums are nonzero, they don't match, and
739 the user hasn't turned it off. */
740
741 void
742 check_checksum(unsigned c1, unsigned c2, const char *name)
743 {
744 if (c1 && c2 && c1 != c2
745 #ifdef KPATHSEA
746 && !kpse_tex_hush ("checksum")
747 #endif
748 ) {
749 #ifndef XDVIPSK
750 char *msg = concat ("Checksum mismatch in %s", name);
751 #else
752 char *msg = concat("Checksum mismatch in ", name);
753 #endif /* XDVIPSK */
754 error (msg);
755 free (msg);
756 }
757 }
758
759 /*
760 * This is our malloc that checks the results. We debug the
761 * allocations but not the frees, since memory fragmentation
762 * might be such that we can never use the free'd memory and
763 * it's wise to be conservative. The only real place we free
764 * is when repacking *huge* characters anyway.
765 */
766 #ifdef DEBUG
767 static integer totalalloc = 0;
768 #endif
769 char *
770 mymalloc(integer n)
771 {
772 char *p;
773
774 #ifdef SMALLMALLOC
775 if (n > 65500L)
776 error("! can't allocate more than 64K!");
777 #endif
778 if (n <= 0) /* catch strange 0 mallocs in flib.c without breaking code */
779 n = 1;
780 #ifdef DEBUG
781 totalalloc += n;
782 if (dd(D_MEM)) {
783 #ifdef SHORTINT
784 fprintf(stderr, "Alloc %ld\n", n);
785 #else
786 fprintf(stderr, "Alloc %d\n", n);
787 #endif
788 }
789 #endif
790 p = (char *) malloc(n);
791 if (p == NULL)
792 error("! no memory");
793 return p;
794 }
795 #ifndef XDVIPSK
796 static void
797 #else
798 void
799 #endif /* XDVIPSK */
800 morestrings(void) {
801 strings = mymalloc((integer)STRINGSIZE);
802 nextstring = strings;
803 maxstring = strings + STRINGSIZE - 200;
804 *nextstring++ = 0;
805 }
806 void
807 checkstrings(void) {
808 if (nextstring - strings > STRINGSIZE / 2)
809 morestrings();
810 }
811 /*
812 * Initialize sets up all the globals and data structures.
813 */
814 static void
815 initialize(void)
816 {
817 int i;
818 const char *s;
819
820 nextfonthd = 0;
821 for (i=0; i<256; i++)
822 xdig[i] = 0;
823 i = 0;
824 for (s="0123456789ABCDEF"; *s!=0; s++)
825 xdig[(int)*s] = i++;
826 i = 10;
827 for (s="abcdef"; *s!=0; s++)
828 xdig[(int)*s] = i++;
829 for(i=0; i < DOWNLOADEDPSSIZE; i++)
830 downloadedpsnames[i] = NULL;
831 unused_top_of_psnames = 0;
832 morestrings();
833 maxpages = 100000;
834 numcopies = 1;
835 iname = fulliname = strings;
836 bitfile = NULL;
837 bytesleft = 0;
838 swmem = SWMEM;
839 oname = OUTPATH;
840 sendcontrolD = 0;
841 shiftlowchars = 0;
842 multiplesects = 0;
843 disablecomments = 0;
844 maxdrift = -1;
845 vmaxdrift = -1;
846 dontmakefont = !MAKE_TEX_PK_BY_DEFAULT;
847 #ifdef XDVIPSK
848 #if defined(WIN32)
849 strcpy(RESIZE_FILTER_BWName, "WinGDI filter with mode BLACKONWHITE");
850 strcpy(RESIZE_FILTER_GrayName, "WinGDI filter with mode COLORONCOLOR");
851 strcpy(RESIZE_FILTER_RGBName, "WinGDI filter with mode COLORONCOLOR");
852 #else
853 strcpy(RESIZE_FILTER_BWName, "Resample filter");
854 strcpy(RESIZE_FILTER_GrayName, "Resample filter");
855 strcpy(RESIZE_FILTER_RGBName, "Resample filter");
856 #endif
857 strcpy(RESIZE_FILTER_CMYKName, "Resample filter");
858
859 if (FT_Init_FreeType(&ft_lib))
860 fprintf(stderr, "\nError: FreeType library initialization failed"); // TODO: error processing
861
862 get_seconds_and_micros(&epochseconds, &microseconds);
863 L = luaL_newstate();
864 luaL_openlibs(L);
865 #endif /* XDVIPSK */
866 }
867 /*
868 * This routine copies a string into the string `pool', safely.
869 */
870 char *
871 newstring(const char *s)
872 {
873 int l;
874 char *q;
875
876 if (s == NULL)
877 return(NULL);
878 l = strlen(s);
879 if (nextstring + l >= maxstring)
880 morestrings();
881 if (nextstring + l >= maxstring)
882 error("! out of string space");
883 strcpy(nextstring, s);
884 q = nextstring;
885 nextstring += l + 1;
886 return(q);
887 }
888 static void
889 newoutname(void) {
890 static int seq = 0;
891 static char *seqptr = 0;
892 char *p, *q;
893
894 if (oname == 0 || *oname == 0)
895 error("! need an output file name to specify separate files");
896 if (*oname != '!' && *oname != '|') {
897 if (seqptr == 0) {
898 q = newstring(oname);
899 for (p = q; *p; p++) /* find last dot after last slash */
900 if (*p == '.')
901 seqptr = p + 1;
902 else if (IS_DIR_SEP (*p))
903 seqptr = 0;
904 #ifdef WIN32
905 else if (IS_KANJI(p))
906 p++;
907 #endif
908 if (seqptr == 0)
909 seqptr = p;
910 nextstring += 5; /* make room for the number, up to five digits */
911 oname = q;
912 }
913 sprintf(seqptr, "%03d", ++seq);
914 }
915 }
916 /*
917 * This routine reverses a list, where a list is defined to be any
918 * structure whose first element is a pointer to another such structure.
919 */
920 VOID *
921 revlist(VOID *p)
922 {
923 struct list {
924 struct list *next;
925 } *pp = (struct list *)p, *qq = 0, *tt;
926
927 while (pp) {
928 tt = pp->next;
929 pp->next = qq;
930 qq = pp;
931 pp = tt;
932 }
933 return (VOID *)qq;
934 }
935 /* this asks for a new set of arguments from the command line */
936 static void
937 queryargs(void)
938 {
939 fputs("Options: ",stdout);
940 fgets(queryline,256,stdin);
941 qargc=1;
942 if ( (qargv[1] = strtok(queryline," \n")) != (char *)NULL ) {
943 qargc=2;
944 while ( ((qargv[qargc] = strtok((char *)NULL," \n")) != (char *)NULL)
945 && (qargc < 31) )
946 qargc++;
947 }
948 qargv[qargc] = (char *)NULL;
949 }
950
951 #ifdef XDVIPSK
952 int CompareUpperCase(char *x, char *y)
953 {
954 #if defined(WIN32)
955 return stricmp(x,y);
956 #else
957 char *s = x;
958 for (; *s; ++s) *s = toupper(*s);
959 return strcmp(x, y);
960 #endif
961 }
962
963 void queryresizefilter(char *filter, char *result, char *displayname, int *gdimode)
964 {
965 switch (*filter) {
966 case 'b': //Box filter
967 *result = 'b';
968 strcpy(displayname, "Box filter");
969 break;
970 case 't': //Bilinear filter
971 *result = 't';
972 strcpy(displayname, "Bilinear filter");
973 break;
974 case 'B': //B-spline filter
975 *result = 'B';
976 strcpy(displayname, "B-spline filter");
977 break;
978 case 'm': //Mitchell and Netravali's two-param bicubic filter
979 *result = 'm';
980 strcpy(displayname, "Mitchell and Netravali's two-param bicubic filter");
981 break;
982 case 'l': //Lanczos-windowed sinc filter
983 *result = 'l';
984 strcpy(displayname, "Lanczos-windowed sinc filter");
985 break;
986 case 'c': //Catmull-Rom spline, Overhauser spline
987 *result = 'c';
988 strcpy(displayname, "Catmull-Rom spline, Overhauser spline filter");
989 break;
990 case 'r': //Resample mode
991 *result = 'r';
992 strcpy(displayname, "Resample filter");
993 break;
994 case 'w': //Windows GDI filter
995 #if defined(WIN32)
996 *result = 'w';
997 filter++;
998 switch (*filter) {
999 case '1': //BLACKONWHITE
1000 *gdimode = 1;
1001 strcpy(displayname, "WinGDI filter with mode BLACKONWHITE");
1002 break;
1003 case '2': //WHITEONBLACK
1004 *gdimode = 2;
1005 strcpy(displayname, "WinGDI filter with mode WHITEONBLACK");
1006 break;
1007 case '3': //COLORONCOLOR
1008 *gdimode = 3;
1009 strcpy(displayname, "WinGDI filter with mode COLORONCOLOR");
1010 break;
1011 case '4': //HALFTONE
1012 *gdimode = 4;
1013 strcpy(displayname, "WinGDI filter with mode HALFTONE");
1014 break;
1015 default:
1016 *gdimode = 1;
1017 strcpy(displayname, "WinGDI filter with mode BLACKONWHITE");
1018 }
1019 #else
1020 *result = 'r';
1021 strcpy(displayname, "Resample filter");
1022 #endif
1023 break;
1024 }
1025 }
1026
1027 void queryresizeargs(char *p)
1028 {
1029 char *s, *f;
1030
1031 if ((s = strtok(p, ",")) != (char *)NULL) {
1032 f = strchr(s, ':');
1033 if (f) {
1034 *f = 0;
1035 f++;
1036 if (CompareUpperCase(s, "BW") == 0)
1037 queryresizefilter(f, &RESIZE_FILTER_BW, RESIZE_FILTER_BWName, &GDI_STRETCH_MODE_BW);
1038 else if (CompareUpperCase(s, "GR") == 0)
1039 queryresizefilter(f, &RESIZE_FILTER_Gray, RESIZE_FILTER_GrayName, &GDI_STRETCH_MODE_Gray);
1040 else if (CompareUpperCase(s, "RGB") == 0)
1041 queryresizefilter(f, &RESIZE_FILTER_RGB, RESIZE_FILTER_RGBName, &GDI_STRETCH_MODE_RGB);
1042 else if (CompareUpperCase(s, "CMYK") == 0)
1043 queryresizefilter(f, &RESIZE_FILTER_CMYK, RESIZE_FILTER_CMYKName, &GDI_STRETCH_MODE_CMYK);
1044 }
1045 while (((s = strtok((char *)NULL, ",")) != (char *)NULL)) {
1046 f = strchr(s, ':');
1047 if (f) {
1048 *f = 0;
1049 f++;
1050 if (CompareUpperCase(s, "BW") == 0)
1051 queryresizefilter(f, &RESIZE_FILTER_BW, RESIZE_FILTER_BWName, &GDI_STRETCH_MODE_BW);
1052 else if (CompareUpperCase(s, "GR") == 0)
1053 queryresizefilter(f, &RESIZE_FILTER_Gray, RESIZE_FILTER_GrayName, &GDI_STRETCH_MODE_Gray);
1054 else if (CompareUpperCase(s, "RGB") == 0)
1055 queryresizefilter(f, &RESIZE_FILTER_RGB, RESIZE_FILTER_RGBName, &GDI_STRETCH_MODE_RGB);
1056 else if (CompareUpperCase(s, "CMYK") == 0)
1057 queryresizefilter(f, &RESIZE_FILTER_CMYK, RESIZE_FILTER_CMYKName, &GDI_STRETCH_MODE_CMYK);
1058 }
1059 }
1060 }
1061 }
1062
1063 Boolean
1064 lua_callback_defined(lua_State *L, const char* lua_callback)
1065 {
1066 Boolean callback_defined;
1067 lua_getglobal(L, lua_callback);
1068 callback_defined = lua_isfunction(L, 1);
1069 lua_pop(L, 1);
1070 return callback_defined;
1071 }
1072
1073 void
1074 load_lua_scripts(const char* luascript)
1075 {
1076 char* luascriptfile = NULL;
1077 assert(luascript);
1078 #ifdef KPATHSEA
1079 luascriptfile = (char *)kpse_find_file (luascript, kpse_lua_format, 0);
1080 #else
1081 luascriptfile = strdup(luascript);
1082 assert(luascriptfile);
1083 #endif
1084 if (luascriptfile) {
1085 int ret;
1086 printf("Loading Lua script: %s\n", luascriptfile);
1087 ret = luaL_loadfile(L, luascriptfile);
1088 if (ret != LUA_OK) {
1089 switch (ret) {
1090 case LUA_ERRSYNTAX:
1091 fprintf(stderr, "Failed to load Lua script file %s:\n\t%s: %s.\n", luascriptfile, "Lua syntax error", lua_tostring(L, -1));
1092 break;
1093 case LUA_ERRMEM:
1094 fprintf(stderr, "Failed to load Lua script file %s:\n\t%s: %s.\n", luascriptfile, "memory allocation error", lua_tostring(L, -1));
1095 break;
1096 case LUA_ERRFILE:
1097 fprintf(stderr, "Failed to load Lua script file %s:\n\t%s: %s.\n", luascriptfile, "cannot open/read the file", lua_tostring(L, -1));
1098 break;
1099 };
1100 lua_pop(L, 1);
1101 } else {
1102 if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
1103 fprintf(stderr, "Failed to load Lua script file %s: %s", luascriptfile, lua_tostring(L, -1));
1104 lua_pop(L, 1);
1105 } else {
1106 lua_prescan_specials = lua_callback_defined(L, (const char*) "prescan_specials_callback");
1107 lua_scan_specials = lua_callback_defined(L, (const char*) "scan_specials_callback");
1108 lua_after_prescan = lua_callback_defined(L, (const char*) "after_prescan_callback");
1109 lua_after_drawchar = lua_callback_defined(L, (const char*) "after_drawchar_callback");
1110 lua_after_drawrule = lua_callback_defined(L, (const char*) "after_drawrule_callback");
1111 lua_process_stack = lua_callback_defined(L, (const char*) "process_stack_callback");
1112 lua_dvips_exit = lua_callback_defined(L, (const char*) "dvips_exit_callback");
1113 };
1114 };
1115 free(luascriptfile);
1116 }
1117 else
1118 if (strcmp(luascript, LUASCRIPTFILE))
1119 fprintf(stderr, "Failed to load Lua script file %s: file not found\n", luascript);
1120 }
1121
1122 int
1123 run_lua_specials(lua_State *L, const char* lua_func, char* p, Boolean lua_available)
1124 {
1125 size_t l = 1;
1126 const char* luares;
1127 if (lua_available) {
1128 lua_getglobal(L, lua_func);
1129 lua_pushstring(L, (const char *) p);
1130 lua_newtable(L);
1131 lua_pushinteger(L, hh);
1132 lua_setfield(L, -2, (const char*) "hh");
1133 lua_pushinteger(L, vv);
1134 lua_setfield(L, -2, (const char*) "vv");
1135 lua_pushinteger(L, pagenum);
1136 lua_setfield(L, -2, (const char*) "pagenum");
1137 if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
1138 fprintf_str(stderr, "error running function 'special' %s %s: %s", lua_func, p, lua_tostring(L, -1));
1139 lua_pop(L, 1);
1140 } else {
1141 int t = lua_type(L, -1);
1142 switch (t) {
1143 case LUA_TSTRING: {
1144 luares = lua_tostring(L, -1);
1145 l = lua_rawlen(L, -1);
1146 if (luares != 0 && l > 0) {
1147 while (strcmp(p,luares) != 0) {
1148 if (nextstring + l >= maxstring)
1149 morestrings();
1150 if (nextstring + l >= maxstring)
1151 error("! out of string space");
1152 strcpy(nextstring, luares);
1153 p = nextstring;
1154 #ifdef DEBUG
1155 if (dd(D_SPECIAL))
1156 fprintf_str(stderr, "Preprocessing special with Lua: %s len=%zu\n", p, l);
1157 #endif
1158 }
1159 } else {
1160 l = 0;
1161 }
1162 break;
1163 }
1164 case LUA_TBOOLEAN: {
1165 if (lua_toboolean(L, -1) == 0)
1166 l = 0;
1167 break;
1168 }
1169 default: {
1170 l = 0;
1171 break;
1172 }
1173 }
1174 lua_pop(L, 1);
1175 }
1176 }
1177 return l;
1178 }
1179
1180 void
1181 run_lua_after_prescan(lua_State *L)
1182 {
1183 lua_getglobal(L, "after_prescan_callback");
1184 lua_newtable(L);
1185 lua_pushinteger(L, hpapersize);
1186 lua_setfield(L, -2, (const char*) "hpapersize");
1187 lua_pushinteger(L, vpapersize);
1188 lua_setfield(L, -2, (const char*) "vpapersize");
1189 lua_pushinteger(L, hoff);
1190 lua_setfield(L, -2, (const char*) "hoff");
1191 lua_pushinteger(L, voff);
1192 lua_setfield(L, -2, (const char*) "voff");
1193 lua_pushinteger(L, actualdpi);
1194 lua_setfield(L, -2, (const char*) "actualdpi");
1195 lua_pushinteger(L, vactualdpi);
1196 lua_setfield(L, -2, (const char*) "vactualdpi");
1197 lua_pushinteger(L, num);
1198 lua_setfield(L, -2, (const char*) "num");
1199 lua_pushinteger(L, den);
1200 lua_setfield(L, -2, (const char*) "den");
1201 lua_pushnumber(L, mag);
1202 lua_setfield(L, -2, (const char*) "mag");
1203 lua_pushinteger(L, totalpages);
1204 lua_setfield(L, -2, (const char*) "totalpages");
1205 lua_pushstring(L, (const char*) iname);
1206 lua_setfield(L, -2, (const char*) "dvifile");
1207 if (lua_pcall(L, 1, 0, 0) != LUA_OK)
1208 {
1209 fprintf_str(stderr, "error running function 'after_prescan_callback': %s", lua_tostring(L, -1));
1210 lua_pop(L, 1);
1211 }
1212 }
1213
1214 void
1215 run_lua_after_drawchar(lua_State *L, chardesctype *c, int cc, int rhh, int rvv, int dir, int lastfont, int tu_count, unsigned int *tounicode)
1216 {
1217 lua_getglobal(L, "after_drawchar_callback");
1218 lua_newtable(L);
1219 lua_pushinteger(L, pagenum);
1220 lua_setfield(L, -2, (const char*) "pagenum");
1221 lua_pushinteger(L, c->charcode);
1222 lua_setfield(L, -2, (const char*) "charcode");
1223 lua_pushinteger(L, c->cid);
1224 lua_setfield(L, -2, (const char*) "cid");
1225 if (curfnt->resfont->enc && (c->charcode >= 0) && (c->charcode < 256)) {
1226 lua_pushstring(L, (const char*) curfnt->resfont->enc[c->charcode]);
1227 lua_setfield(L, -2, (const char*) "glyphname");
1228 }
1229 lua_pushinteger(L, c->pixelwidth);
1230 lua_setfield(L, -2, (const char*) "pixelwidth");
1231 lua_pushinteger(L, rhh);
1232 lua_setfield(L, -2, (const char*) "rhh");
1233 lua_pushinteger(L, rvv);
1234 lua_setfield(L, -2, (const char*) "rvv");
1235 lua_pushinteger(L, dir);
1236 lua_setfield(L, -2, (const char*) "dir");
1237 lua_pushinteger(L, lastfont);
1238 lua_setfield(L, -2, (const char*) "lastfont");
1239 lua_pushliteral(L, "tounicode");
1240 if (tu_count > 0) {
1241 int i;
1242 lua_newtable(L);
1243 for (i=0; i<tu_count; i++) {
1244 lua_pushinteger(L, *(tounicode+i));
1245 lua_rawseti(L, -2, i+1);
1246 }
1247 } else {
1248 lua_pushnil(L);
1249 }
1250 lua_settable(L, -3);
1251 lua_pushinteger(L, dvipos);
1252 lua_setfield(L, -2, (const char*) "bpos");
1253 lua_pushinteger(L, ftell(dvifile));
1254 lua_setfield(L, -2, (const char*) "epos");
1255 lua_pushstring(L, (const char*) curfnt->resfont->TeXname);
1256 lua_setfield(L, -2, (const char*) "tfmname");
1257 lua_pushstring(L, (const char*) curfnt->resfont->Fontfile);
1258 lua_setfield(L, -2, (const char*) "fntfile");
1259 lua_pushstring(L, (const char*) curfnt->resfont->PSname);
1260 lua_setfield(L, -2, (const char*) "psname");
1261 if (lua_pcall(L, 1, 0, 0) != LUA_OK)
1262 {
1263 fprintf_str(stderr, "error running function 'after_drawchar_callback': %s", lua_tostring(L, -1));
1264 lua_pop(L, 1);
1265 }
1266 }
1267
1268 void
1269 run_lua_process_stack(lua_State *L, const char *cmd)
1270 {
1271 lua_getglobal(L, "process_stack_callback");
1272 lua_newtable(L);
1273 lua_pushinteger(L, pagenum);
1274 lua_setfield(L, -2, (const char*) "pagenum");
1275 lua_pushstring(L, cmd);
1276 lua_setfield(L, -2, (const char*) "cmd");
1277 lua_pushinteger(L, hh);
1278 lua_setfield(L, -2, (const char*) "hh");
1279 lua_pushinteger(L, vv);
1280 lua_setfield(L, -2, (const char*) "vv");
1281 lua_pushinteger(L, dir);
1282 lua_setfield(L, -2, (const char*) "dir");
1283 if (lua_pcall(L, 1, 0, 0) != LUA_OK)
1284 {
1285 fprintf_str(stderr, "error running function 'process_stack_callback': %s", lua_tostring(L, -1));
1286 lua_pop(L, 1);
1287 }
1288 }
1289
1290 void
1291 run_lua_dvips_exit(lua_State *L, int code, int log_record_count)
1292 {
1293 lua_getglobal(L, "dvips_exit_callback");
1294 lua_newtable(L);
1295 lua_pushinteger(L, code);
1296 lua_setfield(L, -2, (const char*) "exitcode");
1297 lua_pushinteger(L, log_record_count);
1298 lua_setfield(L, -2, (const char*) "log_record_count");
1299 if (lua_pcall(L, 1, 0, 0) != LUA_OK)
1300 {
1301 fprintf_str(stderr, "error running function 'dvips_exit_callback': %s", lua_tostring(L, -1));
1302 lua_pop(L, 1);
1303 }
1304 }
1305
1306 void
1307 run_lua_after_drawrule(lua_State *L, integer rw, integer rh)
1308 {
1309 lua_getglobal(L, "after_drawrule_callback");
1310 lua_newtable(L);
1311 lua_pushinteger(L, pagenum);
1312 lua_setfield(L, -2, (const char*) "pagenum");
1313 lua_pushinteger(L, hh);
1314 lua_setfield(L, -2, (const char*) "hh");
1315 lua_pushinteger(L, vv);
1316 lua_setfield(L, -2, (const char*) "vv");
1317 lua_pushinteger(L, dir);
1318 lua_setfield(L, -2, (const char*) "dir");
1319 lua_pushinteger(L, rw);
1320 lua_setfield(L, -2, (const char*) "rw");
1321 lua_pushinteger(L, rh);
1322 lua_setfield(L, -2, (const char*) "rh");
1323 lua_pushinteger(L, dvipos);
1324 lua_setfield(L, -2, (const char*) "bpos");
1325 lua_pushinteger(L, ftell(dvifile));
1326 lua_setfield(L, -2, (const char*) "epos");
1327 if (lua_pcall(L, 1, 0, 0) != LUA_OK)
1328 {
1329 fprintf_str(stderr, "error running function 'after_drawrule_callback': %s", lua_tostring(L, -1));
1330 lua_pop(L, 1);
1331 }
1332 }
1333
1334 void load_touni_tables(void)
1335 {
1336 if (luaL_dostring(L, (const char*) glyphlist_table))
1337 {
1338 snprintf(errbuf, ERR_BUF_LEN, "Error: Lua script glyphlist_table execution error: %s.\n", lua_tostring(L, -1));
1339 lua_pop(L, 1);
1340 error(errbuf);
1341 }
1342 if (luaL_dostring(L, (const char*) font_glyph_maps))
1343 {
1344 snprintf(errbuf, ERR_BUF_LEN, "Error: Lua script font_glyph_maps execution error: %s.\n", lua_tostring(L, -1));
1345 lua_pop(L, 1);
1346 error(errbuf);
1347 }
1348 if (luaL_dostring(L, (const char*) xdvipsk_tounicode))
1349 {
1350 snprintf(errbuf, ERR_BUF_LEN, "Error: Lua script xdvipsk_tounicode execution error: %s.\n", lua_tostring(L, -1));
1351 lua_pop(L, 1);
1352 error(errbuf);
1353 }
1354 }
1355
1356 #endif /* XDVIPSK */
1357 /*
1358 * Finally, our main routine.
1359 */
1360 #ifdef VMS
1361 main(void)
1362 #else
1363 int
1364 main(int argc, char **argv)
1365 #endif
1366 {
1367 int i, lastext = -1;
1368 #ifdef MVSXA
1369 int firstext = -1;
1370 #endif
1371 sectiontype *sects;
1372
1373 #ifdef WIN32
1374 #if defined(KPATHSEA)
1375 int ac;
1376 char **av, *enc;
1377 #endif
1378 SET_BINARY(fileno(stdin));
1379 SET_BINARY(fileno(stdout));
1380 #endif
1381
1382 #ifdef __APPLE__ /* for Mac OS X, T. Uchiyama */
1383 struct rlimit rl;
1384 getrlimit(RLIMIT_STACK, &rl);
1385 rl.rlim_cur = 2048 * 1024;
1386 setrlimit(RLIMIT_STACK, &rl);
1387 #endif
1388 #ifdef KPATHSEA
1389 if (argc > 1) {
1390 if (argc == 2 && strncmp(argv[1], "-?", 2) == 0) {
1391 #ifndef XDVIPSK
1392 printf("%s %s\n", banner, banner2);
1393 #else
1394 printf("%s %s\n%s\n", banner, banner2, banner3);
1395 #endif /* XDVIPSK */
1396 help(0);
1397 exit(0);
1398 }
1399 if (argc == 2 && strncmp(argv[1], "-v", 2) == 0) {
1400 #ifndef XDVIPSK
1401 printf("%s %s\n", banner, banner2);
1402 #else
1403 printf("%s %s\n%s\n", banner, banner2, banner3);
1404 #endif /* XDVIPSK */
1405 exit(0);
1406 }
1407 /* print information and exit if dvips finds options --help or --version */
1408 if (strlen(argv[1]) == 6 && strcmp(argv[1], "--help") == 0) {
1409 help (0);
1410 exit (0);
1411 }
1412 if (strlen (argv[1]) == 9 && strcmp(argv[1], "--version") == 0) {
1413 puts (BANNER);
1414 #ifdef XDVIPSK
1415 puts(BANNER3);
1416 #endif /* XDVIPSK */
1417 puts (kpathsea_version_string);
1418 #ifndef XDVIPSK
1419 puts ("There is NO warranty. You may redistribute this software\n\
1420 under the terms of the GNU General Public License\n\
1421 and the Dvips copyright.\n\
1422 For more information about these matters, see the files\n\
1423 named COPYING and dvips.h.\n\
1424 Primary author of Dvips: T. Rokicki.");
1425 #else
1426 puts ("Copyright 2026 VTeX Ltd.\n\
1427 There is NO warranty. You may redistribute this software\n\
1428 under the terms of the GNU General Public License\n\
1429 and the xdvips copyright.\n\
1430 For more information about these matters, see the files\n\
1431 named COPYING and xdvips.h.\n\
1432 Primary author of Dvips: T. Rokicki.");
1433 puts("Modifications authors: A. Povilaitis, S. Tolusis, M. Piesina.");
1434 #endif /* XDVIPSK */
1435 exit (0);
1436 }
1437 }
1438 kpse_set_program_name (argv[0], "dvips");
1439 kpse_set_program_enabled (kpse_pk_format, MAKE_TEX_PK_BY_DEFAULT, kpse_src_compile);
1440 #ifdef WIN32
1441 enc = kpse_var_value("command_line_encoding");
1442 if (get_command_line_args_utf8(enc, &ac, &av)) {
1443 argc = ac;
1444 argv = av;
1445 }
1446 #endif
1447 #endif
1448
1449 #ifdef __THINK__
1450 argc = dcommand(&argv); /* do I/O stream redirection */
1451 #endif
1452 #ifdef VMS /* Grab the command-line buffer */
1453 short len_arg;
1454 $DESCRIPTOR( verb_dsc, "DVIPS "); /* assume the verb is always DVIPS */
1455 struct dsc$descriptor_d temp_dsc = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0};
1456
1457 progname = &thh[0];
1458 strcpy(progname,"DVIPS%ERROR");
1459
1460 lib$get_foreign( &temp_dsc, 0, &len_arg, 0); /* Get the command line */
1461 str$prefix(&temp_dsc, &verb_dsc); /* prepend the VERB */
1462 len_arg += verb_dsc.dsc$w_length; /* update the length */
1463 temp_dsc.dsc$a_pointer[len_arg] = '\0'; /* terminate the string */
1464 gargv = &temp_dsc.dsc$a_pointer; /* point to the buffer */
1465 gargc = 1; /* only one big argv */
1466 #else
1467 progname = argv[0];
1468 gargv = argv;
1469 gargc = argc;
1470 /* we sneak a look at the first arg in case it's debugging */
1471 #ifdef DEBUG
1472 if (argc > 1 && strncmp(argv[1], "-d", 2)==0) {
1473 if (argv[1][2]==0 && argc > 2) {
1474 if (sscanf(argv[2], "%d", &debug_flag)==0)
1475 debug_flag = 0;
1476 } else {
1477 if (sscanf(argv[1]+2, "%d", &debug_flag)==0)
1478 debug_flag = 0;
1479 }
1480 }
1481 #if defined(KPATHSEA) && defined(KPSE_DEBUG)
1482 if (dd(D_FILES)) KPSE_DEBUG_SET (KPSE_DEBUG_FOPEN);
1483 if (dd(D_PATHS)) KPSE_DEBUG_SET (KPSE_DEBUG_PATHS);
1484 if (dd(D_STAT)) KPSE_DEBUG_SET (KPSE_DEBUG_STAT);
1485 if (dd(D_HASH)) KPSE_DEBUG_SET (KPSE_DEBUG_HASH);
1486 if (dd(D_EXPAND)) KPSE_DEBUG_SET (KPSE_DEBUG_EXPAND);
1487 if (dd(D_SEARCH)) KPSE_DEBUG_SET (KPSE_DEBUG_SEARCH);
1488 #endif /* KPATHSEA && KPSE_DEBUG */
1489 #endif /* DEBUG */
1490 #endif /* VMS */
1491 dvips_debug_flag = 0;
1492 { /* for compilers incompatible with c99 */
1493 char *s = getenv ("DVIPSDEBUG");
1494 if (s) {
1495 dvips_debug_flag = 1;
1496 } else {
1497 s = getenv ("KPATHSEA_DEBUG");
1498 if (s) {
1499 dvips_debug_flag = 1;
1500 }
1501 }
1502 }
1503 #ifdef XDVIPSK
1504 openlogfile(argc, argv);
1505 #endif /* XDVIPSK */
1506 initialize();
1507 checkenv(0);
1508 getdefaults(CONFIGFILE);
1509 getdefaults((char *)0);
1510 /*
1511 * This next whole big section of code is straightforward; we just scan
1512 * the options. An argument can either immediately follow its option letter
1513 * or be separated by spaces. Any argument not preceded by '-' and an
1514 * option letter is considered a file name; the program complains if more
1515 * than one file name is given, and uses stdin if none is given.
1516 */
1517 #ifdef VMS
1518 vmscli(void);
1519 papsizes = (struct papsiz *)revlist((void *)papsizes); /* Added by PWD 21-Mar-1997 */
1520 #else
1521 queryoptions = 0;
1522 do
1523 {
1524 for (i=1; i<argc; i++) {
1525 if (*argv[i]=='-') {
1526 char *p=argv[i]+2;
1527 char c=argv[i][1];
1528 switch (c) {
1529 case '-':
1530 queryoptions = 1;
1531 break;
1532 #ifdef XDVIPSK
1533 case 'g':
1534 Exe_Log_Mode = (*p != '0');
1535 break;
1536 case 'H':
1537 TURBO_MODE = (*p != '0');
1538 break;
1539 case 'I':
1540 RESIZE_MODE = (*p != '0');
1541 if (RESIZE_MODE)
1542 queryresizeargs(p);
1543 break;
1544 case 'W':
1545 EMSEARCH_MODE = (*p != '0');
1546 break;
1547 case 'Q':
1548 VTEX_SPEC_MODE = (*p != '0');
1549 break;
1550 #endif /* XDVIPSK */
1551 case 'a':
1552 dopprescan = (*p != '0');
1553 break;
1554 case 'b':
1555 if (strcmp(p, "itmapfontenc") == 0) {
1556 p = argv[++i] ;
1557 if (strcmp(p, "off") == 0) {
1558 bitmapencopt(0) ; // disable bitmap font enc feature
1559 } else if (strcmp(p, "on") == 0) {
1560 bitmapencopt(1) ; // try to include bitmap font encs
1561 } else if (strcmp(p, "strict") == 0) {
1562 bitmapencopt(2) ; // issue warnings for missing encs
1563 } else {
1564 error(
1565 "! -bitmapfontenc option only supports off, on, and strict") ;
1566 }
1567 break ;
1568 }
1569 if (*p == 0 && argv[i+1])
1570 p = argv[++i];
1571 if (sscanf(p, "%d", &pagecopies)==0)
1572 error("! Bad number of page copies option (-b).");
1573 if (pagecopies < 1 || pagecopies > 1000)
1574 error("! can only print one to a thousand page copies");
1575 break;
1576 case 'c' :
1577 if (*p == 0 && argv[i+1])
1578 p = argv[++i];
1579 if (sscanf(p, "%d", &numcopies)==0)
1580 error("! Bad number of copies option (-c).");
1581 break;
1582 case 'd' :
1583 #ifdef DEBUG
1584 {
1585 int old_debug = debug_flag;
1586 static int warned_already = 0;
1587
1588 if (*p == 0 && argv[i+1])
1589 p = argv[++i];
1590 if (sscanf(p, "%d", &debug_flag)==0)
1591 error("! Bad debug option (-d).");
1592 if (debug_flag != old_debug && warned_already++ == 0) {
1593 fprintf(stderr,
1594 "I found a debug option that was not the first argument to the dvips\n");
1595 fprintf(stderr,
1596 "command. Some debugging output may have been lost because of this.\n");
1597 }
1598 break;
1599 }
1600 #else
1601 error("not compiled in debug mode");
1602 break;
1603 #endif /* DEBUG */
1604 case 'e' :
1605 if (*p == 0 && argv[i+1])
1606 p = argv[++i];
1607 if (sscanf(p, "%d", &maxdrift)==0 || maxdrift<0)
1608 error("! Bad maxdrift option (-e).");
1609 vmaxdrift = maxdrift;
1610 break;
1611 case 'f' :
1612 filter = (*p != '0');
1613 if (filter)
1614 oname = "";
1615 noenv = 1;
1616 sendcontrolD = 0;
1617 break;
1618 case 'u' :
1619 {
1620 char PSname[300];
1621 if (*p == 0 && argv[i+1])
1622 p = argv[++i];
1623 strcpy(PSname, p);
1624 if (!strchr(PSname, '.'))
1625 strcat(PSname, ".map"); /* default extension */
1626 if (PSname[0] == '+')
1627 getpsinfo(PSname+1);
1628 else
1629 psmapfile = strdup(PSname); /* a cute small memory leak (just as in 'p' option handling in resident.c) */
1630 }
1631 break;
1632 #ifndef XDVIPSK
1633 case 'h' : case 'H' :
1634 #else
1635 case 'h':
1636 #endif /* XDVIPSK */
1637 if (*p == 0 && argv[i+1])
1638 p = argv[++i];
1639 if (strcmp(p, "-") == 0)
1640 headers_off = 1;
1641 else
1642 add_header(p);
1643 break;
1644 case 'i':
1645 sepfiles = (*p != '0');
1646 if (sepfiles && maxsecsize == 0) {
1647 maxsecsize = 1; /* default section size to one page/file */
1648 }
1649 break;
1650 case 'j':
1651 #ifndef XDVIPSK
1652 partialdownload = (*p != '0');
1653 #else
1654 t1_partialdownload = (*p != '0');
1655 break;
1656 case 'J':
1657 cid_partialdownload = (*p != '0');
1658 break;
1659 #endif /* XDVIPSK */
1660 case 'k':
1661 cropmarks = (*p != '0');
1662 break;
1663 case 'R':
1664 if (*p == '0') {
1665 secure = 0;
1666 } else if (*p == '2') {
1667 secure = 2;
1668 } else {
1669 secure = 1;
1670 }
1671 if (secure)
1672 secure_option = 1; /* Never used */
1673 break;
1674 case 'S':
1675 if (*p == 0 && argv[i+1])
1676 p = argv[++i];
1677 if (sscanf(p, "%d", &maxsecsize)==0)
1678 error("! Bad section size arg (-S).");
1679 break;
1680 case 'm' :
1681 if (STREQ (p, "ode") && argv[i+1]) {
1682 mfmode = argv[++i];
1683 mfmode_option = 1;
1684 } else
1685 manualfeed = (*p != '0');
1686 break;
1687 case 'n' :
1688 if (STREQ (p, "oomega")) {
1689 noomega = 1;
1690 } else if (STREQ (p, "optex")) {
1691 noptex = 1;
1692 #ifdef XDVIPSK
1693 }
1694 else if (STREQ(p, "oluatex")) {
1695 noluatex = 1;
1696 }
1697 else if (STREQ(p, "oToUnicode")) {
1698 noToUnicode = 1;
1699 #endif /* XDVIPSK */
1700 } else {
1701 if (*p == 0 && argv[i+1])
1702 p = argv[++i];
1703 #ifdef SHORTINT
1704 if (sscanf(p, "%ld", &maxpages)==0)
1705 #else /* ~SHORTINT */
1706 if (sscanf(p, "%d", &maxpages)==0)
1707 #endif /* ~SHORTINT */
1708 error("! Bad number of pages option (-n).");
1709 }
1710 break;
1711 case 'o' :
1712 if (*p == 0 && argv[i+1] &&
1713 (STREQ (argv[i+1], "-") || argv[i+1][0] != '-'))
1714 p = argv[++i];
1715 oname_option = 1;
1716 oname = p;
1717 noenv = 1;
1718 sendcontrolD = 0;
1719 #if defined(MSDOS) || defined(OS2)
1720 if (*oname && oname[strlen(oname)-1] == ':')
1721 oname[strlen(oname)-1] = 0; /* strip ':' off */
1722 #endif
1723 break;
1724 case 'O' :
1725 if (*p == 0 && argv[i+1])
1726 p = argv[++i];
1727 handlepapersize(p, &hoff, &voff);
1728 break;
1729 case 'T' :
1730 if (*p == 0 && argv[i+1])
1731 p = argv[++i];
1732 handlepapersize(p, &hpapersize, &vpapersize);
1733 if (landscape) {
1734 error(
1735 "both landscape and papersize specified; ignoring landscape");
1736 landscape = 0;
1737 }
1738 lastpsizwins = 0;
1739 break;
1740 case 'p' :
1741 #if defined(MSDOS) || defined(OS2) || defined(ATARIST)
1742 /* check for emTeX job file (-pj=filename) */
1743 if (*p == 'j') {
1744 p++;
1745 if (*p == '=' || *p == ':')
1746 p++;
1747 mfjobname = newstring(p);
1748 break;
1749 }
1750 /* must be page number instead */
1751 #endif
1752 if (*p == 'p') { /* a -pp specifier for a page list? */
1753 p++;
1754 if (*p == 0 && argv[i+1])
1755 p = argv[++i];
1756 if (ParsePages(p))
1757 error("! Bad page list specifier (-pp).");
1758 pagelist = 1;
1759 break;
1760 }
1761 if (*p == 0 && argv[i+1])
1762 p = argv[++i];
1763 if (*p == '=') {
1764 abspage = 1;
1765 p++;
1766 }
1767 #ifdef SHORTINT
1768 switch(sscanf(p, "%ld.%ld", &firstpage, &firstseq)) {
1769 #else /* ~SHORTINT */
1770 switch(sscanf(p, "%d.%d", &firstpage, &firstseq)) {
1771 #endif /* ~SHORTINT */
1772 case 1: firstseq = 0;
1773 case 2: break;
1774 default:
1775 #ifdef KPATHSEA
1776 error(concat3 ("! Bad first page option (-p ", p, ")."));
1777 #else
1778 error("! Bad first page option (-p).");
1779 #endif
1780 }
1781 notfirst = 1;
1782 break;
1783 case 'l':
1784 if (strncmp(p, "andscaperotate", 14) == 0) {
1785 p += 14 ;
1786 if (*p == 0 || *p == '1') {
1787 landscaperotate = 1 ;
1788 } else if (*p == '0') {
1789 landscaperotate = 0 ;
1790 } else {
1791 error("! -landscaperotate command ended with junk") ;
1792 }
1793 #ifdef XDVIPSK
1794 } else if (STREQ (p, "ua") && argv[i+1]) {
1795 luascript = argv[++i];
1796 #endif /* XDVIPSK */
1797 } else {
1798 if (*p == 0 && argv[i+1])
1799 p = argv[++i];
1800 if (*p == '=') {
1801 abspage = 1;
1802 p++;
1803 }
1804 #ifdef SHORTINT
1805 switch(sscanf(p, "%ld.%ld", &lastpage, &lastseq)) {
1806 #else /* ~SHORTINT */
1807 switch(sscanf(p, "%d.%d", &lastpage, &lastseq)) {
1808 #endif /* ~SHORTINT */
1809 case 1: lastseq = 0;
1810 case 2: break;
1811 default:
1812 #ifdef KPATHSEA
1813 error(concat3 ("! Bad last page option (-l ", p, ")."));
1814 #else
1815 error("! Bad last page option (-l).");
1816 #endif
1817 }
1818 notlast = 1;
1819 }
1820 break;
1821 case 'A':
1822 oddpages = 1;
1823 break;
1824 case 'B':
1825 evenpages = 1;
1826 break;
1827 #ifndef XDVIPSK
1828 case 'q' : case 'Q' :
1829 #else
1830 case 'q':
1831 #endif /* XDVIPSK */
1832 quiet = (*p != '0');
1833 break;
1834 case 'r' :
1835 reverse = (*p != '0');
1836 break;
1837 case 't' :
1838 if (STREQ (p, "itle") && argv[i+1]) {
1839 titlename = argv[++i];
1840 } else {
1841 if (*p == 0 && argv[i+1])
1842 p = argv[++i];
1843 if (strcmp(p, "landscape") == 0) {
1844 if (hpapersize || vpapersize)
1845 error("both landscape and papersize specified; ignoring landscape");
1846 else
1847 landscape = 1;
1848 } else
1849 paperfmt = p;
1850 }
1851 break;
1852 case 'v':
1853 #ifndef XDVIPSK
1854 printf ("%s %s\n", banner, banner2);
1855 #else
1856 printf("%s %s\n%s\n", banner, banner2, banner3);
1857 #endif /* XDVIPSK */
1858 exit (0);
1859 break;
1860 case 'x' : case 'y' :
1861 if (*p == 0 && argv[i+1])
1862 p = argv[++i];
1863 if (sscanf(p, "%lg", &mag)==0 || mag < 1 ||
1864 mag > 1000000)
1865 error("! Bad magnification parameter (-x or -y).");
1866 overridemag = (c == 'x' ? 1 : -1);
1867 break;
1868 case 'C' :
1869 if (*p == 0 && argv[i+1])
1870 p = argv[++i];
1871 if (sscanf(p, "%d", &collatedcopies)==0)
1872 error("! Bad number of collated copies option (-C).");
1873 break;
1874 case 'D' :
1875 if (*p == 0 && argv[i+1])
1876 p = argv[++i];
1877 if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
1878 actualdpi > 10000)
1879 error("! Bad dpi parameter (-D).");
1880 vactualdpi = actualdpi;
1881 /* If we had the mode from config.ps, don't use it.
1882 If they specified one with -mode, keep it. */
1883 if (!mfmode_option)
1884 mfmode = NULL;
1885 break;
1886 case 'E' :
1887 tryepsf = (*p != '0');
1888 removecomments = disablecomments = 0;
1889 if (tryepsf && maxsecsize == 0)
1890 maxsecsize = 1;
1891 break;
1892 case 'K' :
1893 removecomments = (*p != '0');
1894 break;
1895 case 'L' :
1896 lastpsizwins = (*p != '0');
1897 break;
1898 case 'U' :
1899 nosmallchars = (*p != '0');
1900 break;
1901 case 'X' :
1902 if (*p == 0 && argv[i+1])
1903 p = argv[++i];
1904 if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
1905 actualdpi > 10000)
1906 error("! Bad dpi parameter (-D).");
1907 break;
1908 case 'Y' :
1909 if (*p == 0 && argv[i+1])
1910 p = argv[++i];
1911 if (sscanf(p, "%d", &vactualdpi)==0 || vactualdpi < 10 ||
1912 vactualdpi > 10000)
1913 error("! Bad dpi parameter (-D).");
1914 vactualdpi = vactualdpi;
1915 break;
1916 case 'F' :
1917 sendcontrolD = (*p != '0');
1918 break;
1919 case 'G' :
1920 shiftlowchars = (*p != '0');
1921 break;
1922 case 'M':
1923 dontmakefont = (*p != '0');
1924 #ifdef KPATHSEA
1925 kpse_set_program_enabled (kpse_pk_format, !dontmakefont,
1926 kpse_src_cmdline);
1927 #endif
1928 break;
1929 case 'N' :
1930 disablecomments = (*p != '0');
1931 break;
1932 case 'P' :
1933 {
1934 struct papsiz *opapsiz = papsizes;
1935 struct papsiz *npapsiz;
1936 papsizes = 0;
1937 if (*p == 0 && argv[i+1])
1938 p = argv[++i];
1939 printer = p;
1940 noenv = 1;
1941 if (!getdefaults("")) {
1942 /* If no config file, default the output name. */
1943 oname = concat ("| lpr -P", printer);
1944 }
1945 npapsiz = opapsiz;
1946 while (npapsiz && npapsiz->next)
1947 npapsiz = npapsiz->next;
1948 if (npapsiz) {
1949 npapsiz->next = papsizes;
1950 papsizes = opapsiz;
1951 }
1952 }
1953 break;
1954 case 's':
1955 safetyenclose = (*p != '0');
1956 break;
1957 case 'V':
1958 downloadpspk = (*p != '0');
1959 break;
1960 case 'Z':
1961 compressed = (*p != '0');
1962 break;
1963 #ifdef HPS
1964 case 'z':
1965 HPS_FLAG = (*p != '0');
1966 break;
1967 #endif
1968 case '?':
1969 break; /* We print the banner and help msg below. */
1970 default:
1971 #ifdef KPATHSEA
1972 error(concat3 ("! Invalid option `", argv[i],
1973 "'. Try --help for more information."));
1974 #else
1975 error(
1976 "! Bad option, not one of acdefhijklmnopqrstxyzABCDEFKMNOPSTUXYZ?");
1977 #endif
1978 }
1979 } else {
1980 if (*iname == 0) {
1981 register char *p;
1982
1983 lastext = 0;
1984 iname = nextstring;
1985 p = argv[i];
1986 if (NAME_BEGINS_WITH_DEVICE(p)) { /* get past DOSISH drive */
1987 *nextstring++ = *p++;
1988 *nextstring++ = *p++;
1989 }
1990 while (*p) {
1991 *nextstring = *p++;
1992 if (*nextstring == '.')
1993 lastext = nextstring - iname;
1994 else if (IS_DIR_SEP(*nextstring))
1995 lastext = 0;
1996 #ifdef WIN32
1997 else if (IS_KANJI(p-1)) {
1998 nextstring++;
1999 *nextstring = *p++;
2000 }
2001 #endif
2002 nextstring++;
2003 }
2004 *nextstring++ = '.';
2005 *nextstring++ = 'd';
2006 *nextstring++ = 'v';
2007 *nextstring++ = 'i';
2008 *nextstring++ = 0;
2009 } else
2010 #ifdef KPATHSEA
2011 error(concat3("! Second input filename (", argv[i],
2012 ") specified."));
2013 #else
2014 error("! Two input file names specified.");
2015 #endif
2016 }
2017 }
2018 if (noenv == 0) {
2019 register char *p;
2020 struct papsiz *opapsiz = papsizes;
2021 papsizes = 0;
2022 if (0 != (p = getenv("PRINTER"))) {
2023 #if defined(MSDOS) || defined(OS2)
2024 strcpy(nextstring, p);
2025 strcat(nextstring, ".cfg");
2026 #else
2027 strcpy(nextstring, "config.");
2028 strcat(nextstring, p);
2029 #endif
2030 getdefaults(nextstring);
2031 }
2032 {
2033 struct papsiz *npapsiz = opapsiz;
2034 while (npapsiz && npapsiz->next)
2035 npapsiz = npapsiz->next;
2036 if (npapsiz) {
2037 npapsiz->next = papsizes;
2038 papsizes = opapsiz;
2039 }
2040 }
2041 }
2042 papsizes = (struct papsiz *)revlist((void *)papsizes);
2043 if (queryoptions != 0) { /* get new options */
2044 #ifndef XDVIPSK
2045 fprintf(stderr, "%s %s\n", banner, banner2);
2046 #else
2047 fprintf(stderr, "%s %s\n%s\n", banner, banner2, banner3);
2048 #endif /* XDVIPSK */
2049 help(1);
2050 queryargs();
2051 if (qargc == 1)
2052 queryoptions = 0;
2053 else {
2054 qargv[0] = argv[0];
2055 argc=qargc;
2056 argv=qargv;
2057 }
2058 }
2059 } while (queryoptions != 0);
2060 #endif
2061 #if defined(KPATHSEA) && defined(KPSE_DEBUG) && defined(DEBUG) /* this should really be part of a subroutine */
2062 if (dd(D_FILES)) KPSE_DEBUG_SET (KPSE_DEBUG_FOPEN);
2063 if (dd(D_PATHS)) KPSE_DEBUG_SET (KPSE_DEBUG_PATHS);
2064 if (dd(D_STAT)) KPSE_DEBUG_SET (KPSE_DEBUG_STAT);
2065 if (dd(D_HASH)) KPSE_DEBUG_SET (KPSE_DEBUG_HASH);
2066 if (dd(D_EXPAND)) KPSE_DEBUG_SET (KPSE_DEBUG_EXPAND);
2067 if (dd(D_SEARCH)) KPSE_DEBUG_SET (KPSE_DEBUG_SEARCH);
2068 #endif /* KPATHSEA && KPSE_DEBUG && DEBUG */
2069 checkenv(1);
2070 #ifdef KPATHSEA
2071 kpse_init_prog ("DVIPS", actualdpi, mfmode, "cmr10");
2072 kpse_make_tex_discard_errors = quiet;
2073 #endif
2074 /*
2075 * The logic here is a bit convoluted. Since all `additional'
2076 * PostScript font information files are loaded *before* the master
2077 * one, and yet they should be able to override the master one, we
2078 * have to add the information in the master list to the *ends* of
2079 * the hash chain. We do this by reversing the lists, adding them
2080 * to the front, and then reversing them again.
2081 */
2082 #ifdef XDVIPSK
2083 if (!Exe_Log_Mode) {
2084 fclose(Exe_Log);
2085 remove(Exe_Log_Name);
2086 Exe_Log = NULL;
2087 }
2088 load_lua_scripts(luascript);
2089 load_touni_tables();
2090 #endif /* XDVIPSK */
2091 revpslists();
2092 getpsinfo((char *)NULL);
2093 revpslists();
2094 if (dvips_debug_flag) {
2095 if (!quiet)
2096 #ifndef XDVIPSK
2097 fprintf(stderr, "\n%s %s\n", banner, banner2);
2098 #else
2099 fprintf(stderr, "\n%s %s\n%s\n", banner, banner2, banner3);
2100 #endif /* XDVIPSK */
2101 prettycolumn = 0;
2102 } else {
2103 if (!quiet)
2104 #ifndef XDVIPSK
2105 fprintf(stderr, "%s %s\n", banner, banner2);
2106 #else
2107 fprintf(stderr, "\n%s %s\n%s\n", banner, banner2, banner3);
2108 #endif /* XDVIPSK */
2109 }
2110 #ifdef XDVIPSK
2111 if (!quiet) {
2112 if (TURBO_MODE)
2113 (void)fprintf(stderr, "Turbo mode for PS graphics is ON.\n");
2114 if (RESIZE_MODE) {
2115 (void)printf("Resize mode for emTeX graphics is ON. Filters in use:\n");
2116 (void)printf(" %s for BW images.\n", RESIZE_FILTER_BWName);
2117 (void)printf(" %s for Gray images.\n", RESIZE_FILTER_GrayName);
2118 (void)printf(" %s for RGB images.\n", RESIZE_FILTER_RGBName);
2119 (void)printf(" %s for CMYK images.\n", RESIZE_FILTER_CMYKName);
2120 }
2121 if (EMSEARCH_MODE)
2122 (void)fprintf(stderr, "Extended search mode for emTeX graphics is ON.\n");
2123 if (VTEX_SPEC_MODE)
2124 (void)fprintf(stderr, "Skipping VTeX private specials is ON.\n");
2125 }
2126 #endif /* XDVIPSK */
2127 if (*iname) {
2128 dvifile = fopen(iname, READBIN);
2129 /*
2130 * Allow names like a.b.
2131 */
2132 if (dvifile == 0) {
2133 iname[strlen(iname)-4] = 0; /* remove the .dvi suffix */
2134 dvifile = fopen(iname, READBIN);
2135 }
2136 }
2137 if (oname[0] == '-' && oname[1] == 0)
2138 oname = "";
2139 else if (*oname == 0 && ! filter && *iname) {
2140 /* determine output name from input name */
2141 oname = nextstring;
2142 #ifndef VMCMS /* get stuff before LAST "." */
2143 lastext = strlen(iname) - 1;
2144 while (iname[lastext] != '.' && lastext > 0)
2145 lastext--;
2146 if (iname[lastext] != '.')
2147 lastext = strlen(iname) - 1;
2148 #else /* for VM/CMS we take the stuff before FIRST "." */
2149 lastext = strchr(iname,'.') - iname;
2150 if ( lastext <= 0 ) /* if no '.' in "iname" */
2151 lastext = strlen(iname) -1;
2152 #endif
2153 #ifdef MVSXA /* IBM: MVS/XA */
2154 if (strchr(iname, '(') != NULL &&
2155 strchr(iname, ')') != NULL) {
2156 firstext = strchr(iname, '(') - iname + 1;
2157 lastext = strrchr(iname, ')') - iname - 1;
2158 }
2159 else {
2160 if (strrchr(iname, '.') != NULL) {
2161 lastext = strrchr(iname, '.') - iname - 1;
2162 }
2163 else lastext = strlen(iname) - 1;
2164 if (strchr(iname, '\'') != NULL)
2165 firstext = strchr(iname, '.') - iname + 1;
2166 else firstext = 0;
2167 }
2168 #endif /* IBM: MVS/XA */
2169 #ifdef MVSXA /* IBM: MVS/XA */
2170 for (i=firstext; i<=lastext; i++)
2171 #else
2172 for (i=0; i<=lastext; i++)
2173 #endif
2174 *nextstring++ = iname[i];
2175 if (iname[lastext] != '.')
2176 *nextstring++ = '.';
2177 #ifndef VMCMS
2178 *nextstring++ = 'p';
2179 *nextstring++ = 's';
2180 #else /* might as well keep things uppercase */
2181 *nextstring++ = 'P';
2182 *nextstring++ = 'S';
2183 #endif
2184 *nextstring++ = 0;
2185 /*
2186 * Now we check the name, and `throw away' any prefix information.
2187 * This means throwing away anything before (and including) a colon
2188 * or slash.
2189 */
2190 {
2191 const char *p = NAME_BEGINS_WITH_DEVICE(oname) ? oname + 2 : oname;
2192
2193 for (oname=p; *p && p[1]; p++)
2194 if (IS_DIR_SEP(*p))
2195 oname = p + 1;
2196 #ifdef WIN32
2197 else if (IS_KANJI(p))
2198 p++;
2199 #endif
2200 }
2201 }
2202 #ifdef DEBUG
2203 if (dd(D_PATHS)) {
2204 #ifdef SHORTINT
2205 fprintf_str(stderr,"input file %s output file %s swmem %ld\n",
2206 #else /* ~SHORTINT */
2207 fprintf_str(stderr,"input file %s output file %s swmem %d\n",
2208 #endif /* ~SHORTINT */
2209 iname, oname, swmem);
2210 #ifndef KPATHSEA
2211 fprintf_str(stderr,"tfm path %s\npk path %s\n", tfmpath, pkpath);
2212 fprintf_str(stderr,"fig path %s\nvf path %s\n", figpath, vfpath);
2213 fprintf_str(stderr,"config path %s\nheader path %s\n",
2214 configpath, headerpath);
2215 #endif
2216 #ifdef FONTLIB
2217 fprintf_str(stderr,"fli path %s\nfli names %s\n", flipath, fliname);
2218 #endif
2219 } /* dd(D_PATHS) */
2220 #endif /* DEBUG */
2221
2222 /*
2223 * Check if oname != iname
2224 */
2225 if (iname && *iname && oname && *oname) {
2226 #ifdef DOSISH
2227 if (strcasecmp (iname, oname) == 0) {
2228 #else
2229 if (strcmp (iname, oname) == 0) {
2230 #endif
2231 fprintf (stderr, "! Output name should be different from input name.\n");
2232 exit (20);
2233 }
2234 }
2235 /*
2236 * Now we try to open the dvi file.
2237 */
2238 if (!quiet && warningmsg)
2239 error(warningmsg);
2240 headersready = 1;
2241 headerfile = (compressed? CHEADERFILE : HEADERFILE);
2242 add_header(headerfile);
2243 if (*iname != 0) {
2244 fulliname = nextstring;
2245 #ifndef IGNORE_CWD
2246 if (!IS_DIR_SEP(*iname) && !NAME_BEGINS_WITH_DEVICE(iname)) {
2247 getcwd(nextstring, MAXPATHLEN + 2);
2248 while (*nextstring++);
2249 #ifdef VMS /* VMS doesn't need the '/' character appended */
2250 nextstring--; /* so just back up one byte. */
2251 #else
2252 *(nextstring-1) = '/';
2253 #endif
2254 }
2255 #endif
2256 strcpy(nextstring,iname);
2257 while (*nextstring++); /* advance nextstring past iname */
2258 } else if (filter) {
2259 dvifile = stdin;
2260 if (O_BINARY && !isatty(fileno(stdin)))
2261 SET_BINARY(fileno(stdin));
2262 } else {
2263 #ifdef KPATHSEA
2264 #ifndef XDVIPSK
2265 fprintf (stderr, "dvips: Missing DVI file argument (or -f).\n");
2266 fprintf (stderr, "dvips: Try --help for more information.\n");
2267 #else
2268 fprintf (stderr, "xdvipsk: Missing DVI file argument (or -f).\n");
2269 fprintf (stderr, "xdvipsk: Try --help for more information.\n");
2270 #endif /* XDVIPSK */
2271 #else
2272 help(1);
2273 #endif
2274 #ifndef XDVIPSK
2275 exit(1);
2276 #else
2277 dvips_exit(1);
2278 #endif /* XDVIPSK */
2279 }
2280 if (dvifile==NULL) {
2281 error_with_perror("DVI file can't be opened:", iname);
2282 #ifndef XDVIPSK
2283 exit(1);
2284 #else
2285 dvips_exit(1);
2286 #endif /* XDVIPSK */
2287 }
2288 if (fseek(dvifile, 0L, 0) < 0)
2289 error("! DVI file must not be a pipe.");
2290
2291 initcolor();
2292 #ifdef FONTLIB
2293 fliload(); /* read the font libaries */
2294 #endif
2295 #ifdef XDVIPSK
2296 if (!noluatex) {
2297 getotfinfo(fulliname);
2298 LuaMap_cache_init();
2299 srand(time(NULL));
2300 }
2301 usesPSfonts = 0;
2302 usesOTFfonts = 0;
2303 #endif /* XDVIPSK */
2304 /*
2305 * Now we do our main work.
2306 */
2307 swmem += fontmem;
2308 if (maxdrift < 0) {
2309 if (actualdpi <= 599)
2310 maxdrift = actualdpi / 100;
2311 else if (actualdpi < 1199)
2312 maxdrift = actualdpi / 200 + 3;
2313 else
2314 maxdrift = actualdpi / 400 + 6;
2315 }
2316 if (vmaxdrift < 0) {
2317 if (vactualdpi <= 599)
2318 vmaxdrift = vactualdpi / 100;
2319 else if (vactualdpi < 1199)
2320 vmaxdrift = vactualdpi / 200 + 3;
2321 else
2322 vmaxdrift = vactualdpi / 400 + 6;
2323 }
2324 if (dopprescan)
2325 pprescanpages();
2326 #ifdef XDVIPSK
2327 integer maxpages_init = maxpages;
2328 prescanpages(TRUE);
2329 if (fseek(dvifile, 0L, 0) < 0)
2330 error("! DVI file must not be a pipe.");
2331 maxpages = maxpages_init;
2332 prescanpages(FALSE);
2333 if (lua_after_prescan)
2334 run_lua_after_prescan(L);
2335 #else
2336 prescanpages();
2337 #endif /* XDVIPSK */
2338 #if defined MSDOS || defined OS2 || defined(ATARIST)
2339 if (mfjobfile != (FILE*)NULL) {
2340 char answer[5];
2341 fputs("}\n",mfjobfile);
2342 fclose(mfjobfile);
2343 fputs("Exit to make missing fonts now (y/n)? ",stdout);
2344 fgets(answer,4,stdin);
2345 if (*answer=='y' || *answer=='Y')
2346 exit(8); /* exit with errorlevel 8 for emTeX dvidrv */
2347 }
2348 #endif
2349 if (includesfonts)
2350 add_header(IFONTHEADER);
2351 if (usesPSfonts)
2352 add_header(PSFONTHEADER);
2353 #ifdef XDVIPSK
2354 if (usesOTFfonts)
2355 add_header(CIDFONTHEADER);
2356 #endif /* XDVIPSK */
2357 if (usesspecial)
2358 add_header(SPECIALHEADER);
2359 if (usescolor) /* IBM: color */
2360 add_header(COLORHEADER);
2361 #ifdef HPS
2362 if (HPS_FLAG)
2363 add_header(HPSHEADER);
2364 #endif
2365 sects = sections;
2366 totalpages *= collatedcopies;
2367 if (sects == NULL || sects->next == NULL) {
2368 sectioncopies = collatedcopies;
2369 collatedcopies = 1;
2370 } else {
2371 if (! sepfiles)
2372 multiplesects = 1;
2373 }
2374 totalpages *= pagecopies;
2375 if (tryepsf) {
2376 if (paperfmt || landscape || manualfeed ||
2377 collatedcopies > 1 || numcopies > 1 || cropmarks ||
2378 *iname == 0 ||
2379 (totalpages > 1 && !(sepfiles && maxsecsize == 1))) {
2380 error("Can't make it EPSF, sorry");
2381 tryepsf = 0;
2382 }
2383 }
2384 #ifdef HPS
2385 if (HPS_FLAG)
2386 set_bitfile("head.tmp", 0);
2387 #endif
2388 if (! sepfiles) {
2389 initprinter(sections);
2390 outbangspecials();
2391 }
2392
2393 for (i=0; i<collatedcopies; i++) {
2394 sects = sections;
2395 while (sects != NULL) {
2396 if (sepfiles) {
2397 newoutname();
2398 if (! quiet) {
2399 if (prettycolumn + strlen(oname) + 6 > STDOUTSIZE) {
2400 fprintf(stderr, "\n");
2401 prettycolumn = 0;
2402 }
2403 fprintf_str(stderr, "(-> %s) ", oname);
2404 prettycolumn += strlen(oname) + 6;
2405 }
2406 #ifdef HPS
2407 if (HPS_FLAG)
2408 set_bitfile("head.tmp", 0);
2409 #endif
2410 initprinter(sects);
2411 outbangspecials();
2412 #ifndef XDVIPSK
2413 } else if (! quiet) {
2414 if (prettycolumn > STDOUTSIZE) {
2415 fprintf(stderr, "\n");
2416 prettycolumn = 0;
2417 }
2418 fprintf(stderr, ". ");
2419 prettycolumn += 2;
2420 #endif
2421 }
2422 fflush(stderr);
2423 dosection(sects, sectioncopies);
2424 sects = sects->next;
2425 if (sepfiles) {
2426 #ifdef HPS
2427 if (HPS_FLAG)
2428 finish_hps();
2429 #endif
2430 cleanprinter();
2431 }
2432 }
2433 }
2434 freememforpsnames();
2435 if (! sepfiles) {
2436 #ifdef HPS
2437 if (HPS_FLAG)
2438 finish_hps();
2439 #endif
2440 cleanprinter();
2441 }
2442 if (! quiet)
2443 fprintf(stderr, "\n");
2444 #ifdef DEBUG
2445 if (dd(D_MEM)) {
2446 #ifdef SHORTINT
2447 fprintf(stderr, "Total memory allocated: %ld\n", totalalloc);
2448 #else
2449 fprintf(stderr, "Total memory allocated: %d\n", totalalloc);
2450 #endif
2451 }
2452 #endif
2453 #ifndef XDVIPSK
2454 return found_problems ? EXIT_FAILURE : EXIT_SUCCESS;
2455 #else
2456 dvips_exit(0);
2457 #endif /* XDVIPSK */
2458 /*NOTREACHED*/
2459 #ifdef XDVIPSK
2460 return EXIT_SUCCESS;
2461 #endif /* XDVIPSK */
2462 }
2463 #ifdef VMS
2464 #include "[.vms]vmscli.c"
2465 #endif
2466
2467 #ifdef VMCMS /* IBM: VM/CMS */
2468 #include "dvipscms.h"
2469 #endif
2470
2471 #ifdef MVSXA /* IBM: MVS/XA */
2472 #include "dvipsmvs.h"
2473 #endif