home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / web2c / lib / openinout.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  246 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 <kpathsea/c-namemx.h>
  6. #include <kpathsea/c-pathch.h>
  7. #include <kpathsea/tex-make.h>
  8.  
  9. #ifdef BibTeX
  10. /* See comments in bibtex.ch for why we need these.  */
  11. FILE *standardinput /* = stdin */;
  12. FILE *standardoutput /* = stdout */;
  13.  
  14. /* Because we don't generate a .h file with all the global definitions
  15.    for BibTeX, as we do with TeX and Metafont, we must declare these
  16.    variables.  */
  17. extern char nameoffile[];
  18. extern integer namelength;
  19.  
  20. #else /* not BibTeX */
  21.  
  22. #define EXTERN extern /* Don't instantiate data here.  */
  23.  
  24. #ifdef TeX
  25. #include "texd.h"
  26. #else /* Metafont */
  27. #include "mfd.h"
  28. #endif
  29.  
  30. #ifdef FUNNY_CORE_DUMP
  31. /* This is defined in texmf.c.  */
  32. extern void funny_core_dump ();
  33. #endif /* FUNNY_CORE_DUMP */
  34.  
  35. #endif /* not BibTeX */
  36.  
  37. /* Open an input file F, using the path PATHSPEC and passing
  38.    FOPEN_MODE to fopen.  The filename is in `nameoffile', as a Pascal
  39.    string. We return whether or not the open succeeded.  If it did, we
  40.    also set `namelength' to the length of the full pathname that we
  41.    opened.  */
  42.  
  43. boolean
  44. open_input (f, path_index, fopen_mode)
  45.     FILE **f;
  46.     path_constant_type path_index;
  47.     char *fopen_mode;
  48. {
  49.   boolean openable = false;
  50.  
  51. #if defined (FUNNY_CORE_DUMP) && !defined (BibTeX)
  52.   /* This only applies if a preloaded TeX/Metafont is being made;
  53.      it allows automatic creation of the core dump (typing ^\ loses
  54.      since it requires manual intervention).  */
  55.   if ((path_index == TEXINPUTPATH || path_index == MFINPUTPATH)
  56.       && strncmp (nameoffile+1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
  57.     funny_core_dump ();
  58. #endif /* FUNNY_CORE_DUMP and not BibTeX */
  59.  
  60. #ifdef BibTeX
  61.   if (path_index == NO_FILE_PATH)
  62.     {
  63.       unsigned temp_length;
  64.  
  65.       null_terminate (nameoffile + 1);
  66.       *f = fopen (nameoffile + 1, fopen_mode);
  67.       temp_length = strlen (nameoffile + 1);
  68.       space_terminate (nameoffile + 1);
  69.       if (*f != NULL)
  70.         {
  71.           namelength = temp_length;
  72.           openable = true;
  73.         }
  74.     }
  75.  
  76.   else
  77. #endif /* BibTeX */
  78.   
  79.   if (testreadaccess (nameoffile, path_index))
  80.     {
  81.       /* We can assume `nameoffile' is openable, since
  82.          `testreadaccess' just returned true.  */
  83.       *f = xfopen_pas (nameoffile, fopen_mode);
  84.       
  85.       /* If we found the file in the current directory, don't leave the
  86.          `./' at the beginning of `nameoffile', since it looks dumb when
  87.          TeX says `(./foo.tex ... )', and analogously for Metafont.  */
  88.       if (nameoffile[1] == '.' && IS_DIR_SEP (nameoffile[2]))
  89.         {
  90.           unsigned i = 1;
  91.           while (nameoffile[i + 2] != ' ')
  92.             {
  93.               nameoffile[i] = nameoffile[i + 2];
  94.               i++;
  95.             }
  96.           nameoffile[i] = ' ';
  97.           namelength = i - 1;
  98.         }
  99.       else
  100.         namelength = strchr (nameoffile + 1, ' ') - nameoffile - 1;
  101.       
  102. #ifdef TeX
  103.       /* If we just opened a TFM file, we have to read the first byte,
  104.          since TeX wants to look at it.  What a kludge.  */
  105.       if (path_index == TFMFILEPATH)
  106.         { /* See comments in ctex.ch for why we need this.  */
  107.           extern integer tfmtemp;
  108.           tfmtemp = getc (*f);
  109.         }
  110. #endif /* TeX */  
  111.  
  112.       openable = true;
  113.     }
  114.  
  115.   return openable;
  116. }
  117.  
  118.  
  119. /* Call the external program for FORMAT, passing it `nameoffile'.  We'd
  120.    like to pass back the filename the script returns, but I'm not yet
  121.    sure how to do that in Pascal.  */
  122.  
  123. static boolean
  124. make_tex_file (format)
  125.     kpse_file_format_type format;
  126. {
  127.   static boolean kpathsea_dpi_set = 0;
  128.   static boolean maketex_base_dpi_set = 0;
  129.  
  130.   string found;
  131.   /* Since & is a no-op when applied to an array, we must put the
  132.      address of the filename in a variable.  */
  133.   string name = nameoffile;
  134.   
  135.   if (!kpathsea_dpi_set)
  136.     {
  137.       if (!getenv ("KPATHSEA_DPI"))
  138.     xputenv_int ("KPATHSEA_DPI", 300);
  139.       kpathsea_dpi_set = 1;
  140.     }
  141.   if (!maketex_base_dpi_set)
  142.     {
  143.       if (!getenv ("MAKETEX_BASE_DPI"))
  144.     xputenv_int ("MAKETEX_BASE_DPI", 300);
  145.       maketex_base_dpi_set = 1;
  146.     }
  147.  
  148.   make_c_string (&name);
  149.   found = kpse_make_tex (format, name);
  150.   make_pascal_string (&name);
  151.  
  152.   return found != NULL;
  153. }
  154.  
  155.  
  156. /* These are called by TeX or MF if an input or TFM file can't be opened.  */
  157.  
  158. boolean
  159. maketextex ()
  160. {
  161.   return make_tex_file (kpse_tex_format);
  162. }
  163.  
  164. boolean
  165. maketexmf ()
  166. {
  167.   return make_tex_file (kpse_mf_format);
  168. }
  169.  
  170. boolean
  171. maketextfm ()
  172. {
  173.   return make_tex_file (kpse_tfm_format);
  174. }
  175.  
  176. /* Open an output file F either in the current directory or in
  177.    $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
  178.    (Actually, this applies to the BibTeX output files, also, but
  179.    `TEXMFBIBOUTPUT' was just too long.)  The filename is in the global
  180.    `nameoffile', as a Pascal string.  We return whether or not the open
  181.    succeeded.  If it did, the global `namelength' is set to the length
  182.    of the actual filename.  */
  183.  
  184. boolean
  185. open_output (f, fopen_mode)
  186.     FILE **f;
  187.     char *fopen_mode;
  188. {
  189.   unsigned temp_length;
  190.  
  191.   /* Make the filename into a C string.  */
  192.   null_terminate (nameoffile + 1);
  193.   
  194.   /* Is the filename openable as given?  */
  195.   *f = fopen (nameoffile + 1, fopen_mode);
  196.  
  197.   if (*f == NULL)
  198.     { /* Can't open as given.  Try the envvar.  */
  199.       string temp_dir = getenv ("TEXMFOUTPUT");
  200.  
  201.       if (temp_dir != NULL)
  202.         {
  203.           string temp_name = concat3 (temp_dir, "/", nameoffile + 1);
  204.           *f = fopen (temp_name, fopen_mode);
  205.           
  206.           /* If this succeeded, change nameoffile accordingly.  */
  207.           if (*f)
  208.             strcpy (nameoffile + 1, temp_name);
  209.           
  210.           free (temp_name);
  211.         }
  212.     }
  213.  
  214.   /* Back into a Pascal string, but first get its length.  */
  215.   temp_length = strlen (nameoffile + 1);
  216.   space_terminate (nameoffile + 1);
  217.  
  218.   /* Only set `namelength' if we succeeded.  I'm not sure why.  */
  219.   if (*f)
  220.     namelength = temp_length;
  221.   
  222.   return *f != NULL;
  223. }
  224.  
  225. /* Test if the Pascal string BASE concatenated with the extension
  226.    `.SUFFIX' is the same file as just BASE.  SUFFIX is a C string.  */
  227.  
  228. boolean
  229. extensionirrelevantp (base, suffix)
  230.     char *base;
  231.     char *suffix;
  232. {
  233.   boolean ret;
  234.   char temp[PATH_MAX];
  235.   
  236.   make_c_string (&base);
  237.   strcpy (temp, base);
  238.   strcat (temp, ".");
  239.   strcat (temp, suffix);
  240.   
  241.   ret = same_file_p (base, temp);
  242.   make_pascal_string (&base);
  243.   
  244.   return ret;
  245. }
  246.