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 #pragma warning(disable:4115) // kill rpcasync.h complaint
27 #define MYLIBAPI __declspec(dllexport)
32 #pragma warning(disable:4996)
33 #pragma warning(disable:4131) // old style declarator
34 #pragma warning(disable:4135) // conversion between different integral types
35 #pragma warning(disable:4127) // conditional expression is constant
39 #include <direct.h> /* for _getcwd() */
46 #define PATH_SEP_STRING "/"
48 /* openinout.c: open input and output files. */
50 #define BUILDNAMEDIRECT /* avoid malloc for string concat */
52 bool test_read_access (unsigned char *, int); /* in ourpaths.c - bkph */
54 extern char *unixify (char *); /* in pathsrch.c bkph */
56 extern void try_and_open (char *); /* in local.c bkph */
58 extern int shorten_file_name; /* in local.c bkph */
60 #ifdef FUNNY_CORE_DUMP
61 /* This is defined in ./texmf.c. */
62 extern void funny_core_dump();
63 #endif /* FUNNY_CORE_DUMP */
67 #ifdef BUILDNAMEDIRECT
68 /* kpathsea/concat.c */
69 /* kpathsea/concat3.c */
70 /* similar to concat, but AVOIDS using malloc, pass in place to put result */
71 char *xconcat (char *buffer, char *s1, char *s2)
77 { /* treat special case of overlap */
78 memmove (buffer + n1, buffer, n2 + 1); /* trailing null ! */
79 strncpy (buffer, s1, n1);
84 strcat(buffer + n1, s2);
89 /* similar to concat3, but avoids using malloc, pass in place to put result */
90 char *xconcat3 (char *buffer, char *s1, char *s2, char *s3)
97 { /* treat special case of overlap */
98 memmove (buffer + n1 + n2, buffer, n3 + 1); /* trailing null ! */
99 strncpy (buffer, s1, n1);
100 strncpy (buffer + n1, s2, n2);
105 strcat(buffer + n1, s2);
106 strcat(buffer + n1 + n2, s3);
110 #endif /* end of BUILDNAMEDIRECT */
112 #endif /* end of ifdef MSDOS ??? */
115 /* separated out 1996/Jan/20 to make easier to read */
116 /* assumes path does not end in PATH_SEP */
117 void patch_in_path (unsigned char *buffer, unsigned char *name, unsigned char *path)
119 #ifdef BUILDNAMEDIRECT
121 strcpy((char *) buffer, (char *) name);
123 xconcat3((char *) buffer, (char *) path, PATH_SEP_STRING, (char *) name);
126 temp_name = concat3 (path, PATH_SEP_STRING, name);
127 strcpy (buffer, temp_name);
132 int qualified (unsigned char * name)
134 if (strchr((char *) name, PATH_SEP) != NULL ||
135 strchr((char *) name, '\\') != NULL ||
136 strchr((char *) name, ':') != NULL)
143 (ii) name not qualified
146 int prepend_path_if (unsigned char *buffer, unsigned char *name, char *ext, unsigned char *path)
157 if (strstr((char *)name, ext) == NULL)
160 patch_in_path(buffer, name, path);
164 #endif /* end of MSDOS */
166 /* Following works on null-terminated strings */
168 void check_short_name (unsigned char *s)
170 unsigned char *star, *sdot;
173 if ((star = (unsigned char *) strrchr((char *) s, '\\')) != NULL)
175 else if ((star = (unsigned char *) strrchr((char *) s, '/')) != NULL)
177 else if ((star = (unsigned char *) strchr((char *) s, ':')) != NULL)
182 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
185 n = strlen((char *) star);
188 strcpy((char *) star + 8, (char *) star + n);
190 if ((sdot = (unsigned char *) strchr((char *) star, '.')) != NULL)
194 n = strlen((char *) star);
201 /* Following works on both null-terminated names */
202 /* reconvert 254 to '~' in file name 95/Sep/26 */
203 /* reconvert 255 to ' ' in file name 95/Sep/26 */
204 /* we do this in tex3.c start_input() -> scan_file_name() now 95/Sep/26 */
205 /* kpathsea/tilde.c */
206 void retwiddle (unsigned char *s)
208 /* assumes null terminated - 97/June/5 */
209 /* while (*s != '\0' && *s != ' ') { */
212 if (*s == (unsigned char) pseudo_tilde)
214 else if (*s == (unsigned char) pseudo_space)
220 /* in lib/openclose.c */
221 bool open_input (FILE **f, path_constant_type path_index, char *fopen_mode)
223 bool openable = false;
225 #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
226 if (path_index == TEXINPUTPATH &&
227 strncmp (name_of_file + 1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
229 #endif /* FUNNY_CORE_DUMP and not BibTeX */
234 if (strcmp(fopen_mode, "r") == 0)
235 fopen_mode = "rb"; /* so can catch `return' bkph */
239 name_of_file[name_length + 1] = '\0'; /* null terminate */
241 /* reinsert '~' and ' ' in file names - 95/June/5 */
242 /* done late to prevent problems with null_terminate / space_terminate */
243 if (pseudo_tilde != 0 || pseudo_space != 0)
244 retwiddle(name_of_file + 1);
247 /* 8 + 3 file names on Windows NT 95/Feb/20 */
248 if (shorten_file_name)
250 check_short_name(name_of_file + 1);
256 sprintf(log_line, " Open `%s' for input ", name_of_file + 1); /* Pascal */
257 show_line(log_line, 0);
260 if (test_read_access(name_of_file + 1, path_index))
262 *f = xfopen((char *) name_of_file + 1, fopen_mode);
265 if (name_of_file[1] == '.' && (name_of_file[2] == PATH_SEP || name_of_file[2] == '\\'))
267 if (name_of_file[1] == '.' && name_of_file[2] == PATH_SEP)
272 while (name_of_file[i + 2] != '\0')
274 name_of_file[i] = name_of_file[i + 2];
278 name_of_file[i] = '\0';
282 name_length = strlen((char *) name_of_file + 1);
285 if (path_index == TFMFILEPATH)
287 tfm_temp = getc (*f);
292 if (strstr((char *) name_of_file + 1, ".fmt") != NULL)
294 if (format_file == NULL)
296 format_file = xstrdup((char *) name_of_file + 1);
299 else if (strstr((char *)name_of_file + 1, ".poo") != NULL)
301 if (string_file == NULL)
303 string_file = xstrdup((char *) name_of_file + 1);
306 else if (strstr((char *)name_of_file+1, ".tfm") != NULL)
308 if (show_tfm_flag && log_opened)
311 int old_setting = selector;
312 char *s = name_of_file + 1;
321 selector = old_setting;
324 n = strlen((char *) name_of_file + 1);
326 if (file_offset + n > max_print_line)
328 putc('\n', log_file);
334 fprintf(log_file, "(%s)", name_of_file + 1);
336 /* space_terminate (name_of_file + 1); */
337 #endif /* end of WRAPLINES */
340 /* code added 98/Sep/29 to catch first file input */
341 /* is there a problem if this file bombs ? */
342 else if (source_direct == NULL) /* 98/Sep/29 */
346 source_direct = xstrdup((char *) name_of_file + 1);
350 sprintf(log_line, "Methinks the source %s is `%s'\n", "file", source_direct);
351 show_line(log_line, 0);
354 if ((s = strrchr(source_direct, '/')) == NULL)
355 *source_direct = '\0';
361 sprintf(log_line, "Methinks the source %s is `%s'\n", "directory", source_direct);
362 show_line(log_line, 0);
365 #endif /* end of MSDOS */
368 /* space_terminate (name_of_file + 1); */
370 unsigned temp_length = strlen((char *) name_of_file + 1);
371 name_of_file[temp_length + 1] = ' '; /* space terminate */
372 /* set up name_length ??? */
378 /* Call the external program PROGRAM, passing it `name_of_file'. */
379 /* This nonsense probably only works for Unix anyway. bkph */
380 /* For one thing, MakeTeXTFM etc is more than 8 characters ! */
388 char *get_env_shroud (char *); /* defined in texmf.c */
390 /* char outputdirectory[PATH_MAX]; */ /* defined in local.c */
392 extern char * dvi_directory; /* defined in local.c */
393 extern char * log_directory; /* defined in local.c */
394 extern char * aux_directory; /* defined in local.c */
395 extern char * fmt_directory; /* defined in local.c */
396 extern char * pdf_directory; /* defined in local.c */
398 /* At least check for I/O error (such as disk full) when closing */
399 /* Would be better to check while writing - but this is better than nothing */
400 /* This is used for both input and output files, but never mind ... */
402 /* now a_close returns -1 on error --- which could be used by caller */
403 /* probably want to ignore on input files ... */
405 void perrormod (char *s); /* in local.c */
407 // check_fclose not used by anything
408 /* 1993/Nov/20 - bkph */
409 int check_fclose (FILE * f)
412 return 0; // sanity check
414 if (ferror(f) || fclose (f))
416 perrormod("\n! I/O Error");
423 /* open_output moved down here to avoid potential pragma problem */
425 bool open_output (FILE **f, char *fopen_mode)
427 unsigned temp_length;
429 name_of_file[name_length + 1] = '\0'; /* null terminate */
431 if (pseudo_tilde != 0 || pseudo_space != 0)
433 retwiddle(name_of_file + 1);
437 /* 8 + 3 file names on Windows NT 95/Feb/20 */
438 if (shorten_file_name)
440 check_short_name(name_of_file + 1);
446 if (prepend_path_if (name_of_file + 1, name_of_file + 1, ".dvi", (unsigned char *) dvi_directory) ||
447 prepend_path_if (name_of_file + 1, name_of_file + 1, ".log", (unsigned char *) log_directory) ||
448 prepend_path_if (name_of_file + 1, name_of_file + 1, ".aux", (unsigned char *) aux_directory) ||
449 prepend_path_if (name_of_file + 1, name_of_file + 1, ".fmt", (unsigned char *) fmt_directory) ||
450 prepend_path_if (name_of_file + 1, name_of_file + 1, ".pdf", (unsigned char *) pdf_directory))
454 sprintf(log_line, "After prepend %s\n", name_of_file+1);
455 show_line(log_line, 0);
462 sprintf(log_line, " Open `%s' for output ", name_of_file + 1);
463 show_line(log_line, 0);
466 /* Is the filename openable as given? */
468 /* if share_flag is non-zero and we are opening for reading use fsopen */
469 /* but we can assume this is opening here for *output* */
470 *f = fopen((char *) name_of_file + 1, fopen_mode);
472 /* Can't open as given. Try the envvar. */
475 string temp_dir = get_env_shroud ("UFYNGPVU");
477 /* if (deslash) unixify(temp_dir); */ /* deslashify 93/Dec/28 */
479 if (temp_dir != NULL)
481 #ifdef BUILDNAMEDIRECT
482 unsigned char temp_name[PATH_MAX];
483 xconcat3((char *) temp_name, temp_dir, PATH_SEP_STRING, (char *) name_of_file + 1);
485 /* string temp_name = concat3 (temp_dir, "/", name_of_file + 1); */
486 string temp_name = concat3 (temp_dir, PATH_SEP_STRING, name_of_file + 1);
489 unixify((char *) temp_name); /* deslashify 93/Dec/28 */
490 /* If share_flag is non-zero and we are opening for reading use fsopen */
491 /* but we can assume this is opening here for *output* */
492 *f = fopen((char*)temp_name, fopen_mode);
493 /* If this succeeded, change name_of_file accordingly. */
495 strcpy((char*) name_of_file + 1, (char *) temp_name);
496 #ifndef BUILDNAMEDIRECT
502 if (strstr((char *) name_of_file + 1, ".dvi") != NULL)
504 if (qualified(name_of_file + 1))
508 (void) _getcwd(log_line, sizeof(log_line));
509 strcat(log_line, PATH_SEP_STRING);
512 strcat(log_line, (char*) name_of_file + 1);
514 dvi_file_name = xstrdup(log_line);
516 else if (strstr((char *)name_of_file + 1, ".log") != NULL)
518 if (qualified(name_of_file + 1))
522 (void) _getcwd(log_line, sizeof(log_line));
523 strcat(log_line, PATH_SEP_STRING);
526 strcat(log_line, (char *) name_of_file + 1);
528 log_file_name = xstrdup(log_line);
531 temp_length = strlen ((char *)name_of_file + 1);
532 name_of_file[temp_length+1] = ' ';
535 name_length = temp_length;