Debian Codegraph @

texk/web2c/lib/openclose.c

raw
1 /* openclose.c: open and close files for TeX, Metafont, and BibTeX.
2
3 Written 1995 Karl Berry. Public domain. */
4
5 #include <w2c/config.h>
6 #include "lib.h"
7 #include <kpathsea/c-pathch.h>
8 #include <kpathsea/tex-file.h>
9 #include <kpathsea/variable.h>
10 #include <kpathsea/absolute.h>
11 #ifdef PTEX
12 #include <ptexenc/ptexenc.h>
13 #endif
14
15 #ifdef _WIN32
16 static int fsyscp_remove(char *s)
17 {
18 wchar_t *sw = NULL;
19 int ret;
20
21 if(!file_system_codepage)
22 return remove(s);
23
24 sw = get_wstring_from_fsyscp(s, sw);
25 ret = _wremove(sw);
26 if(sw) free(sw);
27 return ret;
28 }
29
30 static int fsyscp_rename(char *s1, char *s2)
31 {
32 wchar_t *sw1 = NULL, *sw2 = NULL;
33 int ret;
34
35 if(!file_system_codepage)
36 return rename(s1, s2);
37
38 sw1 = get_wstring_from_fsyscp(s1, sw1);
39 sw2 = get_wstring_from_fsyscp(s2, sw2);
40 ret = _wrename(sw1, sw2);
41 if(sw1) free(sw1);
42 if(sw2) free(sw2);
43 return ret;
44 }
45
46 FILE *f_fsyscp_fopen(const char *filename, const char *mode)
47 {
48 if(!file_system_codepage)
49 return fopen(filename, mode);
50
51 return fsyscp_fopen(filename, mode);
52 }
53
54 FILE *f_fsyscp_xfopen(const char *filename, const char *mode)
55 {
56 if(!file_system_codepage)
57 return xfopen(filename, mode);
58
59 return fsyscp_xfopen(filename, mode);
60 }
61 #undef fopen
62 #undef xfopen
63 #define fopen f_fsyscp_fopen
64 #define xfopen f_fsyscp_xfopen
65 #define rename fsyscp_rename
66 #define remove fsyscp_remove
67 #endif /* _WIN32 */
68
69 /* The globals we use to communicate. */
70 extern string nameoffile;
71 extern unsigned namelength;
72
73 /* Define some variables. */
74 /* For "file:line:error" style error messages. */
75 string fullnameoffile; /* Defaults to NULL. */
76 static string recorder_name; /* Defaults to NULL. */
77 static FILE *recorder_file; /* Defaults to NULL. */
78 /* For the filename recorder. */
79 boolean recorder_enabled; /* Defaults to false. */
80 /* For the output-dir option. */
81 string output_directory; /* Defaults to NULL. */
82
83 /* For TeX and MetaPost. See below. Always defined so we don't have to
84 #ifdef, and thus this file can be compiled once and go in lib.a. */
85 int tfmtemp;
86 int ocptemp;
87 int texinputtype;
88
89 /* Helpers for the filename recorder... */
90 /* Start the recorder */
91 static void
92 recorder_start(void)
93 {
94 /* Alas, while we'd like to use mkstemp it is not portable,
95 and doing the autoconfiscation (and providing fallbacks) is more
96 than we want to cope with. So we have to be content with using a
97 default name. Throw in the pid so at least parallel builds might
98 work (Debian bug 575731). */
99 string cwd;
100 char pid_str[MAX_INT_LENGTH];
101
102 /* Windows (MSVC) seems to have no pid_t, so instead of storing the
103 value returned by getpid() we immediately consume it. */
104 sprintf (pid_str, "%ld", (long) getpid());
105 recorder_name = concat3(kpse_program_name, pid_str, ".fls");
106
107 /* If an output directory was specified, use it instead of cwd. */
108 if (output_directory) {
109 string temp = concat3(output_directory, DIR_SEP_STRING, recorder_name);
110 free(recorder_name);
111 recorder_name = temp;
112 }
113
114 recorder_file = xfopen(recorder_name, FOPEN_W_MODE);
115
116 cwd = xgetcwd();
117 #if defined(_WIN32)
118 {
119 wchar_t *wpwd;
120 if (file_system_codepage != 0 &&
121 file_system_codepage != win32_codepage) {
122 wpwd = get_wstring_from_mbstring(win32_codepage, cwd, wpwd=NULL);
123 free (cwd);
124 cwd = get_mbstring_from_wstring(file_system_codepage, wpwd, cwd=NULL);
125 free (wpwd);
126 }
127 }
128 #endif /* _WIN32 */
129
130 fprintf(recorder_file, "PWD %s\n", cwd);
131 free(cwd);
132 }
133
134 /* Change the name of the recorder file after we know the log file to
135 the usual thing -- no pid integer and the document file name instead
136 of the program name. Unfortunately, we have to explicitly take
137 -output-directory into account (again), since the NEW_NAME we are
138 called with does not; it is just the log file name with .log replaced
139 by .fls. */
140
141 void
142 recorder_change_filename (string new_name)
143 {
144 string temp = NULL;
145 #if defined(PTEX) && !defined(WIN32)
146 string fname0 = NULL;
147 #endif
148
149 if (!recorder_file)
150 return;
151
152 /* On windows, an opened file cannot be renamed. */
153 #if defined(_WIN32)
154 fclose (recorder_file);
155 #endif /* _WIN32 */
156
157 #if defined(PTEX) && !defined(WIN32)
158 fname0 = ptenc_from_internal_enc_string_to_utf8(new_name);
159 if (fname0) {
160 new_name = fname0;
161 }
162 #endif
163
164 /* If an output directory was specified, use it. */
165 if (output_directory) {
166 temp = concat3(output_directory, DIR_SEP_STRING, new_name);
167 new_name = temp;
168 }
169
170 /* On windows, renaming fails if a file with new_name exists. */
171 #if defined(_WIN32)
172 remove (new_name);
173 #endif /* _WIN32 */
174
175 rename(recorder_name, new_name);
176 free(recorder_name);
177 recorder_name = xstrdup(new_name);
178
179 /* reopen the recorder file by FOPEN_A_MODE. */
180 #if defined(_WIN32)
181 recorder_file = xfopen (recorder_name, FOPEN_A_MODE);
182 #endif /* _WIN32 */
183
184 if (temp)
185 free (temp);
186 #if defined(PTEX) && !defined(WIN32)
187 if (fname0)
188 free (fname0);
189 #endif
190 }
191
192 /* helper for recorder_record_* */
193 static void
194 recorder_record_name (const_string prefix, const_string name)
195 {
196 if (recorder_enabled) {
197 if (!recorder_file)
198 recorder_start();
199 fprintf(recorder_file, "%s %s\n", prefix, name);
200 fflush(recorder_file);
201 }
202 }
203
204 /* record an input file name */
205 void
206 recorder_record_input (const_string name)
207 {
208 recorder_record_name ("INPUT", name);
209 }
210
211 /* record an output file name */
212 void
213 recorder_record_output (const_string name)
214 {
215 recorder_record_name ("OUTPUT", name);
216 }
217
218 /* Open input file *F_PTR, using the kpathsea format FILEFMT and passing
219 FOPEN_MODE to fopen. The filename is in `nameoffile+1'. We return
220 whether or not the open succeeded. If it did, `nameoffile' is set to
221 the full filename opened, and `namelength' to its length. */
222
223 boolean
224 open_input (FILE **f_ptr, int filefmt, const_string fopen_mode)
225 {
226 string fname = NULL;
227 #if defined(PTEX) && !defined(WIN32)
228 string fname0;
229 #endif
230 #ifdef FUNNY_CORE_DUMP
231 /* This only applies if a preloaded TeX/Metafont is being made;
232 it allows automatic creation of the core dump (typing ^\ loses
233 since that requires manual intervention). */
234 if ((filefmt == kpse_tex_format || filefmt == kpse_mf_format
235 || filefmt == kpse_mp_format)
236 && STREQ (nameoffile + 1, "HackyInputFileNameForCoreDump.tex"))
237 funny_core_dump ();
238 #endif
239
240 /* We havent found anything yet. */
241 *f_ptr = NULL;
242 if (fullnameoffile)
243 free(fullnameoffile);
244 fullnameoffile = NULL;
245
246 /* Look in -output-directory first, if the filename is not
247 absolute. This is because .aux and other such files will get
248 written to the output directory, and we have to be able to read
249 them from there. We only look for the name as-is. */
250
251 #if defined(PTEX) && !defined(WIN32)
252 fname0 = ptenc_from_internal_enc_string_to_utf8(nameoffile + 1);
253 if (fname0) {
254 free (nameoffile);
255 namelength = strlen (fname0);
256 nameoffile = xmalloc (namelength + 2);
257 strcpy (nameoffile + 1, fname0);
258 free (fname0);
259 }
260 #endif
261 if (output_directory && !kpse_absolute_p (nameoffile+1, false)) {
262 fname = concat3 (output_directory, DIR_SEP_STRING, nameoffile + 1);
263 *f_ptr = fopen (fname, fopen_mode);
264 #if !defined(_WIN32)
265 /*
266 if fname is a directory, discard it.
267 */
268 if (*f_ptr && dir_p (fname)) {
269 fclose (*f_ptr);
270 *f_ptr = NULL;
271 }
272 #endif
273 if (*f_ptr) {
274 #if defined(PTEX) && !defined(WIN32)
275 fname0 = ptenc_from_utf8_string_to_internal_enc(fname);
276 if (fname0) {
277 free (fname);
278 fname = fname0;
279 }
280 #endif
281 free (nameoffile);
282 namelength = strlen (fname);
283 nameoffile = xmalloc (namelength + 2);
284 strcpy (nameoffile + 1, fname);
285 fullnameoffile = fname;
286 } else {
287 free (fname);
288 }
289 }
290
291 /* No file means do the normal search. */
292 if (*f_ptr == NULL) {
293 /* A negative FILEFMT means don't use a path. */
294 if (filefmt < 0) {
295 /* no_file_path, for BibTeX .aux files and MetaPost things. */
296 *f_ptr = fopen(nameoffile + 1, fopen_mode);
297 /* FIXME... fullnameoffile = xstrdup(nameoffile + 1); */
298 } else {
299 /* The only exception to `must_exist' being true is \openin, for
300 which we set `tex_input_type' to 0 in the change file. */
301 /* According to the pdfTeX people, pounding the disk for .vf files
302 is overkill as well. A more general solution would be nice. */
303 boolean must_exist;
304 must_exist = (filefmt != kpse_tex_format || texinputtype)
305 && (filefmt != kpse_vf_format);
306 fname = kpse_find_file (nameoffile + 1,
307 (kpse_file_format_type)filefmt,
308 must_exist);
309 if (fname) {
310 fullnameoffile = xstrdup(fname);
311 #if defined(PTEX) && !defined(WIN32)
312 fname0 = ptenc_from_utf8_string_to_internal_enc(fullnameoffile);
313 if (fname0) {
314 free (fullnameoffile);
315 fullnameoffile = fname0;
316 }
317 #endif
318 /* If we found the file in the current directory, don't leave
319 the `./' at the beginning of `nameoffile', since it looks
320 dumb when `tex foo' says `(./foo.tex ... )'. On the other
321 hand, if the user said `tex ./foo', and that's what we
322 opened, then keep it -- the user specified it, so we
323 shouldn't remove it. */
324 if (fname[0] == '.' && IS_DIR_SEP (fname[1])
325 && (nameoffile[1] != '.' || !IS_DIR_SEP (nameoffile[2])))
326 {
327 unsigned i = 0;
328 while (fname[i + 2] != 0) {
329 fname[i] = fname[i + 2];
330 i++;
331 }
332 fname[i] = 0;
333 }
334
335 /* This fopen is not allowed to fail. */
336 #if defined(PTEX) && !defined(WIN32)
337 if (filefmt == kpse_tex_format ||
338 filefmt == kpse_bib_format) {
339 *f_ptr = nkf_open (fname, fopen_mode);
340 } else
341 #endif
342 *f_ptr = xfopen (fname, fopen_mode);
343
344 /* kpse_find_file always returns a new string. */
345 #if defined(PTEX) && !defined(WIN32)
346 fname0 = ptenc_from_utf8_string_to_internal_enc(fname);
347 if (fname0) {
348 free (fname);
349 fname = fname0;
350 }
351 #endif
352 free (nameoffile);
353 namelength = strlen (fname);
354 nameoffile = xmalloc (namelength + 2);
355 strcpy (nameoffile + 1, fname);
356 free (fname);
357 }
358 }
359 }
360
361 if (*f_ptr) {
362 recorder_record_input (nameoffile + 1);
363
364 /* If we just opened a TFM file, we have to read the first
365 byte, to pretend we're Pascal. See tex.ch and mp.ch.
366 Ditto for the ocp/ofm Omega file formats. */
367 if (filefmt == kpse_tfm_format) {
368 tfmtemp = getc (*f_ptr);
369 /* We intentionally do not check for EOF here, i.e., an
370 empty TFM file. TeX will see the 255 byte and complain
371 about a bad TFM file, which is what we want. */
372 } else if (filefmt == kpse_ocp_format) {
373 ocptemp = getc (*f_ptr);
374 } else if (filefmt == kpse_ofm_format) {
375 tfmtemp = getc (*f_ptr);
376 }
377 }
378
379 return *f_ptr != NULL;
380 }
381
382
383 /* Open input file *F_PTR (of type FILEFMT), prepending the directory
384 part of the string FNAME to `nameoffile'+1, unless that is already
385 kpse_absolute_p. This is called from BibTeX, to open subsidiary .aux
386 files, with FNAME set to the top-level aux file. The idea is that if
387 we're invoked as bibtex somedir/foo.aux, and foo.aux has an
388 \@input{bar} statement, we should look for somedir/bar.aux too. (See
389 bibtex-auxinclude.test.) */
390
391 boolean
392 open_input_with_dirname (FILE **f_ptr, int filefmt, const char *fname)
393 {
394 boolean ret = false;
395 char *top_dir = xdirname (fname);
396
397 if (top_dir && *top_dir && !STREQ (top_dir, ".")
398 && !kpse_absolute_p (nameoffile+1, true)) {
399 char *newname = concat3 (top_dir, DIR_SEP_STRING, nameoffile+1);
400 free (nameoffile);
401 nameoffile = xmalloc (strlen (newname) + 2);
402 strcpy (nameoffile + 1, newname);
403 ret = open_input (f_ptr, filefmt, FOPEN_RBIN_MODE);
404 free (newname);
405 }
406
407 free (top_dir);
408 return ret;
409 }
410
411
412 /* Open an output file F either in the current directory or in
413 $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
414 (Actually, this also applies to the BibTeX and MetaPost output files,
415 but `TEXMFMPBIBOUTPUT' was just too long.) The filename is in the
416 global `nameoffile' + 1. We return whether or not the open
417 succeeded. If it did, `nameoffile' is reset to the name opened if
418 necessary, and `namelength' to its length. */
419
420 boolean
421 open_output (FILE **f_ptr, const_string fopen_mode)
422 {
423 string fname;
424 string fname0;
425 boolean absolute = kpse_absolute_p(nameoffile+1, false);
426
427 fname = nameoffile + 1;
428 #if defined(PTEX) && !defined(WIN32)
429 fname0 = ptenc_from_internal_enc_string_to_utf8(fname);
430 if (fname0) {
431 if (fname != nameoffile + 1) free(fname);
432 fname = fname0;
433 }
434 #endif
435 /* If we have an explicit output directory, use it. */
436 if (output_directory && !absolute) {
437 fname0 = concat3(output_directory, DIR_SEP_STRING, fname);
438 if (fname0) {
439 if (fname != nameoffile + 1) free(fname);
440 fname = fname0;
441 }
442 }
443
444 /* Is the filename openable as given? */
445 *f_ptr = fopen (fname, fopen_mode);
446
447 if (!*f_ptr) {
448 /* Can't open as given. Try the envvar. */
449 string texmfoutput = kpse_var_value("TEXMFOUTPUT");
450
451 if (texmfoutput && *texmfoutput && !absolute) {
452 if (fname != nameoffile + 1)
453 free(fname);
454 fname = concat3(texmfoutput, DIR_SEP_STRING, nameoffile+1);
455 *f_ptr = fopen(fname, fopen_mode);
456 }
457 }
458 /* If this succeeded, change nameoffile accordingly. */
459 if (*f_ptr) {
460 if (fname != nameoffile + 1) {
461 #if defined(PTEX) && !defined(WIN32)
462 fname0 = ptenc_from_utf8_string_to_internal_enc(fname);
463 if (fname0) {
464 free(fname);
465 fname = fname0;
466 }
467 #endif
468 free (nameoffile);
469 namelength = strlen (fname);
470 nameoffile = xmalloc (namelength + 2);
471 strcpy (nameoffile + 1, fname);
472 }
473 recorder_record_output (fname);
474 }
475 if (fname != nameoffile +1)
476 free(fname);
477 return *f_ptr != NULL;
478 }
479
480 /* Close F. */
481
482 void
483 close_file (FILE *f)
484 {
485 /* If F is null, just return. bad_pool might close a file that has
486 never been opened. */
487 if (!f)
488 return;
489
490 #ifdef PTEX
491 #ifdef WIN32
492 clear_infile_enc (f);
493 if (fclose (f) == EOF) {
494 #else
495 if (nkf_close (f) == EOF) {
496 #endif
497 #else
498 if (fclose (f) == EOF) {
499 #endif
500 /* It's not always nameoffile, we might have opened something else
501 in the meantime. And it's not easy to extract the filenames out
502 of the pool array. So just punt on the filename. Sigh. This
503 probably doesn't need to be a fatal error. */
504 perror ("fclose");
505 }
506 }