PipeStream.st
changeset 1 a27a279701f8
child 2 6526dde5f3ac
equal deleted inserted replaced
0:aa2498ef6470 1:a27a279701f8
       
     1 "
       
     2  COPYRIGHT (c) 1989-93 by Claus Gittinger
       
     3               All Rights Reserved
       
     4 
       
     5  This software is furnished under a license and may be used
       
     6  only in accordance with the terms of that license and with the
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 
       
    13 NonPositionableExternalStream subclass:#PipeStream
       
    14        instanceVariableNames:''
       
    15        classVariableNames:''
       
    16        poolDictionaries:''
       
    17        category:'Streams-External'
       
    18 !
       
    19 
       
    20 PipeStream comment:'
       
    21 
       
    22 COPYRIGHT (c) 1989-93 by Claus Gittinger
       
    23               All Rights Reserved
       
    24 
       
    25 %W% %E%
       
    26 '!
       
    27 
       
    28 %{
       
    29 #include <stdio.h>
       
    30 #ifndef transputer
       
    31 # include <sys/types.h>
       
    32 # include <sys/stat.h>
       
    33 #endif
       
    34 %}
       
    35 
       
    36 !PipeStream class methodsFor:'instance creation'!
       
    37 
       
    38 writingTo:command
       
    39     "create and return a new pipeStream which can write to the unix command
       
    40      given by command."
       
    41 
       
    42     ^ (self basicNew) writingTo:command
       
    43 
       
    44     "PipeStream writingTo:'sort'"
       
    45 !
       
    46 
       
    47 readingFrom:command
       
    48     "create and return a new pipeStream which can read from the unix command
       
    49      given by command."
       
    50 
       
    51     ^ (self basicNew) readingFrom:command
       
    52 
       
    53     "PipeStream readingFrom:'ls'"
       
    54 ! !
       
    55 
       
    56 !PipeStream methodsFor:'instance release'!
       
    57 
       
    58 closeFile
       
    59     "low level close - redefined since we close a pipe here"
       
    60 
       
    61 %{  /* NOCONTEXT */
       
    62 #ifndef transputer
       
    63     pclose(MKFD(_INST(filePointer)));
       
    64 #endif
       
    65 %}
       
    66 ! !
       
    67 
       
    68 !PipeStream methodsFor:'private'!
       
    69 
       
    70 openPipeFor:commandString withMode:mode
       
    71     "open a pipe to commandString; mode may be 'r' or 'w'"
       
    72 
       
    73     |retVal|
       
    74 
       
    75     unBuffered := true.
       
    76 %{
       
    77 #ifndef transputer
       
    78     {
       
    79         FILE *f;
       
    80         extern OBJ ErrorNumber;
       
    81         extern errno;
       
    82 
       
    83         if (_isString(commandString) && _isString(mode)) {
       
    84             f = (FILE *)popen((char *) _stringVal(commandString),
       
    85 			      (char *) _stringVal(mode));
       
    86             if (f == NULL) {
       
    87                 ErrorNumber = _MKSMALLINT(errno);
       
    88             } else {
       
    89                 _INST(filePointer) = MKOBJ(f);
       
    90                 retVal = self;
       
    91             }
       
    92         }
       
    93     }
       
    94 #endif
       
    95 %}
       
    96 .
       
    97     retVal notNil ifTrue:[
       
    98         lobby register:self
       
    99     ].
       
   100     ^ retVal
       
   101 !
       
   102 
       
   103 readingFrom:command
       
   104     "setup the receiver to read from command"
       
   105 
       
   106     self readonly.
       
   107     ^ self openPipeFor:command withMode:'r'
       
   108 !
       
   109 
       
   110 writingTo:command
       
   111     "setup the receiver to write to command"
       
   112 
       
   113     self writeonly.
       
   114     ^ self openPipeFor:command withMode:'w'
       
   115 ! !
       
   116 
       
   117 !PipeStream methodsFor:'redefined basic'!
       
   118 
       
   119 size
       
   120     "redefined since pipes have no size"
       
   121 
       
   122     ^ self shouldNotImplement
       
   123 !
       
   124 
       
   125 position:newpos
       
   126     "redefined since pipes cannot be positioned"
       
   127 
       
   128     ^ self shouldNotImplement
       
   129 !
       
   130 
       
   131 position
       
   132     "redefined since pipes have no position"
       
   133 
       
   134     ^ self shouldNotImplement
       
   135 ! !