home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / tex / texsrc1 / Src / lib / c / openinout < prev    next >
Text File  |  1993-05-07  |  7KB  |  267 lines

  1. /* openinout.c: open input and output files.  These routines used by
  2.    TeX, Metafont, and BibTeX.  */
  3.  
  4. #include "config.h"
  5. #include "c-namemx.h"
  6. #include "c-pathch.h"
  7.  
  8. #ifdef RISCOS
  9. #include "riscos_ex.h"
  10. #endif
  11.  
  12. #ifdef BibTeX
  13. /* See comments in bibtex.ch for why we need these.  */
  14. FILE *standardinput = stdin;
  15. FILE *standardoutput = stdout;
  16.  
  17. /* Because we don't generate a .h file with all the global definitions
  18.    for BibTeX, as we do with TeX and Metafont, we must declare these
  19.    variables.  */
  20. extern char nameoffile[];
  21. extern integer namelength;
  22.  
  23. #else /* not BibTeX */
  24.  
  25. #define EXTERN extern /* Don't instantiate data here.  */
  26.  
  27. #ifdef TeX
  28. #include "texd.h"
  29. #else /* Metafont */
  30. #include "mfd.h"
  31. #endif
  32.  
  33. #ifdef FUNNY_CORE_DUMP
  34. /* This is defined in ./texmf.c.  */
  35. extern void funny_core_dump ();
  36. #endif /* FUNNY_CORE_DUMP */
  37.  
  38. #endif /* not BibTeX */
  39.  
  40. /* Open an input file F, using the path PATHSPEC and passing
  41.    FOPEN_MODE to fopen.  The filename is in `nameoffile', as a Pascal
  42.    string. We return whether or not the open succeeded.  If it did, we
  43.    also set `namelength' to the length of the full pathname that we
  44.    opened.  */
  45.  
  46. boolean
  47. open_input (f, path_index, fopen_mode)
  48.   FILE **f;
  49.   path_constant_type path_index;
  50.   char *fopen_mode;
  51. {
  52.   boolean openable = false;
  53.  
  54. #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
  55.   /* This only applies if a preloaded TeX (or Metafont) is being made;
  56.      it allows for automatic creation of the core dump (typing ^\
  57.      requires manual intervention).  */
  58.   if (path_index == TEXINPUTPATH
  59.       && strncmp (nameoffile+1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
  60.     funny_core_dump ();
  61. #endif /* FUNNY_CORE_DUMP and not BibTeX */
  62.  
  63. #ifdef BibTeX
  64.   if (path_index == NO_FILE_PATH)
  65.     {
  66.       unsigned temp_length;
  67.  
  68.       null_terminate (nameoffile + 1);
  69.       *f = fopen (nameoffile + 1, fopen_mode);
  70.       temp_length = strlen (nameoffile + 1);
  71.       space_terminate (nameoffile + 1);
  72.       if (*f != NULL)
  73.         {
  74.           namelength = temp_length;
  75.           openable = true;
  76.         }
  77.     }
  78.  
  79.   else
  80. #endif /* BibTeX */
  81.   
  82.   if (testreadaccess (nameoffile, path_index))
  83.     {
  84.       /* We can assume `nameoffile' is openable, since
  85.          `testreadaccess' just returned true.  */
  86.       *f = xfopen_pas (nameoffile, fopen_mode);
  87.       
  88.       /* If we found the file in the current directory, don't leave the
  89.          `./' at the beginning of `nameoffile', since it looks dumb when
  90.          TeX says `(./foo.tex ... )', and analogously for Metafont.  */
  91. #ifdef RISCOS
  92.       if (nameoffile[1] == '@' && nameoffile[2] == PATH_SEP)
  93. #else
  94.       if (nameoffile[1] == '.' && nameoffile[2] == PATH_SEP)
  95. #endif
  96.         {
  97.           unsigned i = 1;
  98.           while (nameoffile[i + 2] != ' ')
  99.             {
  100.               nameoffile[i] = nameoffile[i + 2];
  101.               i++;
  102.             }
  103.           nameoffile[i] = ' ';
  104.           namelength = i - 1;
  105.         }
  106.       else
  107.         namelength = strchr (nameoffile + 1, ' ') - nameoffile - 1;
  108.       
  109. #ifdef TeX
  110.       /* If we just opened a TFM file, we have to read the first byte,
  111.          since TeX wants to look at it.  What a kludge.  */
  112.       if (path_index == TFMFILEPATH)
  113.         { /* See comments in ctex.ch for why we need this.  */
  114.           extern integer tfmtemp;
  115.           tfmtemp = getc (*f);
  116.         }
  117. #endif /* TeX */  
  118.  
  119.       openable = true;
  120.     }
  121.  
  122.   return openable;
  123. }
  124.  
  125.  
  126. /* Call the external program PROGRAM, passing it `nameoffile'.  */
  127.  
  128. static boolean
  129. make_tex_file (program)
  130.     string program;
  131. {
  132. #ifdef NO_MAKETEX
  133.   return 0;
  134. #else
  135.   char cmd[NAME_MAX + 1 + PATH_MAX + 1];
  136.   unsigned cmd_len;
  137.   int ret;
  138.   unsigned i = 1; /* For copying from `nameoffile'.  */
  139.  
  140.   /* Wrap another sh around the invocation of the MakeTeX program, so we
  141.      can avoid `sh: MakeTeXTFM: not found' errors confusing the user.
  142.      We don't use fork/exec ourselves, since we'd have to call sh anyway
  143.      to interpret the script.  */
  144.   strcpy (cmd, "sh -c ");
  145.   
  146.   strcat (cmd, program);
  147.   cmd_len = strlen (cmd);
  148.   cmd[cmd_len++] = ' ';
  149.  
  150.   while (nameoffile[i] != ' ')
  151.     cmd[cmd_len++] = nameoffile[i++];
  152.  
  153.   /* Add terminating null.  */
  154.   cmd[cmd_len] = 0;
  155.  
  156.   /* Don't show any output.  */
  157.   strcat (cmd, ">/dev/null 2>&1");
  158.  
  159.   /* Run the command, and return whether or not it succeeded.  */
  160.   ret = system (cmd);
  161.   return ret == EXIT_SUCCESS_CODE;
  162. #endif /* not NO_MAKE_TEX */
  163. }
  164.  
  165.  
  166. /* This is called by TeX if an \input resp. TFM file can't be opened.  */
  167.  
  168. boolean
  169. maketextex ()
  170. {
  171.   return make_tex_file ("MakeTeXTeX");
  172. }
  173.  
  174. boolean
  175. maketextfm ()
  176. {
  177.   return make_tex_file ("MakeTeXTFM");
  178. }
  179.  
  180. boolean
  181. maketexmf ()
  182. {
  183.   return make_tex_file ("MakeTeXMF");
  184. }
  185.  
  186. /* Open an output file F either in the current directory or in
  187.    $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
  188.    (Actually, this applies to the BibTeX output files, also, but
  189.    `TEXMFBIBOUTPUT' was just too long.)  The filename is in the global
  190.    `nameoffile', as a Pascal string.  We return whether or not the open
  191.    succeeded.  If it did, the global `namelength' is set to the length
  192.    of the actual filename.
  193.    For RISC OS the name of this variable is changed to TeXMFOutput$Dir
  194.    to have a consistent name  */
  195.  
  196. boolean
  197. open_output (f, fopen_mode)
  198.   FILE **f;
  199.   char *fopen_mode;
  200. {
  201.   unsigned temp_length;
  202.  
  203.   /* Make the filename into a C string.  */
  204.   null_terminate (nameoffile + 1);
  205.   
  206.   /* Is the filename openable as given?  */
  207.   *f = riscos_fopen(nameoffile + 1, fopen_mode);
  208.  
  209.   if (*f == NULL)
  210.     { /* Can't open as given.  Try the envvar.  */
  211. #ifdef RISCOS
  212.       string temp_dir = getenv ("TeXMFOutput$Dir");
  213. #else
  214.       string temp_dir = getenv ("TEXMFOUTPUT");
  215. #endif
  216.  
  217.       if (temp_dir != NULL)
  218.         {
  219.           /* 29-Apr-93: Changed literal "/" to PATH_SEP_STRING (marks) */
  220.           string temp_name = concat3 (temp_dir, PATH_SEP_STRING, nameoffile + 1);
  221.           *f = fopen (temp_name, fopen_mode);
  222.           
  223.           /* If this succeeded, change nameoffile accordingly.  */
  224.           if (*f)
  225.             strcpy (nameoffile + 1, temp_name);
  226.           
  227.           free (temp_name);
  228.         }
  229.     }
  230.  
  231.   /* Back into a Pascal string, but first get its length.  */
  232.   temp_length = strlen (nameoffile + 1);
  233.   space_terminate (nameoffile + 1);
  234.  
  235.   /* Only set `namelength' if we succeeded.  I'm not sure why.  */
  236.   if (*f)
  237.     namelength = temp_length;
  238.   
  239.   return *f != NULL;
  240. }
  241.  
  242. /* Test if the Pascal string BASE concatenated with the extension
  243.    `.SUFFIX' is the same file as just BASE.  SUFFIX is a C string.  */
  244.  
  245. boolean
  246. extensionirrelevantp (base, suffix)
  247.   char *base;
  248.   char *suffix;
  249. {
  250. #ifdef RISCOS /* can't happen using the chosen scheme */
  251.   return false;
  252. #else
  253.   boolean ret;
  254.   char temp[PATH_MAX];
  255.   
  256.   make_c_string (&base);
  257.   strcpy (temp, base);
  258.   strcat (temp, ".");
  259.   strcat (temp, suffix);
  260.   
  261.   ret = same_file_p (base, temp);
  262.   make_pascal_string (&base);
  263.   
  264.   return ret;
  265. #endif
  266. }
  267.