home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / texmf / source / TeX / common / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  6.6 KB  |  269 lines

  1. /* fileio.c: routines used by TeX, Metafont, and BibTeX.  */
  2.  
  3. #ifndef BibTeX
  4. #define EXTERN extern    /* Don't instantiate date here.  */
  5. #ifdef TeX
  6. #include "texd.h"
  7. #else
  8. #include "mfd.h"
  9. #endif
  10. #endif /* not BibTeX */
  11.  
  12. #include "fileio.h"    /* "../common/fileio.h" */
  13.  
  14. #ifdef AMIGA
  15. # undef message
  16. # include <exec/types.h>
  17. # include <workbench/icon.h>
  18. # include <workbench/workbench.h>
  19. # include <clib/exec_protos.h>
  20. # include <clib/icon_protos.h>
  21. # include <pragmas/exec_pragmas.h>
  22. # include <pragmas/icon_pragmas.h>
  23.   extern int        create_info_file;    /* definiert in init.c */
  24.   extern char        info_file_name[];
  25.   extern struct Library *SysBase;
  26.   extern struct Library *IconBase;
  27. #endif
  28.  
  29.  
  30. /* This is defined in ./texmf.c.  */
  31. extern void funny_core_dump ();
  32.  
  33. #ifdef TeX
  34. /* See comments in ctex.ch for why we need this.  */
  35. #if 0    /* (br) not necessary */
  36. extern integer tfmtemp;
  37. #endif
  38. #endif
  39.  
  40. extern long crlfflag;        /* write \n as \r\n in textfiles ? */
  41.  
  42. #ifdef BibTeX
  43. /* See comments in bibtex.ch for why we need these.  */
  44. FILE *standardinput = stdin;
  45. FILE *standardoutput = stdout;
  46.  
  47. /* Because we don't generate a .h file with all the global definitions
  48.    for BibTeX, as we do with TeX and Metafont, we must declare all these
  49.    variables.  */
  50. extern char nameoffile[];
  51. extern integer namelength;
  52. #endif
  53.  
  54.  
  55.  
  56. /* This constant is used in BibTeX, when opening the top-level .aux file.  */
  57. #define NO_FILE_PATH -1
  58.  
  59. /* Open an input file F, using the path PATHSPEC (the values are defined
  60.    in ./extra.h).  The filename is in `nameoffile', as a Pascal string.
  61.    We return whether or not the open succeeded.  If it did, we also set
  62.    `namelength' to the length of the full pathname that we opened.  */
  63.  
  64. boolean
  65. open_input (f, path_index)
  66.   FILE **f;
  67.   int path_index;
  68. {
  69. #ifdef BSD
  70. #ifndef BibTeX
  71.   /* This only applies if a preloaded TeX (or Metafont) is being made;
  72.      it allows for automatic creation of the core dump (typing ^\
  73.      requires manual intervention).  */
  74.   if (path_index == TEXINPUTPATH
  75.       && strncmp (nameoffile+1, "HackyInputFileNameForCoreDump.tex", 33) == 0)
  76.     funny_core_dump ();
  77. #endif
  78. #endif /* BSD */
  79.  
  80. #ifdef BibTeX
  81.   if (path_index == NO_FILE_PATH)
  82.     {
  83.       unsigned temp_length;
  84.  
  85.       /* We can't use `make_c_string' or `make_pascal_string', since
  86.          `nameoffile' is an array, not a pointer.  */
  87.       end_with_null (nameoffile + 1);
  88. #ifdef atarist
  89.       replace_slash(nameoffile + 1);
  90. #endif
  91.       *f = fopen (nameoffile + 1, "r");
  92. #ifdef atarist
  93.       replace_backslash(nameoffile + 1);
  94. #endif
  95.       temp_length = strlen (nameoffile + 1);
  96.       end_with_space (nameoffile + 1);
  97.       if (*f != NULL)
  98.         {
  99.           namelength = temp_length;
  100.           return true;
  101.         }
  102.       else
  103.         return false;
  104.     }
  105. #endif
  106.  
  107.   /* end_with_null (nameoffile + 1); */ /* is done in testreadaccess */
  108.  
  109.   if ( (*f = testreadaccess (nameoffile + 1 , path_index)) != NULL )
  110.     {
  111. #if 0
  112.       *f = checked_fopen (nameoffile, "r");
  113. #else
  114.       end_with_null (nameoffile + 1);
  115. #endif
  116.  
  117.       
  118.       /* If we found the file in the current directory, don't leave the
  119.          `./' at the beginning of `nameoffile', since it looks dumb when
  120.          TeX says `(./foo.tex ... )', and analogously for Metafont.  */
  121.  
  122.       if (nameoffile[1] == '.' && nameoffile[2] == '/') {
  123. #if 1
  124.     char *name = &nameoffile[1];
  125.  
  126.     while( 1 ) {
  127.       *name = *(name + 2);
  128.       if( *name == '\0' )
  129.         break;
  130.       name++;
  131.     }
  132. #else
  133.         unsigned i;
  134.         for (i = 1; nameoffile[i]; i++)
  135.           nameoffile[i] = nameoffile[i+2];
  136. #endif
  137.       }
  138.  
  139.       namelength = strlen (nameoffile + 1);
  140.  
  141. #if 0
  142. printf("DEBUG: name >%s<  len %d\n", nameoffile+1, namelength);
  143. #endif
  144.  
  145.       /* `nameoffile' is in an anomalous state: it still begins with a
  146.          space, but now it is terminated with a null.  */
  147.       end_with_space (nameoffile + 1);
  148.       
  149. #ifdef TeX
  150.       /* If we just opened a TFM file, we have to read the first byte,
  151.          since TeX wants to look at it.  */
  152. #if 0    /* (br) not necessary for new read_font_info() */
  153.       if (path_index == TFMFILEPATH)
  154.         tfmtemp = getc (*f);
  155. #endif
  156. #endif
  157.  
  158.       return true;
  159.     }
  160.   else {
  161.     /* end_with_space (nameoffile + 1); */
  162.     return false;
  163.   }
  164. }
  165.  
  166.  
  167. /* Open an output file F either in the current directory or in
  168.    $TEXMFOUTPUT/F, if the environment variable `TEXMFOUTPUT' exists.
  169.    (Actually, this applies to the BibTeX output files, also, but
  170.    `TEXMFBIBOUTPUT' was just too long.)  The filename is in the global
  171.    `nameoffile', as a Pascal string.  We return whether or not the open
  172.    succeeded.  If it did, the global `namelength' is set to the length
  173.    of the actual filename.  */
  174.  
  175. boolean
  176. open_output (f
  177. #ifdef atarist
  178.         , txtflag
  179. #endif
  180.               )
  181.   FILE **f;
  182. #ifdef atarist
  183.   int txtflag;        /* output for textfile ?? */
  184. #endif
  185. {
  186.   unsigned temp_length;
  187.  
  188.   /* We can't use `checked_fopen' here, since that routine aborts if the
  189.      file can't be opened.  We also can't use `make_c_string' or
  190.      `make_pascal_string', since `nameoffile' is an array, not a
  191.      pointer.  */
  192.   end_with_null (nameoffile + 1);
  193. #ifdef atarist
  194.   replace_slash(nameoffile + 1);
  195. #endif
  196.  
  197.   *f = fopen (nameoffile + 1,
  198. #ifdef atarist
  199.                 ((txtflag && crlfflag) ? "wt" : "wb")
  200. #else
  201.                 "w"
  202. #endif
  203.                     );
  204.   
  205.   if (*f == NULL)
  206.     {
  207.       /* Can't open in the current directory.  Try the directory
  208.          specified by the environment variable.  */
  209.       char *temp_dir = getenv ("TEXMFOUTPUT");
  210.  
  211.       if (temp_dir != NULL && chdir (temp_dir) == 0)
  212.         *f = fopen (nameoffile + 1,
  213. #ifdef atarist
  214.                 ((txtflag && crlfflag) ? "wt" : "wb")
  215. #else
  216.                 "w"
  217. #endif
  218.                     );
  219.     }
  220.  
  221. #ifdef AMIGA
  222.     if (*f != NULL) {
  223.       struct DiskObject *DiskO;
  224.       char outname[150];
  225.       char *ptr = strrchr(nameoffile+1, '.');
  226.       
  227.       if (create_info_file && ptr != NULL && !strcmp(ptr, ".dvi")) {
  228.     /* Test, ob ein DVI File geoeffnet wird... */
  229.         
  230.         strcpy(outname, nameoffile + 1);
  231.         strcat(outname, ".info");
  232.         
  233.         if (access(outname, 4)) {    /* not read: info-File darf nicht schon existieren */
  234.       if ((ptr = strrchr(&(info_file_name[0]), '.')) != NULL && stricmp(ptr, ".info") == 0) {
  235.         *ptr = '\0';    /* weg mit dem ".info" */
  236.       }
  237.  
  238.       if ((IconBase = OpenLibrary(ICONNAME, 0)) != NULL) {
  239.         DiskO = GetDiskObject(info_file_name);
  240.         if (DiskO != NULL) {
  241.            (void)PutDiskObject(nameoffile+1, DiskO);    /* entweder es geht oder nicht..egal */
  242.            FreeDiskObject(DiskO);
  243.         }
  244.         CloseLibrary(IconBase);
  245.         IconBase = 0;
  246.       }
  247.     }
  248.       }
  249.     }
  250. #endif
  251.  
  252.   temp_length = strlen (nameoffile + 1);
  253. #ifdef atarist
  254.   replace_backslash(nameoffile + 1);
  255. #endif
  256.   end_with_space (nameoffile + 1);
  257.  
  258.   if (*f != NULL)
  259.     {
  260.       namelength = temp_length;
  261.       return true;
  262.     }
  263.   
  264.   else
  265.     return false;
  266. }
  267.  
  268. /* -- eof -- */
  269.