1 /* Copyright 1992 Karl Berry
2 Copyright 2007 TeX Users Group
3 Copyright 2014 Clerk Ma
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 /* openinout.c: open input and output files. */
27 #define PATH_SEP_STRING "/"
29 #define BUILDNAMEDIRECT
31 extern int shorten_file_name; /* in local.c bkph */
33 #ifdef FUNNY_CORE_DUMP
34 extern void funny_core_dump(void);
38 #ifdef BUILDNAMEDIRECT
39 char * xconcat (char *buffer, char *s1, char *s2)
46 memmove(buffer + n1, buffer, n2 + 1);
47 strncpy(buffer, s1, n1);
52 strcat(buffer + n1, s2);
57 char * xconcat3 (char *buffer, char *s1, char *s2, char *s3)
65 memmove(buffer + n1 + n2, buffer, n3 + 1);
66 strncpy(buffer, s1, n1);
67 strncpy(buffer + n1, s2, n2);
72 strcat(buffer + n1, s2);
73 strcat(buffer + n1 + n2, s3);
80 // separated out 1996/Jan/20 to make easier to read
81 // assumes path does not end in PATH_SEP
82 void patch_in_path (unsigned char *buffer, unsigned char *name, unsigned char *path)
85 strcpy((char *) buffer, (char *) name);
87 xconcat3((char *) buffer, (char *) path, PATH_SEP_STRING, (char *) name);
90 int qualified (unsigned char * name)
92 if (strchr((char *) name, PATH_SEP) != NULL ||
93 strchr((char *) name, '\\') != NULL ||
94 strchr((char *) name, ':') != NULL)
101 (ii) name not qualified
104 int prepend_path_if (unsigned char *buffer, unsigned char *name, char *ext, unsigned char *path)
115 if (strstr((char *) name, ext) == NULL)
118 patch_in_path(buffer, name, path);
123 // Following works on null-terminated strings
124 void check_short_name (unsigned char *s)
126 unsigned char *star, *sdot;
129 if ((star = (unsigned char *) strrchr((char *) s, '\\')) != NULL)
131 else if ((star = (unsigned char *) strrchr((char *) s, '/')) != NULL)
133 else if ((star = (unsigned char *) strchr((char *) s, ':')) != NULL)
138 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
141 n = strlen((char *) star);
144 strcpy((char *) star + 8, (char *) star + n);
146 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
150 n = strlen((char *) star);
157 /* Following works on both null-terminated names */
158 /* reconvert 254 to '~' in file name 95/Sep/26 */
159 /* reconvert 255 to ' ' in file name 95/Sep/26 */
160 /* we do this in tex3.c start_input() -> scan_file_name() now 95/Sep/26 */
161 /* kpathsea/tilde.c */
162 void retwiddle (unsigned char *s)
166 if (*s == (unsigned char) pseudo_tilde)
168 else if (*s == (unsigned char) pseudo_space)
174 /* in lib/openclose.c */
175 bool open_input (FILE **f, path_constant_type path_index, char *fopen_mode)
177 bool openable = false;
178 char * file_name = NULL;
180 #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
181 if (path_index == TEXINPUTPATH &&
182 strncmp(name_of_file + 1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
188 if (strcmp(fopen_mode, "r") == 0)
192 name_of_file[name_length + 1] = '\0';
194 /* reinsert '~' and ' ' in file names - 95/June/5 */
195 /* done late to prevent problems with null_terminate / space_terminate */
196 if (pseudo_tilde != 0 || pseudo_space != 0)
197 retwiddle(name_of_file + 1);
199 if (shorten_file_name)
201 check_short_name(name_of_file + 1);
206 sprintf(log_line, " Open `%s' for input ", name_of_file + 1);
207 show_line(log_line, 0);
213 file_name = kpse_find_file((const_string) name_of_file + 1, kpse_tex_format, 0);
217 file_name = kpse_find_file((const_string) name_of_file + 1, kpse_fmt_format, 0);
221 file_name = kpse_find_file((const_string) name_of_file + 1, kpse_tfm_format, 0);
225 if (file_name != NULL)
227 strcpy ((char *) name_of_file + 1, file_name);
228 *f = xfopen((char *) file_name, fopen_mode);
231 if (name_of_file[1] == '.' && (name_of_file[2] == PATH_SEP || name_of_file[2] == '\\'))
233 if (name_of_file[1] == '.' && name_of_file[2] == PATH_SEP)
238 while (name_of_file[i + 2] != '\0')
240 name_of_file[i] = name_of_file[i + 2];
244 name_of_file[i] = '\0';
248 name_length = strlen((char *) name_of_file + 1);
250 if (path_index == TFMFILEPATH)
255 if (strstr((char *) name_of_file + 1, ".fmt") != NULL)
257 if (format_file == NULL)
259 format_file = xstrdup((char *) name_of_file + 1);
263 gz_fmt_file = gzdopen(fileno(*f), "rb9");
266 else if (strstr((char *)name_of_file + 1, ".tfm") != NULL)
268 if (show_tfm_flag && log_opened)
271 n = strlen((char *) name_of_file + 1);
273 if (file_offset + n > max_print_line)
275 putc('\n', log_file);
281 fprintf(log_file, "(%s)", name_of_file + 1);
282 file_offset += n + 3;
285 /* code added 98/Sep/29 to catch first file input */
286 /* is there a problem if this file bombs ? */
287 else if (source_direct == NULL)
291 source_direct = xstrdup((char *) name_of_file + 1);
295 sprintf(log_line, "Methinks the source %s is `%s'\n", "file", source_direct);
296 show_line(log_line, 0);
299 if ((s = strrchr(source_direct, '/')) == NULL)
300 *source_direct = '\0';
306 sprintf(log_line, "Methinks the source %s is `%s'\n", "directory", source_direct);
307 show_line(log_line, 0);
315 unsigned temp_length = strlen((char *) name_of_file + 1);
316 name_of_file[temp_length + 1] = ' ';
322 /* Call the external program PROGRAM, passing it `name_of_file'. */
323 /* This nonsense probably only works for Unix anyway. bkph */
324 /* For one thing, MakeTeXTFM etc is more than 8 characters ! */
326 extern char * dvi_directory;
327 extern char * log_directory;
328 extern char * aux_directory;
329 extern char * fmt_directory;
330 extern char * pdf_directory;
332 /* At least check for I/O error (such as disk full) when closing */
333 /* Would be better to check while writing - but this is better than nothing */
334 /* This is used for both input and output files, but never mind ... */
336 /* now a_close returns -1 on error --- which could be used by caller */
337 /* probably want to ignore on input files ... */
339 // check_fclose not used by anything
340 int check_fclose (FILE * f)
343 return 0; // sanity check
345 if (ferror(f) || fclose (f))
347 perrormod("\n! I/O Error");
354 // open_output moved down here to avoid potential pragma problem
355 bool open_output (FILE **f, char *fopen_mode)
357 unsigned temp_length;
359 name_of_file[name_length + 1] = '\0';
361 if (pseudo_tilde != 0 || pseudo_space != 0)
363 retwiddle(name_of_file + 1);
366 /* 8 + 3 file names on Windows NT 95/Feb/20 */
367 if (shorten_file_name)
369 check_short_name(name_of_file + 1);
372 if (prepend_path_if(name_of_file + 1, name_of_file + 1, ".dvi", (unsigned char *) dvi_directory) ||
373 prepend_path_if(name_of_file + 1, name_of_file + 1, ".log", (unsigned char *) log_directory) ||
374 prepend_path_if(name_of_file + 1, name_of_file + 1, ".aux", (unsigned char *) aux_directory) ||
375 prepend_path_if(name_of_file + 1, name_of_file + 1, ".fmt", (unsigned char *) fmt_directory) ||
376 prepend_path_if(name_of_file + 1, name_of_file + 1, ".pdf", (unsigned char *) pdf_directory))
380 sprintf(log_line, "After prepend %s\n", name_of_file + 1);
381 show_line(log_line, 0);
387 sprintf(log_line, " Open `%s' for output ", name_of_file + 1);
388 show_line(log_line, 0);
391 /* but we can assume this is opening here for *output* */
392 *f = fopen((char *) name_of_file + 1, fopen_mode);
394 /* Can't open as given. Try the envvar. */
397 string temp_dir = kpse_var_value("TEXMFOUTPUT");
399 if (temp_dir != NULL)
401 #ifdef BUILDNAMEDIRECT
402 unsigned char temp_name[PATH_MAX];
403 xconcat3((char *) temp_name, temp_dir, PATH_SEP_STRING, (char *) name_of_file + 1);
405 string temp_name = concat3 (temp_dir, PATH_SEP_STRING, name_of_file + 1);
409 unixify((char *) temp_name);
411 /* but we can assume this is opening here for *output* */
412 *f = fopen((char*)temp_name, fopen_mode);
413 /* If this succeeded, change name_of_file accordingly. */
415 strcpy((char*) name_of_file + 1, (char *) temp_name);
416 #ifndef BUILDNAMEDIRECT
423 if (strstr((char *) name_of_file + 1, ".fmt") != NULL)
425 gz_fmt_file = gzdopen(fileno(*f), "wb9");
429 if (strstr((char *) name_of_file + 1, ".dvi") != NULL)
431 if (qualified(name_of_file + 1))
435 (void) getcwd(log_line, sizeof(log_line));
436 strcat(log_line, PATH_SEP_STRING);
439 strcat(log_line, (char*) name_of_file + 1);
441 dvi_file_name = xstrdup(log_line);
443 else if (strstr((char *) name_of_file + 1, ".pdf") != NULL)
445 if (qualified(name_of_file + 1))
449 (void) getcwd(log_line, sizeof(log_line));
450 strcat(log_line, PATH_SEP_STRING);
453 strcat(log_line, (char*) name_of_file + 1);
455 pdf_file_name = xstrdup(log_line);
457 else if (strstr((char *)name_of_file + 1, ".log") != NULL)
459 if (qualified(name_of_file + 1))
463 (void) getcwd(log_line, sizeof(log_line));
464 strcat(log_line, PATH_SEP_STRING);
467 strcat(log_line, (char *) name_of_file + 1);
469 log_file_name = xstrdup(log_line);
472 temp_length = strlen ((char *)name_of_file + 1);
473 name_of_file[temp_length+1] = ' ';
476 name_length = temp_length;