11/*
2- * Copyright (c) 2015, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2021 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
4747import java .util .ListIterator ;
4848import java .util .Map ;
4949import java .util .Optional ;
50+ import java .util .function .Consumer ;
5051import java .util .function .Function ;
5152import java .util .stream .Collectors ;
5253import java .util .stream .Stream ;
@@ -103,8 +104,8 @@ class ConsoleIOContext extends IOContext {
103104
104105 String prefix = "" ;
105106
106- ConsoleIOContext (JShellTool repl , InputStream cmdin , PrintStream cmdout ) throws Exception {
107- this . allowIncompleteInputs = Boolean . getBoolean ( "jshell.test.allow.incomplete.inputs" );
107+ ConsoleIOContext (JShellTool repl , InputStream cmdin , PrintStream cmdout ,
108+ boolean interactive ) throws Exception {
108109 this .repl = repl ;
109110 Map <String , Object > variables = new HashMap <>();
110111 this .input = new StopDetectingInputStream (() -> repl .stop (),
@@ -116,15 +117,28 @@ public int readBuffered(byte[] b) throws IOException {
116117 }
117118 };
118119 Terminal terminal ;
119- if (System .getProperty ("test.jdk" ) != null ) {
120- terminal = new TestTerminal (nonBlockingInput , cmdout );
120+ boolean allowIncompleteInputs = Boolean .getBoolean ("jshell.test.allow.incomplete.inputs" );
121+ Consumer <LineReaderImpl > setupReader = r -> {};
122+ if (cmdin != System .in ) {
123+ if (System .getProperty ("test.jdk" ) != null ) {
124+ terminal = new TestTerminal (nonBlockingInput , cmdout );
125+ } else {
126+ Size size = null ;
127+ terminal = new ProgrammaticInTerminal (nonBlockingInput , cmdout , interactive ,
128+ size );
129+ if (!interactive ) {
130+ setupReader = r -> r .unsetOpt (Option .BRACKETED_PASTE );
131+ allowIncompleteInputs = true ;
132+ }
133+ }
121134 input .setInputStream (cmdin );
122135 } else {
123136 terminal = TerminalBuilder .builder ().inputStreamWrapper (in -> {
124137 input .setInputStream (in );
125138 return nonBlockingInput ;
126139 }).build ();
127140 }
141+ this .allowIncompleteInputs = allowIncompleteInputs ;
128142 originalAttributes = terminal .getAttributes ();
129143 Attributes noIntr = new Attributes (originalAttributes );
130144 noIntr .setControlChar (ControlChar .VINTR , 0 );
@@ -179,10 +193,11 @@ protected boolean insertCloseSquare() {
179193 }
180194 };
181195
196+ setupReader .accept (reader );
182197 reader .setOpt (Option .DISABLE_EVENT_EXPANSION );
183198
184199 reader .setParser ((line , cursor , context ) -> {
185- if (!allowIncompleteInputs && !repl .isComplete (line )) {
200+ if (!ConsoleIOContext . this . allowIncompleteInputs && !repl .isComplete (line )) {
186201 int pendingBraces = countPendingOpenBraces (line );
187202 throw new EOFError (cursor , cursor , line , null , pendingBraces , null );
188203 }
@@ -230,7 +245,7 @@ public String readLine(String firstLinePrompt, String continuationPrompt,
230245 this .prefix = prefix ;
231246 try {
232247 in .setVariable (LineReader .SECONDARY_PROMPT_PATTERN , continuationPrompt );
233- return in .readLine (firstLinePrompt );
248+ return in .readLine (firstLine ? firstLinePrompt : continuationPrompt );
234249 } catch (UserInterruptException ex ) {
235250 throw (InputInterruptedException ) new InputInterruptedException ().initCause (ex );
236251 } catch (EndOfFileException ex ) {
@@ -1276,28 +1291,31 @@ private History getHistory() {
12761291 return in .getHistory ();
12771292 }
12781293
1279- private static final class TestTerminal extends LineDisciplineTerminal {
1294+ private static class ProgrammaticInTerminal extends LineDisciplineTerminal {
12801295
1281- private static final int DEFAULT_HEIGHT = 24 ;
1296+ protected static final int DEFAULT_HEIGHT = 24 ;
12821297
12831298 private final NonBlockingReader inputReader ;
1299+ private final Size bufferSize ;
1300+
1301+ public ProgrammaticInTerminal (InputStream input , OutputStream output ,
1302+ boolean interactive , Size size ) throws Exception {
1303+ this (input , output , interactive ? "ansi" : "dumb" ,
1304+ size != null ? size : new Size (80 , DEFAULT_HEIGHT ),
1305+ size != null ? size
1306+ : interactive ? new Size (80 , DEFAULT_HEIGHT )
1307+ : new Size (Integer .MAX_VALUE - 1 , DEFAULT_HEIGHT ));
1308+ }
12841309
1285- public TestTerminal (InputStream input , OutputStream output ) throws Exception {
1286- super ("test" , "ansi" , output , Charset .forName ("UTF-8" ));
1310+ protected ProgrammaticInTerminal (InputStream input , OutputStream output ,
1311+ String terminal , Size size , Size bufferSize ) throws Exception {
1312+ super ("non-system-in" , terminal , output , Charset .forName ("UTF-8" ));
12871313 this .inputReader = NonBlocking .nonBlocking (getName (), input , encoding ());
12881314 Attributes a = new Attributes (getAttributes ());
12891315 a .setLocalFlag (LocalFlag .ECHO , false );
12901316 setAttributes (attributes );
1291- int h = DEFAULT_HEIGHT ;
1292- try {
1293- String hp = System .getProperty ("test.terminal.height" );
1294- if (hp != null && !hp .isEmpty ()) {
1295- h = Integer .parseInt (hp );
1296- }
1297- } catch (Throwable ex ) {
1298- // ignore
1299- }
1300- setSize (new Size (80 , h ));
1317+ setSize (size );
1318+ this .bufferSize = bufferSize ;
13011319 }
13021320
13031321 @ Override
@@ -1312,6 +1330,31 @@ protected void doClose() throws IOException {
13121330 inputReader .close ();
13131331 }
13141332
1333+ @ Override
1334+ public Size getBufferSize () {
1335+ return bufferSize ;
1336+ }
1337+ }
1338+
1339+ private static final class TestTerminal extends ProgrammaticInTerminal {
1340+ private static Size computeSize () {
1341+ int h = DEFAULT_HEIGHT ;
1342+ try {
1343+ String hp = System .getProperty ("test.terminal.height" );
1344+ if (hp != null && !hp .isEmpty () && System .getProperty ("test.jdk" ) != null ) {
1345+ h = Integer .parseInt (hp );
1346+ }
1347+ } catch (Throwable ex ) {
1348+ // ignore
1349+ }
1350+ return new Size (80 , h );
1351+ }
1352+ public TestTerminal (InputStream input , OutputStream output ) throws Exception {
1353+ this (input , output , computeSize ());
1354+ }
1355+ private TestTerminal (InputStream input , OutputStream output , Size size ) throws Exception {
1356+ super (input , output , "ansi" , size , size );
1357+ }
13151358 }
13161359
13171360 private static final class CompletionState {
0 commit comments