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
/>.
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
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
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)) {
94 char
*ftemp
= concat3
(output_directory
, DIR_SEP_STRING
, s
);
95 f_ptr
= fopen
(ftemp
, "rb"); /* this code is used for input files only
*/
107 @ find an \.
{\\input
} or \.
{\\read
} file. |n| differentiates between those case.
110 int kpse_available
(const char
*m
) {
112 fprintf
(stdout
,"missing kpse replacement callback '%s', quitting\n",m
);
118 char
*luatex_find_read_file
(const char
*s
, int n
, int callback_index
)
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
);
128 ftemp
= kpse_find_file
(s
, kpse_tex_format
, 1);
132 free
(fullnameoffile
);
133 fullnameoffile
= xstrdup
(ftemp
);
138 @ find other files types
140 char
*luatex_find_file
(const char
*s
, int callback_index
)
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);
153 case find_sfd_file_callback
:
154 ftemp
= kpse_find_file
(s
, kpse_sfd_format
, 0);
156 case find_map_file_callback
:
157 ftemp
= kpse_find_file
(s
, kpse_fontmap_format
, 0);
159 case find_type1_file_callback
:
160 ftemp
= kpse_find_file
(s
, kpse_type1_format
, 0);
162 case find_truetype_file_callback
:
163 ftemp
= kpse_find_file
(s
, kpse_truetype_format
, 0);
165 case find_opentype_file_callback
:
166 ftemp
= kpse_find_file
(s
, kpse_opentype_format
, 0);
168 ftemp
= kpse_find_file
(s
, kpse_truetype_format
, 0);
170 case find_data_file_callback
:
171 ftemp
= find_in_output_directory
(s
);
173 ftemp
= kpse_find_file
(s
, kpse_tex_format
, 0);
175 case find_font_file_callback
:
176 ftemp
= kpse_find_file
(s
, kpse_ofm_format
, 0);
178 ftemp
= kpse_find_file
(s
, kpse_tfm_format
, 0);
180 case find_vf_file_callback
:
181 ftemp
= kpse_find_file
(s
, kpse_ovf_format
, 0);
183 ftemp
= kpse_find_file
(s
, kpse_vf_format
, 0);
185 case find_cidmap_file_callback
:
186 ftemp
= kpse_find_file
(s
, kpse_cid_format
, 0);
190 ("luatex_find_file(): do not know how to handle file %s of type %d\n",
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
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.
217 luatex_open_input
(FILE ** f_ptr
, const char
*fn
, int filefmt
,
218 const_string fopen_mode
, boolean must_exist
)
221 /* We havent found anything yet.
*/
224 free
(fullnameoffile
);
225 fullnameoffile
= NULL;
226 fname
= kpse_find_file
(fn
, (kpse_file_format_type
) filefmt
, must_exist
);
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]))) {
238 while
(fname
[i
+ 2] != 0) {
239 fname
[i
] = fname
[i
+ 2];
244 /* This fopen is not allowed to fail.
*/
245 *f_ptr
= xfopen
(fname
, fopen_mode
);
248 recorder_record_input
(fname
);
250 return
*f_ptr
!= NULL;
254 boolean luatex_open_output
(FILE ** f_ptr
, const char
*fn
,
255 const_string fopen_mode
)
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
);
267 /* Is the filename openable as given?
*/
268 *f_ptr
= fopen
(fname
, fopen_mode
);
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
);
280 recorder_record_output
(fname
);
283 return
*f_ptr
!= NULL;
288 boolean lua_a_open_in
(alpha_file
* f
, char
*fn
, int n
)
291 char
*fnam
; /* string returned by find callback
*/
293 boolean ret
= true
; /* return value
*/
294 boolean file_ok
= true
; /* the status so far
*/
296 input_file_callback_id
[iindex
] = 0;
298 read_file_callback_id
[n
] = 0;
303 fnam
= luatex_find_read_file
(fn
, n
, find_read_file_callback
);
306 callback_id
= callback_defined
(open_read_file_callback
);
307 if
(callback_id
> 0) {
308 k
= run_and_save_callback
(callback_id
, "S->", fnam
);
312 input_file_callback_id
[iindex
] = k
;
314 read_file_callback_id
[n
] = k
;
316 file_ok
= false
; /* read failed
*/
318 } else
{ /* no read callback
*/
319 if
(openinnameok
(fnam
)) {
321 open_in_or_pipe
(f
, fnam
, kpse_tex_format
, FOPEN_RBIN_MODE
,
322 (n
== 0 ? true
: false
));
324 file_ok
= false
; /* open failed
*/
335 boolean lua_a_open_out
(alpha_file
* f
, char
*fn
, int n
)
341 callback_id
= callback_defined
(find_write_file_callback
);
342 if
(callback_id
> 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
*/
349 ret
= open_outfile
(f
, fn
, FOPEN_W_MODE
);
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
);
367 boolean lua_b_open_out
(alpha_file
* f
, char
*fn
)
373 callback_id
= callback_defined
(find_output_file_callback
);
374 if
(callback_id
> 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
);
381 if
(openoutnameok
(fn
)) {
382 ret
= luatex_open_output
(f
, fn
, FOPEN_WBIN_MODE
);
389 void lua_a_close_in
(alpha_file f
, int n
)
390 { /* close a text file
*/
393 callback_id
= input_file_callback_id
[iindex
];
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
);
400 input_file_callback_id
[iindex
] = 0;
402 read_file_callback_id
[n
] = 0;
404 close_file_or_pipe
(f
);
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
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.
471 boolean lua_input_ln
(alpha_file f
, int n
, boolean bypass_eoln
)
476 (void
) bypass_eoln
; /* todo
: variable can be removed
*/
478 callback_id
= input_file_callback_id
[iindex
];
480 callback_id
= read_file_callback_id
[n
];
481 if
(callback_id
> 0) {
485 run_saved_callback
(callback_id
, "reader", "->l", &last_ptr);
486 if
((lua_result
== true
) && (last_ptr != 0)) {
488 if
(last
> max_buf_stack
)
489 max_buf_stack
= last
;
494 lua_result
= input_ln
(f
, bypass_eoln
);
496 if
(lua_result
== true
) {
497 /* Fix up the input buffer using callbacks
*/
499 callback_id
= callback_defined
(process_input_buffer_callback
);
500 if
(callback_id
> 0) {
503 run_callback
(callback_id
, "l->l", (last
- first
),
505 if
((lua_result
== true
) && (last_ptr != 0)) {
507 if
(last
> max_buf_stack
)
508 max_buf_stack
= last
;
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.
)
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
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
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
*/
571 while
((iloc
< last
) && (buffer[iloc] == ' '))
579 fputs
("**", term_out
);
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
);
587 while
((iloc
< last
) && (buffer[iloc] == ' '))
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
*/
612 for
(k
= first
; k
<= last
- 1; k
++)
613 print_char
(buffer
[k
]);
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
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
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
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
{ \
696 if (k<file_name_size) fn[k++]=(unsigned char)(c); \
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) +
709 for (j = str_string(a); j < str_string(a) + str_length(a); j++)
711 for (j = str_string(n); j < str_string(n) + str_length(n); j++)
713 for (j = str_string(e); j < str_string(e) + str_length(e); j++)
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 */
748 if (buffer[iloc] == '&') {
752 while (buffer[j] != ' ')
754 fmt = xmalloc((unsigned) (j - iloc + 1));
755 strncpy(fmt, (char *) (buffer + iloc), (size_t) (j - iloc));
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))
763 fprintf(stdout, "Sorry
, I can't find the format `
%s'
; will try `
%s'.\n
",
764 fmt, TEX_format_default);
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)) {
771 fprintf(stdout, "I can't find the format file `
%s'
!\n
",
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}
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 */
816 old_setting = selector;
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
838 selector = term_only;
839 fn = prompt_file_name("transcript file name
", ".log
");
841 texmf_log_name = (unsigned char *) xstrdup(fn);
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 */
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);
871 strcat(ret, (char *)texmf_log_name);
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);
888 ret = concat3(pwdbuf, DIR_SEP_STRING, fullnameoffile);
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 */
904 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
907 if (cur_cmd != left_brace_cmd) {
908 scan_file_name(); /* set |cur_name| to desired file name */
910 scan_file_name_toks();
912 fn = pack_file_name(cur_name, cur_area, cur_ext);
914 begin_file_reading(); /* set up |cur_file| and new level of input */
915 if (lua_a_open_in(&cur_file, fn, 0))
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| */
925 n = p = fullnameoffile + strlen(fullnameoffile);
926 while (p>fullnameoffile) {
928 if (IS_DIR_SEP(*p)) {
932 if (IS_DIR_SEP(*p)) {
935 while (n>fullnameoffile) {
943 cur_ext = maketexstring(n);
945 cur_name = maketexstring(p);
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);
960 job_name = getjobname(cur_name);
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);
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. */
978 if (lua_input_ln(cur_file, 0, false)) {
982 if (end_line_char_inactive)
985 buffer[ilimit] = (packed_ASCII_code) end_line_char_par;
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
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
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. */
1017 swap_items(char *pp, int nitems, int size)
1020 unsigned total = (unsigned) (nitems * size);
1021 char *q = xmalloc(total);
1024 /* Since `size' does not change, we can write a while loop for each
1025 case, and avoid testing `size' for each time. */
1027 /* 16-byte items happen on the DEC Alpha machine when we are not
1028 doing sharable memory dumps. */
1081 /* Nothing to do. */
1085 FATAL1("Can't swap a
%d-byte item for
(un
)dumping
", size);
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)
1102 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1103 swap_items(p, nitems, item_size);
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));
1111 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1112 swap_items(p, nitems, item_size);
1117 void do_zundump(char *p, int item_size, int nitems, FILE * in_file)
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));
1128 #if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE)
1129 swap_items(p, nitems, item_size);
1134 #define COMPRESSION "R3
"
1136 boolean zopen_w_input(FILE ** f, const char *fname, int format,
1137 const_string fopen_mode)
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);
1154 res = luatex_open_input(f, fname, format, fopen_mode, true);
1157 gz_fmtfile = gzdopen(fileno(*f), "rb
" COMPRESSION);
1163 boolean zopen_w_output(FILE ** f, const char *s, const_string fopen_mode)
1167 *f = fopen(s, fopen_mode);
1172 res = luatex_open_output(f, s, fopen_mode);
1175 gz_fmtfile = gzdopen(fileno(*f), "wb
" COMPRESSION);
1181 void zwclose(FILE * f)
1184 gzclose(gz_fmtfile);
1187 @ create the dvi or pdf file
1189 int open_outfile(FILE ** f, const char *name, const char *mode)
1192 res = fopen(name, mode);
1201 @ the caller should set |tfm_buffer=NULL| and |tfm_size=0|
1203 int readbinfile(FILE * f, unsigned char **tfm_buffer, int *tfm_size)
1207 if (fseek(f, 0, SEEK_END) == 0) {
1208 size = (int) ftell(f);
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;
1223 } /* seek failed, or zero-sized file */
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)
1236 char *safecmd = NULL;
1237 char *cmdname = NULL;
1243 for (pp = cmd; *pp; pp++) {
1244 if (*pp == '\'') *pp = '"'
;
1248 /* If restrictedshell
== 0, any command is allowed.
*/
1249 if
(restrictedshell
== 0) {
1252 const char
*thecmd
= cmd
;
1253 allow
= shell_cmd_is_allowed
(thecmd
, &safecmd, &cmdname);
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",
1263 fprintf
(stderr
, "\nrunpopen command not allowed: %s\n", cmdname
);
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
];
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
*/
1300 fname
= (string
) xmalloc
((unsigned
) (strlen
(fn
) + 1));
1303 free
(fullnameoffile
);
1304 fullnameoffile
= xstrdup
(fname
);
1305 recorder_record_input
(fname
+ 1);
1306 *f_ptr
= runpopen
(fname
+ 1, "r");
1308 for
(i
= 0; i
< NUM_PIPES
; i
++) {
1309 if
(pipes
[i
] == NULL) {
1315 setvbuf
(*f_ptr
, (char
*) NULL, _IONBF
, 0);
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
)
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));
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
)) = '.'
;
1350 *f_ptr
= runpopen
(fname
+ 1, "w");
1352 recorder_record_output
(fname
+ 1);
1355 for
(i
= 0; i
< NUM_PIPES
; i
++) {
1356 if
(pipes
[i
] == NULL) {
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
) {