w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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