/jEdit/tags/jedit-4-3-pre5/bsh/CommandLineReader.java
Java | 107 lines | 44 code | 16 blank | 47 comment | 12 complexity | 356a36935480ea85a3391214c107ca01 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
1/*****************************************************************************
2 * *
3 * This file is part of the BeanShell Java Scripting distribution. *
4 * Documentation and updates may be found at http://www.beanshell.org/ *
5 * *
6 * Sun Public License Notice: *
7 * *
8 * The contents of this file are subject to the Sun Public License Version *
9 * 1.0 (the "License"); you may not use this file except in compliance with *
10 * the License. A copy of the License is available at http://www.sun.com *
11 * *
12 * The Original Code is BeanShell. The Initial Developer of the Original *
13 * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14 * (C) 2000. All Rights Reserved. *
15 * *
16 * GNU Public License Notice: *
17 * *
18 * Alternatively, the contents of this file may be used under the terms of *
19 * the GNU Lesser General Public License (the "LGPL"), in which case the *
20 * provisions of LGPL are applicable instead of those above. If you wish to *
21 * allow use of your version of this file only under the terms of the LGPL *
22 * and not to allow others to use your version of this file under the SPL, *
23 * indicate your decision by deleting the provisions above and replace *
24 * them with the notice and other provisions required by the LGPL. If you *
25 * do not delete the provisions above, a recipient may use your version of *
26 * this file under either the SPL or the LGPL. *
27 * *
28 * Patrick Niemeyer (pat@pat.net) *
29 * Author of Learning Java, O'Reilly & Associates *
30 * http://www.pat.net/~pat/ *
31 * *
32 *****************************************************************************/
33
34
35package bsh;
36
37import java.io.*;
38
39/**
40 This is a quick hack to turn empty lines entered interactively on the
41 command line into ';\n' empty lines for the interpreter. It's just more
42 pleasant to be able to hit return on an empty line and see the prompt
43 reappear.
44
45 This is *not* used when text is sourced from a file non-interactively.
46*/
47class CommandLineReader extends FilterReader {
48
49 public CommandLineReader( Reader in ) {
50 super(in);
51 }
52
53 static final int
54 normal = 0,
55 lastCharNL = 1,
56 sentSemi = 2;
57
58 int state = lastCharNL;
59
60 public int read() throws IOException {
61 int b;
62
63 if ( state == sentSemi ) {
64 state = lastCharNL;
65 return '\n';
66 }
67
68 // skip CR
69 while ( (b = in.read()) == '\r' );
70
71 if ( b == '\n' )
72 if ( state == lastCharNL ) {
73 b = ';';
74 state = sentSemi;
75 } else
76 state = lastCharNL;
77 else
78 state = normal;
79
80 return b;
81 }
82
83 /**
84 This is a degenerate implementation.
85 I don't know how to keep this from blocking if we try to read more
86 than one char... There is no available() for Readers ??
87 */
88 public int read(char buff[], int off, int len) throws IOException
89 {
90 int b = read();
91 if ( b == -1 )
92 return -1; // EOF, not zero read apparently
93 else {
94 buff[off]=(char)b;
95 return 1;
96 }
97 }
98
99 // Test it
100 public static void main( String [] args ) throws Exception {
101 Reader in = new CommandLineReader( new InputStreamReader(System.in) );
102 while ( true )
103 System.out.println( in.read() );
104
105 }
106}
107