PipeStream.st
author Jan Vrany <jan.vrany@labware.com>
Wed, 22 Mar 2023 13:57:18 +0000
branchjv
changeset 25445 1623217d2268
parent 24086 9018daacf498
permissions -rw-r--r--
Cherry-picked OrderedCollection.st from 0b286fd51da7: * d4c86d7c0bfc: #TUNING by stefan, Stefan Vogel <sv@exept.de> * 692b6497a669: #DOCUMENTATION by stefan, Stefan Vogel <sv@exept.de> * d47bb2912953: #DOCUMENTATION by stefan, Stefan Vogel <sv@exept.de> * abb4316c6bff: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 3a8fce0e8d11: #TUNING by stefan, Stefan Vogel <sv@exept.de> * 03d29bf8c5bb: #REFACTORING by stefan, Stefan Vogel <sv@exept.de> * cccc6c4abcfc: #REFACTORING by stefan, Stefan Vogel <sv@exept.de> * 35d957c7a840: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 6b11890f5f2c: #OTHER by cg, Claus Gittinger <cg@exept.de> * abb6108fb06b: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 2c4768bb2e89: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 4029e964d0f1: #FEATURE by cg, Claus Gittinger <cg@exept.de> * ddcab3a9c2df: #OTHER by cg, Claus Gittinger <cg@exept.de> * 2213eb56e0c7: #REFACTORING by exept, Claus Gittinger <cg@exept.de> * 09ca874a6160: #REFACTORING by exept, Claus Gittinger <cg@exept.de> * 30b332af1f33: #BUGFIX by stefan, Stefan Vogel <sv@exept.de> * 779764ba117b: #REFACTORING by cg, Claus Gittinger <cg@exept.de> * b3d232a613c9: #BUGFIX by stefan, Stefan Vogel <sv@exept.de> * c417f7edaec1: #BUGFIX by stefan, Stefan Vogel <sv@exept.de> * 904b6538f379: #FEATURE by exept, Claus Gittinger <cg@exept.de> * c5887f03e01f: #REFACTORING by stefan, Stefan Vogel <sv@exept.de> * 8912d03aff48: #BUGFIX by exept, Claus Gittinger <cg@exept.de> * de5cd1dab4c3: #DOCUMENTATION by exept, Claus Gittinger <cg@exept.de> * 9bbd26603378: #OTHER by exept, Claus Gittinger <cg@exept.de> * c2c9dc110f42: #FEATURE by stefan, Stefan Vogel <sv@exept.de> * 81d123c6703d: #DOCUMENTATION by stefan, Stefan Vogel <sv@exept.de> * 8aadbb21458a: #BUGFIX by stefan, Stefan Vogel <sv@exept.de> * f210dbb8b2f6: #TUNING by stefan, Stefan Vogel <sv@exept.de> * c2c774fc53c0: #FEATURE by exept, Claus Gittinger <cg@exept.de> * b6f462670875: #DOCUMENTATION by exept, Claus Gittinger <cg@exept.de> * 27ae4021d5d6: #FEATURE by stefan, Stefan Vogel <sv@exept.de> * 10d9e9d85594: #TUNING by exept, Claus Gittinger <cg@exept.de> * 2653d855dcc7: #DOCUMENTATION by exept, Claus Gittinger <cg@exept.de> * 6ea1698a1a34: #FEATURE by stefan, Stefan Vogel <sv@exept.de> * 28762315e664: #OTHER by exept, Claus Gittinger <cg@exept.de> * 7142ea786f3e: #TUNING by stefan, Stefan Vogel <sv@exept.de> * 7875acb42b53: #BUGFIX by stefan, Stefan Vogel <sv@exept.de> * 163a0eebc97e: #BUGFIX by Maren, matilk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
     1
"{ Encoding: utf8 }"
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
     2
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     3
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     4
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 99
diff changeset
     5
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
"
5388
bef4d52749a4 terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents: 5243
diff changeset
    14
"{ Package: 'stx:libbasic' }"
bef4d52749a4 terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents: 5243
diff changeset
    15
17460
27b6fcc56477 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17077
diff changeset
    16
"{ NameSpace: Smalltalk }"
27b6fcc56477 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17077
diff changeset
    17
269
93162487a94b *** empty log message ***
claus
parents: 255
diff changeset
    18
NonPositionableExternalStream subclass:#PipeStream
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
    19
	instanceVariableNames:'commandString osProcess'
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    20
	classVariableNames:'BrokenPipeSignal'
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    21
	poolDictionaries:''
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
    22
	category:'Streams-External'
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    23
!
2
claus
parents: 1
diff changeset
    24
217
a0400fdbc933 *** empty log message ***
claus
parents: 180
diff changeset
    25
!PipeStream primitiveDefinitions!
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    26
%{
17077
8f4500d0df9e include os defs
Claus Gittinger <cg@exept.de>
parents: 16953
diff changeset
    27
#include "stxOSDefs.h"
437
claus
parents: 410
diff changeset
    28
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
    29
#if defined(__win32__)
793
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    30
# undef UNIX_LIKE
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    31
# define MSDOS_LIKE
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    32
#endif
3d441d66beeb NT changes
Claus Gittinger <cg@exept.de>
parents: 613
diff changeset
    33
20298
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    34
#ifndef _STDIO_H_INCLUDED_
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    35
# include <stdio.h>
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    36
# define _STDIO_H_INCLUDED_
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    37
#endif
437
claus
parents: 410
diff changeset
    38
20298
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    39
#ifndef _ERRNO_H_INCLUDED_
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    40
# include <errno.h>
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    41
# define _ERRNO_H_INCLUDED_
98dc8dbdd8cf include files
Claus Gittinger <cg@exept.de>
parents: 20099
diff changeset
    42
#endif
437
claus
parents: 410
diff changeset
    43
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    44
#ifndef transputer
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    45
# include <sys/types.h>
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    46
# include <sys/stat.h>
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    47
#endif
230
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    48
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    49
/*
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    50
 * on some systems errno is a macro ... check for it here
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    51
 */
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    52
#ifndef errno
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    53
 extern errno;
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    54
#endif
0300c6797890 interrupt & blocking close
claus
parents: 217
diff changeset
    55
1663
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    56
#ifdef LINUX
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    57
# define BUGGY_STDIO_LIB
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    58
#endif
dd111ec142da *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1662
diff changeset
    59
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    60
%}
180
c488255bd0be *** empty log message ***
claus
parents: 159
diff changeset
    61
! !
88
81dacba7a63a *** empty log message ***
claus
parents: 85
diff changeset
    62
325
claus
parents: 308
diff changeset
    63
!PipeStream primitiveFunctions!
claus
parents: 308
diff changeset
    64
%{
claus
parents: 308
diff changeset
    65
claus
parents: 308
diff changeset
    66
/*
2925
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    67
 * no longer needed - popen is useless ...
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    68
 */
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    69
#undef NEED_POPEN_WITH_VFORK
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    70
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
    71
/*
325
claus
parents: 308
diff changeset
    72
 * some systems (i.e. ultrix) use fork;
16953
4fb3033a379f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 16579
diff changeset
    73
 * we're better off with a popen based on vfork ...
325
claus
parents: 308
diff changeset
    74
 */
claus
parents: 308
diff changeset
    75
#ifdef NEED_POPEN_WITH_VFORK
claus
parents: 308
diff changeset
    76
claus
parents: 308
diff changeset
    77
static int popen_pid = 0;
claus
parents: 308
diff changeset
    78
claus
parents: 308
diff changeset
    79
FILE *
claus
parents: 308
diff changeset
    80
popen(command, type)
claus
parents: 308
diff changeset
    81
/* const */ char *command;
claus
parents: 308
diff changeset
    82
/* const */ char *type;
claus
parents: 308
diff changeset
    83
{
claus
parents: 308
diff changeset
    84
    int pipes[2];
claus
parents: 308
diff changeset
    85
    int itype = (strcmp(type, "w") == 0 ? 1 : 0);
claus
parents: 308
diff changeset
    86
claus
parents: 308
diff changeset
    87
    if (pipe(pipes) == -1)
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    88
	return NULL;
325
claus
parents: 308
diff changeset
    89
claus
parents: 308
diff changeset
    90
    switch (popen_pid = vfork()) {
claus
parents: 308
diff changeset
    91
    case -1:
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    92
	(void)close(pipes[0]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    93
	(void)close(pipes[1]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    94
	return NULL;
325
claus
parents: 308
diff changeset
    95
claus
parents: 308
diff changeset
    96
    case 0:
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    97
	if (itype) {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    98
	    dup2(pipes[0], fileno(stdin));
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
    99
	    close(pipes[1]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   100
	} else {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   101
	    dup2(pipes[1], fileno(stdout));
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   102
	    close(pipes[0]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   103
	}
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   104
	execl("/bin/sh", "/bin/sh", "-c", command, 0);
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   105
	console_fprintf(stderr, "PipeStream [warning]: execlp failed in popen\n");
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   106
	_exit(-1);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   107
	/* NOTREACHED */
325
claus
parents: 308
diff changeset
   108
claus
parents: 308
diff changeset
   109
    default:
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   110
	    if (itype) {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   111
		close(pipes[0]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   112
		return fdopen(pipes[1], "w");
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   113
	    } else {
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   114
		close(pipes[1]);
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   115
		return fdopen(pipes[0], "r");
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   116
	    }
325
claus
parents: 308
diff changeset
   117
    }
claus
parents: 308
diff changeset
   118
}
claus
parents: 308
diff changeset
   119
claus
parents: 308
diff changeset
   120
int
claus
parents: 308
diff changeset
   121
pclose(str)
claus
parents: 308
diff changeset
   122
FILE *str;
claus
parents: 308
diff changeset
   123
{
claus
parents: 308
diff changeset
   124
    int pd = 0;
claus
parents: 308
diff changeset
   125
    int status;
claus
parents: 308
diff changeset
   126
    int err;
claus
parents: 308
diff changeset
   127
claus
parents: 308
diff changeset
   128
    err = fclose(str);
claus
parents: 308
diff changeset
   129
claus
parents: 308
diff changeset
   130
    do {
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   131
	if ((pd = wait(&status)) == -1)
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   132
	{
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   133
		err = EOF;
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   134
		break;
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   135
	}
325
claus
parents: 308
diff changeset
   136
    } while (pd !=  popen_pid);
claus
parents: 308
diff changeset
   137
claus
parents: 308
diff changeset
   138
    if (err == EOF)
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   139
	return  -1;
325
claus
parents: 308
diff changeset
   140
claus
parents: 308
diff changeset
   141
    if (status)
326
d2902942491d *** empty log message ***
claus
parents: 325
diff changeset
   142
	status >>= 8;   /* exit status in high byte */
325
claus
parents: 308
diff changeset
   143
claus
parents: 308
diff changeset
   144
    return status;
claus
parents: 308
diff changeset
   145
}
claus
parents: 308
diff changeset
   146
2925
1a64228425ca *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2896
diff changeset
   147
#endif /* NEED_POPEN_WITH_VFORK */
325
claus
parents: 308
diff changeset
   148
claus
parents: 308
diff changeset
   149
%}
claus
parents: 308
diff changeset
   150
! !
claus
parents: 308
diff changeset
   151
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   152
!PipeStream class methodsFor:'documentation'!
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   153
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   154
copyright
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   155
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   156
 COPYRIGHT (c) 1989 by Claus Gittinger
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   157
	      All Rights Reserved
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
 This software is furnished under a license and may be used
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   160
 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
   161
 inclusion of the above copyright notice.   This software may not
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   162
 be provided or otherwise made available to, or used by, any
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   163
 other person.  No title to or ownership of the software is
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   164
 hereby transferred.
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   165
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   166
!
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
documentation
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   169
"
17460
27b6fcc56477 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17077
diff changeset
   170
    Pipestreams allow reading or writing from/to a unix or dos command.
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   171
    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
   172
    command, a PipeStream can be created with:
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   173
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   174
	PipeStream readingFrom:'ls -l'
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   175
17460
27b6fcc56477 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17077
diff changeset
   176
    the characters of the command's output can be read using the
27b6fcc56477 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17077
diff changeset
   177
    standard stream messages, such as next, nextLine etc.
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   178
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   179
    Example for writing to a command:
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   180
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   181
	PipeStream writingTo:'cat >/tmp/x'
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   182
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   183
    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
   184
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   185
	PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/'''
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   186
1662
ce26ca3d837c linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents: 1648
diff changeset
   187
    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
   188
    buggy (trying to restart the read ...)
ce26ca3d837c linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents: 1648
diff changeset
   189
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   190
    [author:]
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   191
	Claus Gittinger
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   192
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   193
    [see also:]
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   194
	ExternalStream FileStream Socket
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   195
	OperatingSystem
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   196
"
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   197
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   198
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   199
!PipeStream class methodsFor:'initialization'!
2
claus
parents: 1
diff changeset
   200
claus
parents: 1
diff changeset
   201
initialize
claus
parents: 1
diff changeset
   202
    "setup the signal"
claus
parents: 1
diff changeset
   203
57
db9677479d35 *** empty log message ***
claus
parents: 49
diff changeset
   204
    BrokenPipeSignal isNil ifTrue:[
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   205
	BrokenPipeSignal := WriteError newSignalMayProceed:true.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   206
	BrokenPipeSignal nameClass:self message:#brokenPipeSignal.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   207
	BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
57
db9677479d35 *** empty log message ***
claus
parents: 49
diff changeset
   208
    ]
2
claus
parents: 1
diff changeset
   209
! !
claus
parents: 1
diff changeset
   210
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   211
!PipeStream class methodsFor:'instance creation'!
2
claus
parents: 1
diff changeset
   212
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   213
bidirectionalFor:commandString
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   214
    "create and return a new bidirectonal pipeStream which can both be written to
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   215
     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
   216
     The commands error output is send to my own error output."
2
claus
parents: 1
diff changeset
   217
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   218
    ^ self
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   219
	bidirectionalFor:commandString
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   220
	errorDisposition:#stderr
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   221
	inDirectory:nil
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   222
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   223
    "
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   224
	|p|
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   225
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   226
	p := PipeStream bidirectionalFor:'cat -u'.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   227
	p nextPutAll:'Wer ist der Bürgermeister von Wesel'; cr.
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   228
	Transcript showCR:p nextLine.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   229
	p close
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   230
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   231
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   232
    "
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   233
	|p|
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   234
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   235
	p := PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/'''.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   236
	p nextPutAll:'Hello world'; cr.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   237
	p shutDownOutput.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   238
	Transcript showCR:p nextLine.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   239
	p close
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   240
    "
9313
297699a317d3 comment/example
Stefan Vogel <sv@exept.de>
parents: 9193
diff changeset
   241
297699a317d3 comment/example
Stefan Vogel <sv@exept.de>
parents: 9193
diff changeset
   242
    "
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   243
	|p|
9313
297699a317d3 comment/example
Stefan Vogel <sv@exept.de>
parents: 9193
diff changeset
   244
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   245
	p := PipeStream bidirectionalFor:'wc'.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   246
	p nextPutAll:'Hello world'; cr.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   247
	p shutDownOutput.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   248
	Transcript showCR:p nextLine.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   249
	p close
9313
297699a317d3 comment/example
Stefan Vogel <sv@exept.de>
parents: 9193
diff changeset
   250
    "
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   251
!
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   252
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   253
bidirectionalFor:commandString errorDisposition:errorDisposition inDirectory:aDirectory
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   254
    "create and return a new bidirectonal pipeStream which can both be written to
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   255
     and read from the unix command given by commandString.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   256
     The directory will be changed to aDirectory while
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   257
     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
   258
     executed in another directory, to avoid any OS dependencies
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   259
     in your code.
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
     errorDisposition may be one of #discard, #inline or #stderr (default).
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   262
     #discard causes stderr to be discarded (/dev/null),
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   263
     #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
   264
     #stderr causes it to be written to smalltalks own stderr.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   265
     Nil is treated like #stderr"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   266
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   267
    ^ self basicNew
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   268
	openPipeFor:commandString
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   269
	withMode:#'r+'
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   270
	errorDisposition:errorDisposition
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   271
	inDirectory:aDirectory
8968
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
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   274
readingFrom:commandString
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   275
    "create and return a new pipeStream which can read from the unix command
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   276
     given by commandString.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   277
     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
   278
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   279
    ^ self
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   280
        readingFrom:commandString
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   281
        errorDisposition:#stderr
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   282
        inDirectory:nil
8968
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
    "unix:
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   285
        PipeStream readingFrom:'ls -l'.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   286
    "
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
    "
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   289
        |p|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   290
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   291
        p := PipeStream readingFrom:'ls -l'.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   292
        Transcript showCR:p nextLine.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   293
        p close
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   294
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   295
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   296
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   297
    "
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   298
        |p|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   299
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   300
        p := PipeStream readingFrom:'echo error >&2'.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   301
        Transcript showCR:p nextLine.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   302
        p close
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   303
    "
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   304
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   305
    "
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   306
        |s|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   307
        s := PipeStream readingFrom:'sh -c sleep\ 600'.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   308
        (Delay forSeconds:2) wait.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   309
        s abortAndClose
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   310
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   311
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   312
    "
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   313
        |p|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   314
        p := PipeStream readingFrom:'dir'.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   315
        Transcript showCR:p nextLine.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   316
        p close
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   317
    "
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   318
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   319
    "Windows:
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   320
        PipeStream readingFrom:'dir'.
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   321
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   322
    "
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   323
        |p|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   324
        p := PipeStream readingFrom:'dir'.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   325
        Transcript showCR:p nextLine.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   326
        p close
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   327
    "
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   328
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   329
    "Modified: 24.4.1996 / 09:09:25 / stefan"
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   330
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   331
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   332
readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   333
    "similar to #readingFrom, but changes the directory while
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   334
     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
   335
     executed in another directory, to avoid any OS dependencies
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   336
     in your code.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   337
     errorDisposition may be one of #discard, #inline or #stderr (default).
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   338
     #discard causes stderr to be discarded (/dev/null),
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   339
     #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
   340
     #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
   341
     Nil is treated like #stderr"
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   342
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   343
    ^ self basicNew
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   344
        openPipeFor:commandString
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   345
        withMode:#r
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   346
        errorDisposition:errorDisposition
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   347
        inDirectory:aDirectory
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   348
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   349
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   350
    "
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   351
        |p|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   352
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   353
        p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   354
        Transcript showCR:p nextLine.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   355
        p close
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   356
    "
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   357
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   358
    "
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   359
        |p|
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   360
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   361
        p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   362
        Transcript showCR:p nextLine.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   363
        p close
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   364
    "
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   365
!
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   366
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   367
readingFrom:commandString inDirectory:aDirectory
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   368
    "similar to #readingFrom, but changes the directory while
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   369
     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
   370
     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
   371
     in your code.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   372
     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
   373
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   374
     ^ self
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   375
	readingFrom:commandString
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   376
	errorDisposition:#stderr
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   377
	inDirectory:aDirectory
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   378
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   379
    " UNIX:
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   380
	|p|
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   381
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   382
	p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   383
	Transcript showCR:p upToEnd.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   384
	p close
9193
47ffbf7b9ee8 comment
Claus Gittinger <cg@exept.de>
parents: 8976
diff changeset
   385
    "
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   386
    "WINDOOF:
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   387
	|p|
9193
47ffbf7b9ee8 comment
Claus Gittinger <cg@exept.de>
parents: 8976
diff changeset
   388
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   389
	p := PipeStream readingFrom:'dir'.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   390
	Transcript showCR:p upToEnd.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   391
	p close
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   392
   "
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   393
!
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   394
2
claus
parents: 1
diff changeset
   395
writingTo:commandString
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   396
    "create and return a new pipeStream which can write to the unix command
a27a279701f8 Initial revision
claus
parents:
diff changeset
   397
     given by command."
a27a279701f8 Initial revision
claus
parents:
diff changeset
   398
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   399
    ^ self
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   400
	writingTo:commandString errorDisposition:#stderr inDirectory:nil
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   401
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   402
    "unix:
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   403
	 PipeStream writingTo:'sort'
2966
856dfc8a294a lots of VMS changes
Claus Gittinger <cg@exept.de>
parents: 2961
diff changeset
   404
    "
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   405
!
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   406
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   407
writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   408
    "similar to #writingTo, but changes the directory while
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   409
     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
   410
     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
   411
     in your code.
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   412
     errorDisposition may be one of #discard, #inline or #stderr (default).
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   413
     #discard causes stderr to be discarded (/dev/null),
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   414
     #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
   415
     #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
   416
     Nil is treated like #stderr"
2969
1fcf6dfb6004 VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents: 2966
diff changeset
   417
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   418
    ^ self basicNew
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   419
	openPipeFor:commandString
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   420
	withMode:#w
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   421
	errorDisposition:errorDisposition
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   422
	inDirectory:aDirectory
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   423
!
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   424
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   425
writingTo:commandString inDirectory:aDirectory
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   426
    "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
   427
     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
   428
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   429
    ^ self
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   430
	writingTo:commandString errorDisposition:#stderr inDirectory:aDirectory
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   431
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   432
    "unix:
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   433
	 PipeStream writingTo:'sort'
4526
a42dc8e09586 errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents: 4202
diff changeset
   434
    "
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   435
! !
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   436
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   437
!PipeStream class methodsFor:'Signal constants'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   438
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   439
brokenPipeSignal
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   440
    "return the signal used to handle SIGPIPE unix-signals.
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   441
     Since SIGPIPE is asynchronous, we can't decide which smalltalk process
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   442
     should handle BrokenPipeSignal. So the system doesn't raise
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   443
     BrokenPipeSignal for SIGPIPE any longer."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   444
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   445
    ^ BrokenPipeSignal
2985
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   446
eff9e86dd2f9 New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents: 2976
diff changeset
   447
    "Modified: 24.9.1997 / 09:43:23 / stefan"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   448
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   449
16579
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   450
!PipeStream class methodsFor:'utilities'!
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   451
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   452
outputFromCommand:aCommand
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   453
    "open a pipe reading from aCommand and return the complete output as a string.
18638
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   454
     If the command cannot be executed, return nil.
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   455
     The command's current directory will be the smalltalk current directory."
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   456
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   457
    ^ self outputFromCommand:aCommand inDirectory:nil
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   458
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   459
    "
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   460
     PipeStream outputFromCommand:'ls -l'
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   461
    "
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   462
!
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   463
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   464
outputFromCommand:aCommand inDirectory:aDirectoryOrNil
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   465
    "open a pipe reading from aCommand and return the complete output as a string.
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   466
     If the command cannot be executed, return nil.
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   467
     The current directory of the command will be aDirectoryOrNil or the smalltalk's current directory (if nil)"
16579
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   468
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   469
    |p cmdOutput|
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   470
18638
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   471
    p := self readingFrom:aCommand inDirectory:aDirectoryOrNil.
16579
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   472
    p isNil ifTrue:[^ nil].
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   473
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   474
    [
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   475
	cmdOutput := p contentsAsString.
16579
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   476
    ] ensure:[
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 18785
diff changeset
   477
	p close.
16579
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   478
    ].
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   479
    ^ cmdOutput
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   480
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   481
    "
18638
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   482
     PipeStream outputFromCommand:'ls -l' inDirectory:nil
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   483
     PipeStream outputFromCommand:'ls -l' inDirectory:'/'
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   484
     PipeStream outputFromCommand:'ls -l' inDirectory:'/etc'
16579
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   485
    "
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   486
! !
3d51a7434f06 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 16339
diff changeset
   487
99
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   488
!PipeStream methodsFor:'accessing'!
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   489
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   490
commandString
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   491
    "return the command string"
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   492
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   493
    ^ commandString
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   494
!
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   495
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   496
exitStatus
10343
c48f0a54ef69 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9313
diff changeset
   497
    "return the exitStatus"
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   498
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   499
    osProcess isNil ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   500
        ^ nil.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   501
    ].
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   502
    ^ osProcess exitStatus.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   503
    
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   504
    "Created: 28.12.1995 / 14:54:41 / stefan"
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   505
!
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   506
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   507
pid
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   508
    "return pid"
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   509
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   510
    osProcess isNil ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   511
        ^ nil.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   512
    ].
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   513
    ^ osProcess pid.
1648
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   514
faa891d2c1b0 Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents: 1295
diff changeset
   515
    "Created: 28.12.1995 / 14:54:30 / stefan"
24086
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   516
!
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   517
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   518
setCommandString:aString
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   519
    "for OSProcess only (so we see the command in the streams monitor)"
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   520
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   521
    commandString := aString
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   522
9018daacf498 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21252
diff changeset
   523
    "Created: / 29-10-2018 / 15:14:24 / Claus Gittinger"
99
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   524
! !
afba01fbe15c added access method for commandString
claus
parents: 93
diff changeset
   525
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   526
!PipeStream methodsFor:'closing'!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   527
20681
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   528
abortAndClose
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   529
    "close the Stream and terminate the command"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   530
20031
8de79de4b866 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   531
    self unregisterForFinalization.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   532
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   533
    "terminate first under windows"
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   534
    OperatingSystem isMSDOSlike ifTrue:[
20031
8de79de4b866 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   535
        self terminatePipeCommand.
21228
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   536
        self closeFile.
20031
8de79de4b866 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   537
        ^ self.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   538
    ].
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   539
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   540
    "terminate last under unix"
21228
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   541
    self closeFile.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   542
    self terminatePipeCommand.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   543
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   544
20681
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   545
close
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   546
    "low level close
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   547
     This waits for the command to finish.
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   548
     Use abortAndClose for a fast (nonBlocking) close."
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   549
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   550
    handle notNil ifTrue:[
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   551
        super close.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   552
        "/ wait for the pipe-command to terminate.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   553
        self waitForPipeCommandWithTimeout:nil.
20681
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   554
    ].
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   555
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   556
    "Modified: / 12.9.1998 / 16:51:04 / cg"
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   557
!
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   558
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   559
shutDown
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   560
    <resource: #obsolete>
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   561
    "this is a historic leftover kept for backward compatibility.
20970
54cb5bc5bd05 #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 20681
diff changeset
   562
     The name collides with the same name in Socket, which does
20681
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   563
     not hard terminate the connection."
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   564
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   565
    self abortAndClose.
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   566
!
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   567
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   568
shutDownOutput
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   569
    "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
   570
8976
2ff451956d48 #shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents: 8969
diff changeset
   571
    |fd|
2ff451956d48 #shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents: 8969
diff changeset
   572
2ff451956d48 #shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents: 8969
diff changeset
   573
    self isOpen ifTrue:[
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   574
	fd := self fileDescriptor.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   575
	fd notNil ifTrue:[
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   576
	    OperatingSystem shutdownBidirectionalPipeOutput:fd.
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   577
	].
8976
2ff451956d48 #shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents: 8969
diff changeset
   578
    ].
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   579
! !
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   580
5243
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   581
!PipeStream methodsFor:'finalization'!
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   582
6439
0f841258ec4a Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6267
diff changeset
   583
finalize
5243
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   584
    "redefined to avoid blocking in close."
d3b509746522 category change
Claus Gittinger <cg@exept.de>
parents: 5205
diff changeset
   585
20681
814f81a85289 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 20323
diff changeset
   586
    self abortAndClose
18785
5b58b205c252 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 18782
diff changeset
   587
! !
5b58b205c252 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 18782
diff changeset
   588
5b58b205c252 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 18782
diff changeset
   589
!PipeStream methodsFor:'private'!
369
claus
parents: 362
diff changeset
   590
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   591
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   592
    "open a pipe to the OS command in commandString;
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   593
     rwMode may be 'r' or 'w' or 'r+'.
5481
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   594
     errorDisposition controls where the stdErr output should go,
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   595
     and may be one of #discard, #inline or #stderr (default).
10408
8027bf22ae31 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10363
diff changeset
   596
     #discard causes stderr to be discarded (/dev/null),
5481
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   597
     #inline causes it to be written to smalltalks own stdout and
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   598
     #stderr causes it to be written to smalltalks own stderr.
680d6de80808 comment
Claus Gittinger <cg@exept.de>
parents: 5456
diff changeset
   599
     Nil is treated like #stderr"
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   600
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   601
    |pipeArray remotePipeEnd nullOutput errorNumber myPipeEnd result|
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   602
12395
1db7ea277c93 filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents: 11083
diff changeset
   603
    handle notNil ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   604
        "the pipe was already open ...
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   605
         this should (can) not happen."
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   606
        ^ self errorAlreadyOpen
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   607
    ].
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   608
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   609
    rwMode = #r ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   610
        mode := #readonly. didWrite := false.
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   611
        position := 0.      "only reading - can keep track of position"
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   612
    ] ifFalse:[rwMode = #'r+' ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   613
        mode := #readwrite. didWrite := true.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   614
    ] ifFalse:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   615
        mode := #writeonly. didWrite := true.
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   616
        position := 0.      "only writing - can keep track of position"
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   617
    ]].
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   618
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   619
    lastErrorNumber := nil.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   620
    commandString := aCommandString.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   621
    "stdio lib does not work with blocking pipes and interrupts
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   622
     for WIN, Linux, Solaris and probably any other UNIX"
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   623
    buffered := false.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   624
    hitEOF := false.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   625
    binary := false.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   626
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   627
    osProcess := OSProcess new 
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   628
                    command:aCommandString;
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   629
                    directory:aDirectory.
4616
64dd3a9bebf5 *** empty log message ***
ps
parents: 4526
diff changeset
   630
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   631
    mode == #readwrite ifTrue:[
21211
ed5fd0f7a98b #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21210
diff changeset
   632
        pipeArray := self class makeBidirectionalPipe.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   633
        pipeArray isNil ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   634
            lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber.
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   635
            ^ self openError:errorNumber.
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   636
        ].
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   637
        myPipeEnd := pipeArray at:1.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   638
        remotePipeEnd := pipeArray at:2.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   639
        osProcess inStream:remotePipeEnd.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   640
        osProcess outStream:remotePipeEnd.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   641
    ] ifFalse:[
21211
ed5fd0f7a98b #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21210
diff changeset
   642
        pipeArray := self class makePipe.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   643
        pipeArray isNil ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   644
            lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   645
            ^ self openError:errorNumber.
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   646
        ].
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   647
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   648
        mode == #readonly ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   649
            "redirect stdout of subprocess to write to pipe"
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   650
            myPipeEnd := pipeArray at:1.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   651
            remotePipeEnd := pipeArray at:2.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   652
            osProcess outStream:remotePipeEnd.
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   653
        ] ifFalse:[
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   654
            "redirect stdin of subprocess to read from pipe"
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   655
            myPipeEnd := pipeArray at:2.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   656
            remotePipeEnd := pipeArray at:1.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   657
            osProcess inStream:remotePipeEnd.
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   658
        ].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   659
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   660
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   661
    errorDisposition == #discard ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   662
        nullOutput := Filename nullDevice writeStream.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   663
        osProcess errorStream:nullOutput.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   664
    ] ifFalse:[(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   665
        osProcess errorStream:osProcess outStream.
21211
ed5fd0f7a98b #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 21210
diff changeset
   666
    ] ifFalse:[(errorDisposition == #stderr or:[errorDisposition isNil]) ifTrue:[
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   667
        osProcess errorStream:Stderr.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   668
    ] ifFalse:[errorDisposition isStream ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   669
        osProcess errorStream:errorDisposition.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   670
    ]]]].
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   671
21228
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   672
    mode ~~ #readonly ifTrue:[
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   673
        osProcess terminateActionBlock:[
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   674
                "writing doesn't make sense - there is no reader any longer"
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   675
                mode == #readwrite ifTrue:[
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   676
                    "... but allow to read the rest of the command's output"
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   677
                    self shutDownOutput.
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   678
                ] ifFalse:[mode == #writeonly ifTrue:[
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   679
                    self closeFile.
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   680
                ]].
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   681
           ].
7c2104239a0f #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21211
diff changeset
   682
    ].
8969
978613904d0b For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents: 8968
diff changeset
   683
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   684
    result := osProcess startProcess.
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   685
7079
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   686
    "subprocess has been created.
07625dfffcbf Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents: 7060
diff changeset
   687
     close unused filedescriptors"
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   688
    remotePipeEnd notNil ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   689
        remotePipeEnd close.
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   690
    ].
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   691
    nullOutput notNil ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   692
        nullOutput close
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   693
    ].
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   694
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   695
    result ifTrue:[
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   696
        "successfull creation of subprocesss"
21231
985ae4f2c424 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21228
diff changeset
   697
        handle := myPipeEnd handle.
985ae4f2c424 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 21228
diff changeset
   698
        handleType := myPipeEnd handleType.
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   699
        myPipeEnd unregisterForFinalization.    "make sure filedesciptor is not closed by finalizer"
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   700
        myPipeEnd := nil.
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   701
    ] ifFalse:[
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   702
        "the pipe open failed for some reason ...
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   703
         ... this may be either due to an invalid command string,
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   704
         or due to the system running out of memory (when forking
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   705
         the unix process)"
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   706
        lastErrorNumber := OperatingSystem lastErrorNumber.
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   707
        myPipeEnd close.
20099
3924a4006e8b #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20031
diff changeset
   708
        ^ self openError:lastErrorNumber.
3296
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
18782
aa9e2cb1d490 #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 18638
diff changeset
   711
    self registerForFinalization.
3296
bd2da537678b md's WIN32 changes
Claus Gittinger <cg@exept.de>
parents: 3196
diff changeset
   712
4202
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   713
    "Modified: / 23.4.1996 / 17:05:59 / stefan"
b9f85ebd2e38 generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents: 4125
diff changeset
   714
    "Modified: / 28.1.1998 / 14:47:34 / md"
6439
0f841258ec4a Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6267
diff changeset
   715
    "Created: / 19.5.1999 / 12:28:54 / cg"
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   716
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   717
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   718
terminatePipeCommand
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   719
    osProcess notNil ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   720
        osProcess terminateGroup.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   721
    ].
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   722
!
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   723
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   724
waitForPipeCommandWithTimeout:seconds
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   725
    "wait for the pipe command to terminate itself.
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   726
     Return true, if a timeout occurred."
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   727
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   728
    osProcess notNil ifTrue:[
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   729
        ^ osProcess finishSema waitWithTimeout:seconds.
8968
879991b52630 Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents: 8520
diff changeset
   730
    ].
21210
99f6f07d5a33 #FEATURE by stefan
Stefan Vogel <sv@exept.de>
parents: 20970
diff changeset
   731
    ^ false
6439
0f841258ec4a Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6267
diff changeset
   732
! !
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   733
2266
a94af740c68a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2121
diff changeset
   734
!PipeStream class methodsFor:'documentation'!
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   735
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   736
version
18638
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   737
    ^ '$Header$'
12712
c1b3b5846541 Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents: 12548
diff changeset
   738
!
c1b3b5846541 Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents: 12548
diff changeset
   739
c1b3b5846541 Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents: 12548
diff changeset
   740
version_CVS
18638
d5624e45f660 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 17460
diff changeset
   741
    ^ '$Header$'
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   742
! !
7027
0efa9d55fa04 Remove unused method var
Stefan Vogel <sv@exept.de>
parents: 6487
diff changeset
   743
15359
060c677e3e92 class: PipeStream
Claus Gittinger <cg@exept.de>
parents: 14436
diff changeset
   744
613
0af19c3594fc checkin from browser
Claus Gittinger <cg@exept.de>
parents: 582
diff changeset
   745
PipeStream initialize!