PipeStream.st
author Stefan Vogel <sv@exept.de>
Fri, 28 Oct 2005 13:50:57 +0200
changeset 8969 978613904d0b
parent 8968 879991b52630
child 8976 2ff451956d48
permissions -rw-r--r--
For writing Pipestreams: close write side of filedescriptor, when the command terminated.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 99
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
5388
bef4d52749a4 terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents: 5243
diff changeset
    13
"{ Package: 'stx:libbasic' }"
bef4d52749a4 terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents: 5243
diff changeset
    14
269
93162487a94b *** empty log message ***
claus
parents: 255
diff changeset
    15
NonPositionableExternalStream subclass:#PipeStream
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
    16
	instanceVariableNames:'commandString pid exitStatus exitSema exitAction'
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    17
	classVariableNames:'BrokenPipeSignal'
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    18
	poolDictionaries:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    19
	category:'Streams-External'
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    20
!
2
claus
parents: 1
diff changeset
    21
217
a0400fdbc933 *** empty log message ***
claus
parents: 180
diff changeset
    22
!PipeStream primitiveDefinitions!
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    23
%{
437
claus
parents: 410
diff changeset
    24
4616
64dd3a9bebf5 *** empty log message ***
ps
parents: 4526
diff changeset
    25
#if defined(WIN32)
793
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    26
# undef UNIX_LIKE
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    27
# define MSDOS_LIKE
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    28
#endif
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    29
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    30
#include <stdio.h>
437
claus
parents: 410
diff changeset
    31
#define _STDIO_H_INCLUDED_
claus
parents: 410
diff changeset
    32
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    33
#include <errno.h>
437
claus
parents: 410
diff changeset
    34
#define _ERRNO_H_INCLUDED_
claus
parents: 410
diff changeset
    35
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    36
#ifndef transputer
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    37
# include <sys/types.h>
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    38
# include <sys/stat.h>
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    39
#endif
230
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    40
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    41
/*
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    42
 * on some systems errno is a macro ... check for it here
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    43
 */
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    44
#ifndef errno
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    45
 extern errno;
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    46
#endif
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    47
1663
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    48
#ifdef LINUX
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    49
# define BUGGY_STDIO_LIB
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    50
#endif
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    51
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    52
%}
180
c488255bd0be *** empty log message ***
claus
parents: 159
diff changeset
    53
! !
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    54
325
claus
parents: 308
diff changeset
    55
!PipeStream primitiveFunctions!
claus
parents: 308
diff changeset
    56
%{
claus
parents: 308
diff changeset
    57
claus
parents: 308
diff changeset
    58
/*
2925
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    59
 * no longer needed - popen is useless ...
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    60
 */
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    61
#undef NEED_POPEN_WITH_VFORK
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    62
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    63
/*
325
claus
parents: 308
diff changeset
    64
 * some systems (i.e. ultrix) use fork;
claus
parents: 308
diff changeset
    65
 * were better off with a popen based on vfork ...
claus
parents: 308
diff changeset
    66
 */
claus
parents: 308
diff changeset
    67
#ifdef NEED_POPEN_WITH_VFORK
claus
parents: 308
diff changeset
    68
claus
parents: 308
diff changeset
    69
static int popen_pid = 0;
claus
parents: 308
diff changeset
    70
claus
parents: 308
diff changeset
    71
FILE *
claus
parents: 308
diff changeset
    72
popen(command, type)
claus
parents: 308
diff changeset
    73
/* const */ char *command;
claus
parents: 308
diff changeset
    74
/* const */ char *type;
claus
parents: 308
diff changeset
    75
{
claus
parents: 308
diff changeset
    76
    int pipes[2];
claus
parents: 308
diff changeset
    77
    int itype = (strcmp(type, "w") == 0 ? 1 : 0);
claus
parents: 308
diff changeset
    78
claus
parents: 308
diff changeset
    79
    if (pipe(pipes) == -1)
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    80
	return NULL;
325
claus
parents: 308
diff changeset
    81
claus
parents: 308
diff changeset
    82
    switch (popen_pid = vfork()) {
claus
parents: 308
diff changeset
    83
    case -1:
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    84
	(void)close(pipes[0]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    85
	(void)close(pipes[1]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    86
	return NULL;
325
claus
parents: 308
diff changeset
    87
claus
parents: 308
diff changeset
    88
    case 0:
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    89
	if (itype) {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    90
	    dup2(pipes[0], fileno(stdin));
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    91
	    close(pipes[1]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    92
	} else {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    93
	    dup2(pipes[1], fileno(stdout));
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    94
	    close(pipes[0]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    95
	}
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    96
	execl("/bin/sh", "/bin/sh", "-c", command, 0);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    97
	fprintf(stderr, "XRN Error: failed the execlp\n");
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    98
	_exit(-1);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    99
	/* NOTREACHED */
325
claus
parents: 308
diff changeset
   100
claus
parents: 308
diff changeset
   101
    default:
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   102
	    if (itype) {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   103
		close(pipes[0]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   104
		return fdopen(pipes[1], "w");
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   105
	    } else {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   106
		close(pipes[1]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   107
		return fdopen(pipes[0], "r");
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   108
	    }
325
claus
parents: 308
diff changeset
   109
    }
claus
parents: 308
diff changeset
   110
}
claus
parents: 308
diff changeset
   111
claus
parents: 308
diff changeset
   112
int
claus
parents: 308
diff changeset
   113
pclose(str)
claus
parents: 308
diff changeset
   114
FILE *str;
claus
parents: 308
diff changeset
   115
{
claus
parents: 308
diff changeset
   116
    int pd = 0;
claus
parents: 308
diff changeset
   117
    int status;
claus
parents: 308
diff changeset
   118
    int err;
claus
parents: 308
diff changeset
   119
claus
parents: 308
diff changeset
   120
    err = fclose(str);
claus
parents: 308
diff changeset
   121
claus
parents: 308
diff changeset
   122
    do {
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   123
	if ((pd = wait(&status)) == -1)
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   124
	{
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   125
		err = EOF;
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   126
		break;
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   127
	}
325
claus
parents: 308
diff changeset
   128
    } while (pd !=  popen_pid);
claus
parents: 308
diff changeset
   129
claus
parents: 308
diff changeset
   130
    if (err == EOF)
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   131
	return  -1;
325
claus
parents: 308
diff changeset
   132
claus
parents: 308
diff changeset
   133
    if (status)
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   134
	status >>= 8;   /* exit status in high byte */
325
claus
parents: 308
diff changeset
   135
claus
parents: 308
diff changeset
   136
    return status;
claus
parents: 308
diff changeset
   137
}
claus
parents: 308
diff changeset
   138
2925
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
   139
#endif /* NEED_POPEN_WITH_VFORK */
325
claus
parents: 308
diff changeset
   140
claus
parents: 308
diff changeset
   141
%}
claus
parents: 308
diff changeset
   142
! !
claus
parents: 308
diff changeset
   143
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   144
!PipeStream class methodsFor:'documentation'!
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   145
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   146
copyright
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   147
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   148
 COPYRIGHT (c) 1989 by Claus Gittinger
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   149
	      All Rights Reserved
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   150
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   151
 This software is furnished under a license and may be used
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   152
 only in accordance with the terms of that license and with the
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   153
 inclusion of the above copyright notice.   This software may not
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   154
 be provided or otherwise made available to, or used by, any
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   155
 other person.  No title to or ownership of the software is
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   156
 hereby transferred.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   157
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   158
!
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   159
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   160
documentation
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   161
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   162
    Pipestreams allow reading or writing from/to a unix command.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   163
    For example, to get a stream reading the output of an 'ls -l'
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   164
    command, a PipeStream can be created with:
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   165
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   166
        PipeStream readingFrom:'ls -l'
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   167
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   168
    the characters of the commands output can be read using the
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   169
    standard stream messages as next, nextLine etc.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   170
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   171
    Example for writing to a command:
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   172
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   173
        PipeStream writingTo:'cat >/tmp/x'
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   174
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   175
    Bidirectional pipestreams (supporting both reading an writing) may be used for filters:
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   176
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   177
        PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/'''
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   178
1662
ce26ca3d837c linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents: 1648
diff changeset
   179
    Buffered pipes do not work with Linux - the stdio library seems to be
ce26ca3d837c linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents: 1648
diff changeset
   180
    buggy (trying to restart the read ...)
ce26ca3d837c linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents: 1648
diff changeset
   181
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   182
    [author:]
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   183
        Claus Gittinger
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   184
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   185
    [see also:]
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   186
        ExternalStream FileStream Socket
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   187
        OperatingSystem
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   188
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   189
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   190
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   191
!PipeStream class methodsFor:'initialization'!
2
claus
parents: 1
diff changeset
   192
claus
parents: 1
diff changeset
   193
initialize
claus
parents: 1
diff changeset
   194
    "setup the signal"
claus
parents: 1
diff changeset
   195
57
db9677479d35 *** empty log message ***
claus
parents: 49
diff changeset
   196
    BrokenPipeSignal isNil ifTrue:[
8320
229adaeaf183 Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents: 7663
diff changeset
   197
        BrokenPipeSignal := WriteError newSignalMayProceed:true.
229adaeaf183 Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents: 7663
diff changeset
   198
        BrokenPipeSignal nameClass:self message:#brokenPipeSignal.
229adaeaf183 Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents: 7663
diff changeset
   199
        BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
57
db9677479d35 *** empty log message ***
claus
parents: 49
diff changeset
   200
    ]
2
claus
parents: 1
diff changeset
   201
! !
claus
parents: 1
diff changeset
   202
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   203
!PipeStream class methodsFor:'instance creation'!
2
claus
parents: 1
diff changeset
   204
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   205
bidirectionalFor:commandString
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   206
    "create and return a new bidirectonal pipeStream which can both be written to 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   207
     and read from the unix command given by commandString. 
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   208
     The commands error output is send to my own error output."
2
claus
parents: 1
diff changeset
   209
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   210
    ^ self 
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   211
        bidirectionalFor:commandString 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   212
        errorDisposition:#stderr 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   213
        inDirectory:nil
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   214
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   215
    "
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   216
        |p|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   217
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   218
        p := PipeStream bidirectionalFor:'cat -u'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   219
        p nextPutAll:'Wer ist der Bürgermeister von Wesel'; cr.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   220
        Transcript showCR:p nextLine.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   221
        p close
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   222
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   223
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   224
    "
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   225
        |p|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   226
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   227
        p := PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/'''.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   228
        p nextPutAll:'Hello world'; cr.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   229
        p shutDownOutput.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   230
        Transcript showCR:p nextLine.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   231
        p close
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   232
    "
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   233
!
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   234
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   235
bidirectionalFor:commandString errorDisposition:errorDisposition inDirectory:aDirectory
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   236
    "create and return a new bidirectonal pipeStream which can both be written to 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   237
     and read from the unix command given by commandString. 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   238
     The directory will be changed to aDirectory while
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   239
     executing the command. Use this if a command is to be
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   240
     executed in another directory, to avoid any OS dependencies
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   241
     in your code.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   242
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   243
     errorDisposition may be one of #discard, #inline or #stderr (default).
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   244
     #discard causes stderr to be discarded (/dev/null), 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   245
     #inline causes it to be written to smalltalks own stdout and
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   246
     #stderr causes it to be written to smalltalks own stderr.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   247
     Nil is treated like #stderr"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   248
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   249
    ^ self basicNew
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   250
        openPipeFor:commandString 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   251
        withMode:#'r+' 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   252
        errorDisposition:errorDisposition 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   253
        inDirectory:aDirectory
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   254
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   255
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   256
readingFrom:commandString
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   257
    "create and return a new pipeStream which can read from the unix command
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   258
     given by commandString. 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   259
     The commands error output is send to my own error output."
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   260
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   261
    ^ self 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   262
        readingFrom:commandString 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   263
        errorDisposition:#stderr 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   264
        inDirectory:nil
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   265
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   266
    "unix:
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   267
        PipeStream readingFrom:'ls -l'.
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   268
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   269
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   270
    "
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   271
        |p|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   272
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   273
        p := PipeStream readingFrom:'ls -l'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   274
        Transcript showCR:p nextLine.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   275
        p close
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   276
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   277
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   278
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   279
        |s|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   280
        s := PipeStream readingFrom:'sh -c sleep\ 600'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   281
        (Delay forSeconds:2) wait.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   282
        s shutDown
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   283
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   284
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   285
    "vms:
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   286
        PipeStream readingFrom:'dir'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   287
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   288
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   289
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   290
        |p|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   291
        p := PipeStream readingFrom:'dir'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   292
        Transcript showCR:p nextLine.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   293
        p close
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   294
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   295
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   296
    "msdos:
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   297
        PipeStream readingFrom:'dir'.
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   298
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   299
    "
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   300
        |p|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   301
        p := PipeStream readingFrom:'dir'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   302
        Transcript showCR:p nextLine.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   303
        p close
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   304
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   305
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   306
    "Modified: 24.4.1996 / 09:09:25 / stefan"
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   307
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   308
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   309
readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   310
    "similar to #readingFrom, but changes the directory while
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   311
     executing the command. Use this if a command is to be
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   312
     executed in another directory, to avoid any OS dependencies
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   313
     in your code.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   314
     errorDisposition may be one of #discard, #inline or #stderr (default).
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   315
     #discard causes stderr to be discarded (/dev/null), 
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   316
     #inline causes it to be merged into the PipeStream and
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   317
     #stderr causes it to be written to smalltalks own stderr.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   318
     Nil is treated like #stderr"
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   319
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   320
    ^ self basicNew
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   321
        openPipeFor:commandString 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   322
        withMode:#r 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   323
        errorDisposition:errorDisposition 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   324
        inDirectory:aDirectory
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   325
!
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   326
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   327
readingFrom:commandString inDirectory:aDirectory
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   328
    "similar to #readingFrom, but changes the directory while
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   329
     executing the command. Use this if a command is to be
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   330
     executed in another directory, to avoid any OS dependencies
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   331
     in your code.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   332
     The commands error output is send to my own error output."
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   333
3563
16ccd4cefcbe *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3533
diff changeset
   334
     ^ self 
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   335
        readingFrom:commandString
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   336
        errorDisposition:#stderr 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   337
        inDirectory:aDirectory
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   338
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   339
    "        
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   340
        |p|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   341
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   342
        p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   343
        Transcript showCR:p upToEnd.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   344
        p close
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   345
   "
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   346
!
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   347
2
claus
parents: 1
diff changeset
   348
writingTo:commandString
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   349
    "create and return a new pipeStream which can write to the unix command
a27a279701f8 Initial revision
claus
parents:
diff changeset
   350
     given by command."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   351
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   352
    ^ self 
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   353
	writingTo:commandString errorDisposition:#stderr inDirectory:nil
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   354
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   355
    "unix:
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   356
	 PipeStream writingTo:'sort'
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   357
    "
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   358
!
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   359
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   360
writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   361
    "similar to #writingTo, but changes the directory while
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   362
     executing the command. Use this if a command is to be
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   363
     executed in another directory, to avoid any OS dependencies
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   364
     in your code.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   365
     errorDisposition may be one of #discard, #inline or #stderr (default).
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   366
     #discard causes stderr to be discarded (/dev/null), 
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   367
     #inline causes it to be written to smalltalks own stdout and
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   368
     #stderr causes it to be written to smalltalks own stderr.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   369
     Nil is treated like #stderr"
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   370
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   371
    ^ self basicNew
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   372
        openPipeFor:commandString 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   373
        withMode:#w 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   374
        errorDisposition:errorDisposition 
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   375
        inDirectory:aDirectory
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   376
!
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   377
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   378
writingTo:commandString inDirectory:aDirectory
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   379
    "create and return a new pipeStream which can write to the unix command
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   380
     given by commandString. The command is executed in the given directory."
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   381
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   382
    ^ self 
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   383
        writingTo:commandString errorDisposition:#stderr inDirectory:aDirectory
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   384
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   385
    "unix:
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   386
         PipeStream writingTo:'sort'
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   387
    "
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   388
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   389
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   390
!PipeStream class methodsFor:'Signal constants'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   391
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   392
brokenPipeSignal
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   393
    "return the signal used to handle SIGPIPE unix-signals.
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   394
     Since SIGPIPE is asynchronous, we can't decide which smalltalk process
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   395
     should handle BrokenPipeSignal. So the system doesn't raise 
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   396
     BrokenPipeSignal for SIGPIPE any longer."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   397
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   398
    ^ BrokenPipeSignal
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   399
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   400
    "Modified: 24.9.1997 / 09:43:23 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   401
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   402
99
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   403
!PipeStream methodsFor:'accessing'!
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   404
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   405
commandString
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   406
    "return the command string"
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   407
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   408
    ^ commandString
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   409
!
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   410
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   411
exitStatus
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   412
    "return exitStatus"
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   413
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   414
    ^ exitStatus
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   415
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   416
    "Created: 28.12.1995 / 14:54:41 / stefan"
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   417
!
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   418
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   419
pid
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   420
    "return pid"
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   421
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   422
    ^ pid
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   423
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   424
    "Created: 28.12.1995 / 14:54:30 / stefan"
99
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   425
! !
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   426
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   427
!PipeStream methodsFor:'closing'!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   428
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   429
shutDown
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   430
    "close the Stream and terminate the command"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   431
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   432
    Lobby unregister:self.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   433
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   434
    "terminate first under windows"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   435
    OperatingSystem isMSDOSlike ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   436
        self terminatePipeCommand.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   437
        self closeFileDescriptor.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   438
        ^ self.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   439
    ].
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   440
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   441
    "terminate last under unix"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   442
    self closeFileDescriptor.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   443
    self terminatePipeCommand.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   444
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   445
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   446
shutDownOutput
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   447
    "signal to the pipestream's command, that no more data will be sent"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   448
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   449
    OperatingSystem shutdownBidirectionalPipeOutput:self fileDescriptor
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   450
! !
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   451
5243
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   452
!PipeStream methodsFor:'finalization'!
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   453
6439
0f841258ec4a Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6267
diff changeset
   454
finalize
5243
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   455
    "redefined to avoid blocking in close."
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   456
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   457
    self shutDown
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   458
! !
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   459
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   460
!PipeStream protectedMethodsFor:'private'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   461
a27a279701f8 Initial revision
claus
parents:
diff changeset
   462
closeFile
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   463
    "low level close
230
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
   464
     This waits for the command to finish. 
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
   465
     Use shutDown for a fast (nonBlocking) close."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   466
7027
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   467
    filePointer notNil ifTrue:[
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   468
        super closeFile.
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   469
        OperatingSystem isMSDOSlike ifTrue:[
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   470
            self terminatePipeCommand.
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   471
        ].
3834
93ec07316a98 oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents: 3833
diff changeset
   472
7027
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   473
        filePointer := nil.
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   474
        pid notNil ifTrue:[
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   475
            "/ wait for the pipe-command to terminate.
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   476
            self waitForPipeCommandWithTimeout:nil.
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   477
        ].
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   478
    ].
3833
3ae90f8da01a terminate process BEFORE closing the pipe under win32
Claus Gittinger <cg@exept.de>
parents: 3791
diff changeset
   479
3834
93ec07316a98 oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents: 3833
diff changeset
   480
    "Modified: / 12.9.1998 / 16:51:04 / cg"
369
claus
parents: 362
diff changeset
   481
!
claus
parents: 362
diff changeset
   482
claus
parents: 362
diff changeset
   483
closeFileDescriptor
claus
parents: 362
diff changeset
   484
    "alternative very low level close 
claus
parents: 362
diff changeset
   485
     This closes the underlying OS-fileDescriptor 
claus
parents: 362
diff changeset
   486
     - and will NOT write any buffered data to the stream.
claus
parents: 362
diff changeset
   487
     You have been warned."
claus
parents: 362
diff changeset
   488
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   489
    |action|
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   490
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   491
%{  
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   492
#if !defined(transputer)
369
claus
parents: 362
diff changeset
   493
    OBJ fp;
claus
parents: 362
diff changeset
   494
    FILE *f;
4616
64dd3a9bebf5 *** empty log message ***
ps
parents: 4526
diff changeset
   495
    extern close();
6267
10016cc16511 pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents: 5880
diff changeset
   496
    int retVal;
369
claus
parents: 362
diff changeset
   497
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 793
diff changeset
   498
    if ((fp = __INST(filePointer)) != nil) {
6487
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   499
        __INST(filePointer) = nil;
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   500
        f = __FILEVal(fp);
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   501
        if (@global(FileOpenTrace) == true) {
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   502
            fprintf(stderr, "close [PipeStream] %x fd=%d\n", f, fileno(f));
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   503
        }
3744
e2c57149f3b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3563
diff changeset
   504
#ifdef WIN32
6487
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   505
        do {
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   506
            __threadErrno = 0;
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   507
            retVal = __STX_C_CALL1( "close", (void*)close, (void*)fileno(f) );
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   508
        } while ((retVal < 0) && (__threadErrno == EINTR));
3744
e2c57149f3b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3563
diff changeset
   509
#else
6487
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   510
        __BEGIN_INTERRUPTABLE__
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   511
        close(fileno(f));
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   512
        __END_INTERRUPTABLE__
3744
e2c57149f3b4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3563
diff changeset
   513
#endif
369
claus
parents: 362
diff changeset
   514
    }
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   515
#endif /* not transputer  */
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   516
%}.
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   517
    exitAction notNil ifTrue:[
6487
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   518
        action := exitAction.
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   519
        exitAction := nil.
78c18e4cb6ff @global stuff for classvars
Stefan Vogel <sv@exept.de>
parents: 6439
diff changeset
   520
        action value.
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   521
    ]
8520
fcf5cc177a63 Protect #closeFile.
Stefan Vogel <sv@exept.de>
parents: 8320
diff changeset
   522
! !
fcf5cc177a63 Protect #closeFile.
Stefan Vogel <sv@exept.de>
parents: 8320
diff changeset
   523
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   524
!PipeStream methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   525
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   526
exitAction:aBlock
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   527
    "define a block to be evaluated when the pipe is closed.
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   528
     This is only used with VMS, to remove any temporary COM file.
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   529
     (see readingFrom:inDirectory:)"
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   530
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   531
    exitAction := aBlock
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   532
!
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   533
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   534
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   535
    "open a pipe to the OS command in commandString; 
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   536
     rwMode may be 'r' or 'w' or 'r+'.
5481
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   537
     errorDisposition controls where the stdErr output should go,
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   538
     and may be one of #discard, #inline or #stderr (default).
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   539
     #discard causes stderr to be discarded (/dev/null), 
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   540
     #inline causes it to be written to smalltalks own stdout and
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   541
     #stderr causes it to be written to smalltalks own stderr.
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   542
     Nil is treated like #stderr"
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   543
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   544
    |blocked pipeFdArray execFdArray execFd myFd shellAndArgs
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   545
     shellPath shellArgs mbx mbxName 
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   546
     realCmd execDirectory tmpComFile nullOutput|
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   547
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   548
    filePointer notNil ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   549
        "the pipe was already open ...
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   550
         this should (can) not happen."
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   551
        ^ self errorAlreadyOpen
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   552
    ].
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   553
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   554
    rwMode = #r ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   555
        mode := #readonly. didWrite := false.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   556
    ] ifFalse:[rwMode = #'r+' ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   557
        mode := #readwrite. didWrite := true.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   558
    ] ifFalse:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   559
        mode := #writeonly. didWrite := true.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   560
    ]].
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   561
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   562
    lastErrorNumber := nil.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   563
    exitStatus := nil.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   564
    exitSema := Semaphore new name:'pipe exitSema'.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   565
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   566
    realCmd := aCommandString.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   567
    execDirectory := aDirectory.
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   568
    execFdArray := #(0 1 2) copy.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   569
3791
76ea6c85ca1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3773
diff changeset
   570
    OperatingSystem isVMSlike ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   571
        "/
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   572
        "/ the generated COM-file includes a 'set default'
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   573
        "/
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   574
        tmpComFile := OperatingSystem createCOMFileForVMSCommand:aCommandString in:aDirectory.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   575
        realCmd := '@' , tmpComFile osName.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   576
        execDirectory := nil.
4616
64dd3a9bebf5 *** empty log message ***
ps
parents: 4526
diff changeset
   577
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   578
        mbx := OperatingSystem createMailBox.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   579
        mbx isNil ifTrue:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   580
            lastErrorNumber := OperatingSystem currentErrorNumber.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   581
            tmpComFile delete.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   582
            ^ self openError
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   583
        ].
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   584
        mbxName := OperatingSystem mailBoxNameOf:mbx.
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   585
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   586
        "/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   587
        shellPath := ''.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   588
        shellArgs := realCmd.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   589
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   590
        rwMode = #r ifTrue:[
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   591
            "redirect stdout of subprocess to write to mailbox"
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   592
            execFdArray at:2 put:mbx.
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   593
        ] ifFalse:[
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   594
            "redirect stdin of subprocess to read from mailbox"
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   595
            execFdArray at:1 put:mbx.
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   596
        ].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   597
    ] ifFalse:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   598
        shellAndArgs := OperatingSystem commandAndArgsForOSCommand:realCmd.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   599
        shellPath := shellAndArgs at:1.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   600
        shellArgs := shellAndArgs at:2.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   601
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   602
        mode == #readwrite ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   603
            pipeFdArray := OperatingSystem makeBidirectionalPipe.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   604
            pipeFdArray isNil ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   605
                lastErrorNumber := OperatingSystem currentErrorNumber.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   606
                ^ self openError
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   607
            ].
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   608
            myFd := pipeFdArray at:1.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   609
            execFd := pipeFdArray at:2.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   610
            execFdArray at:1 put:execFd.
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   611
            execFdArray at:2 put:execFd.
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   612
        ] ifFalse:[
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   613
            pipeFdArray := OperatingSystem makePipe.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   614
            pipeFdArray isNil ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   615
                lastErrorNumber := OperatingSystem currentErrorNumber.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   616
                ^ self openError
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   617
            ].
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   618
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   619
            mode == #readonly ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   620
                "redirect stdout of subprocess to write to pipe"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   621
                myFd := pipeFdArray at:1.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   622
                execFd := pipeFdArray at:2.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   623
                execFdArray at:2 put:execFd.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   624
            ] ifFalse:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   625
                "redirect stdin of subprocess to read from pipe"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   626
                myFd := pipeFdArray at:2.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   627
                execFd := pipeFdArray at:1.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   628
                execFdArray at:1 put:execFd.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   629
            ].
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   630
        ].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   631
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   632
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   633
    errorDisposition == #discard ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   634
        nullOutput := Filename nullDevice writeStream.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   635
        execFdArray at:3 put:nullOutput fileDescriptor
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   636
    ] ifFalse:[(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   637
        execFdArray at:3 put:1
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   638
    ]].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   639
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   640
    "/ must block here, to avoid races due to early finishing
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   641
    "/ subprocesses ...
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   642
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   643
    blocked := OperatingSystem blockInterrupts.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   644
3563
16ccd4cefcbe *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3533
diff changeset
   645
    pid := Processor 
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   646
               monitor:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   647
                  OperatingSystem 
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   648
                      exec:shellPath
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   649
                      withArguments:shellArgs
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   650
                      environment:nil
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   651
                      fileDescriptors:execFdArray
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   652
                      fork:true
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   653
                      newPgrp:true
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   654
                      inDirectory:execDirectory.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   655
               ]
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   656
               action:[:status |
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   657
                  status stillAlive ifFalse:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   658
                      exitStatus := status.
8969
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   659
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   660
                      "writing doesn't make sense - there is no reader any longer"
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   661
                      mode == #readwrite ifTrue:[
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   662
                          "... but allow to read the rest of the command's output"
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   663
                          self shutDownOutput.
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   664
                      ] ifFalse:[mode == #writeonly ifTrue:[
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   665
                          self closeFileDescriptor.  
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   666
                      ]].
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   667
                    
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   668
                      OperatingSystem closePid:pid.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   669
                      pid := nil.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   670
                      exitSema signal.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   671
                  ].
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   672
               ].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   673
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   674
    "subprocess has been created.
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   675
     close unused filedescriptors"
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   676
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   677
    execFd notNil ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   678
        OperatingSystem closeFd:execFd.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   679
    ].
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   680
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   681
    nullOutput notNil ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   682
        nullOutput close
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   683
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   684
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   685
    pid notNil ifTrue:[
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   686
        "successfull creation of subprocesss"
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   687
        OperatingSystem isVMSlike ifTrue:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   688
            "/
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   689
            "/ reopen the mailbox as a file ...
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   690
            "/
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   691
            mbxName := OperatingSystem mailBoxNameOf:mbx.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   692
            mbxName notNil ifTrue:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   693
                super open:mbxName withMode:rwMode.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   694
                exitAction := [tmpComFile delete].
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   695
            ].
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   696
        ] ifFalse:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   697
            self setFileDescriptor:myFd mode:rwMode.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   698
        ]
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   699
    ] ifFalse:[
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   700
        "creation of subprocesss failed"
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   701
        lastErrorNumber := OperatingSystem currentErrorNumber.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   702
        OperatingSystem isVMSlike ifTrue:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   703
            OperatingSystem destroyMailBox:mbx.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   704
            tmpComFile delete.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   705
        ] ifFalse:[
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   706
            OperatingSystem closeFd:myFd.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   707
        ].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   708
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   709
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   710
    blocked ifFalse:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   711
        OperatingSystem unblockInterrupts
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   712
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   713
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   714
    lastErrorNumber notNil ifTrue:[
7060
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   715
        "
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   716
         the pipe open failed for some reason ...
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   717
         ... this may be either due to an invalid command string,
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   718
         or due to the system running out of memory (when forking
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   719
         the unix process)
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   720
        "
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   721
        exitAction value.
7e1eec6ab21e Call 'final' method #executeCommand:....
Stefan Vogel <sv@exept.de>
parents: 7027
diff changeset
   722
        ^ self openError
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   723
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   724
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   725
    commandString := realCmd.
5673
58ebbb50cd3a Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents: 5624
diff changeset
   726
58ebbb50cd3a Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents: 5624
diff changeset
   727
    "stdio lib does not work with blocking pipes and interrupts
58ebbb50cd3a Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents: 5624
diff changeset
   728
     for WIN, Linux, Solaris and probably any other UNIX"
58ebbb50cd3a Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents: 5624
diff changeset
   729
    buffered := false.
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 7079
diff changeset
   730
    position := ZeroPosition.
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   731
    hitEOF := false.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   732
    binary := false.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   733
    Lobby register:self.
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   734
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   735
    "Modified: / 23.4.1996 / 17:05:59 / stefan"
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   736
    "Modified: / 28.1.1998 / 14:47:34 / md"
6439
0f841258ec4a Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6267
diff changeset
   737
    "Created: / 19.5.1999 / 12:28:54 / cg"
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   738
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   739
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   740
terminatePipeCommand
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   741
    |tpid|
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   742
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   743
    (tpid := pid) notNil ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   744
        OperatingSystem terminateProcessGroup:tpid.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   745
        OperatingSystem terminateProcess:tpid.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   746
        OperatingSystem closePid:tpid.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   747
        pid := nil.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   748
    ].
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   749
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   750
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   751
waitForPipeCommandWithTimeout:seconds
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   752
    "wait for the pipe command to terminate itself.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   753
     Return true, if a timeout occurred."
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   754
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   755
    pid notNil ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   756
	[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   757
	    pid notNil ifTrue:[
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   758
		exitSema waitWithTimeout:seconds.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   759
	    ]
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   760
	] valueUninterruptably
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   761
    ].
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   762
    ^ pid notNil
6439
0f841258ec4a Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6267
diff changeset
   763
! !
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   764
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   765
!PipeStream class methodsFor:'documentation'!
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   766
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   767
version
8969
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   768
    ^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.101 2005-10-28 11:50:57 stefan Exp $'
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   769
! !
7027
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   770
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   771
PipeStream initialize!