001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package bsh;
033:
034: import java.io.*;
035:
036: /**
037: This is a quick hack to turn empty lines entered interactively on the
038: command line into ';\n' empty lines for the interpreter. It's just more
039: pleasant to be able to hit return on an empty line and see the prompt
040: reappear.
041:
042: This is *not* used when text is sourced from a file non-interactively.
043: */
044: class CommandLineReader extends FilterReader {
045:
046: public CommandLineReader(Reader in) {
047: super (in);
048: }
049:
050: static final int normal = 0, lastCharNL = 1, sentSemi = 2;
051:
052: int state = lastCharNL;
053:
054: public int read() throws IOException {
055: int b;
056:
057: if (state == sentSemi) {
058: state = lastCharNL;
059: return '\n';
060: }
061:
062: // skip CR
063: while ((b = in.read()) == '\r')
064: ;
065:
066: if (b == '\n')
067: if (state == lastCharNL) {
068: b = ';';
069: state = sentSemi;
070: } else
071: state = lastCharNL;
072: else
073: state = normal;
074:
075: return b;
076: }
077:
078: /**
079: This is a degenerate implementation.
080: I don't know how to keep this from blocking if we try to read more
081: than one char... There is no available() for Readers ??
082: */
083: public int read(char buff[], int off, int len) throws IOException {
084: int b = read();
085: if (b == -1)
086: return -1; // EOF, not zero read apparently
087: else {
088: buff[off] = (char) b;
089: return 1;
090: }
091: }
092:
093: // Test it
094: public static void main(String[] args) throws Exception {
095: Reader in = new CommandLineReader(new InputStreamReader(
096: System.in));
097: while (true)
098: System.out.println(in.read());
099:
100: }
101: }
|