1
1
/* openclose.c: open and close files for TeX, Metafont, and BibTeX.
3
Written 1995, 96 Karl Berry. Public domain. */
3
Written 1995 Karl Berry. Public domain. */
8
8
#include <kpathsea/tex-file.h>
9
9
#include <kpathsea/variable.h>
10
10
#include <kpathsea/absolute.h>
12
#include <ptexenc/ptexenc.h>
12
15
/* The globals we use to communicate. */
13
16
extern string nameoffile;
14
17
extern unsigned namelength;
15
/* For "file:line:error style error messages. */
16
extern string fullnameoffile;
17
/* For the filename recorder. */
18
extern boolean recorder_enabled;
19
/* For the output-dir option. */
20
extern string output_directory;
22
19
/* Define some variables. */
20
/* For "file:line:error" style error messages. */
23
21
string fullnameoffile; /* Defaults to NULL. */
24
22
static string recorder_name; /* Defaults to NULL. */
25
23
static FILE *recorder_file; /* Defaults to NULL. */
24
/* For the filename recorder. */
26
25
boolean recorder_enabled; /* Defaults to false. */
26
/* For the output-dir option. */
27
27
string output_directory; /* Defaults to NULL. */
29
29
/* For TeX and MetaPost. See below. Always defined so we don't have to
38
38
recorder_start(void)
40
/* Alas, while we might want to use mkstemp it is not portable.
41
So we have to be content with using a default name... */
40
/* Alas, while we'd like to use mkstemp it is not portable,
41
and doing the autoconfiscation (and providing fallbacks) is more
42
than we want to cope with. So we have to be content with using a
43
default name. Throw in the pid so at least parallel builds might
44
work (Debian bug 575731). */
47
char pid_str[MAX_INT_LENGTH];
48
sprintf (pid_str, "%ld", (long) pid);
44
recorder_name = (string)xmalloc(strlen(kpse_program_name)+5);
50
recorder_name = xmalloc(strlen(kpse_program_name)
51
+ strlen (pid_str) + 5);
45
52
strcpy(recorder_name, kpse_program_name);
53
strcat(recorder_name, pid_str);
46
54
strcat(recorder_name, ".fls");
48
56
/* If an output directory was specified, use it instead of cwd. */
73
81
/* helper for recorder_record_* */
75
recorder_record_name (string prefix, const_string nameoffile)
83
recorder_record_name (const_string prefix, const_string name)
77
85
if (recorder_enabled) {
78
86
if (!recorder_file)
80
fprintf(recorder_file, "%s %s\n", prefix, nameoffile);
88
fprintf(recorder_file, "%s %s\n", prefix, name);
81
89
fflush(recorder_file);
85
/* record an input file */
93
/* record an input file name */
87
recorder_record_input (const_string nameoffile)
95
recorder_record_input (const_string name)
89
recorder_record_name ("INPUT", nameoffile);
97
recorder_record_name ("INPUT", name);
92
/* record an output file */
100
/* record an output file name */
94
recorder_record_output (const_string nameoffile)
102
recorder_record_output (const_string name)
96
recorder_record_name ("OUTPUT", nameoffile);
104
recorder_record_name ("OUTPUT", name);
99
107
/* Open an input file F, using the kpathsea format FILEFMT and passing
121
129
free(fullnameoffile);
122
130
fullnameoffile = NULL;
124
/* Handle -output-directory.
125
FIXME: We assume that it is OK to look here first. Possibly it
126
would be better to replace lookups in "." with lookups in the
127
output_directory followed by "." but to do this requires much more
128
invasive surgery in libkpathsea. */
129
/* FIXME: This code assumes that the filename of the input file is
130
not an absolute filename. */
131
if (output_directory) {
132
fname = concat3(output_directory, DIR_SEP_STRING, nameoffile + 1);
133
*f_ptr = fopen(fname, fopen_mode);
132
/* Look in -output-directory first, if the filename is not
133
absolute. This is because .aux and other such files will get
134
written to the output directory, and we have to be able to read
135
them from there. We only look for the name as-is. */
136
if (output_directory && !kpse_absolute_p (nameoffile+1, false)) {
137
fname = concat3 (output_directory, DIR_SEP_STRING, nameoffile + 1);
138
*f_ptr = fopen (fname, fopen_mode);
136
141
namelength = strlen (fname);
137
nameoffile = (string)xmalloc (namelength + 2);
142
nameoffile = xmalloc (namelength + 2);
138
143
strcpy (nameoffile + 1, fname);
139
144
fullnameoffile = fname;
181
186
/* kpse_find_file always returns a new string. */
182
187
free (nameoffile);
183
188
namelength = strlen (fname);
184
nameoffile = (string)xmalloc (namelength + 2);
189
nameoffile = xmalloc (namelength + 2);
185
190
strcpy (nameoffile + 1, fname);
188
193
/* This fopen is not allowed to fail. */
195
if (filefmt == kpse_tex_format ||
196
filefmt == kpse_bib_format) {
197
*f_ptr = nkf_open (nameoffile + 1, fopen_mode);
189
200
*f_ptr = xfopen (nameoffile + 1, fopen_mode);
241
252
string texmfoutput = kpse_var_value("TEXMFOUTPUT");
243
254
if (texmfoutput && *texmfoutput && !absolute) {
244
string fname = concat3(texmfoutput, DIR_SEP_STRING, nameoffile+1);
255
if (fname != nameoffile + 1)
257
fname = concat3(texmfoutput, DIR_SEP_STRING, nameoffile+1);
245
258
*f_ptr = fopen(fname, fopen_mode);
250
263
if (fname != nameoffile + 1) {
251
264
free (nameoffile);
252
265
namelength = strlen (fname);
253
nameoffile = (string)xmalloc (namelength + 2);
266
nameoffile = xmalloc (namelength + 2);
254
267
strcpy (nameoffile + 1, fname);
256
269
recorder_record_output (fname);
287
if (nkf_close (f) == EOF) {
273
289
if (fclose (f) == EOF) {
274
291
/* It's not always nameoffile, we might have opened something else
275
292
in the meantime. And it's not easy to extract the filenames out
276
293
of the pool array. So just punt on the filename. Sigh. This