1 /* texmf.h: Main include file for TeX and Metafont in C. This file is
2    included by {tex,mf}d.h, which is the first include in the C files
3    output by web2c.  */
4 
5 #include "cpascal.h"
6 #include <kpathsea/c-pathch.h> /* for IS_DIR_SEP, used in the change files */
7 #include <kpathsea/tex-make.h> /* for kpse_make_tex_discard_errors */
8 
9 #ifdef XeTeX
10 #ifdef XETEX_MAC
11 /* include this here to avoid conflict between clang's emmintrin.h and
12  * texmfmem.h. Should be removed once a fixed clang is widely available
13  * http://llvm.org/bugs/show_bug.cgi?id=14964 */
14 #include <ApplicationServices/ApplicationServices.h>
15 #endif
16 /* added typedefs for unicodefile and voidpointer */
17 #define XETEX_UNICODE_FILE_DEFINED	1
18 typedef struct {
19   FILE *f;
20   long  savedChar;
21   short skipNextLF;
22   short encodingMode;
23   void *conversionData;
24 } UFILE;
25 typedef UFILE* unicodefile;
26 
27 typedef void* voidpointer;
28 #endif
29 
30 /* If we have these macros, use them, as they provide a better guide to
31    the endianess when cross-compiling. */
32 #if defined (BYTE_ORDER) && defined (BIG_ENDIAN) && defined (LITTLE_ENDIAN)
33 #ifdef WORDS_BIGENDIAN
34 #undef WORDS_BIGENDIAN
35 #endif
36 #if BYTE_ORDER == BIG_ENDIAN
37 #define WORDS_BIGENDIAN
38 #endif
39 #endif
40 /* More of the same, but now NeXT-specific. */
41 #ifdef NeXT
42 #ifdef WORDS_BIGENDIAN
43 #undef WORDS_BIGENDIAN
44 #endif
45 #ifdef __BIG_ENDIAN__
46 #define WORDS_BIGENDIAN
47 #endif
48 #endif
49 
50 /* Some things are the same except for the name.  */
51 #ifdef TeX
52 #if defined (pdfTeX)
53 #define TEXMFPOOLNAME "pdftex.pool"
54 #define TEXMFENGINENAME "pdftex"
55 #elif defined (eTeX)
56 #define TEXMFPOOLNAME "etex.pool"
57 #define TEXMFENGINENAME "etex"
58 #elif defined (XeTeX)
59 #define TEXMFPOOLNAME "xetex.pool"
60 #define TEXMFENGINENAME "xetex"
61 #elif defined (Aleph)
62 #define TEXMFPOOLNAME "aleph.pool"
63 #define TEXMFENGINENAME "aleph"
64 #elif defined (pTeX)
65 #define TEXMFPOOLNAME "ptex.pool"
66 #define TEXMFENGINENAME "ptex"
67 #include "ptexdir/kanji.h"
68 #elif defined (epTeX)
69 #define TEXMFPOOLNAME "eptex.pool"
70 #define TEXMFENGINENAME "eptex"
71 #include "ptexdir/kanji.h"
72 #elif defined (upTeX)
73 #define TEXMFPOOLNAME "uptex.pool"
74 #define TEXMFENGINENAME "uptex"
75 #include "uptexdir/kanji.h"
76 #elif defined (eupTeX)
77 #define TEXMFPOOLNAME "euptex.pool"
78 #define TEXMFENGINENAME "euptex"
79 #include "uptexdir/kanji.h"
80 #else
81 #define TEXMFPOOLNAME "tex.pool"
82 #define TEXMFENGINENAME "tex"
83 #endif
84 #define DUMP_FILE fmtfile
85 #define DUMP_FORMAT kpse_fmt_format
86 #define writedvi WRITE_OUT
87 #define flushdvi flush_out
88 #define OUT_FILE dvifile
89 #define OUT_BUF dvibuf
90 #endif /* TeX */
91 #ifdef MF
92 #define TEXMFPOOLNAME "mf.pool"
93 #define TEXMFENGINENAME "metafont"
94 #define DUMP_FILE basefile
95 #define DUMP_FORMAT kpse_base_format
96 #define writegf WRITE_OUT
97 #define OUT_FILE gffile
98 #define OUT_BUF gfbuf
99 #endif /* MF */
100 
101 /* Restore underscores.  */
102 #define dumpname dump_name
103 #define kpsedvipsconfigformat kpse_dvips_config_format
104 #define kpsefontmapformat kpse_fontmap_format
105 #define kpsemfpoolformat kpse_mfpool_format
106 #define kpsempformat kpse_mp_format
107 #define kpsemppoolformat kpse_mppool_format
108 #define kpsetexpoolformat kpse_texpool_format
109 #define kpsetexformat kpse_tex_format
110 
111 /* Hacks for TeX that are better not to #ifdef, see lib/openclose.c.  */
112 extern int tfmtemp, texinputtype;
113 
114 /* pdfTeX routines also used for e-pTeX and e-upTeX */
115 #if defined (pdfTeX) || defined (epTeX) || defined (eupTeX)
116 extern char start_time_str[];
117 extern void pdftex_fail(const char *fmt, ...);
118 extern void initstarttime(void);
119 extern char *makecstring(integer s);
120 extern char *makecfilename(integer s);
121 extern void getcreationdate(void);
122 extern void getfilemoddate(integer s);
123 extern void getfilesize(integer s);
124 extern void getfiledump(integer s, int offset, int length);
125 #endif
126 
127 /* pdftex etc. except for tex use these for pipe support */
128 #if defined(TeX) && !defined(onlyTeX)
129 extern boolean open_in_or_pipe (FILE **, int, const_string fopen_mode);
130 extern boolean open_out_or_pipe (FILE **, const_string fopen_mode);
131 extern void close_file_or_pipe (FILE *);
132 #define ENABLE_PIPES 1
133 #else
134 #define ENABLE_PIPES 0
135 #endif
136 
137 /* Executing shell commands.  */
138 extern int runsystem (const char *cmd);
139 
140 /* The entry point.  */
141 extern void maininit (int ac, string *av);
142 #if defined(WIN32) && !defined(__MINGW32__) && defined(DLLPROC)
143 extern __declspec(dllexport) int DLLPROC (int ac, string *av);
144 #else
145 #undef DLLPROC
146 #endif
147 
148 /* All but the Omega family use this. */
149 #if !defined(Aleph)
150 extern void readtcxfile (void);
151 extern string translate_filename;
152 #define translatefilename translate_filename
153 #endif
154 
155 #ifdef TeX
156 /* The type `glueratio' should be a floating point type which won't
157    unnecessarily increase the size of the memoryword structure.  This is
158    the basic requirement.  On most machines, if you're building a
159    normal-sized TeX, then glueratio must probably meet the following
160    restriction: sizeof(glueratio) <= sizeof(integer).  Usually, then,
161    glueratio must be `float'.  But if you build a big TeX, you can (on
162    most machines) and should make it `double' to avoid loss of precision
163    and conversions to and from double during calculations.  (All this
164    also goes for Metafont.)  Furthermore, if you have enough memory, it
165    won't hurt to have this defined to be `double' for running the
166    trip/trap tests.
167 
168    This type is set automatically to `float' by configure if a small TeX
169    is built.  */
170 #ifndef GLUERATIO_TYPE
171 #define GLUERATIO_TYPE double
172 #endif
173 typedef GLUERATIO_TYPE glueratio;
174 
175 #if defined(__DJGPP__) && defined (IPC)
176 #undef IPC
177 #endif
178 
179 #ifdef IPC
180 extern void ipcpage (int);
181 #endif /* IPC */
182 #endif /* TeX */
183 
184 /* How to flush the DVI file.  */
185 #define flush_out() fflush (OUT_FILE)
186 
187 /* Used to write to a TFM file.  */
188 #define put2bytes(f, h) do { \
189     integer v = (integer) (h); putbyte (v >> 8, f);  putbyte (v & 0xff, f); \
190   } while (0)
191 #define put4bytes(f, w) do { \
192     integer v = (integer) (w); \
193     putbyte (v >> 24, f); putbyte (v >> 16, f); \
194     putbyte (v >> 8, f);  putbyte (v & 0xff, f); \
195   } while (0)
196 
197 /* Read a line of input as quickly as possible.  */
198 #define	inputln(stream, flag) input_line (stream)
199 #ifdef XeTeX
200 extern boolean input_line (UFILE *);
201 #else
202 extern boolean input_line (FILE *);
203 #endif
204 
205 /* This routine has to return four values.  */
206 #define	dateandtime(i,j,k,l) get_date_and_time (&(i), &(j), &(k), &(l))
207 extern void get_date_and_time (integer *, integer *, integer *, integer *);
208 
209 #if defined(pdfTeX)
210 /* Get high-res time info. */
211 #define secondsandmicros(i,j) get_seconds_and_micros (&(i), &(j))
212 extern void get_seconds_and_micros (integer *, integer *);
213 #endif
214 
215 /* Copy command-line arguments into the buffer, despite the name.  */
216 extern void topenin (void);
217 
218 /* Can't prototype this since it uses poolpointer and ASCIIcode, which
219    are defined later in mfd.h, and mfd.h uses stuff from here.  */
220 /* Therefore the department of ugly hacks decided to move this declaration
221    to the *coerce.h files. */
222 /* extern void calledit (); */
223 
224 /* These defines reroute the file i/o calls to the new pipe-enabled
225    functions in texmfmp.c*/
226 
227 #if ENABLE_PIPES
228 #undef aopenin
229 #undef aopenout
230 #undef aclose
231 #define aopenin(f,p)  open_in_or_pipe(&(f),p,FOPEN_RBIN_MODE)
232 #define aopenout(f)   open_out_or_pipe(&(f),FOPEN_W_MODE)
233 #define aclose(f)     close_file_or_pipe(f)
234 #endif
235 
236 /* `bopenin' (and out) is used only for reading (and writing) .tfm
237    files; `wopenin' (and out) only for dump files.  The filenames are
238    passed in as a global variable, `nameoffile'.  */
239 #define bopenin(f)	open_input (&(f), kpse_tfm_format, FOPEN_RBIN_MODE)
240 #define ocpopenin(f)	open_input (&(f), kpse_ocp_format, FOPEN_RBIN_MODE)
241 #define ofmopenin(f)	open_input (&(f), kpse_ofm_format, FOPEN_RBIN_MODE)
242 
243 #define bopenout(f)	open_output (&(f), FOPEN_WBIN_MODE)
244 #define bclose		aclose
245 #ifdef XeTeX
246 /* f is declared as gzFile, but we temporarily use it for a FILE *
247    so that we can use the standard open calls */
248 #define wopenin(f)	(open_input ((FILE**)&(f), DUMP_FORMAT, FOPEN_RBIN_MODE) \
249 						&& (f = gzdopen(fileno((FILE*)f), FOPEN_RBIN_MODE)))
250 #define wopenout(f)	(open_output ((FILE**)&(f), FOPEN_WBIN_MODE) \
251 						&& (f = gzdopen(fileno((FILE*)f), FOPEN_WBIN_MODE)) \
252 						&& (gzsetparams(f, 1, Z_DEFAULT_STRATEGY) == Z_OK))
253 #define wclose(f)	gzclose(f)
254 #else
255 #define wopenin(f)	open_input (&(f), DUMP_FORMAT, FOPEN_RBIN_MODE)
256 #define wopenout	bopenout
257 #define wclose		aclose
258 #endif
259 
260 #ifdef XeTeX
261 #if ENABLE_PIPES
262 extern boolean u_open_in_or_pipe(unicodefile* f, integer filefmt, const_string fopen_mode, integer mode, integer encodingData);
263 #define uopenin(f,p,m,d) u_open_in_or_pipe(&(f), p, FOPEN_RBIN_MODE, m, d)
264 #else
265 #define uopenin(f,p,m,d) u_open_in(&(f), p, FOPEN_RBIN_MODE, m, d)
266 #endif
267 #endif
268 
269 /* Used in tex.ch (section 1338) to get a core dump in debugging mode.  */
270 #ifdef unix
271 #define dumpcore abort
272 #else
273 #define dumpcore uexit (1)
274 #endif
275 
276 #ifdef MF
277 extern boolean initscreen (void);
278 extern void updatescreen (void);
279 /* Can't prototype these for same reason as `calledit' above.  */
280 #if 0  /* Therefore the real declaration is found in the coerce.h files.  */
281 extern void blankrectangle (/*screencol, screencol, screenrow, screenrow*/);
282 extern void paintrow (/*screenrow, pixelcolor, transspec, screencol*/);
283 #endif
284 #endif /* MF */
285 
286 
287 /* (Un)dumping.  These are called from the change file.  */
288 #define	dumpthings(base, len) \
289   do_dump ((char *) &(base), sizeof (base), (int) (len), DUMP_FILE)
290 #define	undumpthings(base, len) \
291   do_undump ((char *) &(base), sizeof (base), (int) (len), DUMP_FILE)
292 
293 #ifndef PRIdPTR
294 #define PRIdPTR "ld"
295 #endif
296 #ifndef PRIxPTR
297 #define PRIxPTR "lx"
298 #endif
299 
300 /* Like do_undump, but check each value against LOW and HIGH.  The
301    slowdown isn't significant, and this improves the chances of
302    detecting incompatible format files.  In fact, Knuth himself noted
303    this problem with Web2c some years ago, so it seems worth fixing.  We
304    can't make this a subroutine because then we lose the type of BASE.  */
305 #define undumpcheckedthings(low, high, base, len)			\
306   do {                                                                  \
307     unsigned i;                                                         \
308     undumpthings (base, len);                                           \
309     for (i = 0; i < (len); i++) {                                       \
310       if ((&(base))[i] < (low) || (&(base))[i] > (high)) {              \
311         FATAL5 ("Item %u (=%" PRIdPTR ") of .fmt array at %" PRIxPTR    \
312                 " <%" PRIdPTR " or >%" PRIdPTR,                         \
313                 i, (uintptr_t) (&(base))[i], (uintptr_t) &(base),       \
314                 (uintptr_t) low, (uintptr_t) high);                     \
315       }                                                                 \
316     }									\
317   } while (0)
318 
319 /* Like undump_checked_things, but only check the upper value. We use
320    this when the base type is unsigned, and thus all the values will be
321    greater than zero by definition.  */
322 #define undumpuppercheckthings(high, base, len)				\
323   do {                                                                  \
324     unsigned i;                                                         \
325     undumpthings (base, len);                                           \
326     for (i = 0; i < (len); i++) {                                       \
327       if ((&(base))[i] > (high)) {              			\
328         FATAL4 ("Item %u (=%" PRIdPTR ") of .fmt array at %" PRIxPTR    \
329                 " >%" PRIdPTR,                                          \
330                 i, (uintptr_t) (&(base))[i], (uintptr_t) &(base),       \
331                 (uintptr_t) high);                         		\
332       }                                                                 \
333     }									\
334   } while (0)
335 
336 /* We define the routines to do the actual work in texmf.c.  */
337 #ifdef XeTeX
338 #include <zlib.h>
339 extern void do_dump (char *, int, int, gzFile);
340 extern void do_undump (char *, int, int, gzFile);
341 #else
342 extern void do_dump (char *, int, int, FILE *);
343 extern void do_undump (char *, int, int, FILE *);
344 #endif
345 
346 /* Use the above for all the other dumping and undumping.  */
347 #define generic_dump(x) dumpthings (x, 1)
348 #define generic_undump(x) undumpthings (x, 1)
349 
350 #define dumpwd   generic_dump
351 #define dumphh   generic_dump
352 #define dumpqqqq generic_dump
353 #define undumpwd   generic_undump
354 #define undumphh   generic_undump
355 #define	undumpqqqq generic_undump
356 
357 /* `dump_int' is called with constant integers, so we put them into a
358    variable first.  */
359 #define	dumpint(x)							\
360   do									\
361     {									\
362       integer x_val = (x);						\
363       generic_dump (x_val);						\
364     }									\
365   while (0)
366 
367 /* web2c/regfix puts variables in the format file loading into
368    registers.  Some compilers aren't willing to take addresses of such
369    variables.  So we must kludge.  */
370 #if defined(REGFIX) || defined(WIN32)
371 #define undumpint(x)							\
372   do									\
373     {									\
374       integer x_val;							\
375       generic_undump (x_val);						\
376       x = x_val;							\
377     }									\
378   while (0)
379 #else
380 #define	undumpint generic_undump
381 #endif
382 
383 /* Handle SyncTeX, if requested */
384 #if defined(TeX)
385 # if defined(__SyncTeX__)
386 #  include "synctexdir/synctex-common.h"
387 extern char *generic_synctex_get_current_name(void);
388 # endif
389 #endif
390 
391