| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2009-2010 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <kpathsea/absolute.h> |
| 26 |
|
| 27 |
/*tex |
| 28 |
|
| 29 |
The bane of portability is the fact that different operating systems treat input |
| 30 |
and output quite differently, perhaps because computer scientists have not given |
| 31 |
sufficient attention to this problem. People have felt somehow that input and |
| 32 |
output are not part of ``real'' programming. Well, it is true that some kinds of |
| 33 |
programming are more fun than others. With existing input/output conventions |
| 34 |
being so diverse and so messy, the only sources of joy in such parts of the code |
| 35 |
are the rare occasions when one can find a way to make the program a little less |
| 36 |
bad than it might have been. We have two choices, either to attack I/O now and |
| 37 |
get it over with, or to postpone I/O until near the end. Neither prospect is very |
| 38 |
attractive, so let's get it over with. |
| 39 |
|
| 40 |
The basic operations we need to do are (1)~inputting and outputting of text, to |
| 41 |
or from a file or the user's terminal; (2)~inputting and outputting of eight-bit |
| 42 |
bytes, to or from a file; (3)~instructing the operating system to initiate |
| 43 |
(``open'') or to terminate (``close'') input or output from a specified file; |
| 44 |
(4)~testing whether the end of an input file has been reached. |
| 45 |
|
| 46 |
\TeX\ needs to deal with two kinds of files. We shall use the term |alpha_file| |
| 47 |
for a file that contains textual data, and the term |byte_file| for a file that |
| 48 |
contains eight-bit binary information. These two types turn out to be the same on |
| 49 |
many computers, but sometimes there is a significant distinction, so we shall be |
| 50 |
careful to distinguish between them. Standard protocols for transferring such |
| 51 |
files from computer to computer, via high-speed networks, are now becoming |
| 52 |
available to more and more communities of users. |
| 53 |
|
| 54 |
The program actually makes use also of a third kind of file, called a |
| 55 |
|word_file|, when dumping and reloading base information for its own |
| 56 |
initialization. We shall define a word file later; but it will be possible for us |
| 57 |
to specify simple operations on word files before they are defined. |
| 58 |
|
| 59 |
We finally did away with |nameoffile| and |namelength|, but the variables have to |
| 60 |
be kept otherwise there will be link errors from |openclose.c| in the web2c |
| 61 |
library |
| 62 |
|
| 63 |
*/ |
| 64 |
|
| 65 |
char *nameoffile; |
| 66 |
int namelength; |
| 67 |
|
| 68 |
/*tex |
| 69 |
|
| 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 integer |
| 72 |
to uniquely identify a callback table. This id replaces the file point |f| in |
| 73 |
this case, because the input does not have to be a file in the traditional |
| 74 |
sense. |
| 75 |
|
| 76 |
Signalling this fact is achieved by having two arrays of integers. |
| 77 |
|
| 78 |
*/ |
| 79 |
|
| 80 |
int *input_file_callback_id; |
| 81 |
int read_file_callback_id[17]; |
| 82 |
|
| 83 |
/*tex |
| 84 |
|
| 85 |
Here we handle |-output-directory|. We assume that it is OK to look here |
| 86 |
first. Possibly it would be better to replace lookups in "." with lookups in |
| 87 |
the |output_directory| followed by "." but to do this requires much more |
| 88 |
invasive surgery in libkpathsea. |
| 89 |
|
| 90 |
*/ |
| 91 |
|
| 92 |
static char *find_in_output_directory(const char *s) |
| 93 |
{ |
| 94 |
if (output_directory && !kpse_absolute_p(s, false)) { |
| 95 |
char *ftemp = concat3(output_directory, DIR_SEP_STRING, s); |
| 96 |
char *ret_val = NULL ; |
| 97 |
/*tex This code is used for input files only. */ |
| 98 |
ret_val = kpse_find_file(ftemp, kpse_tex_format, 1); |
| 99 |
free(ftemp); |
| 100 |
return ret_val; |
| 101 |
} |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
/*tex |
| 106 |
|
| 107 |
Find an \.{\\input} or \.{\\read} file. |n| differentiates between those |
| 108 |
case. |
| 109 |
|
| 110 |
*/ |
| 111 |
|
| 112 |
int kpse_available(const char *m) { |
| 113 |
if (!kpse_init) { |
| 114 |
fprintf(stdout,"missing kpse replacement callback '%s', quitting\n",m); |
| 115 |
exit(1); |
| 116 |
} |
| 117 |
return 1 ; |
| 118 |
} |
| 119 |
|
| 120 |
char *luatex_find_read_file(const char *s, int n, int callback_index) |
| 121 |
{ |
| 122 |
char *ftemp = NULL; |
| 123 |
int callback_id = callback_defined(callback_index); |
| 124 |
if (callback_id > 0) { |
| 125 |
(void) run_callback(callback_id, "dS->R", n, s, &ftemp); |
| 126 |
} else if (kpse_available("find_read_file")) { |
| 127 |
/*tex Use kpathsea here. */ |
| 128 |
ftemp = find_in_output_directory(s); |
| 129 |
if (!ftemp) |
| 130 |
ftemp = kpse_find_file(s, kpse_tex_format, 1); |
| 131 |
} |
| 132 |
if (ftemp) { |
| 133 |
if (fullnameoffile) |
| 134 |
free(fullnameoffile); |
| 135 |
fullnameoffile = xstrdup(ftemp); |
| 136 |
} |
| 137 |
return ftemp; |
| 138 |
} |
| 139 |
|
| 140 |
/*tex Find other files types. */ |
| 141 |
|
| 142 |
char *luatex_find_file(const char *s, int callback_index) |
| 143 |
{ |
| 144 |
char *ftemp = NULL; |
| 145 |
int callback_id = callback_defined(callback_index); |
| 146 |
if (callback_id > 0) { |
| 147 |
(void) run_callback(callback_id, "S->R", s, &ftemp); |
| 148 |
} else if (kpse_available("find_read_file")) { |
| 149 |
/*tex Use kpathsea here. */ |
| 150 |
switch (callback_index) { |
| 151 |
case find_enc_file_callback: |
| 152 |
ftemp = kpse_find_file(s, kpse_enc_format, 0); |
| 153 |
break; |
| 154 |
case find_map_file_callback: |
| 155 |
ftemp = kpse_find_file(s, kpse_fontmap_format, 0); |
| 156 |
break; |
| 157 |
case find_type1_file_callback: |
| 158 |
ftemp = kpse_find_file(s, kpse_type1_format, 0); |
| 159 |
break; |
| 160 |
case find_truetype_file_callback: |
| 161 |
ftemp = kpse_find_file(s, kpse_truetype_format, 0); |
| 162 |
break; |
| 163 |
case find_opentype_file_callback: |
| 164 |
ftemp = kpse_find_file(s, kpse_opentype_format, 0); |
| 165 |
if (ftemp == NULL) |
| 166 |
ftemp = kpse_find_file(s, kpse_truetype_format, 0); |
| 167 |
break; |
| 168 |
case find_data_file_callback: |
| 169 |
ftemp = find_in_output_directory(s); |
| 170 |
if (!ftemp) |
| 171 |
ftemp = kpse_find_file(s, kpse_tex_format, 1); |
| 172 |
break; |
| 173 |
case find_font_file_callback: |
| 174 |
ftemp = kpse_find_file(s, kpse_ofm_format, 1); |
| 175 |
if (ftemp == NULL) |
| 176 |
ftemp = kpse_find_file(s, kpse_tfm_format, 1); |
| 177 |
break; |
| 178 |
case find_vf_file_callback: |
| 179 |
ftemp = kpse_find_file(s, kpse_ovf_format, 0); |
| 180 |
if (ftemp == NULL) |
| 181 |
ftemp = kpse_find_file(s, kpse_vf_format, 0); |
| 182 |
break; |
| 183 |
case find_cidmap_file_callback: |
| 184 |
ftemp = kpse_find_file(s, kpse_cid_format, 0); |
| 185 |
break; |
| 186 |
default: |
| 187 |
printf("luatex_find_file(): do not know how to handle file %s of type %d\n", s, callback_index); |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
return ftemp; |
| 192 |
} |
| 193 |
|
| 194 |
/*tex |
| 195 |
|
| 196 |
\LUATEX\ used to have private functions for these that did not use kpathsea, |
| 197 |
but since the file paranoia tests have to come from kpathsea anyway, that is |
| 198 |
no longer useful. The only downside to using luatex is that if one wants to |
| 199 |
disable kpathsea via the Lua startup script, it is now an absolute |
| 200 |
requirement that all file discovery callbacks are specified. Just using the |
| 201 |
find_read_file, but not setting open_read_file, for example, does not work |
| 202 |
any more if kpathsea is not to be used at all. |
| 203 |
|
| 204 |
*/ |
| 205 |
|
| 206 |
#define openoutnameok(A) kpse_out_name_ok (A) |
| 207 |
#define openinnameok(A) kpse_in_name_ok (A) |
| 208 |
|
| 209 |
/*tex |
| 210 |
|
| 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 not the |
| 213 |
open succeeded. |
| 214 |
|
| 215 |
*/ |
| 216 |
|
| 217 |
boolean luatex_open_input(FILE ** f_ptr, const char *fn, int filefmt, const_string fopen_mode, boolean must_exist) |
| 218 |
{ |
| 219 |
/*tex We haven't found anything yet. */ |
| 220 |
string fname = NULL; |
| 221 |
*f_ptr = NULL; |
| 222 |
if (fullnameoffile) |
| 223 |
free(fullnameoffile); |
| 224 |
fullnameoffile = NULL; |
| 225 |
fname = kpse_find_file(fn, (kpse_file_format_type) filefmt, must_exist); |
| 226 |
if (fname) { |
| 227 |
fullnameoffile = xstrdup(fname); |
| 228 |
/*tex |
| 229 |
|
| 230 |
If we found the file in the current directory, don't leave the `./' |
| 231 |
at the beginning of `fn', since it looks dumb when `tex foo' says |
| 232 |
`(./foo.tex ... )'. On the other hand, if the user said `tex ./foo', |
| 233 |
and that's what we opened, then keep it -- the user specified it, so |
| 234 |
we shouldn't remove it. |
| 235 |
|
| 236 |
*/ |
| 237 |
if (fname[0] == '.' && IS_DIR_SEP(fname[1]) && (fn[0] != '.' || !IS_DIR_SEP(fn[1]))) { |
| 238 |
unsigned i = 0; |
| 239 |
while (fname[i + 2] != 0) { |
| 240 |
fname[i] = fname[i + 2]; |
| 241 |
i++; |
| 242 |
} |
| 243 |
fname[i] = 0; |
| 244 |
} |
| 245 |
/*tex This fopen is not allowed to fail. */ |
| 246 |
*f_ptr = xfopen(fname, fopen_mode); |
| 247 |
} |
| 248 |
if (*f_ptr) { |
| 249 |
recorder_record_input(fname); |
| 250 |
} |
| 251 |
return *f_ptr != NULL; |
| 252 |
} |
| 253 |
|
| 254 |
boolean luatex_open_output(FILE ** f_ptr, const char *fn, const_string fopen_mode) |
| 255 |
{ |
| 256 |
char *fname; |
| 257 |
boolean absolute = kpse_absolute_p(fn, false); |
| 258 |
/*tex If we have an explicit output directory, use it. */ |
| 259 |
if (output_directory && !absolute) { |
| 260 |
fname = concat3(output_directory, DIR_SEP_STRING, fn); |
| 261 |
} else { |
| 262 |
fname = xstrdup(fn); |
| 263 |
} |
| 264 |
/*tex Is the filename openable as given? */ |
| 265 |
*f_ptr = fopen(fname, fopen_mode); |
| 266 |
if (!*f_ptr) { |
| 267 |
/*tex Can't open as given. Try the envvar. */ |
| 268 |
string texmfoutput = kpse_var_value("TEXMFOUTPUT"); |
| 269 |
if (texmfoutput && *texmfoutput && !absolute) { |
| 270 |
fname = concat3(texmfoutput, DIR_SEP_STRING, fn); |
| 271 |
*f_ptr = fopen(fname, fopen_mode); |
| 272 |
} |
| 273 |
} |
| 274 |
if (*f_ptr) { |
| 275 |
recorder_record_output(fname); |
| 276 |
} |
| 277 |
free(fname); |
| 278 |
return *f_ptr != NULL; |
| 279 |
} |
| 280 |
|
| 281 |
boolean lua_a_open_in(alpha_file * f, char *fn, int n) |
| 282 |
{ |
| 283 |
int k; |
| 284 |
char *fnam; |
| 285 |
int callback_id; |
| 286 |
boolean ret = true; |
| 287 |
boolean file_ok = true; |
| 288 |
if (n == 0) { |
| 289 |
input_file_callback_id[iindex] = 0; |
| 290 |
} else { |
| 291 |
read_file_callback_id[n] = 0; |
| 292 |
} |
| 293 |
if (*fn == '|') |
| 294 |
fnam = fn; |
| 295 |
else |
| 296 |
fnam = luatex_find_read_file(fn, n, find_read_file_callback); |
| 297 |
if (!fnam) |
| 298 |
return false; |
| 299 |
callback_id = callback_defined(open_read_file_callback); |
| 300 |
if (callback_id > 0) { |
| 301 |
k = run_and_save_callback(callback_id, "S->", fnam); |
| 302 |
if (k > 0) { |
| 303 |
ret = true; |
| 304 |
if (n == 0) |
| 305 |
input_file_callback_id[iindex] = k; |
| 306 |
else |
| 307 |
read_file_callback_id[n] = k; |
| 308 |
} else { |
| 309 |
/*tex read failed */ |
| 310 |
file_ok = false; |
| 311 |
} |
| 312 |
} else { |
| 313 |
/*tex no read callback */ |
| 314 |
if (openinnameok(fn)) { |
| 315 |
ret = open_in_or_pipe(f, fnam, kpse_tex_format, FOPEN_RBIN_MODE, (n == 0 ? true : false)); |
| 316 |
} else { |
| 317 |
/*tex open failed */ |
| 318 |
file_ok = false; |
| 319 |
} |
| 320 |
} |
| 321 |
if (!file_ok) { |
| 322 |
ret = false; |
| 323 |
} |
| 324 |
return ret; |
| 325 |
} |
| 326 |
|
| 327 |
boolean lua_a_open_out(alpha_file * f, char *fn, int n) |
| 328 |
{ |
| 329 |
boolean test; |
| 330 |
char *fnam = NULL; |
| 331 |
int callback_id; |
| 332 |
boolean ret = false; |
| 333 |
callback_id = callback_defined(find_write_file_callback); |
| 334 |
if (callback_id > 0) { |
| 335 |
test = run_callback(callback_id, "dS->R", n, fn, &fnam); |
| 336 |
if ((test) && (fnam != NULL) && (strlen(fnam) > 0)) { |
| 337 |
/*tex |
| 338 |
|
| 339 |
There is no message here because if that is needed the macro |
| 340 |
package should do that in the callback code. As elsewhere, |
| 341 |
messaging is left to \LUA\ then. |
| 342 |
|
| 343 |
*/ |
| 344 |
ret = open_outfile(f, fnam, FOPEN_W_MODE); |
| 345 |
free(fnam); |
| 346 |
} |
| 347 |
} else { |
| 348 |
if (openoutnameok(fn)) { |
| 349 |
if (n > 0 && selector != term_only && log_file) { |
| 350 |
/*tex |
| 351 |
|
| 352 |
This message to the log is for downward compatibility with |
| 353 |
other tex's as there are scripts out there that act on this |
| 354 |
message. An alternative is to let a macro package write an |
| 355 |
explicit message. |
| 356 |
|
| 357 |
*/ |
| 358 |
fprintf(log_file,"\n\\openout%i = %s\n",n-1,fn); |
| 359 |
} |
| 360 |
ret = open_out_or_pipe(f, fn, FOPEN_W_MODE); |
| 361 |
} |
| 362 |
} |
| 363 |
return ret; |
| 364 |
} |
| 365 |
|
| 366 |
boolean lua_b_open_out(alpha_file * f, char *fn) |
| 367 |
{ |
| 368 |
boolean test; |
| 369 |
char *fnam = NULL; |
| 370 |
int callback_id; |
| 371 |
boolean ret = false; |
| 372 |
callback_id = callback_defined(find_output_file_callback); |
| 373 |
if (callback_id > 0) { |
| 374 |
test = run_callback(callback_id, "S->R", fn, &fnam); |
| 375 |
if ((test) && (fnam != NULL) && (strlen(fnam) > 0)) { |
| 376 |
ret = open_outfile(f, fnam, FOPEN_WBIN_MODE); |
| 377 |
free(fnam); |
| 378 |
} |
| 379 |
} else { |
| 380 |
if (openoutnameok(fn)) { |
| 381 |
ret = luatex_open_output(f, fn, FOPEN_WBIN_MODE); |
| 382 |
} |
| 383 |
} |
| 384 |
return ret; |
| 385 |
} |
| 386 |
|
| 387 |
void lua_a_close_in(alpha_file f, int n) |
| 388 |
{ |
| 389 |
int callback_id; |
| 390 |
if (n == 0) |
| 391 |
callback_id = input_file_callback_id[iindex]; |
| 392 |
else |
| 393 |
callback_id = read_file_callback_id[n]; |
| 394 |
if (callback_id > 0) { |
| 395 |
run_saved_callback(callback_id, "close", "->"); |
| 396 |
destroy_saved_callback(callback_id); |
| 397 |
if (n == 0) |
| 398 |
input_file_callback_id[iindex] = 0; |
| 399 |
else |
| 400 |
read_file_callback_id[n] = 0; |
| 401 |
} else { |
| 402 |
close_file_or_pipe(f); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void lua_a_close_out(alpha_file f) |
| 407 |
{ |
| 408 |
close_file_or_pipe(f); |
| 409 |
} |
| 410 |
|
| 411 |
/*tex |
| 412 |
|
| 413 |
Binary input and output are done with C's ordinary procedures, so we don't |
| 414 |
have to make any other special arrangements for binary~I/O. Text output is |
| 415 |
also easy to do with standard routines. The treatment of text input is more |
| 416 |
difficult, however, because of the necessary translation to |ASCII_code| |
| 417 |
values. \TeX's conventions should be efficient, and they should blend nicely |
| 418 |
with the user's operating environment. |
| 419 |
|
| 420 |
Input from text files is read one line at a time, using a routine called |
| 421 |
|lua_input_ln|. This function is defined in terms of global variables called |
| 422 |
|buffer|, |first|, and |last| that will be described in detail later; for |
| 423 |
now, it suffices for us to know that |buffer| is an array of |ASCII_code| |
| 424 |
values, and that |first| and |last| are indices into this array representing |
| 425 |
the beginning and ending of a line of text. |
| 426 |
|
| 427 |
*/ |
| 428 |
|
| 429 |
/*tex lines of characters being read */ |
| 430 |
|
| 431 |
packed_ASCII_code *buffer; |
| 432 |
|
| 433 |
/*tex the first unused position in |buffer| */ |
| 434 |
|
| 435 |
int first; |
| 436 |
|
| 437 |
/*tex end of the line just input to |buffer| */ |
| 438 |
|
| 439 |
int last; |
| 440 |
|
| 441 |
/*tex largest index used in |buffer| */ |
| 442 |
|
| 443 |
int max_buf_stack; |
| 444 |
|
| 445 |
/*tex |
| 446 |
|
| 447 |
The |lua_input_ln| function brings the next line of input from the specified |
| 448 |
file into available positions of the buffer array and returns the value |
| 449 |
|true|, unless the file has already been entirely read, in which case it |
| 450 |
returns |false| and sets |last:=first|. In general, the |ASCII_code| numbers |
| 451 |
that represent the next line of the file are input into |buffer[first]|, |
| 452 |
|buffer[first+1]|, \dots, |buffer[last-1]|; and the global variable |last| is |
| 453 |
set equal to |first| plus the length of the line. Trailing blanks are removed |
| 454 |
from the line; thus, either |last=first| (in which case the line was entirely |
| 455 |
blank) or |buffer[last-1]<>" "|. |
| 456 |
|
| 457 |
An overflow error is given, however, if the normal actions of |lua_input_ln| |
| 458 |
would make |last>=buf_size|; this is done so that other parts of \TeX\ can |
| 459 |
safely look at the contents of |buffer[last+1]| without overstepping the |
| 460 |
bounds of the |buffer| array. Upon entry to |lua_input_ln|, the condition |
| 461 |
|first<buf_size| will always hold, so that there is always room for an |
| 462 |
``empty'' line. |
| 463 |
|
| 464 |
The variable |max_buf_stack|, which is used to keep track of how large the |
| 465 |
|buf_size| parameter must be to accommodate the present job, is also kept up |
| 466 |
to date by |lua_input_ln|. |
| 467 |
|
| 468 |
If the |bypass_eoln| parameter is |true|, |lua_input_ln| will do a |get| |
| 469 |
before looking at the first character of the line; this skips over an |eoln| |
| 470 |
that was in |f^|. The procedure does not do a |get| when it reaches the end |
| 471 |
of the line; therefore it can be used to acquire input from the user's |
| 472 |
terminal as well as from ordinary text files. |
| 473 |
|
| 474 |
Since the inner loop of |lua_input_ln| is part of \TeX's ``inner |
| 475 |
loop''---each character of input comes in at this place---it is wise to |
| 476 |
reduce system overhead by making use of special routines that read in an |
| 477 |
entire array of characters at once, if such routines are available. |
| 478 |
|
| 479 |
*/ |
| 480 |
|
| 481 |
boolean lua_input_ln(alpha_file f, int n, boolean bypass_eoln) |
| 482 |
{ |
| 483 |
boolean lua_result; |
| 484 |
int last_ptr; |
| 485 |
int callback_id; |
| 486 |
/*tex Todo: variable can be removed: */ |
| 487 |
(void) bypass_eoln; |
| 488 |
if (n == 0) |
| 489 |
callback_id = input_file_callback_id[iindex]; |
| 490 |
else |
| 491 |
callback_id = read_file_callback_id[n]; |
| 492 |
if (callback_id > 0) { |
| 493 |
last = first; |
| 494 |
last_ptr = first; |
| 495 |
lua_result = |
| 496 |
run_saved_callback(callback_id, "reader", "->l", &last_ptr); |
| 497 |
if ((lua_result == true) && (last_ptr != 0)) { |
| 498 |
last = last_ptr; |
| 499 |
if (last > max_buf_stack) |
| 500 |
max_buf_stack = last; |
| 501 |
} else { |
| 502 |
lua_result = false; |
| 503 |
} |
| 504 |
} else { |
| 505 |
lua_result = input_ln(f, bypass_eoln); |
| 506 |
} |
| 507 |
if (lua_result == true) { |
| 508 |
/*tex Fix up the input buffer using callbacks */ |
| 509 |
if (last >= first) { |
| 510 |
callback_id = callback_defined(process_input_buffer_callback); |
| 511 |
if (callback_id > 0) { |
| 512 |
last_ptr = first; |
| 513 |
lua_result = |
| 514 |
run_callback(callback_id, "l->l", (last - first), |
| 515 |
&last_ptr); |
| 516 |
if ((lua_result == true) && (last_ptr != 0)) { |
| 517 |
last = last_ptr; |
| 518 |
if (last > max_buf_stack) |
| 519 |
max_buf_stack = last; |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
return true; |
| 524 |
} |
| 525 |
return false; |
| 526 |
} |
| 527 |
|
| 528 |
/*tex |
| 529 |
|
| 530 |
We need a special routine to read the first line of \TeX\ input from the |
| 531 |
user's terminal. This line is different because it is read before we have |
| 532 |
opened the transcript file; there is sort of a ``chicken and egg'' problem |
| 533 |
here. If the user types `\.{\\input paper}' on the first line, or if some |
| 534 |
macro invoked by that line does such an \.{\\input}, the transcript file will |
| 535 |
be named `\.{paper.log}'; but if no \.{\\input} commands are performed during |
| 536 |
the first line of terminal input, the transcript file will acquire its |
| 537 |
default name `\.{texput.log}'. (The transcript file will not contain error |
| 538 |
messages generated by the first line before the first \.{\\input} command.) |
| 539 |
|
| 540 |
The first line is special also because it may be read before \TeX\ has input |
| 541 |
a format file. In such cases, normal error messages cannot yet be given. The |
| 542 |
following code uses concepts that will be explained later. |
| 543 |
|
| 544 |
Different systems have different ways to get started. But regardless of what |
| 545 |
conventions are adopted, the routine that initializes the terminal should |
| 546 |
satisfy the following specifications: |
| 547 |
|
| 548 |
\startitemize[n] |
| 549 |
|
| 550 |
\startitem |
| 551 |
It should open file |term_in| for input from the terminal. (The file |
| 552 |
|term_out| will already be open for output to the terminal.) |
| 553 |
\stopitem |
| 554 |
|
| 555 |
\startitem |
| 556 |
If the user has given a command line, this line should be considered |
| 557 |
the first line of terminal input. Otherwise the user should be |
| 558 |
prompted with `\.{**}', and the first line of input should be |
| 559 |
whatever is typed in response. |
| 560 |
\stopitem |
| 561 |
|
| 562 |
\startitem |
| 563 |
The first line of input, which might or might not be a command line, |
| 564 |
should appear in locations |first| to |last-1| of the |buffer| array. |
| 565 |
\stopitem |
| 566 |
|
| 567 |
\startitem |
| 568 |
The global variable |loc| should be set so that the character to be |
| 569 |
read next by \TeX\ is in |buffer[loc]|. This character should not be |
| 570 |
blank, and we should have |loc<last|. |
| 571 |
\stopitem |
| 572 |
|
| 573 |
\stopitemize |
| 574 |
|
| 575 |
It may be necessary to prompt the user several times before a non-blank line |
| 576 |
comes in. The prompt is `\.{**}' instead of the later `\.*' because the |
| 577 |
meaning is slightly different: `\.{\\input}' need not be typed immediately |
| 578 |
after~`\.{**}'.) |
| 579 |
|
| 580 |
The following program does the required initialization. Iff anything has been |
| 581 |
specified on the command line, then |t_open_in| will return with |last > |
| 582 |
first|. |
| 583 |
|
| 584 |
*/ |
| 585 |
|
| 586 |
boolean init_terminal(void) |
| 587 |
{ |
| 588 |
/*tex This gets the terminal input started. */ |
| 589 |
t_open_in(); |
| 590 |
if (last > first) { |
| 591 |
iloc = first; |
| 592 |
while ((iloc < last) && (buffer[iloc] == ' ')) |
| 593 |
incr(iloc); |
| 594 |
if (iloc < last) { |
| 595 |
return true; |
| 596 |
} |
| 597 |
} |
| 598 |
while (1) { |
| 599 |
wake_up_terminal(); |
| 600 |
fputs("**", term_out); |
| 601 |
update_terminal(); |
| 602 |
if (!input_ln(term_in, true)) { |
| 603 |
/*tex This shouldn't happen. */ |
| 604 |
fputs("\n! End of file on the terminal... why?\n", term_out); |
| 605 |
return false; |
| 606 |
} |
| 607 |
iloc = first; |
| 608 |
while ((iloc < last) && (buffer[iloc] == ' ')) { |
| 609 |
incr(iloc); |
| 610 |
} |
| 611 |
/*tex Return unless the line was all blank. */ |
| 612 |
if (iloc < last) { |
| 613 |
return true; |
| 614 |
} |
| 615 |
fputs("Please type the name of your input file.\n", term_out); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
/*tex |
| 621 |
|
| 622 |
Here is a procedure that asks the user to type a line of input, assuming that |
| 623 |
the |selector| setting is either |term_only| or |term_and_log|. The input is |
| 624 |
placed into locations |first| through |last-1| of the |buffer| array, and |
| 625 |
echoed on the transcript file if appropriate. |
| 626 |
|
| 627 |
*/ |
| 628 |
|
| 629 |
void term_input(void) |
| 630 |
{ |
| 631 |
/*tex Index into |buffer|: */ |
| 632 |
int k; |
| 633 |
/*tex Now the user sees the prompt for sure: */ |
| 634 |
update_terminal(); |
| 635 |
if (!input_ln(term_in, true)) { |
| 636 |
/*tex initialize limit to avoid printing some garbage. */ |
| 637 |
ilimit=0; |
| 638 |
fatal_error("End of file on the terminal!"); |
| 639 |
} |
| 640 |
/*tex The user's line ended with \.{<return>}: */ |
| 641 |
term_offset = 0; |
| 642 |
/*tex Prepare to echo the input. */ |
| 643 |
decr(selector); |
| 644 |
if (last != first) { |
| 645 |
for (k = first; k <= last - 1; k++) |
| 646 |
print_char(buffer[k]); |
| 647 |
} |
| 648 |
print_ln(); |
| 649 |
/*tex Restore previous status. */ |
| 650 |
incr(selector); |
| 651 |
} |
| 652 |
|
| 653 |
/*tex |
| 654 |
|
| 655 |
It's time now to fret about file names. Besides the fact that different |
| 656 |
operating systems treat files in different ways, we must cope with the fact |
| 657 |
that completely different naming conventions are used by different groups of |
| 658 |
people. The following programs show what is required for one particular |
| 659 |
operating system; similar routines for other systems are not difficult to |
| 660 |
devise. |
| 661 |
|
| 662 |
\TeX\ assumes that a file name has three parts: the name proper; its |
| 663 |
``extension''; and a ``file area'' where it is found in an external file |
| 664 |
system. The extension of an input file or a write file is assumed to be |
| 665 |
`\.{.tex}' unless otherwise specified; it is `\.{.log}' on the transcript |
| 666 |
file that records each run of \TeX; it is `\.{.tfm}' on the font metric files |
| 667 |
that describe characters in the fonts \TeX\ uses; it is `\.{.dvi}' on the |
| 668 |
output files that specify typesetting information; and it is `\.{.fmt}' on |
| 669 |
the format files written by \.{INITEX} to initialize \TeX. The file area can |
| 670 |
be arbitrary on input files, but files are usually output to the user's |
| 671 |
current area. If an input file cannot be found on the specified area, \TeX\ |
| 672 |
will look for it on a special system area; this special area is intended for |
| 673 |
commonly used input files like \.{webmac.tex}. |
| 674 |
|
| 675 |
Simple uses of \TeX\ refer only to file names that have no explicit extension |
| 676 |
or area. For example, a person usually says `\.{\\input} \.{paper}' or |
| 677 |
`\.{\\font\\tenrm} \.= \.{helvetica}' instead of `\.{\\input} \.{paper.new}' |
| 678 |
or `\.{\\font\\tenrm} \.= \.{<csd.knuth>test}'. Simple file names are best, |
| 679 |
because they make the \TeX\ source files portable; whenever a file name |
| 680 |
consists entirely of letters and digits, it should be treated in the same way |
| 681 |
by all implementations of \TeX. However, users need the ability to refer to |
| 682 |
other files in their environment, especially when responding to error |
| 683 |
messages concerning unopenable files; therefore we want to let them use the |
| 684 |
syntax that appears in their favorite operating system. |
| 685 |
|
| 686 |
The following procedures don't allow spaces to be part of file names; but |
| 687 |
some users seem to like names that are spaced-out. System-dependent changes |
| 688 |
to allow such things should probably be made with reluctance, and only when |
| 689 |
an entire file name that includes spaces is ``quoted'' somehow. |
| 690 |
|
| 691 |
Here are the global values that file names will be scanned into. |
| 692 |
|
| 693 |
*/ |
| 694 |
|
| 695 |
/*tex name of file just scanned */ |
| 696 |
|
| 697 |
str_number cur_name; |
| 698 |
|
| 699 |
/*tex file area just scanned, or \.{""} */ |
| 700 |
|
| 701 |
str_number cur_area; |
| 702 |
|
| 703 |
/*tex file extension just scanned, or \.{""} */ |
| 704 |
|
| 705 |
str_number cur_ext; |
| 706 |
|
| 707 |
/*tex |
| 708 |
|
| 709 |
The file names we shall deal with have the following structure: If the name |
| 710 |
contains `\./' or `\.:' (for Amiga only), the file area consists of all |
| 711 |
characters up to and including the final such character; otherwise the file |
| 712 |
area is null. If the remaining file name contains `\..', the file extension |
| 713 |
consists of all such characters from the last `\..' to the end, otherwise the |
| 714 |
file extension is null. |
| 715 |
|
| 716 |
We can scan such file names easily by using two global variables that keep |
| 717 |
track of the occurrences of area and extension delimiters: |
| 718 |
|
| 719 |
*/ |
| 720 |
|
| 721 |
/*tex the most recent `\./', if any */ |
| 722 |
|
| 723 |
pool_pointer area_delimiter; |
| 724 |
|
| 725 |
/*tex the relevant `\..', if any */ |
| 726 |
|
| 727 |
pool_pointer ext_delimiter; |
| 728 |
|
| 729 |
/*tex |
| 730 |
|
| 731 |
Input files that can't be found in the user's area may appear in a standard |
| 732 |
system area called |TEX_area|. Font metric files whose areas are not given |
| 733 |
explicitly are assumed to appear in a standard system area called |
| 734 |
|TEX_font_area|. $\Omega$'s compiled translation process files whose areas |
| 735 |
are not given explicitly are assumed to appear in a standard system area. |
| 736 |
These system area names will, of course, vary from place to place. |
| 737 |
|
| 738 |
*/ |
| 739 |
|
| 740 |
#define append_to_fn(A) do { \ |
| 741 |
c=(A); \ |
| 742 |
if (c!='"') { \ |
| 743 |
if (k<file_name_size) fn[k++]=(unsigned char)(c); \ |
| 744 |
} \ |
| 745 |
} while (0) |
| 746 |
|
| 747 |
|
| 748 |
char *pack_file_name(str_number n, str_number a, str_number e) |
| 749 |
{ |
| 750 |
/*tex character being packed */ |
| 751 |
ASCII_code c; |
| 752 |
/*tex index into |str_pool| */ |
| 753 |
unsigned char *j; |
| 754 |
/*tex number of positions filled in |fn| */ |
| 755 |
int k = 0; |
| 756 |
unsigned char *fn = xmallocarray(packed_ASCII_code, str_length(a) + str_length(n) + str_length(e) + 1); |
| 757 |
for (j = str_string(a); j < str_string(a) + str_length(a); j++) |
| 758 |
append_to_fn(*j); |
| 759 |
for (j = str_string(n); j < str_string(n) + str_length(n); j++) |
| 760 |
append_to_fn(*j); |
| 761 |
for (j = str_string(e); j < str_string(e) + str_length(e); j++) |
| 762 |
append_to_fn(*j); |
| 763 |
fn[k] = 0; |
| 764 |
return (char *) fn; |
| 765 |
} |
| 766 |
|
| 767 |
/*tex |
| 768 |
|
| 769 |
A messier routine is also needed, since format file names must be scanned |
| 770 |
before \TeX's string mechanism has been initialized. We shall use the global |
| 771 |
variable |TEX_format_default| to supply the text for default system areas and |
| 772 |
extensions related to format files. |
| 773 |
|
| 774 |
Under \UNIX\ we don't give the area part, instead depending on the path |
| 775 |
searching that will happen during file opening. Also, the length will be set |
| 776 |
in the main program. |
| 777 |
|
| 778 |
*/ |
| 779 |
|
| 780 |
char *TEX_format_default; |
| 781 |
|
| 782 |
|
| 783 |
/*tex |
| 784 |
|
| 785 |
This part of the program becomes active when a ``virgin'' \TeX\ is trying to |
| 786 |
get going, just after the preliminary initialization, or when the user is |
| 787 |
substituting another format file by typing `\.\&' after the initial `\.{**}' |
| 788 |
prompt. The buffer contains the first line of input in |
| 789 |
|buffer[loc..(last-1)]|, where |loc<last| and |buffer[loc]<>" "|. |
| 790 |
|
| 791 |
*/ |
| 792 |
|
| 793 |
char *open_fmt_file(void) |
| 794 |
{ |
| 795 |
/*tex The first space after the format file name: */ |
| 796 |
int j; |
| 797 |
char *fmt = NULL; |
| 798 |
int dist; |
| 799 |
j = iloc; |
| 800 |
if (buffer[iloc] == '&') { |
| 801 |
incr(iloc); |
| 802 |
j = iloc; |
| 803 |
buffer[last] = ' '; |
| 804 |
while (buffer[j] != ' ') |
| 805 |
incr(j); |
| 806 |
fmt = xmalloc((unsigned) (j - iloc + 1)); |
| 807 |
strncpy(fmt, (char *) (buffer + iloc), (size_t) (j - iloc)); |
| 808 |
fmt[j - iloc] = 0; |
| 809 |
dist = (int) (strlen(fmt) - strlen(DUMP_EXT)); |
| 810 |
if (!(strstr(fmt, DUMP_EXT) == fmt + dist)) |
| 811 |
fmt = concat(fmt, DUMP_EXT); |
| 812 |
if (zopen_w_input(&fmt_file, fmt, DUMP_FORMAT, FOPEN_RBIN_MODE)) |
| 813 |
goto FOUND; |
| 814 |
wake_up_terminal(); |
| 815 |
fprintf(stdout, "Sorry, I can't find the format `%s'; will try `%s'.\n", |
| 816 |
fmt, TEX_format_default); |
| 817 |
update_terminal(); |
| 818 |
} |
| 819 |
/*tex Now pull out all the stops: try for the system \.{plain} file. */ |
| 820 |
fmt = TEX_format_default; |
| 821 |
if (!zopen_w_input(&fmt_file, fmt, DUMP_FORMAT, FOPEN_RBIN_MODE)) { |
| 822 |
wake_up_terminal(); |
| 823 |
fprintf(stdout, "I can't find the format file `%s'!\n", |
| 824 |
TEX_format_default); |
| 825 |
return NULL; |
| 826 |
} |
| 827 |
FOUND: |
| 828 |
iloc = j; |
| 829 |
return fmt; |
| 830 |
} |
| 831 |
|
| 832 |
/*tex |
| 833 |
|
| 834 |
The global variable |name_in_progress| is used to prevent recursive use of |
| 835 |
|scan_file_name|, since the |begin_name| and other procedures communicate via |
| 836 |
global variables. Recursion would arise only by devious tricks like |
| 837 |
`\.{\\input\\input f}'; such attempts at sabotage must be thwarted. |
| 838 |
Furthermore, |name_in_progress| prevents \.{\\input} from being initiated |
| 839 |
when a font size specification is being scanned. |
| 840 |
|
| 841 |
Another global variable, |job_name|, contains the file name that was first |
| 842 |
\.{\\input} by the user. This name is extended by `\.{.log}' and `\.{.dvi}' |
| 843 |
and `\.{.fmt}' in the names of \TeX's output files. |
| 844 |
|
| 845 |
*/ |
| 846 |
|
| 847 |
/*tex is a file name being scanned? */ |
| 848 |
|
| 849 |
boolean name_in_progress; |
| 850 |
|
| 851 |
/*tex principal file name */ |
| 852 |
|
| 853 |
str_number job_name; |
| 854 |
|
| 855 |
/*tex has the transcript file been opened? */ |
| 856 |
|
| 857 |
boolean log_opened_global; |
| 858 |
|
| 859 |
/*tex |
| 860 |
|
| 861 |
Initially |job_name=0|; it becomes nonzero as soon as the true name is known. |
| 862 |
We have |job_name=0| if and only if the `\.{log}' file has not been opened, |
| 863 |
except of course for a short time just after |job_name| has become nonzero. |
| 864 |
|
| 865 |
*/ |
| 866 |
|
| 867 |
/*tex full name of the log file */ |
| 868 |
|
| 869 |
unsigned char *texmf_log_name; |
| 870 |
|
| 871 |
/*tex |
| 872 |
|
| 873 |
The |open_log_file| routine is used to open the transcript file and to help |
| 874 |
it catch up to what has previously been printed on the terminal. |
| 875 |
|
| 876 |
*/ |
| 877 |
|
| 878 |
void open_log_file(void) |
| 879 |
{ |
| 880 |
/*tex previous |selector| setting */ |
| 881 |
int old_setting; |
| 882 |
/*tex index into |buffer| */ |
| 883 |
int k; |
| 884 |
/*tex end of first input line */ |
| 885 |
int l; |
| 886 |
char *fn; |
| 887 |
old_setting = selector; |
| 888 |
if (job_name == 0) |
| 889 |
job_name = getjobname(maketexstring("texput")); |
| 890 |
fn = pack_job_name(".fls"); |
| 891 |
recorder_change_filename(fn); |
| 892 |
fn = pack_job_name(".log"); |
| 893 |
while (!lua_a_open_out(&log_file, fn, 0)) { |
| 894 |
/*tex |
| 895 |
|
| 896 |
Try to get a different log file name. Sometimes |open_log_file| is |
| 897 |
called at awkward moments when \TeX\ is unable to print error |
| 898 |
messages or even to |show_context|. The |prompt_file_name| routine |
| 899 |
can result in a |fatal_error|, but the |error| routine will not be |
| 900 |
invoked because |log_opened| will be false. |
| 901 |
|
| 902 |
The normal idea of |batch_mode| is that nothing at all should be |
| 903 |
written on the terminal. However, in the unusual case that no log |
| 904 |
file could be opened, we make an exception and allow an explanatory |
| 905 |
message to be seen. |
| 906 |
|
| 907 |
Incidentally, the program always refers to the log file as a |
| 908 |
`\.{transcript file}', because some systems cannot use the extension |
| 909 |
`\.{.log}' for this file. |
| 910 |
*/ |
| 911 |
selector = term_only; |
| 912 |
fn = prompt_file_name("transcript file name", ".log"); |
| 913 |
} |
| 914 |
texmf_log_name = (unsigned char *) xstrdup(fn); |
| 915 |
selector = log_only; |
| 916 |
log_opened_global = true; |
| 917 |
if (callback_defined(start_run_callback) == 0) { |
| 918 |
/*tex Print the banner line, including current date and time. */ |
| 919 |
log_banner(luatex_version_string); |
| 920 |
/*tex Make sure bottom level is in memory. */ |
| 921 |
input_stack[input_ptr] = cur_input; |
| 922 |
tprint_nl("**"); |
| 923 |
/*tex The last position of first line. */ |
| 924 |
l = input_stack[0].limit_field; |
| 925 |
if (buffer[l] == end_line_char_par) { |
| 926 |
/*tex maybe also handle multichar endlinechar */ |
| 927 |
decr(l); |
| 928 |
} |
| 929 |
for (k = 1; k <= l; k++) { |
| 930 |
print_char(buffer[k]); |
| 931 |
} |
| 932 |
/*tex now the transcript file contains the first line of input */ |
| 933 |
print_ln(); |
| 934 |
} |
| 935 |
/*tex should be done always */ |
| 936 |
flush_loggable_info(); |
| 937 |
/*tex should be done always */ |
| 938 |
selector = old_setting + 2; |
| 939 |
} |
| 940 |
|
| 941 |
/*tex |
| 942 |
|
| 943 |
This function is needed by synctex to make its log appear in the right spot |
| 944 |
when |output_directory| is set. |
| 945 |
|
| 946 |
*/ |
| 947 |
|
| 948 |
char *get_full_log_name (void) |
| 949 |
{ |
| 950 |
if (output_directory) { |
| 951 |
char *ret = xmalloc(strlen((char *)texmf_log_name)+2+strlen(output_directory)); |
| 952 |
ret = strcpy(ret, output_directory); |
| 953 |
strcat(ret, "/"); |
| 954 |
strcat(ret, (char *)texmf_log_name); |
| 955 |
return ret; |
| 956 |
} else { |
| 957 |
return xstrdup((const char*)texmf_log_name); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
/*tex Synctex uses this to get the anchored path of an input file. */ |
| 962 |
|
| 963 |
char *luatex_synctex_get_current_name (void) |
| 964 |
{ |
| 965 |
char *pwdbuf = NULL, *ret; |
| 966 |
if (kpse_absolute_p(fullnameoffile, false)) { |
| 967 |
return xstrdup(fullnameoffile); |
| 968 |
} |
| 969 |
pwdbuf = xgetcwd(); |
| 970 |
ret = concat3(pwdbuf, DIR_SEP_STRING, fullnameoffile); |
| 971 |
free(pwdbuf) ; |
| 972 |
return ret; |
| 973 |
} |
| 974 |
|
| 975 |
/*tex |
| 976 |
|
| 977 |
Let's turn now to the procedure that is used to initiate file reading when an |
| 978 |
`\.{\\input}' command is being processed. |
| 979 |
|
| 980 |
*/ |
| 981 |
|
| 982 |
void start_input(void) |
| 983 |
{ |
| 984 |
str_number temp_str; |
| 985 |
char *fn; |
| 986 |
do { |
| 987 |
get_x_token(); |
| 988 |
} while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd)); |
| 989 |
|
| 990 |
back_input(); |
| 991 |
if (cur_cmd != left_brace_cmd) { |
| 992 |
/*tex Set |cur_name| to desired file name. */ |
| 993 |
scan_file_name(); |
| 994 |
} else { |
| 995 |
scan_file_name_toks(); |
| 996 |
} |
| 997 |
fn = pack_file_name(cur_name, cur_area, cur_ext); |
| 998 |
while (1) { |
| 999 |
/*tex Set up |cur_file| and new level of input. */ |
| 1000 |
begin_file_reading(); |
| 1001 |
if (lua_a_open_in(&cur_file, fn, 0)) { |
| 1002 |
break; |
| 1003 |
} |
| 1004 |
/*tex Remove the level that didn't work. */ |
| 1005 |
end_file_reading(); |
| 1006 |
fn = prompt_file_name("input file name", ""); |
| 1007 |
} |
| 1008 |
iname = maketexstring(fullnameoffile); |
| 1009 |
/*tex |
| 1010 |
|
| 1011 |
Now that we have |fullnameoffile|, it is time to post-adjust |cur_name| |
| 1012 |
and |cur_ext| for trailing |.tex|. |
| 1013 |
|
| 1014 |
*/ |
| 1015 |
{ |
| 1016 |
char *n, *p; |
| 1017 |
n = p = fullnameoffile + strlen(fullnameoffile); |
| 1018 |
while (p>fullnameoffile) { |
| 1019 |
p--; |
| 1020 |
if (IS_DIR_SEP(*p)) { |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
if (IS_DIR_SEP(*p)) { |
| 1025 |
p++; |
| 1026 |
} |
| 1027 |
while (n>fullnameoffile) { |
| 1028 |
n--; |
| 1029 |
if (*n == '.') { |
| 1030 |
break; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
if (n>p) { |
| 1034 |
int q = *n; |
| 1035 |
cur_ext = maketexstring(n); |
| 1036 |
*n = 0; |
| 1037 |
cur_name = maketexstring(p); |
| 1038 |
*n = q; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
source_filename_stack[in_open] = iname; |
| 1042 |
full_source_filename_stack[in_open] = xstrdup(fullnameoffile); |
| 1043 |
/*tex We can try to conserve string pool space now. */ |
| 1044 |
temp_str = search_string(iname); |
| 1045 |
if (temp_str > 0) { |
| 1046 |
flush_str(iname); |
| 1047 |
iname = temp_str; |
| 1048 |
} |
| 1049 |
if (job_name == 0) { |
| 1050 |
job_name = getjobname(cur_name); |
| 1051 |
open_log_file(); |
| 1052 |
} |
| 1053 |
/*tex |
| 1054 |
|
| 1055 |
|open_log_file| doesn't |show_context|, so |limit| and |loc| needn't be |
| 1056 |
set to meaningful values yet. |
| 1057 |
|
| 1058 |
*/ |
| 1059 |
report_start_file(filetype_tex,fullnameoffile); |
| 1060 |
incr(open_parens); |
| 1061 |
update_terminal(); |
| 1062 |
istate = new_line; |
| 1063 |
/*tex Prepare new file {\sl Sync\TeX} information. */ |
| 1064 |
if (! synctex_get_no_files()) { |
| 1065 |
/*tex Give control to the {\sl Sync\TeX} controller. */ |
| 1066 |
synctexstartinput(); |
| 1067 |
} |
| 1068 |
/*tex |
| 1069 |
|
| 1070 |
Read the first line of the new file. Here we have to remember to tell the |
| 1071 |
|lua_input_ln| routine not to start with a |get|. If the file is empty, |
| 1072 |
it is considered to contain a single blank line. |
| 1073 |
|
| 1074 |
*/ |
| 1075 |
line = 1; |
| 1076 |
if (lua_input_ln(cur_file, 0, false)) { |
| 1077 |
; |
| 1078 |
} |
| 1079 |
firm_up_the_line(); |
| 1080 |
if (end_line_char_inactive) |
| 1081 |
decr(ilimit); |
| 1082 |
else |
| 1083 |
buffer[ilimit] = (packed_ASCII_code) end_line_char_par; |
| 1084 |
first = ilimit + 1; |
| 1085 |
iloc = istart; |
| 1086 |
} |
| 1087 |
|
| 1088 |
/*tex |
| 1089 |
|
| 1090 |
Because the format is zipped we read and write dump files through zlib. |
| 1091 |
Earlier versions recast |*f| from |FILE *| to |gzFile|, but there is no |
| 1092 |
guarantee that these have the same size, so a static variable is needed. |
| 1093 |
|
| 1094 |
*/ |
| 1095 |
|
| 1096 |
static gzFile gz_fmtfile = NULL; |
| 1097 |
|
| 1098 |
/*tex |
| 1099 |
|
| 1100 |
As distributed, the dump files are architecture dependent; specifically, |
| 1101 |
BigEndian and LittleEndian architectures produce different files. These |
| 1102 |
routines always output BigEndian files. This still does not guarantee them to |
| 1103 |
be architecture-independent, because it is possible to make a format that |
| 1104 |
dumps a glue ratio, i.e., a floating-point number. Fortunately, none of the |
| 1105 |
standard formats do that. |
| 1106 |
|
| 1107 |
*/ |
| 1108 |
|
| 1109 |
#if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE) |
| 1110 |
|
| 1111 |
/*tex |
| 1112 |
|
| 1113 |
This macro is always invoked as a statement. It assumes a variable `temp'. |
| 1114 |
|
| 1115 |
*/ |
| 1116 |
|
| 1117 |
# define SWAP(x, y) do { temp = x; x = y; y = temp; } while (0) |
| 1118 |
|
| 1119 |
/*tex |
| 1120 |
|
| 1121 |
Make the NITEMS items pointed at by P, each of size SIZE, be the |
| 1122 |
opposite-endianness of whatever they are now. |
| 1123 |
|
| 1124 |
*/ |
| 1125 |
|
| 1126 |
static void swap_items(char *pp, int nitems, int size) |
| 1127 |
{ |
| 1128 |
char temp; |
| 1129 |
unsigned total = (unsigned) (nitems * size); |
| 1130 |
char *q = xmalloc(total); |
| 1131 |
char *p = q; |
| 1132 |
memcpy(p,pp,total); |
| 1133 |
/*tex |
| 1134 |
|
| 1135 |
Since `size' does not change, we can write a while loop for each case, |
| 1136 |
and avoid testing `size' for each time. |
| 1137 |
|
| 1138 |
*/ |
| 1139 |
switch (size) { |
| 1140 |
case 16: |
| 1141 |
/*tex |
| 1142 |
|
| 1143 |
16-byte items happen on the DEC Alpha machine when we are not doing |
| 1144 |
sharable memory dumps. |
| 1145 |
|
| 1146 |
*/ |
| 1147 |
while (nitems--) { |
| 1148 |
SWAP(p[0], p[15]); |
| 1149 |
SWAP(p[1], p[14]); |
| 1150 |
SWAP(p[2], p[13]); |
| 1151 |
SWAP(p[3], p[12]); |
| 1152 |
SWAP(p[4], p[11]); |
| 1153 |
SWAP(p[5], p[10]); |
| 1154 |
SWAP(p[6], p[9]); |
| 1155 |
SWAP(p[7], p[8]); |
| 1156 |
p += size; |
| 1157 |
} |
| 1158 |
break; |
| 1159 |
|
| 1160 |
case 12: |
| 1161 |
while (nitems--) { |
| 1162 |
SWAP(p[0], p[11]); |
| 1163 |
SWAP(p[1], p[10]); |
| 1164 |
SWAP(p[2], p[9]); |
| 1165 |
SWAP(p[3], p[8]); |
| 1166 |
SWAP(p[4], p[7]); |
| 1167 |
SWAP(p[5], p[6]); |
| 1168 |
p += size; |
| 1169 |
} |
| 1170 |
break; |
| 1171 |
|
| 1172 |
case 8: |
| 1173 |
while (nitems--) { |
| 1174 |
SWAP(p[0], p[7]); |
| 1175 |
SWAP(p[1], p[6]); |
| 1176 |
SWAP(p[2], p[5]); |
| 1177 |
SWAP(p[3], p[4]); |
| 1178 |
p += size; |
| 1179 |
} |
| 1180 |
break; |
| 1181 |
|
| 1182 |
case 4: |
| 1183 |
while (nitems--) { |
| 1184 |
SWAP(p[0], p[3]); |
| 1185 |
SWAP(p[1], p[2]); |
| 1186 |
p += size; |
| 1187 |
} |
| 1188 |
break; |
| 1189 |
|
| 1190 |
case 2: |
| 1191 |
while (nitems--) { |
| 1192 |
SWAP(p[0], p[1]); |
| 1193 |
p += size; |
| 1194 |
} |
| 1195 |
break; |
| 1196 |
case 1: |
| 1197 |
/*tex Nothing to do. */ |
| 1198 |
break; |
| 1199 |
default: |
| 1200 |
FATAL1("Can't swap a %d-byte item for (un)dumping", size); |
| 1201 |
} |
| 1202 |
memcpy(pp,q,total); |
| 1203 |
xfree(q); |
| 1204 |
} |
| 1205 |
#endif |
| 1206 |
|
| 1207 |
/*tex |
| 1208 |
|
| 1209 |
That second swap is to make sure following calls don't get confused in the |
| 1210 |
case of |dump_things|. |
| 1211 |
|
| 1212 |
*/ |
| 1213 |
|
| 1214 |
void do_zdump(char *p, int item_size, int nitems, FILE * out_file) |
| 1215 |
{ |
| 1216 |
int err; |
| 1217 |
(void) out_file; |
| 1218 |
if (nitems == 0) |
| 1219 |
return; |
| 1220 |
#if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE) |
| 1221 |
swap_items(p, nitems, item_size); |
| 1222 |
#endif |
| 1223 |
if (gzwrite(gz_fmtfile, (void *) p, (unsigned) (item_size * nitems)) != |
| 1224 |
item_size * nitems) { |
| 1225 |
fprintf(stderr, "! Could not write %d %d-byte item(s): %s.\n", nitems, item_size, gzerror(gz_fmtfile, &err)); |
| 1226 |
uexit(1); |
| 1227 |
} |
| 1228 |
#if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE) |
| 1229 |
swap_items(p, nitems, item_size); |
| 1230 |
#endif |
| 1231 |
} |
| 1232 |
|
| 1233 |
void do_zundump(char *p, int item_size, int nitems, FILE * in_file) |
| 1234 |
{ |
| 1235 |
int err; |
| 1236 |
(void) in_file; |
| 1237 |
if (nitems == 0) |
| 1238 |
return; |
| 1239 |
if (gzread(gz_fmtfile, (void *) p, (unsigned) (item_size * nitems)) <= 0) { |
| 1240 |
fprintf(stderr, "Could not undump %d %d-byte item(s): %s.\n", nitems, item_size, gzerror(gz_fmtfile, &err)); |
| 1241 |
uexit(1); |
| 1242 |
} |
| 1243 |
#if !defined (WORDS_BIGENDIAN) && !defined (NO_DUMP_SHARE) |
| 1244 |
swap_items(p, nitems, item_size); |
| 1245 |
#endif |
| 1246 |
} |
| 1247 |
|
| 1248 |
/*tex |
| 1249 |
|
| 1250 |
Tests has shown that a level 3 compression is the most optimal tradeoff |
| 1251 |
between file size and load time. |
| 1252 |
|
| 1253 |
*/ |
| 1254 |
|
| 1255 |
#define COMPRESSION "R3" |
| 1256 |
|
| 1257 |
boolean zopen_w_input(FILE ** f, const char *fname, int format, const_string fopen_mode) |
| 1258 |
{ |
| 1259 |
int callbackid; |
| 1260 |
int res; |
| 1261 |
char *fnam; |
| 1262 |
callbackid = callback_defined(find_format_file_callback); |
| 1263 |
if (callbackid > 0) { |
| 1264 |
res = run_callback(callbackid, "S->R", fname, &fnam); |
| 1265 |
if (res && fnam && strlen(fnam) > 0) { |
| 1266 |
*f = fopen(fnam, fopen_mode); |
| 1267 |
if (*f == NULL) { |
| 1268 |
return 0; |
| 1269 |
} |
| 1270 |
} else { |
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
} else { |
| 1274 |
res = luatex_open_input(f, fname, format, fopen_mode, true); |
| 1275 |
} |
| 1276 |
if (res) { |
| 1277 |
gz_fmtfile = gzdopen(fileno(*f), "rb" COMPRESSION); |
| 1278 |
} |
| 1279 |
return res; |
| 1280 |
} |
| 1281 |
|
| 1282 |
boolean zopen_w_output(FILE ** f, const char *s, const_string fopen_mode) |
| 1283 |
{ |
| 1284 |
int res = 1; |
| 1285 |
if (luainit) { |
| 1286 |
*f = fopen(s, fopen_mode); |
| 1287 |
if (*f == NULL) { |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
} else { |
| 1291 |
res = luatex_open_output(f, s, fopen_mode); |
| 1292 |
} |
| 1293 |
if (res) { |
| 1294 |
gz_fmtfile = gzdopen(fileno(*f), "wb" COMPRESSION); |
| 1295 |
} |
| 1296 |
return res; |
| 1297 |
} |
| 1298 |
|
| 1299 |
void zwclose(FILE * f) |
| 1300 |
{ |
| 1301 |
(void) f; |
| 1302 |
gzclose(gz_fmtfile); |
| 1303 |
} |
| 1304 |
|
| 1305 |
/*tex Create the \DVI\ or \PDF\ file. */ |
| 1306 |
|
| 1307 |
int open_outfile(FILE ** f, const char *name, const char *mode) |
| 1308 |
{ |
| 1309 |
FILE *res; |
| 1310 |
res = fopen(name, mode); |
| 1311 |
if (res != NULL) { |
| 1312 |
*f = res; |
| 1313 |
return 1; |
| 1314 |
} |
| 1315 |
return 0; |
| 1316 |
} |
| 1317 |
|
| 1318 |
/*tex The caller should set |tfm_buffer=NULL| and |tfm_size=0|. */ |
| 1319 |
|
| 1320 |
int readbinfile(FILE * f, unsigned char **tfm_buffer, int *tfm_size) |
| 1321 |
{ |
| 1322 |
void *buf; |
| 1323 |
int size; |
| 1324 |
if (fseek(f, 0, SEEK_END) == 0) { |
| 1325 |
size = (int) ftell(f); |
| 1326 |
if (size > 0) { |
| 1327 |
buf = xmalloc((unsigned) size); |
| 1328 |
if (fseek(f, 0, SEEK_SET) == 0) { |
| 1329 |
if (fread((void *) buf, (size_t) size, 1, f) == 1) { |
| 1330 |
*tfm_buffer = (unsigned char *) buf; |
| 1331 |
*tfm_size = size; |
| 1332 |
return 1; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} else { |
| 1336 |
*tfm_buffer = NULL; |
| 1337 |
*tfm_size = 0; |
| 1338 |
return 1; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
/*tex Either seek failed or we have a zero-sized file. */ |
| 1342 |
return 0; |
| 1343 |
} |
| 1344 |
|
| 1345 |
/*tex |
| 1346 |
|
| 1347 |
Like |os.execute()|, the |runpopen()| function is called only when |
| 1348 |
|shellenabledp == 1|. Unlike |os.execute()| we write errors to stderr, since |
| 1349 |
we have nowhere better to use; and of course we return a file handle (or |
| 1350 |
NULL) instead of a status indicator. |
| 1351 |
|
| 1352 |
*/ |
| 1353 |
|
| 1354 |
static FILE *runpopen(char *cmd, const char *mode) |
| 1355 |
{ |
| 1356 |
FILE *f = NULL; |
| 1357 |
char *safecmd = NULL; |
| 1358 |
char *cmdname = NULL; |
| 1359 |
int allow; |
| 1360 |
#ifdef WIN32 |
| 1361 |
char *pp; |
| 1362 |
|
| 1363 |
for (pp = cmd; *pp; pp++) { |
| 1364 |
if (*pp == '\'') *pp = '"'; |
| 1365 |
} |
| 1366 |
#endif |
| 1367 |
/*tex If |restrictedshell| is zero, any command is allowed. */ |
| 1368 |
if (restrictedshell == 0) { |
| 1369 |
allow = 1; |
| 1370 |
} else { |
| 1371 |
const char *thecmd = cmd; |
| 1372 |
allow = shell_cmd_is_allowed(thecmd, &safecmd, &cmdname); |
| 1373 |
} |
| 1374 |
if (allow == 1) |
| 1375 |
f = popen(cmd, mode); |
| 1376 |
else if (allow == 2) |
| 1377 |
f = popen(safecmd, mode); |
| 1378 |
else if (allow == -1) |
| 1379 |
fprintf(stderr, "\nrunpopen quotation error in command line: %s\n", cmd); |
| 1380 |
else |
| 1381 |
fprintf(stderr, "\nrunpopen command not allowed: %s\n", cmdname); |
| 1382 |
if (safecmd) |
| 1383 |
free(safecmd); |
| 1384 |
if (cmdname) |
| 1385 |
free(cmdname); |
| 1386 |
return f; |
| 1387 |
} |
| 1388 |
|
| 1389 |
/*tex |
| 1390 |
|
| 1391 |
The code that implements |popen()| needs an array for tracking possible pipe |
| 1392 |
file pointers, because these need to be closed using |pclose()|. |
| 1393 |
|
| 1394 |
*/ |
| 1395 |
|
| 1396 |
#define NUM_PIPES 16 |
| 1397 |
static FILE *pipes[NUM_PIPES]; |
| 1398 |
|
| 1399 |
#ifdef WIN32 |
| 1400 |
FILE *Poptr; |
| 1401 |
#endif |
| 1402 |
|
| 1403 |
boolean open_in_or_pipe(FILE ** f_ptr, char *fn, int filefmt, const_string fopen_mode, boolean must_exist) |
| 1404 |
{ |
| 1405 |
string fname = NULL; |
| 1406 |
int i; |
| 1407 |
/*tex |
| 1408 |
|
| 1409 |
Opening a read pipe is straightforward, only have to skip past the pipe |
| 1410 |
symbol in the file name. filename quoting is assumed to happen elsewhere |
| 1411 |
(it does :-)) |
| 1412 |
|
| 1413 |
*/ |
| 1414 |
if (shellenabledp && *fn == '|') { |
| 1415 |
/*tex The user requested a pipe. */ |
| 1416 |
*f_ptr = NULL; |
| 1417 |
fname = (string) xmalloc((unsigned) (strlen(fn) + 1)); |
| 1418 |
strcpy(fname, fn); |
| 1419 |
if (fullnameoffile) |
| 1420 |
free(fullnameoffile); |
| 1421 |
fullnameoffile = xstrdup(fname); |
| 1422 |
recorder_record_input(fname + 1); |
| 1423 |
*f_ptr = runpopen(fname + 1, "r"); |
| 1424 |
free(fname); |
| 1425 |
for (i = 0; i < NUM_PIPES; i++) { |
| 1426 |
if (pipes[i] == NULL) { |
| 1427 |
pipes[i] = *f_ptr; |
| 1428 |
break; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
if (*f_ptr) |
| 1432 |
setvbuf(*f_ptr, (char *) NULL, _IONBF, 0); |
| 1433 |
#ifdef WIN32 |
| 1434 |
Poptr = *f_ptr; |
| 1435 |
#endif |
| 1436 |
return *f_ptr != NULL; |
| 1437 |
} |
| 1438 |
return luatex_open_input(f_ptr, fn, filefmt, fopen_mode, must_exist); |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
boolean open_out_or_pipe(FILE ** f_ptr, char *fn, const_string fopen_mode) |
| 1443 |
{ |
| 1444 |
string fname; |
| 1445 |
int i; |
| 1446 |
/*tex |
| 1447 |
|
| 1448 |
Opening a write pipe takes a little bit more work, because TeX will |
| 1449 |
perhaps have appended ".tex". To avoid user confusion as much as |
| 1450 |
possible, this extension is stripped only when the command is a bare |
| 1451 |
word. Some small string trickery is needed to make sure the correct |
| 1452 |
number of bytes is free()-d afterwards. |
| 1453 |
*/ |
| 1454 |
if (shellenabledp && *fn == '|') { |
| 1455 |
/*tex The user requested a pipe. */ |
| 1456 |
fname = (string) xmalloc((unsigned) (strlen(fn) + 1)); |
| 1457 |
strcpy(fname, fn); |
| 1458 |
if (strchr(fname, ' ') == NULL && strchr(fname, '>') == NULL) { |
| 1459 |
/*tex |
| 1460 |
|
| 1461 |
\METAPOST\ and \METAFIONT\ currently do not use this code, but it |
| 1462 |
is better to be prepared. Hm, what has this todo with \LUATEX ? |
| 1463 |
|
| 1464 |
*/ |
| 1465 |
if (STREQ((fname + strlen(fname) - 3), "tex")) |
| 1466 |
*(fname + strlen(fname) - 4) = 0; |
| 1467 |
*f_ptr = runpopen(fname + 1, "w"); |
| 1468 |
*(fname + strlen(fname)) = '.'; |
| 1469 |
} else { |
| 1470 |
*f_ptr = runpopen(fname + 1, "w"); |
| 1471 |
} |
| 1472 |
recorder_record_output(fname + 1); |
| 1473 |
free(fname); |
| 1474 |
for (i = 0; i < NUM_PIPES; i++) { |
| 1475 |
if (pipes[i] == NULL) { |
| 1476 |
pipes[i] = *f_ptr; |
| 1477 |
break; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
if (*f_ptr) |
| 1481 |
setvbuf(*f_ptr, (char *) NULL, _IONBF, 0); |
| 1482 |
return *f_ptr != NULL; |
| 1483 |
} |
| 1484 |
return luatex_open_output(f_ptr, fn, fopen_mode); |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
void close_file_or_pipe(FILE * f) |
| 1489 |
{ |
| 1490 |
int i; |
| 1491 |
if (shellenabledp) { |
| 1492 |
for (i = 0; i <= 15; i++) { |
| 1493 |
/*tex If this file was a pipe, |pclose()| it and return. */ |
| 1494 |
if (pipes[i] == f) { |
| 1495 |
if (f) { |
| 1496 |
pclose(f); |
| 1497 |
#ifdef WIN32 |
| 1498 |
Poptr = NULL; |
| 1499 |
#endif |
| 1500 |
} |
| 1501 |
pipes[i] = NULL; |
| 1502 |
return; |
| 1503 |
} |
| 1504 |
} |
| 1505 |
} |
| 1506 |
close_file(f); |
| 1507 |
} |