home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / tex / texsrc1 / Src / lib / c / riscos_ex < prev    next >
Encoding:
Text File  |  1993-05-21  |  5.5 KB  |  190 lines

  1. /* riscos_ex.c: RISC OS extras
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  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, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. This file contains functions that are lacking in Acorn ANSI C v4
  20. or that are too RISC OS-specific to be simply changed in the relevant
  21. .c or .h files
  22.  
  23. All functions are prefixed by `riscos_' to avoid function name clashes
  24.  
  25. The author is Mark J. Sinke, reachable at 
  26.   marks@stack.urc.tue.nl
  27. or
  28.   Mark J. Sinke
  29.   Mendelssohnstraat 5
  30.   5144 GD WAALWIJK
  31.   The Netherlands */
  32.  
  33. #define RISCOS_EX_C_FILE
  34.  
  35. #include "config.h"
  36.  
  37. #include <kernel.h>
  38.  
  39. char *current_path = "@";
  40.  
  41. /*--------------------------------------------------------------------------
  42.   riscos_absolute(filename): return true if file name is absolute 
  43.   -------------------------------------------------------------------------- */
  44. /* I think the following is sufficient; should look up PRMs */
  45. boolean
  46. riscos_absolute(char *filename)
  47. {            
  48.   return (strpbrk(filename, ":$&\\@^") != NULL);
  49. }
  50.  
  51. /* --------------------------------------------------------------------
  52.    riscossetname(name): set `current path' to current dir 
  53.    -------------------------------------------------------------------- */
  54. void riscossetname(char *name)
  55. {
  56.   char *pos;
  57.   char *work;
  58.  
  59.   null_terminate(name + 1);
  60.   work = xstrdup(name + 1);
  61.   if (riscos_absolute(work)) {
  62.     pos = strrchr(work, '.');
  63.     if (pos) {
  64.       *pos = '\0';
  65.       pos = strrchr(work, '.');
  66.       if (pos) {
  67.         *pos = '\0';
  68.         current_path = work;
  69.       }
  70.     }
  71.   }  
  72.   else free(work);
  73. #ifdef RISCOS_DEBUG
  74.   fprintf(stderr, "\nPath set to \"%s\" (from \"%s\")\n", current_path, name + 1);
  75. #endif
  76.   space_terminate(name + 1);
  77. }
  78.   
  79. /* --------------------------------------------------------------------
  80.    riscos_isdir(filename): return true if filename is a directory
  81.    -------------------------------------------------------------------- */
  82.  
  83. /* OS_File 17: Read catalogue information for a named object
  84.                (without path searching)
  85.                Return 0 if not found, 1 if file, 2 if directory */
  86.  
  87. boolean
  88. riscos_isdir(char *filename)
  89. {
  90.   _kernel_osfile_block inout;
  91.   int last = strlen(filename) - 1;
  92.   char end = filename[last];
  93.   int result;
  94.  
  95.   if (end == '.') filename[last] = '\0';
  96.  
  97.   result = (_kernel_osfile(17, filename, &inout) == 2);
  98.  
  99.   filename[last] = end;
  100.   return result;
  101. }
  102.  
  103. /* --------------------------------------------------------------------
  104.    riscos_fopen(filename, mode): open a file for output, possibly
  105.                                  creating a directory and dealing with '@'
  106.    This function replaces all fopen()'s in TeX and friends!
  107.    -------------------------------------------------------------------- */
  108.  
  109. /* OS_File 8: Create directory
  110.               r4 ( = `start' field) contains nr. of entries (0 for default)
  111.               On return r0 == 8 if all went ok */
  112.  
  113. static boolean cdir(char *filename) /* create a directory for the given file name */
  114. {
  115.   _kernel_osfile_block inout;
  116.   char *sep;
  117.   int result;
  118.  
  119.   sep = strrchr(filename, '.'); /* find extension */
  120.   if (!sep) return false;
  121.   *sep = '\0'; /* terminate directory name */
  122.   inout.start = 0;
  123.   result = _kernel_osfile(8, filename, &inout);
  124.   *sep = '.'; /* restore name */
  125.   return (result == 8);
  126. }
  127.  
  128. /* ------------------------------------------------------------------------
  129.    riscos_settypenamed(name,type): set type of named file
  130.    ------------------------------------------------------------------------ */
  131.  
  132. /* OS_File 18: Settype
  133.                r2 (`load') = file type
  134.    PRM p. 849
  135. */
  136.  
  137. static void
  138. riscos_settypenamed(char *nameoffile, int filetype)
  139. {
  140.   _kernel_osfile_block block;
  141.  
  142.   block.load = filetype;
  143.   _kernel_osfile(18, nameoffile, &block);
  144. }
  145.  
  146. int riscostype=0xfff; /* Default to `text' output */
  147.  
  148. FILE *riscos_fopen(char *filename, char *mode)
  149. {
  150.   FILE *f;
  151.   char *newname;
  152.  
  153. #ifdef RISCOS_DEBUG
  154.   fprintf(stderr, "\nriscos_fopen(\"%s\", \"%s\"); current_path = \"%s\"\n", filename, mode, current_path);
  155. #endif
  156.   if (*filename == '@') newname = concat(current_path, filename + 1);
  157.   else if (riscos_absolute(filename)) newname = filename;
  158.   else newname = concat3(current_path, ".", filename);
  159.   if (strcmp(mode, "w") == 0) { /* Write: set type, try cdir */
  160.     f = fopen(newname, mode);
  161.     if (!f && cdir(newname)) f = fopen(newname, mode);
  162.     if (f) {
  163. #ifdef RISCOS_DEBUG
  164.       fprintf(stderr, "file \"%s\" opened, will set type to 0x%x\n", newname, riscostype);
  165. #endif
  166.       fclose(f);
  167.       riscos_settypenamed(newname, riscostype);
  168.       f = fopen(newname, "w+");
  169.       riscostype = 0xfff; /* revert to 'text' again */
  170.     }
  171.   } else { /* read: Try without .sty */
  172.     f = fopen(newname, mode);
  173.     if (!f) {
  174.       char *pos;
  175.       pos = strrchr(newname, '.');
  176.       if (pos && strcmp(pos + 1, "sty") == 0) {
  177.         *pos = '\0';
  178. #ifdef RISCOS_DEBUG
  179.         fprintf(stderr, "... trying without .sty (\"%s\")\n", newname);
  180. #endif
  181.         f = fopen(newname, mode);
  182.         *pos = '.';
  183.       }
  184.     }
  185.   }  
  186.   if (newname != filename) free(newname);
  187.   return f;
  188. }
  189.  
  190.