home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / modu1096.zip / GPMsym / pipeutilities.def < prev    next >
Text File  |  1996-08-29  |  3KB  |  82 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 PipeUtilities;
  15.  
  16.   IMPORT IMPLEMENTATION FROM "pipeutilities.o";
  17.  
  18.   FROM UxFiles IMPORT File;
  19.   FROM Types IMPORT Int32;
  20.  
  21.   CONST input  = 0; output = 1; stderr = 2;
  22.  
  23.   TYPE  FDPair = ARRAY [input .. output] OF Int32;
  24.  
  25.   PROCEDURE GetPipe(VAR pipeFDs : FDPair);
  26.   (* Opens a pair of file descriptors with the pipe ends in them *)
  27.  
  28.   PROCEDURE ClosePipe(pipeFD : FDPair);
  29.   (* Closes both descriptors of the given pipe  *)
  30.  
  31. (*
  32.  *  The next procedures connect an input or output stream (UxFiles.File) 
  33.  *  to one end of the pipe, the unused end is closed. Use UxFiles 
  34.  *  operations on the stream, and call UxFiles.Close(stream) to finish.
  35.  *)
  36.  
  37.   PROCEDURE FileInFromPipe(    pipeFD : FDPair;
  38.                VAR stream : File;
  39.                VAR done   : BOOLEAN);
  40.  
  41.   PROCEDURE FileOutToPipe (    pipeFD : FDPair;
  42.                VAR stream : File;
  43.                VAR done   : BOOLEAN);
  44.  
  45. (*
  46.  *  The next procedures attach the standard input or output streams to 
  47.  *  the opened pipe. Terminal, InOut, and StdError may then be used.
  48.  *)
  49.  
  50.   PROCEDURE InputFrom(pipe : FDPair);
  51.   (* Dups the nominated pipe to standard input  *)
  52.  
  53.   PROCEDURE OutputTo(pipe : FDPair);
  54.   (* Dups the nominated pipe to standard output *)
  55.  
  56.   PROCEDURE ErrorOutTo(pipe : FDPair);
  57.   (* Dups the nominated pipe to standard error  *)
  58.  
  59.   (* typical usage : 
  60.    *
  61.    *    FROM PipeUtilities IMPORT 
  62.    *        FDPair, GetPipe, ClosePipe, InputFrom, OutputTo;
  63.    *    FROM UxProcesses IMPORT Fork, Execp, Wait;
  64.    *    FROM BuildArgs IMPORT Arg2;
  65.    *      VAR  pip1 : FDPair;
  66.    *      ...
  67.    *      GetPipe(pip1);
  68.    *      IF Fork() = 0 THEN (* in child 1 here *)
  69.    *        OutputTo(pip1);  (* copy pipe desc  *)
  70.    *        ClosePipe(pip1); (* close original  *)
  71.    *        Execp("cat",Arg2("cat","file.mod"));
  72.    *      ELSIF Fork() = 0 THEN (* child 2 here *)
  73.    *        InputFrom(pip1); (* copy pipe desc  *)
  74.    *        ClosePipe(pip1); (* close original  *)
  75.    *        Execp("grep",Arg2("grep","foo"));
  76.    *      ELSE (* in parent process here *)
  77.    *        ClosePipe(pip1); (* so end of child 2 will wake child 1 *)
  78.    *        id := Wait(res); id := Wait(res); (* wait for both *)
  79.    *      END;
  80.    *)
  81. END PipeUtilities.
  82.