@@ -1270,20 +1270,19 @@ static class PipePump extends Thread {
1270
1270
private final OutputStream out ;
1271
1271
private final byte [] buffer ;
1272
1272
private volatile boolean finish ;
1273
- private volatile boolean flush ;
1274
1273
1275
- public PipePump (InputStream in , OutputStream out ) {
1274
+ public PipePump (String name , InputStream in , OutputStream out ) {
1275
+ this .setName (name );
1276
1276
this .in = in ;
1277
1277
this .out = out ;
1278
1278
this .buffer = new byte [MAX_READ ];
1279
1279
this .finish = false ;
1280
- this .flush = false ;
1281
1280
}
1282
1281
1283
1282
@ Override
1284
1283
public void run () {
1285
1284
try {
1286
- while (!finish || ( flush && in .available () > 0 ) ) {
1285
+ while (!finish || in .available () > 0 ) {
1287
1286
if (Thread .interrupted ()) {
1288
1287
finish = true ;
1289
1288
}
@@ -1297,14 +1296,10 @@ public void run() {
1297
1296
}
1298
1297
}
1299
1298
1300
- public void finish (boolean force_flush ) {
1299
+ public void finish () {
1301
1300
finish = true ;
1302
- flush = force_flush ;
1303
- if (flush ) {
1304
- // If we need to flush, make ourselves max priority to pump data out as quickly
1305
- // as possible
1306
- setPriority (Thread .MAX_PRIORITY );
1307
- }
1301
+ // Make ourselves max priority to flush data out as quickly as possible
1302
+ setPriority (Thread .MAX_PRIORITY );
1308
1303
Thread .yield ();
1309
1304
}
1310
1305
}
@@ -1320,20 +1315,28 @@ int system(String cmd) {
1320
1315
Env env = context .getEnv ();
1321
1316
try {
1322
1317
ProcessBuilder pb = new ProcessBuilder (command );
1323
- pb .redirectInput (Redirect .PIPE );
1324
- pb .redirectOutput (Redirect .PIPE );
1325
- pb .redirectError (Redirect .PIPE );
1318
+ PipePump stdout = null , stderr = null ;
1319
+ boolean stdsArePipes = !terminalIsInteractive (context );
1320
+ if (stdsArePipes ) {
1321
+ pb .redirectInput (Redirect .PIPE );
1322
+ pb .redirectOutput (Redirect .PIPE );
1323
+ pb .redirectError (Redirect .PIPE );
1324
+ } else {
1325
+ pb .inheritIO ();
1326
+ }
1326
1327
Process proc = pb .start ();
1327
- PipePump stdin = new PipePump (env .in (), proc .getOutputStream ());
1328
- PipePump stdout = new PipePump (proc .getInputStream (), env .out ());
1329
- PipePump stderr = new PipePump (proc .getErrorStream (), env .err ());
1330
- stdin .start ();
1331
- stdout .start ();
1332
- stderr .start ();
1328
+ if (stdsArePipes ) {
1329
+ proc .getOutputStream ().close (); // stdin will be closed
1330
+ stdout = new PipePump (cmd + " [stdout]" , proc .getInputStream (), env .out ());
1331
+ stderr = new PipePump (cmd + " [stderr]" , proc .getErrorStream (), env .err ());
1332
+ stdout .start ();
1333
+ stderr .start ();
1334
+ }
1333
1335
int exitStatus = proc .waitFor ();
1334
- stdin .finish (false );
1335
- stdout .finish (true );
1336
- stderr .finish (true );
1336
+ if (stdsArePipes ) {
1337
+ stdout .finish ();
1338
+ stderr .finish ();
1339
+ }
1337
1340
return exitStatus ;
1338
1341
} catch (IOException | InterruptedException e ) {
1339
1342
return -1 ;
0 commit comments