PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/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. package bsh;
  34. import java.io.*;
  35. /**
  36. This is a quick hack to turn empty lines entered interactively on the
  37. command line into ';\n' empty lines for the interpreter. It's just more
  38. pleasant to be able to hit return on an empty line and see the prompt
  39. reappear.
  40. This is *not* used when text is sourced from a file non-interactively.
  41. */
  42. class CommandLineReader extends FilterReader {
  43. public CommandLineReader( Reader in ) {
  44. super(in);
  45. }
  46. static final int
  47. normal = 0,
  48. lastCharNL = 1,
  49. sentSemi = 2;
  50. int state = lastCharNL;
  51. public int read() throws IOException {
  52. int b;
  53. if ( state == sentSemi ) {
  54. state = lastCharNL;
  55. return '\n';
  56. }
  57. // skip CR
  58. while ( (b = in.read()) == '\r' );
  59. if ( b == '\n' )
  60. if ( state == lastCharNL ) {
  61. b = ';';
  62. state = sentSemi;
  63. } else
  64. state = lastCharNL;
  65. else
  66. state = normal;
  67. return b;
  68. }
  69. /**
  70. This is a degenerate implementation.
  71. I don't know how to keep this from blocking if we try to read more
  72. than one char... There is no available() for Readers ??
  73. */
  74. public int read(char buff[], int off, int len) throws IOException
  75. {
  76. int b = read();
  77. if ( b == -1 )
  78. return -1; // EOF, not zero read apparently
  79. else {
  80. buff[off]=(char)b;
  81. return 1;
  82. }
  83. }
  84. // Test it
  85. public static void main( String [] args ) throws Exception {
  86. Reader in = new CommandLineReader( new InputStreamReader(System.in) );
  87. while ( true )
  88. System.out.println( in.read() );
  89. }
  90. }