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
25 #define PATH_SEP_STRING "/"
27 /* openinout.c: open input and output files. */
29 #define BUILDNAMEDIRECT /* avoid malloc for string concat */
31 extern char *unixify (char *); /* in pathsrch.c bkph */
33 extern int shorten_file_name; /* in local.c bkph */
35 #ifdef FUNNY_CORE_DUMP
36 extern void funny_core_dump();
40 #ifdef BUILDNAMEDIRECT
41 char * xconcat (char *buffer, char *s1, char *s2)
48 memmove (buffer + n1, buffer, n2 + 1);
49 strncpy (buffer, s1, n1);
54 strcat(buffer + n1, s2);
59 char * xconcat3 (char *buffer, char *s1, char *s2, char *s3)
67 memmove (buffer + n1 + n2, buffer, n3 + 1);
68 strncpy (buffer, s1, n1);
69 strncpy (buffer + n1, s2, n2);
74 strcat(buffer + n1, s2);
75 strcat(buffer + n1 + n2, s3);
82 // separated out 1996/Jan/20 to make easier to read
83 // assumes path does not end in PATH_SEP
84 void patch_in_path (unsigned char *buffer, unsigned char *name, unsigned char *path)
87 strcpy((char *) buffer, (char *) name);
89 xconcat3((char *) buffer, (char *) path, PATH_SEP_STRING, (char *) name);
92 int qualified (unsigned char * name)
94 if (strchr((char *) name, PATH_SEP) != NULL ||
95 strchr((char *) name, '\\') != NULL ||
96 strchr((char *) name, ':') != NULL)
103 (ii) name not qualified
106 int prepend_path_if (unsigned char *buffer, unsigned char *name, char *ext, unsigned char *path)
117 if (strstr((char *)name, ext) == NULL)
120 patch_in_path(buffer, name, path);
126 // Following works on null-terminated strings
127 void check_short_name (unsigned char *s)
129 unsigned char *star, *sdot;
132 if ((star = (unsigned char *) strrchr((char *) s, '\\')) != NULL)
134 else if ((star = (unsigned char *) strrchr((char *) s, '/')) != NULL)
136 else if ((star = (unsigned char *) strchr((char *) s, ':')) != NULL)
141 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
144 n = strlen((char *) star);
147 strcpy((char *) star + 8, (char *) star + n);
149 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
153 n = strlen((char *) star);
160 /* Following works on both null-terminated names */
161 /* reconvert 254 to '~' in file name 95/Sep/26 */
162 /* reconvert 255 to ' ' in file name 95/Sep/26 */
163 /* we do this in tex3.c start_input() -> scan_file_name() now 95/Sep/26 */
164 /* kpathsea/tilde.c */
165 void retwiddle (unsigned char *s)
167 /* assumes null terminated - 97/June/5 */
168 /* while (*s != '\0' && *s != ' ') { */
171 if (*s == (unsigned char) pseudo_tilde)
173 else if (*s == (unsigned char) pseudo_space)
179 /* in lib/openclose.c */
180 bool open_input (FILE **f, path_constant_type path_index, char *fopen_mode)
182 bool openable = false;
183 char * file_name = NULL;
185 #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
186 if (path_index == TEXINPUTPATH &&
187 strncmp (name_of_file + 1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
193 if (strcmp(fopen_mode, "r") == 0)
194 fopen_mode = "rb"; /* so can catch `return' bkph */
197 name_of_file[name_length + 1] = '\0';
199 /* reinsert '~' and ' ' in file names - 95/June/5 */
200 /* done late to prevent problems with null_terminate / space_terminate */
201 if (pseudo_tilde != 0 || pseudo_space != 0)
202 retwiddle(name_of_file + 1);
204 if (shorten_file_name)
206 check_short_name(name_of_file + 1);
211 sprintf(log_line, " Open `%s' for input ", name_of_file + 1);
212 show_line(log_line, 0);
218 file_name = kpse_find_file((const_string) name_of_file + 1, kpse_tex_format, 0);
221 file_name = kpse_find_file((const_string) name_of_file + 1, kpse_fmt_format, 0);
224 file_name = kpse_find_file((const_string) name_of_file + 1, kpse_tfm_format, 0);
228 if (file_name != NULL)
230 strcpy ((char *) name_of_file + 1, file_name);
231 *f = xfopen((char *) file_name, fopen_mode);
234 if (name_of_file[1] == '.' && (name_of_file[2] == PATH_SEP || name_of_file[2] == '\\'))
236 if (name_of_file[1] == '.' && name_of_file[2] == PATH_SEP)
241 while (name_of_file[i + 2] != '\0')
243 name_of_file[i] = name_of_file[i + 2];
247 name_of_file[i] = '\0';
251 name_length = strlen((char *) name_of_file + 1);
253 if (path_index == TFMFILEPATH)
255 tfm_temp = getc (*f);
258 if (strstr((char *) name_of_file + 1, ".fmt") != NULL)
260 if (format_file == NULL)
262 format_file = xstrdup((char *) name_of_file + 1);
265 else if (strstr((char *)name_of_file+1, ".tfm") != NULL)
267 if (show_tfm_flag && log_opened)
270 n = strlen((char *) name_of_file + 1);
272 if (file_offset + n > max_print_line)
274 putc('\n', log_file);
280 fprintf(log_file, "(%s)", name_of_file + 1);
281 file_offset += n + 3;
284 /* code added 98/Sep/29 to catch first file input */
285 /* is there a problem if this file bombs ? */
286 else if (source_direct == NULL) /* 98/Sep/29 */
290 source_direct = xstrdup((char *) name_of_file + 1);
294 sprintf(log_line, "Methinks the source %s is `%s'\n", "file", source_direct);
295 show_line(log_line, 0);
298 if ((s = strrchr(source_direct, '/')) == NULL)
299 *source_direct = '\0';
305 sprintf(log_line, "Methinks the source %s is `%s'\n", "directory", source_direct);
306 show_line(log_line, 0);
314 unsigned temp_length = strlen((char *) name_of_file + 1);
315 name_of_file[temp_length + 1] = ' '; /* space terminate */
321 /* Call the external program PROGRAM, passing it `name_of_file'. */
322 /* This nonsense probably only works for Unix anyway. bkph */
323 /* For one thing, MakeTeXTFM etc is more than 8 characters ! */
325 char *get_env_shroud (char *); /* defined in texmf.c */
327 extern char * dvi_directory;
328 extern char * log_directory;
329 extern char * aux_directory;
330 extern char * fmt_directory;
331 extern char * pdf_directory;
333 /* At least check for I/O error (such as disk full) when closing */
334 /* Would be better to check while writing - but this is better than nothing */
335 /* This is used for both input and output files, but never mind ... */
337 /* now a_close returns -1 on error --- which could be used by caller */
338 /* probably want to ignore on input files ... */
340 extern void perrormod (char *s); /* in local.c */
342 // check_fclose not used by anything
343 int check_fclose (FILE * f)
346 return 0; // sanity check
348 if (ferror(f) || fclose (f))
350 perrormod("\n! I/O Error");
357 // open_output moved down here to avoid potential pragma problem
358 bool open_output (FILE **f, char *fopen_mode)
360 unsigned temp_length;
362 name_of_file[name_length + 1] = '\0';
364 if (pseudo_tilde != 0 || pseudo_space != 0)
366 retwiddle(name_of_file + 1);
369 /* 8 + 3 file names on Windows NT 95/Feb/20 */
370 if (shorten_file_name)
372 check_short_name(name_of_file + 1);
375 if (prepend_path_if (name_of_file + 1, name_of_file + 1, ".dvi", (unsigned char *) dvi_directory) ||
376 prepend_path_if (name_of_file + 1, name_of_file + 1, ".log", (unsigned char *) log_directory) ||
377 prepend_path_if (name_of_file + 1, name_of_file + 1, ".aux", (unsigned char *) aux_directory) ||
378 prepend_path_if (name_of_file + 1, name_of_file + 1, ".fmt", (unsigned char *) fmt_directory) ||
379 prepend_path_if (name_of_file + 1, name_of_file + 1, ".pdf", (unsigned char *) pdf_directory))
383 sprintf(log_line, "After prepend %s\n", name_of_file + 1);
384 show_line(log_line, 0);
390 sprintf(log_line, " Open `%s' for output ", name_of_file + 1);
391 show_line(log_line, 0);
394 /* Is the filename openable as given? */
396 /* but we can assume this is opening here for *output* */
397 *f = fopen((char *) name_of_file + 1, fopen_mode);
399 /* Can't open as given. Try the envvar. */
402 string temp_dir = get_env_shroud ("UFYNGPVU");
404 /* if (deslash) unixify(temp_dir); */ /* deslashify 93/Dec/28 */
406 if (temp_dir != NULL)
408 #ifdef BUILDNAMEDIRECT
409 unsigned char temp_name[PATH_MAX];
410 xconcat3((char *) temp_name, temp_dir, PATH_SEP_STRING, (char *) name_of_file + 1);
412 /* string temp_name = concat3 (temp_dir, "/", name_of_file + 1); */
413 string temp_name = concat3 (temp_dir, PATH_SEP_STRING, name_of_file + 1);
416 unixify((char *) temp_name); /* deslashify 93/Dec/28 */
418 /* but we can assume this is opening here for *output* */
419 *f = fopen((char*)temp_name, fopen_mode);
420 /* If this succeeded, change name_of_file accordingly. */
422 strcpy((char*) name_of_file + 1, (char *) temp_name);
423 #ifndef BUILDNAMEDIRECT
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;