home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / modu1096.zip / GPMsym / uxhandles.def < prev    next >
Text File  |  1996-08-29  |  4KB  |  99 lines

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