beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / tex / texfileio.w
blobe04bf991afb0909f3a9b35209465a741b58163b6
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 @ @c
29 #define end_line_char int_par(end_line_char_code)
32 @ The bane of portability is the fact that different operating systems treat
33 input and output quite differently, perhaps because computer scientists
34 have not given sufficient attention to this problem. People have felt somehow
35 that input and output are not part of ``real'' programming. Well, it is true
36 that some kinds of programming are more fun than others. With existing
37 input/output conventions being so diverse and so messy, the only sources of
38 joy in such parts of the code are the rare occasions when one can find a
39 way to make the program a little less bad than it might have been. We have
40 two choices, either to attack I/O now and get it over with, or to postpone
41 I/O until near the end. Neither prospect is very attractive, so let's
42 get it over with.
44 The basic operations we need to do are (1)~inputting and outputting of
45 text, to or from a file or the user's terminal; (2)~inputting and
46 outputting of eight-bit bytes, to or from a file; (3)~instructing the
47 operating system to initiate (``open'') or to terminate (``close'') input or
48 output from a specified file; (4)~testing whether the end of an input
49 file has been reached.
51 \TeX\ needs to deal with two kinds of files.
52 We shall use the term |alpha_file| for a file that contains textual data,
53 and the term |byte_file| for a file that contains eight-bit binary information.
54 These two types turn out to be the same on many computers, but
55 sometimes there is a significant distinction, so we shall be careful to
56 distinguish between them. Standard protocols for transferring
57 such files from computer to computer, via high-speed networks, are
58 now becoming available to more and more communities of users.
60 The program actually makes use also of a third kind of file, called a
61 |word_file|, when dumping and reloading base information for its own
62 initialization. We shall define a word file later; but it will be possible
63 for us to specify simple operations on word files before they are defined.
65 @ We finally did away with |nameoffile| and |namelength|, but the variables
66 have to be kept otherwise there will be link errors from |openclose.c| in
67 the web2c library
70 char *nameoffile;
71 int namelength;
74 @ When input files are opened via a callback, they will also be read using
75 callbacks. for that purpose, the |open_read_file_callback| returns an
76 integer to uniquely identify a callback table. This id replaces the file
77 point |f| in this case, because the input does not have to be a file
78 in the traditional sense.
80 Signalling this fact is achieved by having two arrays of integers.
83 int *input_file_callback_id;
84 int read_file_callback_id[17];
86 @ Handle -output-directory.
88 We assume that it is OK to look here first. Possibly it
89 would be better to replace lookups in "." with lookups in the
90 |output_directory| followed by "." but to do this requires much more
91 invasive surgery in libkpathsea.
94 static char *find_in_output_directory(const char *s)
96 if (output_directory && !kpse_absolute_p(s, false)) {
97 FILE *f_ptr;
98 char *ftemp = concat3(output_directory, DIR_SEP_STRING, s);
99 f_ptr = fopen(ftemp, "rb"); /* this code is used for input files only */
100 if (f_ptr) {
101 fclose(f_ptr);
102 return ftemp;
103 } else {
104 free(ftemp);
108 return NULL;
111 @ find an \.{\\input} or \.{\\read} file. |n| differentiates between those case.
114 char *luatex_find_read_file(const char *s, int n, int callback_index)
116 char *ftemp = NULL;
117 int callback_id = callback_defined(callback_index);
118 if (callback_id > 0) {
119 (void) run_callback(callback_id, "dS->S", n, s, &ftemp);
120 } else {
121 /* use kpathsea here */
122 ftemp = find_in_output_directory(s);
123 if (!ftemp)
124 ftemp = kpse_find_file(s, kpse_tex_format, 1);
126 if (ftemp) {
127 if (fullnameoffile)
128 free(fullnameoffile);
129 fullnameoffile = xstrdup(ftemp);
131 return ftemp;
134 @ find other files types
136 char *luatex_find_file(const char *s, int callback_index)
138 char *ftemp = NULL;
139 int callback_id = callback_defined(callback_index);
140 if (callback_id > 0) {
141 (void) run_callback(callback_id, "S->R", s, &ftemp);
143 } else {
144 /* use kpathsea here */
145 switch (callback_index) {
146 case find_enc_file_callback:
147 ftemp = kpse_find_file(s, kpse_enc_format, 0);
148 break;
149 case find_sfd_file_callback:
150 ftemp = kpse_find_file(s, kpse_sfd_format, 0);
151 break;
152 case find_map_file_callback:
153 ftemp = kpse_find_file(s, kpse_fontmap_format, 0);
154 break;
155 case find_type1_file_callback:
156 ftemp = kpse_find_file(s, kpse_type1_format, 0);
157 break;
158 case find_truetype_file_callback:
159 ftemp = kpse_find_file(s, kpse_truetype_format, 0);
160 break;
161 case find_opentype_file_callback:
162 ftemp = kpse_find_file(s, kpse_opentype_format, 0);
163 if (ftemp == NULL)
164 ftemp = kpse_find_file(s, kpse_truetype_format, 0);
165 break;
166 case find_data_file_callback:
167 ftemp = find_in_output_directory(s);
168 if (!ftemp)
169 ftemp = kpse_find_file(s, kpse_tex_format, 0);
170 break;
171 case find_font_file_callback:
172 ftemp = kpse_find_file(s, kpse_ofm_format, 0);
173 if (ftemp == NULL)
174 ftemp = kpse_find_file(s, kpse_tfm_format, 0);
175 break;
176 case find_vf_file_callback:
177 ftemp = kpse_find_file(s, kpse_ovf_format, 0);
178 if (ftemp == NULL)
179 ftemp = kpse_find_file(s, kpse_vf_format, 0);
180 break;
181 case find_cidmap_file_callback:
182 ftemp = kpse_find_file(s, kpse_cid_format, 0);
183 break;
184 default:
185 printf
186 ("luatex_find_file(): do not know how to handle file %s of type %d\n",
187 s, callback_index);
188 break;
191 return ftemp;
195 @ LuaTeX used to have private functions for these that did not use kpathsea,
196 but since the file paranoia tests have to come from kpathsea anyway, that is no
197 longer useful. The only downside to using luatex is that if one wants to disable
198 kpathsea via the Lua startup script, it is now an absolute requirement that all
199 file discovery callbacks are specified. Just using the find_read_file, but not
200 setting open_read_file, for example, does not work any more if kpathsea is not
201 to be used at all.
204 #define openoutnameok(A) kpse_out_name_ok (A)
205 #define openinnameok(A) kpse_in_name_ok (A)
207 @ Open an input file F, using the kpathsea format FILEFMT and passing
208 |FOPEN_MODE| to fopen. The filename is in `fn'. We return whether or
209 not the open succeeded.
212 boolean
213 luatex_open_input(FILE ** f_ptr, const char *fn, int filefmt,
214 const_string fopen_mode, boolean must_exist)
216 string fname = NULL;
217 /* We havent found anything yet. */
218 *f_ptr = NULL;
219 if (fullnameoffile)
220 free(fullnameoffile);
221 fullnameoffile = NULL;
222 fname = kpse_find_file(fn, (kpse_file_format_type) filefmt, must_exist);
223 if (fname) {
224 fullnameoffile = xstrdup(fname);
225 /* If we found the file in the current directory, don't leave
226 the `./' at the beginning of `fn', since it looks
227 dumb when `tex foo' says `(./foo.tex ... )'. On the other
228 hand, if the user said `tex ./foo', and that's what we
229 opened, then keep it -- the user specified it, so we
230 shouldn't remove it. */
231 if (fname[0] == '.' && IS_DIR_SEP(fname[1])
232 && (fn[0] != '.' || !IS_DIR_SEP(fn[1]))) {
233 unsigned i = 0;
234 while (fname[i + 2] != 0) {
235 fname[i] = fname[i + 2];
236 i++;
238 fname[i] = 0;
240 /* This fopen is not allowed to fail. */
241 *f_ptr = xfopen(fname, fopen_mode);
243 if (*f_ptr) {
244 recorder_record_input(fname);
246 return *f_ptr != NULL;
249 @ @c
250 boolean luatex_open_output(FILE ** f_ptr, const char *fn,
251 const_string fopen_mode)
253 char *fname;
254 boolean absolute = kpse_absolute_p(fn, false);
256 /* If we have an explicit output directory, use it. */
257 if (output_directory && !absolute) {
258 fname = concat3(output_directory, DIR_SEP_STRING, fn);
259 } else {
260 fname = xstrdup(fn);
263 /* Is the filename openable as given? */
264 *f_ptr = fopen(fname, fopen_mode);
266 if (!*f_ptr) {
267 /* Can't open as given. Try the envvar. */
268 string texmfoutput = kpse_var_value("TEXMFOUTPUT");
270 if (texmfoutput && *texmfoutput && !absolute) {
271 fname = concat3(texmfoutput, DIR_SEP_STRING, fn);
272 *f_ptr = fopen(fname, fopen_mode);
275 if (*f_ptr) {
276 recorder_record_output(fname);
278 free(fname);
279 return *f_ptr != NULL;
283 @ @c
284 boolean lua_a_open_in(alpha_file * f, char *fn, int n)
286 int k;
287 char *fnam; /* string returned by find callback */
288 int callback_id;
289 boolean ret = true; /* return value */
290 boolean file_ok = true; /* the status so far */
291 if (n == 0) {
292 input_file_callback_id[iindex] = 0;
293 } else {
294 read_file_callback_id[n] = 0;
296 if (*fn == '|')
297 fnam = fn;
298 else
299 fnam = luatex_find_read_file(fn, n, find_read_file_callback);
300 if (!fnam)
301 return false;
302 callback_id = callback_defined(open_read_file_callback);
303 if (callback_id > 0) {
304 k = run_and_save_callback(callback_id, "S->", fnam);
305 if (k > 0) {
306 ret = true;
307 if (n == 0)
308 input_file_callback_id[iindex] = k;
309 else
310 read_file_callback_id[n] = k;
311 } else {
312 file_ok = false; /* read failed */
314 } else { /* no read callback */
315 if (openinnameok(fnam)) {
316 ret =
317 open_in_or_pipe(f, fnam, kpse_tex_format, FOPEN_RBIN_MODE,
318 (n == 0 ? true : false));
319 } else {
320 file_ok = false; /* open failed */
323 if (!file_ok) {
324 ret = false;
326 return ret;
330 @ @c
331 boolean lua_a_open_out(alpha_file * f, char *fn, int n)
333 boolean test;
334 str_number fnam;
335 int callback_id;
336 boolean ret = false;
337 callback_id = callback_defined(find_write_file_callback);
338 if (callback_id > 0) {
339 fnam = 0;
340 test = run_callback(callback_id, "dS->s", n, fn, &fnam);
341 if ((test) && (fnam != 0) && (str_length(fnam) > 0)) {
342 /* There is no message here because if that is needed the macro package */
343 /* should do that in the callback code. As elsewhere, messaging is left */
344 /* to lua then. */
345 ret = open_outfile(f, fn, FOPEN_W_MODE);
347 } else {
348 if (openoutnameok(fn)) {
349 if (n > 0 && selector != term_only) {
350 /* This message to the log is for downward compatibility with other tex's */
351 /* as there are scripts out there that act on this message. An alternative */
352 /* is to let a macro package write an explicit message. */
353 fprintf(log_file,"\n\\openout%i = %s\n",n-1,fn);
355 ret = open_out_or_pipe(f, fn, FOPEN_W_MODE);
358 return ret;
362 @ @c
363 boolean lua_b_open_out(alpha_file * f, char *fn)
365 boolean test;
366 str_number fnam;
367 int callback_id;
368 boolean ret = false;
369 callback_id = callback_defined(find_output_file_callback);
370 if (callback_id > 0) {
371 fnam = 0;
372 test = run_callback(callback_id, "S->s", fn, &fnam);
373 if ((test) && (fnam != 0) && (str_length(fnam) > 0)) {
374 ret = open_outfile(f, fn, FOPEN_WBIN_MODE);
376 } else {
377 if (openoutnameok(fn)) {
378 ret = luatex_open_output(f, fn, FOPEN_WBIN_MODE);
381 return ret;
384 @ @c
385 void lua_a_close_in(alpha_file f, int n)
386 { /* close a text file */
387 int callback_id;
388 if (n == 0)
389 callback_id = input_file_callback_id[iindex];
390 else
391 callback_id = read_file_callback_id[n];
392 if (callback_id > 0) {
393 run_saved_callback(callback_id, "close", "->");
394 destroy_saved_callback(callback_id);
395 if (n == 0)
396 input_file_callback_id[iindex] = 0;
397 else
398 read_file_callback_id[n] = 0;
399 } else {
400 close_file_or_pipe(f);
404 @ @c
405 void lua_a_close_out(alpha_file f)
406 { /* close a text file */
407 close_file_or_pipe(f);
411 @ Binary input and output are done with C's ordinary
412 procedures, so we don't have to make any other special arrangements for
413 binary~I/O. Text output is also easy to do with standard routines.
414 The treatment of text input is more difficult, however, because
415 of the necessary translation to |ASCII_code| values.
416 \TeX's conventions should be efficient, and they should
417 blend nicely with the user's operating environment.
419 Input from text files is read one line at a time, using a routine called
420 |lua_input_ln|. This function is defined in terms of global variables called
421 |buffer|, |first|, and |last| that will be described in detail later; for
422 now, it suffices for us to know that |buffer| is an array of |ASCII_code|
423 values, and that |first| and |last| are indices into this array
424 representing the beginning and ending of a line of text.
427 packed_ASCII_code *buffer; /* lines of characters being read */
428 int first; /* the first unused position in |buffer| */
429 int last; /* end of the line just input to |buffer| */
430 int max_buf_stack; /* largest index used in |buffer| */
433 @ The |lua_input_ln| function brings the next line of input from the specified
434 file into available positions of the buffer array and returns the value
435 |true|, unless the file has already been entirely read, in which case it
436 returns |false| and sets |last:=first|. In general, the |ASCII_code|
437 numbers that represent the next line of the file are input into
438 |buffer[first]|, |buffer[first+1]|, \dots, |buffer[last-1]|; and the
439 global variable |last| is set equal to |first| plus the length of the
440 line. Trailing blanks are removed from the line; thus, either |last=first|
441 (in which case the line was entirely blank) or |buffer[last-1]<>" "|.
443 An overflow error is given, however, if the normal actions of |lua_input_ln|
444 would make |last>=buf_size|; this is done so that other parts of \TeX\
445 can safely look at the contents of |buffer[last+1]| without overstepping
446 the bounds of the |buffer| array. Upon entry to |lua_input_ln|, the condition
447 |first<buf_size| will always hold, so that there is always room for an
448 ``empty'' line.
450 The variable |max_buf_stack|, which is used to keep track of how large
451 the |buf_size| parameter must be to accommodate the present job, is
452 also kept up to date by |lua_input_ln|.
454 If the |bypass_eoln| parameter is |true|, |lua_input_ln| will do a |get|
455 before looking at the first character of the line; this skips over
456 an |eoln| that was in |f^|. The procedure does not do a |get| when it
457 reaches the end of the line; therefore it can be used to acquire input
458 from the user's terminal as well as from ordinary text files.
460 Since the inner loop of |lua_input_ln| is part of \TeX's ``inner loop''---each
461 character of input comes in at this place---it is wise to reduce system
462 overhead by making use of special routines that read in an entire array
463 of characters at once, if such routines are available.
464 @^inner loop@>
467 boolean lua_input_ln(alpha_file f, int n, boolean bypass_eoln)
469 boolean lua_result;
470 int last_ptr;
471 int callback_id;
472 (void) bypass_eoln; /* todo: variable can be removed */
473 if (n == 0)
474 callback_id = input_file_callback_id[iindex];
475 else
476 callback_id = read_file_callback_id[n];
477 if (callback_id > 0) {
478 last = first;
479 last_ptr = first;
480 lua_result =
481 run_saved_callback(callback_id, "reader", "->l", &last_ptr);
482 if ((lua_result == true) && (last_ptr != 0)) {
483 last = last_ptr;
484 if (last > max_buf_stack)
485 max_buf_stack = last;
486 } else {
487 lua_result = false;
489 } else {
490 lua_result = input_ln(f, bypass_eoln);
492 if (lua_result == true) {
493 /* Fix up the input buffer using callbacks */
494 if (last >= first) {
495 callback_id = callback_defined(process_input_buffer_callback);
496 if (callback_id > 0) {
497 last_ptr = first;
498 lua_result =
499 run_callback(callback_id, "l->l", (last - first),
500 &last_ptr);
501 if ((lua_result == true) && (last_ptr != 0)) {
502 last = last_ptr;
503 if (last > max_buf_stack)
504 max_buf_stack = last;
508 return true;
510 return false;
514 @ We need a special routine to read the first line of \TeX\ input from
515 the user's terminal. This line is different because it is read before we
516 have opened the transcript file; there is sort of a ``chicken and
517 egg'' problem here. If the user types `\.{\\input paper}' on the first
518 line, or if some macro invoked by that line does such an \.{\\input},
519 the transcript file will be named `\.{paper.log}'; but if no \.{\\input}
520 commands are performed during the first line of terminal input, the transcript
521 file will acquire its default name `\.{texput.log}'. (The transcript file
522 will not contain error messages generated by the first line before the
523 first \.{\\input} command.)
524 @.texput@>
526 The first line is special also because it may be read before \TeX\ has
527 input a format file. In such cases, normal error messages cannot yet
528 be given. The following code uses concepts that will be explained later.
530 @ Different systems have different ways to get started. But regardless of
531 what conventions are adopted, the routine that initializes the terminal
532 should satisfy the following specifications:
534 \yskip\textindent{1)}It should open file |term_in| for input from the
535 terminal. (The file |term_out| will already be open for output to the
536 terminal.)
538 \textindent{2)}If the user has given a command line, this line should be
539 considered the first line of terminal input. Otherwise the
540 user should be prompted with `\.{**}', and the first line of input
541 should be whatever is typed in response.
543 \textindent{3)}The first line of input, which might or might not be a
544 command line, should appear in locations |first| to |last-1| of the
545 |buffer| array.
547 \textindent{4)}The global variable |loc| should be set so that the
548 character to be read next by \TeX\ is in |buffer[loc]|. This
549 character should not be blank, and we should have |loc<last|.
551 \yskip\noindent(It may be necessary to prompt the user several times
552 before a non-blank line comes in. The prompt is `\.{**}' instead of the
553 later `\.*' because the meaning is slightly different: `\.{\\input}' need
554 not be typed immediately after~`\.{**}'.)
556 The following program does the required initialization.
557 Iff anything has been specified on the command line, then |t_open_in|
558 will return with |last > first|.
559 @^system dependencies@>
562 boolean init_terminal(void)
563 { /* gets the terminal input started */
564 t_open_in();
565 if (last > first) {
566 iloc = first;
567 while ((iloc < last) && (buffer[iloc] == ' '))
568 incr(iloc);
569 if (iloc < last) {
570 return true;
573 while (1) {
574 wake_up_terminal();
575 fputs("**", term_out);
576 update_terminal();
577 if (!input_ln(term_in, true)) {
578 /* this shouldn't happen */
579 fputs("\n! End of file on the terminal... why?\n", term_out);
580 return false;
582 iloc = first;
583 while ((iloc < last) && (buffer[iloc] == ' '))
584 incr(iloc);
585 if (iloc < last) {
586 return true; /* return unless the line was all blank */
588 fputs("Please type the name of your input file.\n", term_out);
593 @ Here is a procedure that asks the user to type a line of input,
594 assuming that the |selector| setting is either |term_only| or |term_and_log|.
595 The input is placed into locations |first| through |last-1| of the
596 |buffer| array, and echoed on the transcript file if appropriate.
599 void term_input(void)
600 { /* gets a line from the terminal */
601 int k; /* index into |buffer| */
602 update_terminal(); /* now the user sees the prompt for sure */
603 if (!input_ln(term_in, true))
604 fatal_error("End of file on the terminal!");
605 term_offset = 0; /* the user's line ended with \.{<return>} */
606 decr(selector); /* prepare to echo the input */
607 if (last != first) {
608 for (k = first; k <= last - 1; k++)
609 print_char(buffer[k]);
611 print_ln();
612 incr(selector); /* restore previous status */
616 @ It's time now to fret about file names. Besides the fact that different
617 operating systems treat files in different ways, we must cope with the
618 fact that completely different naming conventions are used by different
619 groups of people. The following programs show what is required for one
620 particular operating system; similar routines for other systems are not
621 difficult to devise.
622 @^fingers@>
623 @^system dependencies@>
625 \TeX\ assumes that a file name has three parts: the name proper; its
626 ``extension''; and a ``file area'' where it is found in an external file
627 system. The extension of an input file or a write file is assumed to be
628 `\.{.tex}' unless otherwise specified; it is `\.{.log}' on the
629 transcript file that records each run of \TeX; it is `\.{.tfm}' on the font
630 metric files that describe characters in the fonts \TeX\ uses; it is
631 `\.{.dvi}' on the output files that specify typesetting information; and it
632 is `\.{.fmt}' on the format files written by \.{INITEX} to initialize \TeX.
633 The file area can be arbitrary on input files, but files are usually
634 output to the user's current area. If an input file cannot be
635 found on the specified area, \TeX\ will look for it on a special system
636 area; this special area is intended for commonly used input files like
637 \.{webmac.tex}.
639 Simple uses of \TeX\ refer only to file names that have no explicit
640 extension or area. For example, a person usually says `\.{\\input} \.{paper}'
641 or `\.{\\font\\tenrm} \.= \.{helvetica}' instead of `\.{\\input}
642 \.{paper.new}' or `\.{\\font\\tenrm} \.= \.{<csd.knuth>test}'. Simple file
643 names are best, because they make the \TeX\ source files portable;
644 whenever a file name consists entirely of letters and digits, it should be
645 treated in the same way by all implementations of \TeX. However, users
646 need the ability to refer to other files in their environment, especially
647 when responding to error messages concerning unopenable files; therefore
648 we want to let them use the syntax that appears in their favorite
649 operating system.
651 The following procedures don't allow spaces to be part of
652 file names; but some users seem to like names that are spaced-out.
653 System-dependent changes to allow such things should probably
654 be made with reluctance, and only when an entire file name that
655 includes spaces is ``quoted'' somehow.
657 Here are the global values that file names will be scanned into.
660 str_number cur_name; /* name of file just scanned */
661 str_number cur_area; /* file area just scanned, or \.{""} */
662 str_number cur_ext; /* file extension just scanned, or \.{""} */
665 @ The file names we shall deal with have the
666 following structure: If the name contains `\./' or `\.:'
667 (for Amiga only), the file area
668 consists of all characters up to and including the final such character;
669 otherwise the file area is null. If the remaining file name contains
670 `\..', the file extension consists of all such characters from the last
671 `\..' to the end, otherwise the file extension is null.
673 We can scan such file names easily by using two global variables that keep track
674 of the occurrences of area and extension delimiters:
677 pool_pointer area_delimiter; /* the most recent `\./', if any */
678 pool_pointer ext_delimiter; /* the relevant `\..', if any */
681 @ Input files that can't be found in the user's area may appear in a standard
682 system area called |TEX_area|. Font metric files whose areas are not given
683 explicitly are assumed to appear in a standard system area called
684 |TEX_font_area|. $\Omega$'s compiled translation process files whose areas
685 are not given explicitly are assumed to appear in a standard system area.
686 These system area names will, of course, vary from place to place.
689 #define append_to_fn(A) do { \
690 c=(A); \
691 if (c!='"') { \
692 if (k<file_name_size) fn[k++]=(unsigned char)(c); \
694 } while (0)
697 char *pack_file_name(str_number n, str_number a, str_number e)
699 ASCII_code c; /* character being packed */
700 unsigned char *j; /* index into |str_pool| */
701 int k = 0; /* number of positions filled in |fn| */
702 unsigned char *fn = xmallocarray(packed_ASCII_code,
703 str_length(a) + str_length(n) +
704 str_length(e) + 1);
705 for (j = str_string(a); j < str_string(a) + str_length(a); j++)
706 append_to_fn(*j);
707 for (j = str_string(n); j < str_string(n) + str_length(n); j++)
708 append_to_fn(*j);
709 for (j = str_string(e); j < str_string(e) + str_length(e); j++)
710 append_to_fn(*j);
711 fn[k] = 0;
712 return (char *) fn;
717 @ A messier routine is also needed, since format file names must be scanned
718 before \TeX's string mechanism has been initialized. We shall use the
719 global variable |TEX_format_default| to supply the text for default system areas
720 and extensions related to format files.
721 @^system dependencies@>
723 Under {\mc UNIX} we don't give the area part, instead depending
724 on the path searching that will happen during file opening. Also, the
725 length will be set in the main program.
728 char *TEX_format_default;
731 @ This part of the program becomes active when a ``virgin'' \TeX\ is trying to get going,
732 just after the preliminary initialization, or when the user is substituting another
733 format file by typing `\.\&' after the initial `\.{**}' prompt. The buffer
734 contains the first line of input in |buffer[loc..(last-1)]|, where
735 |loc<last| and |buffer[loc]<>" "|.
738 char *open_fmt_file(void)
740 int j; /* the first space after the format file name */
741 char *fmt = NULL;
742 int dist;
743 j = iloc;
744 if (buffer[iloc] == '&') {
745 incr(iloc);
746 j = iloc;
747 buffer[last] = ' ';
748 while (buffer[j] != ' ')
749 incr(j);
750 fmt = xmalloc((unsigned) (j - iloc + 1));
751 strncpy(fmt, (char *) (buffer + iloc), (size_t) (j - iloc));
752 fmt[j - iloc] = 0;
753 dist = (int) (strlen(fmt) - strlen(DUMP_EXT));
754 if (!(strstr(fmt, DUMP_EXT) == fmt + dist))
755 fmt = concat(fmt, DUMP_EXT);
756 if (zopen_w_input(&fmt_file, fmt, DUMP_FORMAT, FOPEN_RBIN_MODE))
757 goto FOUND;
758 wake_up_terminal();
759 fprintf(stdout, "Sorry, I can't find the format `%s'; will try `%s'.\n",
760 fmt, TEX_format_default);
761 update_terminal();
763 /* now pull out all the stops: try for the system \.{plain} file */
764 fmt = TEX_format_default;
765 if (!zopen_w_input(&fmt_file, fmt, DUMP_FORMAT, FOPEN_RBIN_MODE)) {
766 wake_up_terminal();
767 fprintf(stdout, "I can't find the format file `%s'!\n",
768 TEX_format_default);
769 return NULL;
771 FOUND:
772 iloc = j;
773 return fmt;
777 @ The global variable |name_in_progress| is used to prevent recursive
778 use of |scan_file_name|, since the |begin_name| and other procedures
779 communicate via global variables. Recursion would arise only by
780 devious tricks like `\.{\\input\\input f}'; such attempts at sabotage
781 must be thwarted. Furthermore, |name_in_progress| prevents \.{\\input}
782 @^recursion@>
783 from being initiated when a font size specification is being scanned.
785 Another global variable, |job_name|, contains the file name that was first
786 \.{\\input} by the user. This name is extended by `\.{.log}' and `\.{.dvi}'
787 and `\.{.fmt}' in the names of \TeX's output files.
790 boolean name_in_progress; /* is a file name being scanned? */
791 str_number job_name; /* principal file name */
792 boolean log_opened_global; /* has the transcript file been opened? */
795 @ Initially |job_name=0|; it becomes nonzero as soon as the true name is known.
796 We have |job_name=0| if and only if the `\.{log}' file has not been opened,
797 except of course for a short time just after |job_name| has become nonzero.
800 unsigned char *texmf_log_name; /* full name of the log file */
802 @ The |open_log_file| routine is used to open the transcript file and to help
803 it catch up to what has previously been printed on the terminal.
806 void open_log_file(void)
808 int old_setting; /* previous |selector| setting */
809 int k; /* index into |buffer| */
810 int l; /* end of first input line */
811 char *fn;
812 old_setting = selector;
813 if (job_name == 0)
814 job_name = getjobname(maketexstring("texput")); /* TODO */
815 fn = pack_job_name(".fls");
816 recorder_change_filename(fn);
817 fn = pack_job_name(".log");
818 while (!lua_a_open_out(&log_file, fn, 0)) {
819 /* Try to get a different log file name */
820 /* Sometimes |open_log_file| is called at awkward moments when \TeX\ is
821 unable to print error messages or even to |show_context|.
822 The |prompt_file_name| routine can result in a |fatal_error|, but the |error|
823 routine will not be invoked because |log_opened| will be false.
825 The normal idea of |batch_mode| is that nothing at all should be written
826 on the terminal. However, in the unusual case that
827 no log file could be opened, we make an exception and allow
828 an explanatory message to be seen.
830 Incidentally, the program always refers to the log file as a `\.{transcript
831 file}', because some systems cannot use the extension `\.{.log}' for
832 this file.
834 selector = term_only;
835 fn = prompt_file_name("transcript file name", ".log");
837 texmf_log_name = (unsigned char *) xstrdup(fn);
838 selector = log_only;
839 log_opened_global = true;
840 if (callback_defined(start_run_callback) == 0) {
841 /* Print the banner line, including current date and time */
842 log_banner(luatex_version_string);
844 input_stack[input_ptr] = cur_input; /* make sure bottom level is in memory */
845 tprint_nl("**");
846 l = input_stack[0].limit_field; /* last position of first line */
847 if (buffer[l] == end_line_char)
848 decr(l); /* TODO: multichar endlinechar */
849 for (k = 1; k <= l; k++)
850 print_char(buffer[k]);
851 print_ln(); /* now the transcript file contains the first line of input */
853 flush_loggable_info(); /* should be done always */
854 selector = old_setting + 2; /* |log_only| or |term_and_log| */
857 @ This function is needed by synctex to make its log appear in the right
858 spot when |output_directory| is set.
861 char *get_full_log_name (void)
863 if (output_directory) {
864 char *ret = xmalloc(strlen((char *)texmf_log_name)+2+strlen(output_directory));
865 ret = strcpy(ret, output_directory);
866 strcat(ret, "/");
867 strcat(ret, (char *)texmf_log_name);
868 return ret;
869 } else {
870 return xstrdup((const char*)texmf_log_name);
874 @ Synctex uses this to get the anchored path of an input file.
877 char *luatex_synctex_get_current_name (void)
879 char *pwdbuf = NULL, *ret;
880 if (kpse_absolute_p(fullnameoffile, false)) {
881 return xstrdup(fullnameoffile);
883 pwdbuf = xgetcwd();
884 ret = concat3(pwdbuf, DIR_SEP_STRING, fullnameoffile);
885 free(pwdbuf) ;
886 return ret;
890 @ Let's turn now to the procedure that is used to initiate file reading
891 when an `\.{\\input}' command is being processed.
894 void start_input(void)
895 { /* \TeX\ will \.{\\input} something */
896 str_number temp_str;
897 char *fn;
898 do {
899 get_x_token();
900 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
902 back_input();
903 if (cur_cmd != left_brace_cmd) {
904 scan_file_name(); /* set |cur_name| to desired file name */
905 } else {
906 scan_file_name_toks();
908 fn = pack_file_name(cur_name, cur_area, cur_ext);
909 while (1) {
910 begin_file_reading(); /* set up |cur_file| and new level of input */
911 if (lua_a_open_in(&cur_file, fn, 0))
912 break;
913 end_file_reading(); /* remove the level that didn't work */
914 fn = prompt_file_name("input file name", "");
916 iname = maketexstring(fullnameoffile);
917 /* Now that we have |fullnameoffile|, it is time to post-adjust
918 |cur_name| and |cur_ext| for trailing |.tex| */
920 char *n, *p;
921 n = p = fullnameoffile + strlen(fullnameoffile);
922 while (p>fullnameoffile) {
923 p--;
924 if (IS_DIR_SEP(*p)) {
925 break;
928 if (IS_DIR_SEP(*p)) {
929 p++;
931 while (n>fullnameoffile) {
932 n--;
933 if (*n == '.') {
934 break;
937 if (n>p) {
938 int q = *n;
939 cur_ext = maketexstring(n);
940 *n = 0;
941 cur_name = maketexstring(p);
942 *n = q;
947 source_filename_stack[in_open] = iname;
948 full_source_filename_stack[in_open] = xstrdup(fullnameoffile);
949 /* we can try to conserve string pool space now */
950 temp_str = search_string(iname);
951 if (temp_str > 0) {
952 flush_str(iname);
953 iname = temp_str;
955 if (job_name == 0) {
956 job_name = getjobname(cur_name);
957 open_log_file();
959 /* |open_log_file| doesn't |show_context|, so |limit|
960 and |loc| needn't be set to meaningful values yet */
961 report_start_file(filetype_tex,fullnameoffile);
962 incr(open_parens);
963 update_terminal();
964 istate = new_line;
965 /* Prepare new file {\sl Sync\TeX} information */
966 synctexstartinput(); /* Give control to the {\sl Sync\TeX} controller */
968 /* Read the first line of the new file */
969 /* Here we have to remember to tell the |lua_input_ln| routine not to
970 start with a |get|. If the file is empty, it is considered to
971 contain a single blank line. */
973 line = 1;
974 if (lua_input_ln(cur_file, 0, false)) {
977 firm_up_the_line();
978 if (end_line_char_inactive)
979 decr(ilimit);
980 else
981 buffer[ilimit] = (packed_ASCII_code) end_line_char;
982 first = ilimit + 1;
983 iloc = istart;
986 @ Read and write dump files through zlib
988 @ Earlier versions recast |*f| from |FILE *| to |gzFile|, but there is
989 no guarantee that these have the same size, so a static variable
990 is needed.
993 static gzFile gz_fmtfile = NULL;
995 @ As distributed, the dump files are
996 architecture dependent; specifically, BigEndian and LittleEndian
997 architectures produce different files. These routines always output
998 BigEndian files. This still does not guarantee them to be
999 architecture-independent, because it is possible to make a format
1000 that dumps a glue ratio, i.e., a floating-point number. Fortunately,
1001 none of the standard formats do that.
1004 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1006 /* This macro is always invoked as a statement. It assumes a variable
1007 `temp'. */
1008 # define SWAP(x, y) do { temp = x; x = y; y = temp; } while (0)
1010 /* Make the NITEMS items pointed at by P, each of size SIZE, be the
1011 opposite-endianness of whatever they are now. */
1012 static void
1013 swap_items(char *pp, int nitems, int size)
1015 char temp;
1016 unsigned total = (unsigned) (nitems * size);
1017 char *q = xmalloc(total);
1018 char *p = q;
1019 memcpy(p,pp,total);
1020 /* Since `size' does not change, we can write a while loop for each
1021 case, and avoid testing `size' for each time. */
1022 switch (size) {
1023 /* 16-byte items happen on the DEC Alpha machine when we are not
1024 doing sharable memory dumps. */
1025 case 16:
1026 while (nitems--) {
1027 SWAP(p[0], p[15]);
1028 SWAP(p[1], p[14]);
1029 SWAP(p[2], p[13]);
1030 SWAP(p[3], p[12]);
1031 SWAP(p[4], p[11]);
1032 SWAP(p[5], p[10]);
1033 SWAP(p[6], p[9]);
1034 SWAP(p[7], p[8]);
1035 p += size;
1037 break;
1039 case 12:
1040 while (nitems--) {
1041 SWAP(p[0], p[11]);
1042 SWAP(p[1], p[10]);
1043 SWAP(p[2], p[9]);
1044 SWAP(p[3], p[8]);
1045 SWAP(p[4], p[7]);
1046 SWAP(p[5], p[6]);
1047 p += size;
1049 break;
1051 case 8:
1052 while (nitems--) {
1053 SWAP(p[0], p[7]);
1054 SWAP(p[1], p[6]);
1055 SWAP(p[2], p[5]);
1056 SWAP(p[3], p[4]);
1057 p += size;
1059 break;
1061 case 4:
1062 while (nitems--) {
1063 SWAP(p[0], p[3]);
1064 SWAP(p[1], p[2]);
1065 p += size;
1067 break;
1069 case 2:
1070 while (nitems--) {
1071 SWAP(p[0], p[1]);
1072 p += size;
1074 break;
1076 case 1:
1077 /* Nothing to do. */
1078 break;
1080 default:
1081 FATAL1("Can't swap a %d-byte item for (un)dumping", size);
1083 memcpy(pp,q,total);
1084 xfree(q);
1086 #endif /* not WORDS_BIGENDIAN and not NO_DUMP_SHARE */
1088 @ That second swap is to make sure following calls don't get
1089 confused in the case of |dump_things|.
1092 void do_zdump(char *p, int item_size, int nitems, FILE * out_file)
1094 int err;
1095 (void) out_file;
1096 if (nitems == 0)
1097 return;
1098 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1099 swap_items(p, nitems, item_size);
1100 #endif
1101 if (gzwrite(gz_fmtfile, (void *) p, (unsigned) (item_size * nitems)) !=
1102 item_size * nitems) {
1103 fprintf(stderr, "! Could not write %d %d-byte item(s): %s.\n", nitems,
1104 item_size, gzerror(gz_fmtfile, &err));
1105 uexit(1);
1107 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1108 swap_items(p, nitems, item_size);
1109 #endif
1112 @ @c
1113 void do_zundump(char *p, int item_size, int nitems, FILE * in_file)
1115 int err;
1116 (void) in_file;
1117 if (nitems == 0)
1118 return;
1119 if (gzread(gz_fmtfile, (void *) p, (unsigned) (item_size * nitems)) <= 0) {
1120 fprintf(stderr, "Could not undump %d %d-byte item(s): %s.\n",
1121 nitems, item_size, gzerror(gz_fmtfile, &err));
1122 uexit(1);
1124 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1125 swap_items(p, nitems, item_size);
1126 #endif
1129 @ @c
1130 #define COMPRESSION "R3"
1132 boolean zopen_w_input(FILE ** f, const char *fname, int format,
1133 const_string fopen_mode)
1135 int callbackid;
1136 int res;
1137 char *fnam;
1138 callbackid = callback_defined(find_format_file_callback);
1139 if (callbackid > 0) {
1140 res = run_callback(callbackid, "S->S", fname, &fnam);
1141 if (res && fnam && strlen(fnam) > 0) {
1142 *f = fopen(fnam, fopen_mode);
1143 if (*f == NULL) {
1144 return 0;
1146 } else {
1147 return 0;
1149 } else {
1150 res = luatex_open_input(f, fname, format, fopen_mode, true);
1152 if (res) {
1153 gz_fmtfile = gzdopen(fileno(*f), "rb" COMPRESSION);
1155 return res;
1158 @ @c
1159 boolean zopen_w_output(FILE ** f, const char *s, const_string fopen_mode)
1161 int res = 1;
1162 if (luainit) {
1163 *f = fopen(s, fopen_mode);
1164 if (*f == NULL) {
1165 return 0;
1167 } else {
1168 res = luatex_open_output(f, s, fopen_mode);
1170 if (res) {
1171 gz_fmtfile = gzdopen(fileno(*f), "wb" COMPRESSION);
1173 return res;
1176 @ @c
1177 void zwclose(FILE * f)
1179 (void) f;
1180 gzclose(gz_fmtfile);
1183 @ create the dvi or pdf file
1185 int open_outfile(FILE ** f, const char *name, const char *mode)
1187 FILE *res;
1188 res = fopen(name, mode);
1189 if (res != NULL) {
1190 *f = res;
1191 return 1;
1193 return 0;
1197 @ the caller should set |tfm_buffer=NULL| and |tfm_size=0|
1199 int readbinfile(FILE * f, unsigned char **tfm_buffer, int *tfm_size)
1201 void *buf;
1202 int size;
1203 if (fseek(f, 0, SEEK_END) == 0) {
1204 size = (int) ftell(f);
1205 if (size > 0) {
1206 buf = xmalloc((unsigned) size);
1207 if (fseek(f, 0, SEEK_SET) == 0) {
1208 if (fread((void *) buf, (size_t) size, 1, f) == 1) {
1209 *tfm_buffer = (unsigned char *) buf;
1210 *tfm_size = size;
1211 return 1;
1214 } else {
1215 *tfm_buffer = NULL;
1216 *tfm_size = 0;
1217 return 1;
1219 } /* seek failed, or zero-sized file */
1220 return 0;
1223 @ Like |os.execute()|, the |runpopen()| function is called only when
1224 |shellenabledp == 1|. Unlike |os.execute()| we write errors to stderr, since we
1225 have nowhere better to use; and of course we return a file handle (or NULL)
1226 instead of a status indicator.
1229 static FILE *runpopen(char *cmd, const char *mode)
1231 FILE *f = NULL;
1232 char *safecmd = NULL;
1233 char *cmdname = NULL;
1234 int allow;
1236 #ifdef WIN32
1237 char *pp;
1239 for (pp = cmd; *pp; pp++) {
1240 if (*pp == '\'') *pp = '"';
1242 #endif
1244 /* If restrictedshell == 0, any command is allowed. */
1245 if (restrictedshell == 0) {
1246 allow = 1;
1247 } else {
1248 const char *thecmd = cmd;
1249 allow = shell_cmd_is_allowed(thecmd, &safecmd, &cmdname);
1251 if (allow == 1)
1252 f = popen(cmd, mode);
1253 else if (allow == 2)
1254 f = popen(safecmd, mode);
1255 else if (allow == -1)
1256 fprintf(stderr, "\nrunpopen quotation error in command line: %s\n",
1257 cmd);
1258 else
1259 fprintf(stderr, "\nrunpopen command not allowed: %s\n", cmdname);
1261 if (safecmd)
1262 free(safecmd);
1263 if (cmdname)
1264 free(cmdname);
1265 return f;
1268 @ piped I/O
1271 @ The code that implements |popen()| needs an array for tracking
1272 possible pipe file pointers, because these need to be
1273 closed using |pclose()|.
1276 #define NUM_PIPES 16
1277 static FILE *pipes[NUM_PIPES];
1279 #ifdef WIN32
1280 FILE *Poptr;
1281 #endif
1283 boolean open_in_or_pipe(FILE ** f_ptr, char *fn, int filefmt,
1284 const_string fopen_mode, boolean must_exist)
1286 string fname = NULL;
1287 int i; /* iterator */
1289 /* opening a read pipe is straightforward, only have to
1290 skip past the pipe symbol in the file name. filename
1291 quoting is assumed to happen elsewhere (it does :-)) */
1293 if (shellenabledp && *fn == '|') {
1294 /* the user requested a pipe */
1295 *f_ptr = NULL;
1296 fname = (string) xmalloc((unsigned) (strlen(fn) + 1));
1297 strcpy(fname, fn);
1298 if (fullnameoffile)
1299 free(fullnameoffile);
1300 fullnameoffile = xstrdup(fname);
1301 recorder_record_input(fname + 1);
1302 *f_ptr = runpopen(fname + 1, "r");
1303 free(fname);
1304 for (i = 0; i < NUM_PIPES; i++) {
1305 if (pipes[i] == NULL) {
1306 pipes[i] = *f_ptr;
1307 break;
1310 if (*f_ptr)
1311 setvbuf(*f_ptr, (char *) NULL, _IONBF, 0);
1312 #ifdef WIN32
1313 Poptr = *f_ptr;
1314 #endif
1316 return *f_ptr != NULL;
1319 return luatex_open_input(f_ptr, fn, filefmt, fopen_mode, must_exist);
1323 boolean open_out_or_pipe(FILE ** f_ptr, char *fn, const_string fopen_mode)
1325 string fname;
1326 int i; /* iterator */
1328 /* opening a write pipe takes a little bit more work, because TeX
1329 will perhaps have appended ".tex". To avoid user confusion as
1330 much as possible, this extension is stripped only when the command
1331 is a bare word. Some small string trickery is needed to make
1332 sure the correct number of bytes is free()-d afterwards */
1334 if (shellenabledp && *fn == '|') {
1335 /* the user requested a pipe */
1336 fname = (string) xmalloc((unsigned) (strlen(fn) + 1));
1337 strcpy(fname, fn);
1338 if (strchr(fname, ' ') == NULL && strchr(fname, '>') == NULL) {
1339 /* mp and mf currently do not use this code, but it
1340 is better to be prepared */
1341 if (STREQ((fname + strlen(fname) - 3), "tex"))
1342 *(fname + strlen(fname) - 4) = 0;
1343 *f_ptr = runpopen(fname + 1, "w");
1344 *(fname + strlen(fname)) = '.';
1345 } else {
1346 *f_ptr = runpopen(fname + 1, "w");
1348 recorder_record_output(fname + 1);
1349 free(fname);
1351 for (i = 0; i < NUM_PIPES; i++) {
1352 if (pipes[i] == NULL) {
1353 pipes[i] = *f_ptr;
1354 break;
1358 if (*f_ptr)
1359 setvbuf(*f_ptr, (char *) NULL, _IONBF, 0);
1361 return *f_ptr != NULL;
1364 return luatex_open_output(f_ptr, fn, fopen_mode);
1368 void close_file_or_pipe(FILE * f)
1370 int i; /* iterator */
1372 if (shellenabledp) {
1373 for (i = 0; i <= 15; i++) {
1374 /* if this file was a pipe, |pclose()| it and return */
1375 if (pipes[i] == f) {
1376 if (f) {
1377 pclose(f);
1378 #ifdef WIN32
1379 Poptr = NULL;
1380 #endif
1382 pipes[i] = NULL;
1383 return;
1387 close_file(f);