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
21 #include <direct.h> /* for _getcwd() */
31 #define PATH_SEP_STRING "/"
33 /* openinout.c: open input and output files. */
35 #define BUILDNAMEDIRECT /* avoid malloc for string concat */
37 extern char *unixify (char *); /* in pathsrch.c bkph */
39 extern int shorten_file_name; /* in local.c bkph */
41 #ifdef FUNNY_CORE_DUMP
42 extern void funny_core_dump();
47 #ifdef BUILDNAMEDIRECT
48 // similar to concat, but AVOIDS using malloc, pass in place to put result
49 char *xconcat (char *buffer, char *s1, char *s2)
56 memmove (buffer + n1, buffer, n2 + 1);
57 strncpy (buffer, s1, n1);
62 strcat(buffer + n1, s2);
67 // similar to concat3, but avoids using malloc, pass in place to put result
68 char *xconcat3 (char *buffer, char *s1, char *s2, char *s3)
76 memmove (buffer + n1 + n2, buffer, n3 + 1);
77 strncpy (buffer, s1, n1);
78 strncpy (buffer + n1, s2, n2);
83 strcat(buffer + n1, s2);
84 strcat(buffer + n1 + n2, s3);
94 // separated out 1996/Jan/20 to make easier to read
95 // assumes path does not end in PATH_SEP
96 void patch_in_path (unsigned char *buffer, unsigned char *name, unsigned char *path)
99 strcpy((char *) buffer, (char *) name);
101 xconcat3((char *) buffer, (char *) path, PATH_SEP_STRING, (char *) name);
104 int qualified (unsigned char * name)
106 if (strchr((char *) name, PATH_SEP) != NULL ||
107 strchr((char *) name, '\\') != NULL ||
108 strchr((char *) name, ':') != NULL)
115 (ii) name not qualified
118 int prepend_path_if (unsigned char *buffer, unsigned char *name, char *ext, unsigned char *path)
129 if (strstr((char *)name, ext) == NULL)
132 patch_in_path(buffer, name, path);
136 #endif /* end of MSDOS */
138 // Following works on null-terminated strings
139 void check_short_name (unsigned char *s)
141 unsigned char *star, *sdot;
144 if ((star = (unsigned char *) strrchr((char *) s, '\\')) != NULL)
146 else if ((star = (unsigned char *) strrchr((char *) s, '/')) != NULL)
148 else if ((star = (unsigned char *) strchr((char *) s, ':')) != NULL)
153 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
156 n = strlen((char *) star);
159 strcpy((char *) star + 8, (char *) star + n);
161 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
165 n = strlen((char *) star);
172 /* Following works on both null-terminated names */
173 /* reconvert 254 to '~' in file name 95/Sep/26 */
174 /* reconvert 255 to ' ' in file name 95/Sep/26 */
175 /* we do this in tex3.c start_input() -> scan_file_name() now 95/Sep/26 */
176 /* kpathsea/tilde.c */
177 void retwiddle (unsigned char *s)
179 /* assumes null terminated - 97/June/5 */
180 /* while (*s != '\0' && *s != ' ') { */
183 if (*s == (unsigned char) pseudo_tilde)
185 else if (*s == (unsigned char) pseudo_space)
191 /* in lib/openclose.c */
192 bool open_input (FILE **f, path_constant_type path_index, char *fopen_mode)
194 bool openable = false;
195 char * file_name = NULL;
197 #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
198 if (path_index == TEXINPUTPATH &&
199 strncmp (name_of_file + 1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
206 if (strcmp(fopen_mode, "r") == 0)
207 fopen_mode = "rb"; /* so can catch `return' bkph */
211 name_of_file[name_length + 1] = '\0'; /* null terminate */
213 /* reinsert '~' and ' ' in file names - 95/June/5 */
214 /* done late to prevent problems with null_terminate / space_terminate */
215 if (pseudo_tilde != 0 || pseudo_space != 0)
216 retwiddle(name_of_file + 1);
219 /* 8 + 3 file names on Windows NT 95/Feb/20 */
220 if (shorten_file_name)
222 check_short_name(name_of_file + 1);
228 sprintf(log_line, " Open `%s' for input ", name_of_file + 1); /* Pascal */
229 show_line(log_line, 0);
235 file_name = kpse_find_file((const_string)name_of_file + 1, kpse_tex_format, 0);
238 file_name = kpse_find_file((const_string)name_of_file + 1, kpse_fmt_format, 0);
241 file_name = kpse_find_file((const_string)name_of_file + 1, kpse_tfm_format, 0);
245 if (file_name != NULL)
247 strcpy ((char *)name_of_file + 1, file_name);
248 *f = xfopen((char *) file_name, fopen_mode);
251 if (name_of_file[1] == '.' && (name_of_file[2] == PATH_SEP || name_of_file[2] == '\\'))
253 if (name_of_file[1] == '.' && name_of_file[2] == PATH_SEP)
258 while (name_of_file[i + 2] != '\0')
260 name_of_file[i] = name_of_file[i + 2];
264 name_of_file[i] = '\0';
268 name_length = strlen((char *) name_of_file + 1);
270 if (path_index == TFMFILEPATH)
272 tfm_temp = getc (*f);
273 //ungetc(tfm_temp, *f);
277 if (strstr((char *) name_of_file + 1, ".fmt") != NULL)
279 if (format_file == NULL)
281 format_file = xstrdup((char *) name_of_file + 1);
284 else if (strstr((char *)name_of_file+1, ".tfm") != NULL)
286 if (show_tfm_flag && log_opened)
289 n = strlen((char *) name_of_file + 1);
291 if (file_offset + n > max_print_line)
293 putc('\n', log_file);
299 fprintf(log_file, "(%s)", name_of_file + 1);
300 file_offset += n + 3;
303 /* code added 98/Sep/29 to catch first file input */
304 /* is there a problem if this file bombs ? */
305 else if (source_direct == NULL) /* 98/Sep/29 */
309 source_direct = xstrdup((char *) name_of_file + 1);
313 sprintf(log_line, "Methinks the source %s is `%s'\n", "file", source_direct);
314 show_line(log_line, 0);
317 if ((s = strrchr(source_direct, '/')) == NULL)
318 *source_direct = '\0';
324 sprintf(log_line, "Methinks the source %s is `%s'\n", "directory", source_direct);
325 show_line(log_line, 0);
328 #endif /* end of MSDOS */
333 unsigned temp_length = strlen((char *) name_of_file + 1);
334 name_of_file[temp_length + 1] = ' '; /* space terminate */
340 /* Call the external program PROGRAM, passing it `name_of_file'. */
341 /* This nonsense probably only works for Unix anyway. bkph */
342 /* For one thing, MakeTeXTFM etc is more than 8 characters ! */
344 char *get_env_shroud (char *); /* defined in texmf.c */
346 extern char * dvi_directory;
347 extern char * log_directory;
348 extern char * aux_directory;
349 extern char * fmt_directory;
350 extern char * pdf_directory;
352 /* At least check for I/O error (such as disk full) when closing */
353 /* Would be better to check while writing - but this is better than nothing */
354 /* This is used for both input and output files, but never mind ... */
356 /* now a_close returns -1 on error --- which could be used by caller */
357 /* probably want to ignore on input files ... */
359 extern void perrormod (char *s); /* in local.c */
361 // check_fclose not used by anything
362 int check_fclose (FILE * f)
365 return 0; // sanity check
367 if (ferror(f) || fclose (f))
369 perrormod("\n! I/O Error");
376 // open_output moved down here to avoid potential pragma problem
377 bool open_output (FILE **f, char *fopen_mode)
379 unsigned temp_length;
381 name_of_file[name_length + 1] = '\0';
383 if (pseudo_tilde != 0 || pseudo_space != 0)
385 retwiddle(name_of_file + 1);
389 /* 8 + 3 file names on Windows NT 95/Feb/20 */
390 if (shorten_file_name)
392 check_short_name(name_of_file + 1);
397 if (prepend_path_if (name_of_file + 1, name_of_file + 1, ".dvi", (unsigned char *) dvi_directory) ||
398 prepend_path_if (name_of_file + 1, name_of_file + 1, ".log", (unsigned char *) log_directory) ||
399 prepend_path_if (name_of_file + 1, name_of_file + 1, ".aux", (unsigned char *) aux_directory) ||
400 prepend_path_if (name_of_file + 1, name_of_file + 1, ".fmt", (unsigned char *) fmt_directory) ||
401 prepend_path_if (name_of_file + 1, name_of_file + 1, ".pdf", (unsigned char *) pdf_directory))
405 sprintf(log_line, "After prepend %s\n", name_of_file + 1);
406 show_line(log_line, 0);
413 sprintf(log_line, " Open `%s' for output ", name_of_file + 1);
414 show_line(log_line, 0);
417 /* Is the filename openable as given? */
419 /* if share_flag is non-zero and we are opening for reading use fsopen */
420 /* but we can assume this is opening here for *output* */
421 *f = fopen((char *) name_of_file + 1, fopen_mode);
423 /* Can't open as given. Try the envvar. */
426 string temp_dir = get_env_shroud ("UFYNGPVU");
428 /* if (deslash) unixify(temp_dir); */ /* deslashify 93/Dec/28 */
430 if (temp_dir != NULL)
432 #ifdef BUILDNAMEDIRECT
433 unsigned char temp_name[PATH_MAX];
434 xconcat3((char *) temp_name, temp_dir, PATH_SEP_STRING, (char *) name_of_file + 1);
436 /* string temp_name = concat3 (temp_dir, "/", name_of_file + 1); */
437 string temp_name = concat3 (temp_dir, PATH_SEP_STRING, name_of_file + 1);
440 unixify((char *) temp_name); /* deslashify 93/Dec/28 */
441 /* If share_flag is non-zero and we are opening for reading use fsopen */
442 /* but we can assume this is opening here for *output* */
443 *f = fopen((char*)temp_name, fopen_mode);
444 /* If this succeeded, change name_of_file accordingly. */
446 strcpy((char*) name_of_file + 1, (char *) temp_name);
447 #ifndef BUILDNAMEDIRECT
453 if (strstr((char *) name_of_file + 1, ".dvi") != NULL)
455 if (qualified(name_of_file + 1))
460 (void) _getcwd(log_line, sizeof(log_line));
462 (void) getcwd(log_line, sizeof(log_line));
464 strcat(log_line, PATH_SEP_STRING);
467 strcat(log_line, (char*) name_of_file + 1);
469 dvi_file_name = xstrdup(log_line);
471 else if (strstr((char *) name_of_file + 1, ".pdf") != NULL)
473 if (qualified(name_of_file + 1))
478 (void) _getcwd(log_line, sizeof(log_line));
480 (void) getcwd(log_line, sizeof(log_line));
482 strcat(log_line, PATH_SEP_STRING);
485 strcat(log_line, (char*) name_of_file + 1);
487 pdf_file_name = xstrdup(log_line);
489 else if (strstr((char *)name_of_file + 1, ".log") != NULL)
491 if (qualified(name_of_file + 1))
496 (void) _getcwd(log_line, sizeof(log_line));
498 (void) getcwd(log_line, sizeof(log_line));
500 strcat(log_line, PATH_SEP_STRING);
503 strcat(log_line, (char *) name_of_file + 1);
505 log_file_name = xstrdup(log_line);
508 temp_length = strlen ((char *)name_of_file + 1);
509 name_of_file[temp_length+1] = ' ';
512 name_length = temp_length;