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