sync with experimental
[luatex.git] / source / texk / web2c / luatexdir / tex / texfileio.w
blobc222eef975b8614628bff85e2b159760299a3d07
1 % texfileio.w
3 % Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
7 % LuaTeX is free software; you can redistribute it and/or modify it under
8 % the terms of the GNU General Public License as published by the Free
9 % Software Foundation; either version 2 of the License, or (at your
10 % option) any later version.
12 % LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 % ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 % FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 % License for more details.
17 % You should have received a copy of the GNU General Public License along
18 % with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20 @ @c
23 #include "ptexlib.h"
25 #include <string.h>
26 #include <kpathsea/absolute.h>
28 @ The bane of portability is the fact that different operating systems treat
29 input and output quite differently, perhaps because computer scientists
30 have not given sufficient attention to this problem. People have felt somehow
31 that input and output are not part of ``real'' programming. Well, it is true
32 that some kinds of programming are more fun than others. With existing
33 input/output conventions being so diverse and so messy, the only sources of
34 joy in such parts of the code are the rare occasions when one can find a
35 way to make the program a little less bad than it might have been. We have
36 two choices, either to attack I/O now and get it over with, or to postpone
37 I/O until near the end. Neither prospect is very attractive, so let's
38 get it over with.
40 The basic operations we need to do are (1)~inputting and outputting of
41 text, to or from a file or the user's terminal; (2)~inputting and
42 outputting of eight-bit bytes, to or from a file; (3)~instructing the
43 operating system to initiate (``open'') or to terminate (``close'') input or
44 output from a specified file; (4)~testing whether the end of an input
45 file has been reached.
47 \TeX\ needs to deal with two kinds of files.
48 We shall use the term |alpha_file| for a file that contains textual data,
49 and the term |byte_file| for a file that contains eight-bit binary information.
50 These two types turn out to be the same on many computers, but
51 sometimes there is a significant distinction, so we shall be careful to
52 distinguish between them. Standard protocols for transferring
53 such files from computer to computer, via high-speed networks, are
54 now becoming available to more and more communities of users.
56 The program actually makes use also of a third kind of file, called a
57 |word_file|, when dumping and reloading base information for its own
58 initialization. We shall define a word file later; but it will be possible
59 for us to specify simple operations on word files before they are defined.
61 @ We finally did away with |nameoffile| and |namelength|, but the variables
62 have to be kept otherwise there will be link errors from |openclose.c| in
63 the web2c library
66 char *nameoffile;
67 int namelength;
70 @ When input files are opened via a callback, they will also be read using
71 callbacks. for that purpose, the |open_read_file_callback| returns an
72 integer to uniquely identify a callback table. This id replaces the file
73 point |f| in this case, because the input does not have to be a file
74 in the traditional sense.
76 Signalling this fact is achieved by having two arrays of integers.
79 int *input_file_callback_id;
80 int read_file_callback_id[17];
82 @ Handle -output-directory.
84 We assume that it is OK to look here first. Possibly it
85 would be better to replace lookups in "." with lookups in the
86 |output_directory| followed by "." but to do this requires much more
87 invasive surgery in libkpathsea.
90 static char *find_in_output_directory(const char *s)
92 if (output_directory && !kpse_absolute_p(s, false)) {
93 FILE *f_ptr;
94 char *ftemp = concat3(output_directory, DIR_SEP_STRING, s);
95 f_ptr = fopen(ftemp, "rb"); /* this code is used for input files only */
96 if (f_ptr) {
97 fclose(f_ptr);
98 return ftemp;
99 } else {
100 free(ftemp);
104 return NULL;
107 @ find an \.{\\input} or \.{\\read} file. |n| differentiates between those case.
110 int kpse_available(const char *m) {
111 if (!kpse_init) {
112 fprintf(stdout,"missing kpse replacement callback '%s', quitting\n",m);
113 exit(1);
115 return 1 ;
118 char *luatex_find_read_file(const char *s, int n, int callback_index)
120 char *ftemp = NULL;
121 int callback_id = callback_defined(callback_index);
122 if (callback_id > 0) {
123 (void) run_callback(callback_id, "dS->R", n, s, &ftemp);
124 } else if (kpse_available("find_read_file")) {
125 /* use kpathsea here */
126 ftemp = find_in_output_directory(s);
127 if (!ftemp)
128 ftemp = kpse_find_file(s, kpse_tex_format, 1);
130 if (ftemp) {
131 if (fullnameoffile)
132 free(fullnameoffile);
133 fullnameoffile = xstrdup(ftemp);
135 return ftemp;
138 @ find other files types
140 char *luatex_find_file(const char *s, int callback_index)
142 char *ftemp = NULL;
143 int callback_id = callback_defined(callback_index);
144 if (callback_id > 0) {
145 (void) run_callback(callback_id, "S->R", s, &ftemp);
147 } else if (kpse_available("find_read_file")) {
148 /* use kpathsea here */
149 switch (callback_index) {
150 case find_enc_file_callback:
151 ftemp = kpse_find_file(s, kpse_enc_format, 0);
152 break;
153 case find_sfd_file_callback:
154 ftemp = kpse_find_file(s, kpse_sfd_format, 0);
155 break;
156 case find_map_file_callback:
157 ftemp = kpse_find_file(s, kpse_fontmap_format, 0);
158 break;
159 case find_type1_file_callback:
160 ftemp = kpse_find_file(s, kpse_type1_format, 0);
161 break;
162 case find_truetype_file_callback:
163 ftemp = kpse_find_file(s, kpse_truetype_format, 0);
164 break;
165 case find_opentype_file_callback:
166 ftemp = kpse_find_file(s, kpse_opentype_format, 0);
167 if (ftemp == NULL)
168 ftemp = kpse_find_file(s, kpse_truetype_format, 0);
169 break;
170 case find_data_file_callback:
171 ftemp = find_in_output_directory(s);
172 if (!ftemp)
173 ftemp = kpse_find_file(s, kpse_tex_format, 0);
174 break;
175 case find_font_file_callback:
176 ftemp = kpse_find_file(s, kpse_ofm_format, 0);
177 if (ftemp == NULL)
178 ftemp = kpse_find_file(s, kpse_tfm_format, 0);
179 break;
180 case find_vf_file_callback:
181 ftemp = kpse_find_file(s, kpse_ovf_format, 0);
182 if (ftemp == NULL)
183 ftemp = kpse_find_file(s, kpse_vf_format, 0);
184 break;
185 case find_cidmap_file_callback:
186 ftemp = kpse_find_file(s, kpse_cid_format, 0);
187 break;
188 default:
189 printf
190 ("luatex_find_file(): do not know how to handle file %s of type %d\n",
191 s, callback_index);
192 break;
195 return ftemp;
199 @ LuaTeX used to have private functions for these that did not use kpathsea,
200 but since the file paranoia tests have to come from kpathsea anyway, that is no
201 longer useful. The only downside to using luatex is that if one wants to disable
202 kpathsea via the Lua startup script, it is now an absolute requirement that all
203 file discovery callbacks are specified. Just using the find_read_file, but not
204 setting open_read_file, for example, does not work any more if kpathsea is not
205 to be used at all.
208 #define openoutnameok(A) kpse_out_name_ok (A)
209 #define openinnameok(A) kpse_in_name_ok (A)
211 @ Open an input file F, using the kpathsea format FILEFMT and passing
212 |FOPEN_MODE| to fopen. The filename is in `fn'. We return whether or
213 not the open succeeded.
216 boolean
217 luatex_open_input(FILE ** f_ptr, const char *fn, int filefmt,
218 const_string fopen_mode, boolean must_exist)
220 string fname = NULL;
221 /* We havent found anything yet. */
222 *f_ptr = NULL;
223 if (fullnameoffile)
224 free(fullnameoffile);
225 fullnameoffile = NULL;
226 fname = kpse_find_file(fn, (kpse_file_format_type) filefmt, must_exist);
227 if (fname) {
228 fullnameoffile = xstrdup(fname);
229 /* If we found the file in the current directory, don't leave
230 the `./' at the beginning of `fn', since it looks
231 dumb when `tex foo' says `(./foo.tex ... )'. On the other
232 hand, if the user said `tex ./foo', and that's what we
233 opened, then keep it -- the user specified it, so we
234 shouldn't remove it. */
235 if (fname[0] == '.' && IS_DIR_SEP(fname[1])
236 && (fn[0] != '.' || !IS_DIR_SEP(fn[1]))) {
237 unsigned i = 0;
238 while (fname[i + 2] != 0) {
239 fname[i] = fname[i + 2];
240 i++;
242 fname[i] = 0;
244 /* This fopen is not allowed to fail. */
245 *f_ptr = xfopen(fname, fopen_mode);
247 if (*f_ptr) {
248 recorder_record_input(fname);
250 return *f_ptr != NULL;
253 @ @c
254 boolean luatex_open_output(FILE ** f_ptr, const char *fn,
255 const_string fopen_mode)
257 char *fname;
258 boolean absolute = kpse_absolute_p(fn, false);
260 /* If we have an explicit output directory, use it. */
261 if (output_directory && !absolute) {
262 fname = concat3(output_directory, DIR_SEP_STRING, fn);
263 } else {
264 fname = xstrdup(fn);
267 /* Is the filename openable as given? */
268 *f_ptr = fopen(fname, fopen_mode);
270 if (!*f_ptr) {
271 /* Can't open as given. Try the envvar. */
272 string texmfoutput = kpse_var_value("TEXMFOUTPUT");
274 if (texmfoutput && *texmfoutput && !absolute) {
275 fname = concat3(texmfoutput, DIR_SEP_STRING, fn);
276 *f_ptr = fopen(fname, fopen_mode);
279 if (*f_ptr) {
280 recorder_record_output(fname);
282 free(fname);
283 return *f_ptr != NULL;
287 @ @c
288 boolean lua_a_open_in(alpha_file * f, char *fn, int n)
290 int k;
291 char *fnam; /* string returned by find callback */
292 int callback_id;
293 boolean ret = true; /* return value */
294 boolean file_ok = true; /* the status so far */
295 if (n == 0) {
296 input_file_callback_id[iindex] = 0;
297 } else {
298 read_file_callback_id[n] = 0;
300 if (*fn == '|')
301 fnam = fn;
302 else
303 fnam = luatex_find_read_file(fn, n, find_read_file_callback);
304 if (!fnam)
305 return false;
306 callback_id = callback_defined(open_read_file_callback);
307 if (callback_id > 0) {
308 k = run_and_save_callback(callback_id, "S->", fnam);
309 if (k > 0) {
310 ret = true;
311 if (n == 0)
312 input_file_callback_id[iindex] = k;
313 else
314 read_file_callback_id[n] = k;
315 } else {
316 file_ok = false; /* read failed */
318 } else { /* no read callback */
319 if (openinnameok(fnam)) {
320 ret =
321 open_in_or_pipe(f, fnam, kpse_tex_format, FOPEN_RBIN_MODE,
322 (n == 0 ? true : false));
323 } else {
324 file_ok = false; /* open failed */
327 if (!file_ok) {
328 ret = false;
330 return ret;
334 @ @c
335 boolean lua_a_open_out(alpha_file * f, char *fn, int n)
337 boolean test;
338 str_number fnam;
339 int callback_id;
340 boolean ret = false;
341 callback_id = callback_defined(find_write_file_callback);
342 if (callback_id > 0) {
343 fnam = 0;
344 test = run_callback(callback_id, "dS->s", n, fn, &fnam);
345 if ((test) && (fnam != 0) && (str_length(fnam) > 0)) {
346 /* There is no message here because if that is needed the macro package */
347 /* should do that in the callback code. As elsewhere, messaging is left */
348 /* to lua then. */
349 ret = open_outfile(f, fn, FOPEN_W_MODE);
351 } else {
352 if (openoutnameok(fn)) {
353 if (n > 0 && selector != term_only) {
354 /* This message to the log is for downward compatibility with other tex's */
355 /* as there are scripts out there that act on this message. An alternative */
356 /* is to let a macro package write an explicit message. */
357 fprintf(log_file,"\n\\openout%i = %s\n",n-1,fn);
359 ret = open_out_or_pipe(f, fn, FOPEN_W_MODE);
362 return ret;
366 @ @c
367 boolean lua_b_open_out(alpha_file * f, char *fn)
369 boolean test;
370 str_number fnam;
371 int callback_id;
372 boolean ret = false;
373 callback_id = callback_defined(find_output_file_callback);
374 if (callback_id > 0) {
375 fnam = 0;
376 test = run_callback(callback_id, "S->s", fn, &fnam);
377 if ((test) && (fnam != 0) && (str_length(fnam) > 0)) {
378 ret = open_outfile(f, fn, FOPEN_WBIN_MODE);
380 } else {
381 if (openoutnameok(fn)) {
382 ret = luatex_open_output(f, fn, FOPEN_WBIN_MODE);
385 return ret;
388 @ @c
389 void lua_a_close_in(alpha_file f, int n)
390 { /* close a text file */
391 int callback_id;
392 if (n == 0)
393 callback_id = input_file_callback_id[iindex];
394 else
395 callback_id = read_file_callback_id[n];
396 if (callback_id > 0) {
397 run_saved_callback(callback_id, "close", "->");
398 destroy_saved_callback(callback_id);
399 if (n == 0)
400 input_file_callback_id[iindex] = 0;
401 else
402 read_file_callback_id[n] = 0;
403 } else {
404 close_file_or_pipe(f);
408 @ @c
409 void lua_a_close_out(alpha_file f)
410 { /* close a text file */
411 close_file_or_pipe(f);
415 @ Binary input and output are done with C's ordinary
416 procedures, so we don't have to make any other special arrangements for
417 binary~I/O. Text output is also easy to do with standard routines.
418 The treatment of text input is more difficult, however, because
419 of the necessary translation to |ASCII_code| values.
420 \TeX's conventions should be efficient, and they should
421 blend nicely with the user's operating environment.
423 Input from text files is read one line at a time, using a routine called
424 |lua_input_ln|. This function is defined in terms of global variables called
425 |buffer|, |first|, and |last| that will be described in detail later; for
426 now, it suffices for us to know that |buffer| is an array of |ASCII_code|
427 values, and that |first| and |last| are indices into this array
428 representing the beginning and ending of a line of text.
431 packed_ASCII_code *buffer; /* lines of characters being read */
432 int first; /* the first unused position in |buffer| */
433 int last; /* end of the line just input to |buffer| */
434 int max_buf_stack; /* largest index used in |buffer| */
437 @ The |lua_input_ln| function brings the next line of input from the specified
438 file into available positions of the buffer array and returns the value
439 |true|, unless the file has already been entirely read, in which case it
440 returns |false| and sets |last:=first|. In general, the |ASCII_code|
441 numbers that represent the next line of the file are input into
442 |buffer[first]|, |buffer[first+1]|, \dots, |buffer[last-1]|; and the
443 global variable |last| is set equal to |first| plus the length of the
444 line. Trailing blanks are removed from the line; thus, either |last=first|
445 (in which case the line was entirely blank) or |buffer[last-1]<>" "|.
447 An overflow error is given, however, if the normal actions of |lua_input_ln|
448 would make |last>=buf_size|; this is done so that other parts of \TeX\
449 can safely look at the contents of |buffer[last+1]| without overstepping
450 the bounds of the |buffer| array. Upon entry to |lua_input_ln|, the condition
451 |first<buf_size| will always hold, so that there is always room for an
452 ``empty'' line.
454 The variable |max_buf_stack|, which is used to keep track of how large
455 the |buf_size| parameter must be to accommodate the present job, is
456 also kept up to date by |lua_input_ln|.
458 If the |bypass_eoln| parameter is |true|, |lua_input_ln| will do a |get|
459 before looking at the first character of the line; this skips over
460 an |eoln| that was in |f^|. The procedure does not do a |get| when it
461 reaches the end of the line; therefore it can be used to acquire input
462 from the user's terminal as well as from ordinary text files.
464 Since the inner loop of |lua_input_ln| is part of \TeX's ``inner loop''---each
465 character of input comes in at this place---it is wise to reduce system
466 overhead by making use of special routines that read in an entire array
467 of characters at once, if such routines are available.
468 @^inner loop@>
471 boolean lua_input_ln(alpha_file f, int n, boolean bypass_eoln)
473 boolean lua_result;
474 int last_ptr;
475 int callback_id;
476 (void) bypass_eoln; /* todo: variable can be removed */
477 if (n == 0)
478 callback_id = input_file_callback_id[iindex];
479 else
480 callback_id = read_file_callback_id[n];
481 if (callback_id > 0) {
482 last = first;
483 last_ptr = first;
484 lua_result =
485 run_saved_callback(callback_id, "reader", "->l", &last_ptr);
486 if ((lua_result == true) && (last_ptr != 0)) {
487 last = last_ptr;
488 if (last > max_buf_stack)
489 max_buf_stack = last;
490 } else {
491 lua_result = false;
493 } else {
494 lua_result = input_ln(f, bypass_eoln);
496 if (lua_result == true) {
497 /* Fix up the input buffer using callbacks */
498 if (last >= first) {
499 callback_id = callback_defined(process_input_buffer_callback);
500 if (callback_id > 0) {
501 last_ptr = first;
502 lua_result =
503 run_callback(callback_id, "l->l", (last - first),
504 &last_ptr);
505 if ((lua_result == true) && (last_ptr != 0)) {
506 last = last_ptr;
507 if (last > max_buf_stack)
508 max_buf_stack = last;
512 return true;
514 return false;
518 @ We need a special routine to read the first line of \TeX\ input from
519 the user's terminal. This line is different because it is read before we
520 have opened the transcript file; there is sort of a ``chicken and
521 egg'' problem here. If the user types `\.{\\input paper}' on the first
522 line, or if some macro invoked by that line does such an \.{\\input},
523 the transcript file will be named `\.{paper.log}'; but if no \.{\\input}
524 commands are performed during the first line of terminal input, the transcript
525 file will acquire its default name `\.{texput.log}'. (The transcript file
526 will not contain error messages generated by the first line before the
527 first \.{\\input} command.)
528 @.texput@>
530 The first line is special also because it may be read before \TeX\ has
531 input a format file. In such cases, normal error messages cannot yet
532 be given. The following code uses concepts that will be explained later.
534 @ Different systems have different ways to get started. But regardless of
535 what conventions are adopted, the routine that initializes the terminal
536 should satisfy the following specifications:
538 \yskip\textindent{1)}It should open file |term_in| for input from the
539 terminal. (The file |term_out| will already be open for output to the
540 terminal.)
542 \textindent{2)}If the user has given a command line, this line should be
543 considered the first line of terminal input. Otherwise the
544 user should be prompted with `\.{**}', and the first line of input
545 should be whatever is typed in response.
547 \textindent{3)}The first line of input, which might or might not be a
548 command line, should appear in locations |first| to |last-1| of the
549 |buffer| array.
551 \textindent{4)}The global variable |loc| should be set so that the
552 character to be read next by \TeX\ is in |buffer[loc]|. This
553 character should not be blank, and we should have |loc<last|.
555 \yskip\noindent(It may be necessary to prompt the user several times
556 before a non-blank line comes in. The prompt is `\.{**}' instead of the
557 later `\.*' because the meaning is slightly different: `\.{\\input}' need
558 not be typed immediately after~`\.{**}'.)
560 The following program does the required initialization.
561 Iff anything has been specified on the command line, then |t_open_in|
562 will return with |last > first|.
563 @^system dependencies@>
566 boolean init_terminal(void)
567 { /* gets the terminal input started */
568 t_open_in();
569 if (last > first) {
570 iloc = first;
571 while ((iloc < last) && (buffer[iloc] == ' '))
572 incr(iloc);
573 if (iloc < last) {
574 return true;
577 while (1) {
578 wake_up_terminal();
579 fputs("**", term_out);
580 update_terminal();
581 if (!input_ln(term_in, true)) {
582 /* this shouldn't happen */
583 fputs("\n! End of file on the terminal... why?\n", term_out);
584 return false;
586 iloc = first;
587 while ((iloc < last) && (buffer[iloc] == ' '))
588 incr(iloc);
589 if (iloc < last) {
590 return true; /* return unless the line was all blank */
592 fputs("Please type the name of your input file.\n", term_out);
597 @ Here is a procedure that asks the user to type a line of input,
598 assuming that the |selector| setting is either |term_only| or |term_and_log|.
599 The input is placed into locations |first| through |last-1| of the
600 |buffer| array, and echoed on the transcript file if appropriate.
603 void term_input(void)
604 { /* gets a line from the terminal */
605 int k; /* index into |buffer| */
606 update_terminal(); /* now the user sees the prompt for sure */
607 if (!input_ln(term_in, true))
608 fatal_error("End of file on the terminal!");
609 term_offset = 0; /* the user's line ended with \.{<return>} */
610 decr(selector); /* prepare to echo the input */
611 if (last != first) {
612 for (k = first; k <= last - 1; k++)
613 print_char(buffer[k]);
615 print_ln();
616 incr(selector); /* restore previous status */
620 @ It's time now to fret about file names. Besides the fact that different
621 operating systems treat files in different ways, we must cope with the
622 fact that completely different naming conventions are used by different
623 groups of people. The following programs show what is required for one
624 particular operating system; similar routines for other systems are not
625 difficult to devise.
626 @^fingers@>
627 @^system dependencies@>
629 \TeX\ assumes that a file name has three parts: the name proper; its
630 ``extension''; and a ``file area'' where it is found in an external file
631 system. The extension of an input file or a write file is assumed to be
632 `\.{.tex}' unless otherwise specified; it is `\.{.log}' on the
633 transcript file that records each run of \TeX; it is `\.{.tfm}' on the font
634 metric files that describe characters in the fonts \TeX\ uses; it is
635 `\.{.dvi}' on the output files that specify typesetting information; and it
636 is `\.{.fmt}' on the format files written by \.{INITEX} to initialize \TeX.
637 The file area can be arbitrary on input files, but files are usually
638 output to the user's current area. If an input file cannot be
639 found on the specified area, \TeX\ will look for it on a special system
640 area; this special area is intended for commonly used input files like
641 \.{webmac.tex}.
643 Simple uses of \TeX\ refer only to file names that have no explicit
644 extension or area. For example, a person usually says `\.{\\input} \.{paper}'
645 or `\.{\\font\\tenrm} \.= \.{helvetica}' instead of `\.{\\input}
646 \.{paper.new}' or `\.{\\font\\tenrm} \.= \.{<csd.knuth>test}'. Simple file
647 names are best, because they make the \TeX\ source files portable;
648 whenever a file name consists entirely of letters and digits, it should be
649 treated in the same way by all implementations of \TeX. However, users
650 need the ability to refer to other files in their environment, especially
651 when responding to error messages concerning unopenable files; therefore
652 we want to let them use the syntax that appears in their favorite
653 operating system.
655 The following procedures don't allow spaces to be part of
656 file names; but some users seem to like names that are spaced-out.
657 System-dependent changes to allow such things should probably
658 be made with reluctance, and only when an entire file name that
659 includes spaces is ``quoted'' somehow.
661 Here are the global values that file names will be scanned into.
664 str_number cur_name; /* name of file just scanned */
665 str_number cur_area; /* file area just scanned, or \.{""} */
666 str_number cur_ext; /* file extension just scanned, or \.{""} */
669 @ The file names we shall deal with have the
670 following structure: If the name contains `\./' or `\.:'
671 (for Amiga only), the file area
672 consists of all characters up to and including the final such character;
673 otherwise the file area is null. If the remaining file name contains
674 `\..', the file extension consists of all such characters from the last
675 `\..' to the end, otherwise the file extension is null.
677 We can scan such file names easily by using two global variables that keep track
678 of the occurrences of area and extension delimiters:
681 pool_pointer area_delimiter; /* the most recent `\./', if any */
682 pool_pointer ext_delimiter; /* the relevant `\..', if any */
685 @ Input files that can't be found in the user's area may appear in a standard
686 system area called |TEX_area|. Font metric files whose areas are not given
687 explicitly are assumed to appear in a standard system area called
688 |TEX_font_area|. $\Omega$'s compiled translation process files whose areas
689 are not given explicitly are assumed to appear in a standard system area.
690 These system area names will, of course, vary from place to place.
693 #define append_to_fn(A) do { \
694 c=(A); \
695 if (c!='"') { \
696 if (k<file_name_size) fn[k++]=(unsigned char)(c); \
698 } while (0)
701 char *pack_file_name(str_number n, str_number a, str_number e)
703 ASCII_code c; /* character being packed */
704 unsigned char *j; /* index into |str_pool| */
705 int k = 0; /* number of positions filled in |fn| */
706 unsigned char *fn = xmallocarray(packed_ASCII_code,
707 str_length(a) + str_length(n) +
708 str_length(e) + 1);
709 for (j = str_string(a); j < str_string(a) + str_length(a); j++)
710 append_to_fn(*j);
711 for (j = str_string(n); j < str_string(n) + str_length(n); j++)
712 append_to_fn(*j);
713 for (j = str_string(e); j < str_string(e) + str_length(e); j++)
714 append_to_fn(*j);
715 fn[k] = 0;
716 return (char *) fn;
721 @ A messier routine is also needed, since format file names must be scanned
722 before \TeX's string mechanism has been initialized. We shall use the
723 global variable |TEX_format_default| to supply the text for default system areas
724 and extensions related to format files.
725 @^system dependencies@>
727 Under {\mc UNIX} we don't give the area part, instead depending
728 on the path searching that will happen during file opening. Also, the
729 length will be set in the main program.
732 char *TEX_format_default;
735 @ This part of the program becomes active when a ``virgin'' \TeX\ is trying to get going,
736 just after the preliminary initialization, or when the user is substituting another
737 format file by typing `\.\&' after the initial `\.{**}' prompt. The buffer
738 contains the first line of input in |buffer[loc..(last-1)]|, where
739 |loc<last| and |buffer[loc]<>" "|.
742 char *open_fmt_file(void)
744 int j; /* the first space after the format file name */
745 char *fmt = NULL;
746 int dist;
747 j = iloc;
748 if (buffer[iloc] == '&') {
749 incr(iloc);
750 j = iloc;
751 buffer[last] = ' ';
752 while (buffer[j] != ' ')
753 incr(j);
754 fmt = xmalloc((unsigned) (j - iloc + 1));
755 strncpy(fmt, (char *) (buffer + iloc), (size_t) (j - iloc));
756 fmt[j - iloc] = 0;
757 dist = (int) (strlen(fmt) - strlen(DUMP_EXT));
758 if (!(strstr(fmt, DUMP_EXT) == fmt + dist))
759 fmt = concat(fmt, DUMP_EXT);
760 if (zopen_w_input(&fmt_file, fmt, DUMP_FORMAT, FOPEN_RBIN_MODE))
761 goto FOUND;
762 wake_up_terminal();
763 fprintf(stdout, "Sorry, I can't find the format `%s'; will try `%s'.\n",
764 fmt, TEX_format_default);
765 update_terminal();
767 /* now pull out all the stops: try for the system \.{plain} file */
768 fmt = TEX_format_default;
769 if (!zopen_w_input(&fmt_file, fmt, DUMP_FORMAT, FOPEN_RBIN_MODE)) {
770 wake_up_terminal();
771 fprintf(stdout, "I can't find the format file `%s'!\n",
772 TEX_format_default);
773 return NULL;
775 FOUND:
776 iloc = j;
777 return fmt;
781 @ The global variable |name_in_progress| is used to prevent recursive
782 use of |scan_file_name|, since the |begin_name| and other procedures
783 communicate via global variables. Recursion would arise only by
784 devious tricks like `\.{\\input\\input f}'; such attempts at sabotage
785 must be thwarted. Furthermore, |name_in_progress| prevents \.{\\input}
786 @^recursion@>
787 from being initiated when a font size specification is being scanned.
789 Another global variable, |job_name|, contains the file name that was first
790 \.{\\input} by the user. This name is extended by `\.{.log}' and `\.{.dvi}'
791 and `\.{.fmt}' in the names of \TeX's output files.
794 boolean name_in_progress; /* is a file name being scanned? */
795 str_number job_name; /* principal file name */
796 boolean log_opened_global; /* has the transcript file been opened? */
799 @ Initially |job_name=0|; it becomes nonzero as soon as the true name is known.
800 We have |job_name=0| if and only if the `\.{log}' file has not been opened,
801 except of course for a short time just after |job_name| has become nonzero.
804 unsigned char *texmf_log_name; /* full name of the log file */
806 @ The |open_log_file| routine is used to open the transcript file and to help
807 it catch up to what has previously been printed on the terminal.
810 void open_log_file(void)
812 int old_setting; /* previous |selector| setting */
813 int k; /* index into |buffer| */
814 int l; /* end of first input line */
815 char *fn;
816 old_setting = selector;
817 if (job_name == 0)
818 job_name = getjobname(maketexstring("texput")); /* TODO */
819 fn = pack_job_name(".fls");
820 recorder_change_filename(fn);
821 fn = pack_job_name(".log");
822 while (!lua_a_open_out(&log_file, fn, 0)) {
823 /* Try to get a different log file name */
824 /* Sometimes |open_log_file| is called at awkward moments when \TeX\ is
825 unable to print error messages or even to |show_context|.
826 The |prompt_file_name| routine can result in a |fatal_error|, but the |error|
827 routine will not be invoked because |log_opened| will be false.
829 The normal idea of |batch_mode| is that nothing at all should be written
830 on the terminal. However, in the unusual case that
831 no log file could be opened, we make an exception and allow
832 an explanatory message to be seen.
834 Incidentally, the program always refers to the log file as a `\.{transcript
835 file}', because some systems cannot use the extension `\.{.log}' for
836 this file.
838 selector = term_only;
839 fn = prompt_file_name("transcript file name", ".log");
841 texmf_log_name = (unsigned char *) xstrdup(fn);
842 selector = log_only;
843 log_opened_global = true;
844 if (callback_defined(start_run_callback) == 0) {
845 /* Print the banner line, including current date and time */
846 log_banner(luatex_version_string);
848 input_stack[input_ptr] = cur_input; /* make sure bottom level is in memory */
849 tprint_nl("**");
850 l = input_stack[0].limit_field; /* last position of first line */
851 if (buffer[l] == end_line_char_par)
852 decr(l); /* TODO: multichar endlinechar */
853 for (k = 1; k <= l; k++)
854 print_char(buffer[k]);
855 print_ln(); /* now the transcript file contains the first line of input */
857 flush_loggable_info(); /* should be done always */
858 selector = old_setting + 2; /* |log_only| or |term_and_log| */
861 @ This function is needed by synctex to make its log appear in the right
862 spot when |output_directory| is set.
865 char *get_full_log_name (void)
867 if (output_directory) {
868 char *ret = xmalloc(strlen((char *)texmf_log_name)+2+strlen(output_directory));
869 ret = strcpy(ret, output_directory);
870 strcat(ret, "/");
871 strcat(ret, (char *)texmf_log_name);
872 return ret;
873 } else {
874 return xstrdup((const char*)texmf_log_name);
878 @ Synctex uses this to get the anchored path of an input file.
881 char *luatex_synctex_get_current_name (void)
883 char *pwdbuf = NULL, *ret;
884 if (kpse_absolute_p(fullnameoffile, false)) {
885 return xstrdup(fullnameoffile);
887 pwdbuf = xgetcwd();
888 ret = concat3(pwdbuf, DIR_SEP_STRING, fullnameoffile);
889 free(pwdbuf) ;
890 return ret;
894 @ Let's turn now to the procedure that is used to initiate file reading
895 when an `\.{\\input}' command is being processed.
898 void start_input(void)
899 { /* \TeX\ will \.{\\input} something */
900 str_number temp_str;
901 char *fn;
902 do {
903 get_x_token();
904 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
906 back_input();
907 if (cur_cmd != left_brace_cmd) {
908 scan_file_name(); /* set |cur_name| to desired file name */
909 } else {
910 scan_file_name_toks();
912 fn = pack_file_name(cur_name, cur_area, cur_ext);
913 while (1) {
914 begin_file_reading(); /* set up |cur_file| and new level of input */
915 if (lua_a_open_in(&cur_file, fn, 0))
916 break;
917 end_file_reading(); /* remove the level that didn't work */
918 fn = prompt_file_name("input file name", "");
920 iname = maketexstring(fullnameoffile);
921 /* Now that we have |fullnameoffile|, it is time to post-adjust
922 |cur_name| and |cur_ext| for trailing |.tex| */
924 char *n, *p;
925 n = p = fullnameoffile + strlen(fullnameoffile);
926 while (p>fullnameoffile) {
927 p--;
928 if (IS_DIR_SEP(*p)) {
929 break;
932 if (IS_DIR_SEP(*p)) {
933 p++;
935 while (n>fullnameoffile) {
936 n--;
937 if (*n == '.') {
938 break;
941 if (n>p) {
942 int q = *n;
943 cur_ext = maketexstring(n);
944 *n = 0;
945 cur_name = maketexstring(p);
946 *n = q;
951 source_filename_stack[in_open] = iname;
952 full_source_filename_stack[in_open] = xstrdup(fullnameoffile);
953 /* we can try to conserve string pool space now */
954 temp_str = search_string(iname);
955 if (temp_str > 0) {
956 flush_str(iname);
957 iname = temp_str;
959 if (job_name == 0) {
960 job_name = getjobname(cur_name);
961 open_log_file();
963 /* |open_log_file| doesn't |show_context|, so |limit|
964 and |loc| needn't be set to meaningful values yet */
965 report_start_file(filetype_tex,fullnameoffile);
966 incr(open_parens);
967 update_terminal();
968 istate = new_line;
969 /* Prepare new file {\sl Sync\TeX} information */
970 synctexstartinput(); /* Give control to the {\sl Sync\TeX} controller */
972 /* Read the first line of the new file */
973 /* Here we have to remember to tell the |lua_input_ln| routine not to
974 start with a |get|. If the file is empty, it is considered to
975 contain a single blank line. */
977 line = 1;
978 if (lua_input_ln(cur_file, 0, false)) {
981 firm_up_the_line();
982 if (end_line_char_inactive)
983 decr(ilimit);
984 else
985 buffer[ilimit] = (packed_ASCII_code) end_line_char_par;
986 first = ilimit + 1;
987 iloc = istart;
990 @ Read and write dump files through zlib
992 @ Earlier versions recast |*f| from |FILE *| to |gzFile|, but there is
993 no guarantee that these have the same size, so a static variable
994 is needed.
997 static gzFile gz_fmtfile = NULL;
999 @ As distributed, the dump files are
1000 architecture dependent; specifically, BigEndian and LittleEndian
1001 architectures produce different files. These routines always output
1002 BigEndian files. This still does not guarantee them to be
1003 architecture-independent, because it is possible to make a format
1004 that dumps a glue ratio, i.e., a floating-point number. Fortunately,
1005 none of the standard formats do that.
1008 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1010 /* This macro is always invoked as a statement. It assumes a variable
1011 `temp'. */
1012 # define SWAP(x, y) do { temp = x; x = y; y = temp; } while (0)
1014 /* Make the NITEMS items pointed at by P, each of size SIZE, be the
1015 opposite-endianness of whatever they are now. */
1016 static void
1017 swap_items(char *pp, int nitems, int size)
1019 char temp;
1020 unsigned total = (unsigned) (nitems * size);
1021 char *q = xmalloc(total);
1022 char *p = q;
1023 memcpy(p,pp,total);
1024 /* Since `size' does not change, we can write a while loop for each
1025 case, and avoid testing `size' for each time. */
1026 switch (size) {
1027 /* 16-byte items happen on the DEC Alpha machine when we are not
1028 doing sharable memory dumps. */
1029 case 16:
1030 while (nitems--) {
1031 SWAP(p[0], p[15]);
1032 SWAP(p[1], p[14]);
1033 SWAP(p[2], p[13]);
1034 SWAP(p[3], p[12]);
1035 SWAP(p[4], p[11]);
1036 SWAP(p[5], p[10]);
1037 SWAP(p[6], p[9]);
1038 SWAP(p[7], p[8]);
1039 p += size;
1041 break;
1043 case 12:
1044 while (nitems--) {
1045 SWAP(p[0], p[11]);
1046 SWAP(p[1], p[10]);
1047 SWAP(p[2], p[9]);
1048 SWAP(p[3], p[8]);
1049 SWAP(p[4], p[7]);
1050 SWAP(p[5], p[6]);
1051 p += size;
1053 break;
1055 case 8:
1056 while (nitems--) {
1057 SWAP(p[0], p[7]);
1058 SWAP(p[1], p[6]);
1059 SWAP(p[2], p[5]);
1060 SWAP(p[3], p[4]);
1061 p += size;
1063 break;
1065 case 4:
1066 while (nitems--) {
1067 SWAP(p[0], p[3]);
1068 SWAP(p[1], p[2]);
1069 p += size;
1071 break;
1073 case 2:
1074 while (nitems--) {
1075 SWAP(p[0], p[1]);
1076 p += size;
1078 break;
1080 case 1:
1081 /* Nothing to do. */
1082 break;
1084 default:
1085 FATAL1("Can't swap a %d-byte item for (un)dumping", size);
1087 memcpy(pp,q,total);
1088 xfree(q);
1090 #endif /* not WORDS_BIGENDIAN and not NO_DUMP_SHARE */
1092 @ That second swap is to make sure following calls don't get
1093 confused in the case of |dump_things|.
1096 void do_zdump(char *p, int item_size, int nitems, FILE * out_file)
1098 int err;
1099 (void) out_file;
1100 if (nitems == 0)
1101 return;
1102 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1103 swap_items(p, nitems, item_size);
1104 #endif
1105 if (gzwrite(gz_fmtfile, (void *) p, (unsigned) (item_size * nitems)) !=
1106 item_size * nitems) {
1107 fprintf(stderr, "! Could not write %d %d-byte item(s): %s.\n", nitems,
1108 item_size, gzerror(gz_fmtfile, &err));
1109 uexit(1);
1111 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1112 swap_items(p, nitems, item_size);
1113 #endif
1116 @ @c
1117 void do_zundump(char *p, int item_size, int nitems, FILE * in_file)
1119 int err;
1120 (void) in_file;
1121 if (nitems == 0)
1122 return;
1123 if (gzread(gz_fmtfile, (void *) p, (unsigned) (item_size * nitems)) <= 0) {
1124 fprintf(stderr, "Could not undump %d %d-byte item(s): %s.\n",
1125 nitems, item_size, gzerror(gz_fmtfile, &err));
1126 uexit(1);
1128 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1129 swap_items(p, nitems, item_size);
1130 #endif
1133 @ @c
1134 #define COMPRESSION "R3"
1136 boolean zopen_w_input(FILE ** f, const char *fname, int format,
1137 const_string fopen_mode)
1139 int callbackid;
1140 int res;
1141 char *fnam;
1142 callbackid = callback_defined(find_format_file_callback);
1143 if (callbackid > 0) {
1144 res = run_callback(callbackid, "S->R", fname, &fnam);
1145 if (res && fnam && strlen(fnam) > 0) {
1146 *f = fopen(fnam, fopen_mode);
1147 if (*f == NULL) {
1148 return 0;
1150 } else {
1151 return 0;
1153 } else {
1154 res = luatex_open_input(f, fname, format, fopen_mode, true);
1156 if (res) {
1157 gz_fmtfile = gzdopen(fileno(*f), "rb" COMPRESSION);
1159 return res;
1162 @ @c
1163 boolean zopen_w_output(FILE ** f, const char *s, const_string fopen_mode)
1165 int res = 1;
1166 if (luainit) {
1167 *f = fopen(s, fopen_mode);
1168 if (*f == NULL) {
1169 return 0;
1171 } else {
1172 res = luatex_open_output(f, s, fopen_mode);
1174 if (res) {
1175 gz_fmtfile = gzdopen(fileno(*f), "wb" COMPRESSION);
1177 return res;
1180 @ @c
1181 void zwclose(FILE * f)
1183 (void) f;
1184 gzclose(gz_fmtfile);
1187 @ create the dvi or pdf file
1189 int open_outfile(FILE ** f, const char *name, const char *mode)
1191 FILE *res;
1192 res = fopen(name, mode);
1193 if (res != NULL) {
1194 *f = res;
1195 return 1;
1197 return 0;
1201 @ the caller should set |tfm_buffer=NULL| and |tfm_size=0|
1203 int readbinfile(FILE * f, unsigned char **tfm_buffer, int *tfm_size)
1205 void *buf;
1206 int size;
1207 if (fseek(f, 0, SEEK_END) == 0) {
1208 size = (int) ftell(f);
1209 if (size > 0) {
1210 buf = xmalloc((unsigned) size);
1211 if (fseek(f, 0, SEEK_SET) == 0) {
1212 if (fread((void *) buf, (size_t) size, 1, f) == 1) {
1213 *tfm_buffer = (unsigned char *) buf;
1214 *tfm_size = size;
1215 return 1;
1218 } else {
1219 *tfm_buffer = NULL;
1220 *tfm_size = 0;
1221 return 1;
1223 } /* seek failed, or zero-sized file */
1224 return 0;
1227 @ Like |os.execute()|, the |runpopen()| function is called only when
1228 |shellenabledp == 1|. Unlike |os.execute()| we write errors to stderr, since we
1229 have nowhere better to use; and of course we return a file handle (or NULL)
1230 instead of a status indicator.
1233 static FILE *runpopen(char *cmd, const char *mode)
1235 FILE *f = NULL;
1236 char *safecmd = NULL;
1237 char *cmdname = NULL;
1238 int allow;
1240 #ifdef WIN32
1241 char *pp;
1243 for (pp = cmd; *pp; pp++) {
1244 if (*pp == '\'') *pp = '"';
1246 #endif
1248 /* If restrictedshell == 0, any command is allowed. */
1249 if (restrictedshell == 0) {
1250 allow = 1;
1251 } else {
1252 const char *thecmd = cmd;
1253 allow = shell_cmd_is_allowed(thecmd, &safecmd, &cmdname);
1255 if (allow == 1)
1256 f = popen(cmd, mode);
1257 else if (allow == 2)
1258 f = popen(safecmd, mode);
1259 else if (allow == -1)
1260 fprintf(stderr, "\nrunpopen quotation error in command line: %s\n",
1261 cmd);
1262 else
1263 fprintf(stderr, "\nrunpopen command not allowed: %s\n", cmdname);
1265 if (safecmd)
1266 free(safecmd);
1267 if (cmdname)
1268 free(cmdname);
1269 return f;
1272 @ piped I/O
1275 @ The code that implements |popen()| needs an array for tracking
1276 possible pipe file pointers, because these need to be
1277 closed using |pclose()|.
1280 #define NUM_PIPES 16
1281 static FILE *pipes[NUM_PIPES];
1283 #ifdef WIN32
1284 FILE *Poptr;
1285 #endif
1287 boolean open_in_or_pipe(FILE ** f_ptr, char *fn, int filefmt,
1288 const_string fopen_mode, boolean must_exist)
1290 string fname = NULL;
1291 int i; /* iterator */
1293 /* opening a read pipe is straightforward, only have to
1294 skip past the pipe symbol in the file name. filename
1295 quoting is assumed to happen elsewhere (it does :-)) */
1297 if (shellenabledp && *fn == '|') {
1298 /* the user requested a pipe */
1299 *f_ptr = NULL;
1300 fname = (string) xmalloc((unsigned) (strlen(fn) + 1));
1301 strcpy(fname, fn);
1302 if (fullnameoffile)
1303 free(fullnameoffile);
1304 fullnameoffile = xstrdup(fname);
1305 recorder_record_input(fname + 1);
1306 *f_ptr = runpopen(fname + 1, "r");
1307 free(fname);
1308 for (i = 0; i < NUM_PIPES; i++) {
1309 if (pipes[i] == NULL) {
1310 pipes[i] = *f_ptr;
1311 break;
1314 if (*f_ptr)
1315 setvbuf(*f_ptr, (char *) NULL, _IONBF, 0);
1316 #ifdef WIN32
1317 Poptr = *f_ptr;
1318 #endif
1320 return *f_ptr != NULL;
1323 return luatex_open_input(f_ptr, fn, filefmt, fopen_mode, must_exist);
1327 boolean open_out_or_pipe(FILE ** f_ptr, char *fn, const_string fopen_mode)
1329 string fname;
1330 int i; /* iterator */
1332 /* opening a write pipe takes a little bit more work, because TeX
1333 will perhaps have appended ".tex". To avoid user confusion as
1334 much as possible, this extension is stripped only when the command
1335 is a bare word. Some small string trickery is needed to make
1336 sure the correct number of bytes is free()-d afterwards */
1338 if (shellenabledp && *fn == '|') {
1339 /* the user requested a pipe */
1340 fname = (string) xmalloc((unsigned) (strlen(fn) + 1));
1341 strcpy(fname, fn);
1342 if (strchr(fname, ' ') == NULL && strchr(fname, '>') == NULL) {
1343 /* mp and mf currently do not use this code, but it
1344 is better to be prepared */
1345 if (STREQ((fname + strlen(fname) - 3), "tex"))
1346 *(fname + strlen(fname) - 4) = 0;
1347 *f_ptr = runpopen(fname + 1, "w");
1348 *(fname + strlen(fname)) = '.';
1349 } else {
1350 *f_ptr = runpopen(fname + 1, "w");
1352 recorder_record_output(fname + 1);
1353 free(fname);
1355 for (i = 0; i < NUM_PIPES; i++) {
1356 if (pipes[i] == NULL) {
1357 pipes[i] = *f_ptr;
1358 break;
1362 if (*f_ptr)
1363 setvbuf(*f_ptr, (char *) NULL, _IONBF, 0);
1365 return *f_ptr != NULL;
1368 return luatex_open_output(f_ptr, fn, fopen_mode);
1372 void close_file_or_pipe(FILE * f)
1374 int i; /* iterator */
1376 if (shellenabledp) {
1377 for (i = 0; i <= 15; i++) {
1378 /* if this file was a pipe, |pclose()| it and return */
1379 if (pipes[i] == f) {
1380 if (f) {
1381 pclose(f);
1382 #ifdef WIN32
1383 Poptr = NULL;
1384 #endif
1386 pipes[i] = NULL;
1387 return;
1391 close_file(f);