home *** CD-ROM | disk | FTP | other *** search
- /* riscos_ex.c: RISC OS extras
-
- Copyright (C) 1992 Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- This file contains functions that are lacking in Acorn ANSI C v4
- or that are too RISC OS-specific to be simply changed in the relevant
- .c or .h files
-
- All functions are prefixed by `riscos_' to avoid function name clashes
-
- The author is Mark J. Sinke, reachable at
- marks@stack.urc.tue.nl
- or
- Mark J. Sinke
- Mendelssohnstraat 5
- 5144 GD WAALWIJK
- The Netherlands */
-
- #define RISCOS_EX_C_FILE
-
- #include "config.h"
-
- #include <kernel.h>
-
- char *current_path = "@";
-
- /*--------------------------------------------------------------------------
- riscos_absolute(filename): return true if file name is absolute
- -------------------------------------------------------------------------- */
- /* I think the following is sufficient; should look up PRMs */
- boolean
- riscos_absolute(char *filename)
- {
- return (strpbrk(filename, ":$&\\@^") != NULL);
- }
-
- /* --------------------------------------------------------------------
- riscossetname(name): set `current path' to current dir
- -------------------------------------------------------------------- */
- void riscossetname(char *name)
- {
- char *pos;
- char *work;
-
- null_terminate(name + 1);
- work = xstrdup(name + 1);
- if (riscos_absolute(work)) {
- pos = strrchr(work, '.');
- if (pos) {
- *pos = '\0';
- pos = strrchr(work, '.');
- if (pos) {
- *pos = '\0';
- current_path = work;
- }
- }
- }
- else free(work);
- #ifdef RISCOS_DEBUG
- fprintf(stderr, "\nPath set to \"%s\" (from \"%s\")\n", current_path, name + 1);
- #endif
- space_terminate(name + 1);
- }
-
- /* --------------------------------------------------------------------
- riscos_isdir(filename): return true if filename is a directory
- -------------------------------------------------------------------- */
-
- /* OS_File 17: Read catalogue information for a named object
- (without path searching)
- Return 0 if not found, 1 if file, 2 if directory */
-
- boolean
- riscos_isdir(char *filename)
- {
- _kernel_osfile_block inout;
- int last = strlen(filename) - 1;
- char end = filename[last];
- int result;
-
- if (end == '.') filename[last] = '\0';
-
- result = (_kernel_osfile(17, filename, &inout) == 2);
-
- filename[last] = end;
- return result;
- }
-
- /* --------------------------------------------------------------------
- riscos_fopen(filename, mode): open a file for output, possibly
- creating a directory and dealing with '@'
- This function replaces all fopen()'s in TeX and friends!
- -------------------------------------------------------------------- */
-
- /* OS_File 8: Create directory
- r4 ( = `start' field) contains nr. of entries (0 for default)
- On return r0 == 8 if all went ok */
-
- static boolean cdir(char *filename) /* create a directory for the given file name */
- {
- _kernel_osfile_block inout;
- char *sep;
- int result;
-
- sep = strrchr(filename, '.'); /* find extension */
- if (!sep) return false;
- *sep = '\0'; /* terminate directory name */
- inout.start = 0;
- result = _kernel_osfile(8, filename, &inout);
- *sep = '.'; /* restore name */
- return (result == 8);
- }
-
- /* ------------------------------------------------------------------------
- riscos_settypenamed(name,type): set type of named file
- ------------------------------------------------------------------------ */
-
- /* OS_File 18: Settype
- r2 (`load') = file type
- PRM p. 849
- */
-
- static void
- riscos_settypenamed(char *nameoffile, int filetype)
- {
- _kernel_osfile_block block;
-
- block.load = filetype;
- _kernel_osfile(18, nameoffile, &block);
- }
-
- int riscostype=0xfff; /* Default to `text' output */
-
- FILE *riscos_fopen(char *filename, char *mode)
- {
- FILE *f;
- char *newname;
-
- #ifdef RISCOS_DEBUG
- fprintf(stderr, "\nriscos_fopen(\"%s\", \"%s\"); current_path = \"%s\"\n", filename, mode, current_path);
- #endif
- if (*filename == '@') newname = concat(current_path, filename + 1);
- else if (riscos_absolute(filename)) newname = filename;
- else newname = concat3(current_path, ".", filename);
- if (strcmp(mode, "w") == 0) { /* Write: set type, try cdir */
- f = fopen(newname, mode);
- if (!f && cdir(newname)) f = fopen(newname, mode);
- if (f) {
- #ifdef RISCOS_DEBUG
- fprintf(stderr, "file \"%s\" opened, will set type to 0x%x\n", newname, riscostype);
- #endif
- fclose(f);
- riscos_settypenamed(newname, riscostype);
- f = fopen(newname, "w+");
- riscostype = 0xfff; /* revert to 'text' again */
- }
- } else { /* read: Try without .sty */
- f = fopen(newname, mode);
- if (!f) {
- char *pos;
- pos = strrchr(newname, '.');
- if (pos && strcmp(pos + 1, "sty") == 0) {
- *pos = '\0';
- #ifdef RISCOS_DEBUG
- fprintf(stderr, "... trying without .sty (\"%s\")\n", newname);
- #endif
- f = fopen(newname, mode);
- *pos = '.';
- }
- }
- }
- if (newname != filename) free(newname);
- return f;
- }
-
-