home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ober1096.zip / libsrc / uxhandles.obe < prev    next >
Text File  |  1995-04-04  |  3KB  |  95 lines

  1. (*===============================================================*)
  2. (*            Library Module for Gardens Point Oberon            *) 
  3. (*        Interface to Gardens Point Modula Library Module       *)
  4. (*===============================================================*)
  5.  
  6. FOREIGN MODULE UxHandles;
  7.  
  8. IS IN "uxhandles.o";
  9.  
  10. IMPORT GPMRTS, UxFiles;
  11.  
  12. (* 
  13.  *  This module complements UxFiles by giving stream access to
  14.  *  handles including standard handles --- stdIn, stdOut, stdErr
  15.  *
  16.  *  BEWARE that streams attached to standard handles stdOut and
  17.  *  stdErr may require a FlushStream or CloseStream call to
  18.  *  force output to appear on interactive terminals
  19.  *
  20.  *  TYPE UxFiles.OpenMode = (ReadOnly, WriteOnly, ReadWrite);
  21.  *)
  22.  
  23. TYPE    
  24.   FileDesc* = INTEGER(* [-1 .. 32767]*);
  25.  
  26. CONST    
  27.   error*  = -1;
  28.   stdIn*  = 0;
  29.   stdOut* = 1;
  30.   stdErr* = 2;
  31.  
  32. PROCEDURE OpenFileHandle*(path : ARRAY OF CHAR;
  33.                           mode : UxFiles.OpenMode) : FileDesc;
  34. (*
  35.  *  precondition  : path must be a constant string, or be nul
  36.  *            terminated if a variable array of chars.
  37.  *  postcondition : returns error if cannot be opened, else
  38.  *            the opened file handle.
  39.  *)
  40.  
  41. PROCEDURE CloseHandle*(handle : FileDesc; 
  42.                        VAR ok : BOOLEAN);
  43. (*
  44.  *  the file descriptor "handle is closed
  45.  *)
  46.  
  47. PROCEDURE StreamOfHandle*(fd : FileDesc) : UxFiles.File;
  48. PROCEDURE CloseStream*(stream : UxFiles.File; VAR ok : BOOLEAN);
  49. PROCEDURE FlushStream*(stream : UxFiles.File; VAR ok : BOOLEAN);
  50. (*
  51.  *  These procedures open, close and flush a stream on the 
  52.  *  nominated file handle, allowing UxFiles procs to be used.
  53.  *  CloseStream is identical to UxFiles.Close
  54.  *  FlushStream is often needed to force output
  55.  * 
  56.  *  Example -- to place a stream on a standard handle
  57.  *
  58.  *    stdInFile := StreamOfHandle(stdIn);
  59.  *)
  60.  
  61. PROCEDURE RedirectHandle*(old : FileDesc;    (* overwritten *)
  62.               new : FileDesc;    (* replacement *)
  63.               VAR save : FileDesc;    (* copy of old *)
  64.               VAR done : BOOLEAN);
  65. (*
  66.  *  precondition  : old and new are open file descriptors.
  67.  *  postcondition : input/output sent to "old" will go to the
  68.  *            stream previously associated with "new"
  69.  *            The descriptor "new" is closed. The handle
  70.  *            previously "old" is duplicated in "save"
  71.  *)
  72.  
  73. PROCEDURE RestoreHandle*(old : FileDesc;    (* overwritten *)
  74.              saved : FileDesc;    (* replacement *)
  75.              VAR done : BOOLEAN);
  76. (*
  77.  *  precondition  : old and new are open file descriptors.
  78.  *  postcondition : input/output sent to "old" will go to the
  79.  *            stream previously associated with "saved"
  80.  *            The descriptor "saved" is closed, and the
  81.  *            previous handle "old" is closed also
  82.  *
  83.  *  Example of typical use -- redirecting standard error
  84.  *
  85.  *    newFD := UxFiles.Open("foo.err", UxFiles.WriteOnly);
  86.  *      -- and check that newFD is not error ...
  87.  *    UxHandles.RedirectHandle(UxHandles.stdErr,newFD,errSav,ok);
  88.  *    (* closes newFD, and does 2> foo.err *)
  89.  *      -- now fork, exec or whatever ...
  90.  *    UxHandles.RestoreHandle(UxHandles.stdErr,errSav,ok);
  91.  *    (* stderr restored, foo.err is closed *)
  92.  *)
  93.  
  94. END UxHandles.
  95.