Пример #1
0
 public void run() {
   try {
     for (; ; ) {
       byte[] buf = new byte[8192];
       int l = out.read(buf);
       InputStreamReader r = new InputStreamReader(new ByteArrayInputStream(buf, 0, l));
       StringBuilder sb = new StringBuilder();
       for (; ; ) {
         int c = r.read();
         if (c == -1) {
           break;
         }
         sb.append((char) c);
       }
       if (sb.length() > 0) {
         terminal.write(sb.toString());
       }
       String s = terminal.read();
       if (s != null && s.length() > 0) {
         for (byte b : s.getBytes()) {
           in.write(b);
         }
       }
     }
   } catch (IOException e) {
     closed = true;
   }
 }
Пример #2
0
    public SessionTerminal(CommandProcessor commandProcessor, ThreadIO threadIO)
        throws IOException {
      try {
        this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT);
        terminal.write("\u001b\u005B20\u0068"); // set newline mode on

        in = new PipedOutputStream();
        out = new PipedInputStream();
        PrintStream pipedOut = new PrintStream(new PipedOutputStream(out), true);

        console =
            new Console(
                commandProcessor,
                threadIO,
                new PipedInputStream(in),
                pipedOut,
                pipedOut,
                new WebTerminal(TERM_WIDTH, TERM_HEIGHT),
                null,
                null);
        CommandSession session = console.getSession();
        session.put("APPLICATION", System.getProperty("karaf.name", "root"));
        session.put("USER", "karaf");
        session.put("COLUMNS", Integer.toString(TERM_WIDTH));
        session.put("LINES", Integer.toString(TERM_HEIGHT));
      } catch (IOException e) {
        LOG.info("Exception attaching to console", e);
        throw e;
      } catch (Exception e) {
        LOG.info("Exception attaching to console", e);
        throw (IOException) new IOException().initCause(e);
      }
      new Thread(console).start();
      new Thread(this).start();
    }
Пример #3
0
 public String handle(String str, boolean forceDump) throws IOException {
   try {
     if (str != null && str.length() > 0) {
       String d = terminal.pipe(str);
       for (byte b : d.getBytes()) {
         in.write(b);
       }
       in.flush();
     }
   } catch (IOException e) {
     closed = true;
     throw e;
   }
   try {
     return terminal.dump(10, forceDump);
   } catch (InterruptedException e) {
     throw new InterruptedIOException(e.toString());
   }
 }
Пример #4
0
  /**
   * Read a character from the console.
   *
   * @return the character, or -1 if an EOF is received.
   */
  public final int readVirtualKey() throws IOException {
    int c = terminal.readVirtualKey(in);

    if (debugger != null) {
      debug("keystroke: " + c + "");
    }

    // clear any echo characters
    clearEcho(c);

    return c;
  }
Пример #5
0
  /** Clear the echoed characters for the specified character code. */
  int clearEcho(int c) throws IOException {
    // if the terminal is not echoing, then just return...
    if (!terminal.getEcho()) {
      return 0;
    }

    // otherwise, clear
    int num = countEchoCharacters((char) c);
    back(num);
    drawBuffer(num);

    return num;
  }
Пример #6
0
  protected void stopTerminal() {
    try {
      TerminalFactory f = DefaultTerminalFactory.getInstance();
      Terminal t = f.getTerminal();
      t.stop();
    } catch (IOException ex) {
      // Log info:
      {
        String message = "Failure to stop terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    } catch (Throwable ex) {
      // Log info:
      {
        String message = "Failure to stop terminal!";
        log.log(Level.SEVERE, message, ex);
      }

      throw new RuntimeException(ex);
    }
  }
Пример #7
0
  /** Clear the screen by issuing the ANSI "clear screen" code. */
  public boolean clearScreen() throws IOException {
    if (!terminal.isANSISupported()) {
      return false;
    }

    // send the ANSI code to clear the screen
    printString(((char) 27) + "[2J");
    flushConsole();

    // then send the ANSI code to go to position 1,1
    printString(((char) 27) + "[1;1H");
    flushConsole();

    redrawLine();

    return true;
  }
Пример #8
0
  /**
   * Read a line from the <i>in</i> {@link InputStream}, and return the line (without any trailing
   * newlines).
   *
   * @param prompt the prompt to issue to the console, may be null.
   * @return a line that is read from the terminal, or null if there was null input (e.g.,
   *     <i>CTRL-D</i> was pressed).
   */
  public String readLine(final String prompt, final Character mask) throws IOException {
    this.mask = mask;
    if (prompt != null) this.prompt = prompt;

    try {
      terminal.beforeReadLine(this, this.prompt, mask);

      if ((this.prompt != null) && (this.prompt.length() > 0)) {
        out.write(this.prompt);
        out.flush();
      }

      // if the terminal is unsupported, just use plain-java reading
      if (!terminal.isSupported()) {
        return readLine(in);
      }

      while (true) {
        int[] next = readBinding();

        if (next == null) {
          return null;
        }

        int c = next[0];
        int code = next[1];

        if (c == -1) {
          return null;
        }

        boolean success = true;

        switch (code) {
          case EXIT: // ctrl-d
            if (buf.buffer.length() == 0) {
              return null;
            }

          case COMPLETE: // tab
            success = complete();
            break;

          case MOVE_TO_BEG:
            success = setCursorPosition(0);
            break;

          case KILL_LINE: // CTRL-K
            success = killLine();
            break;

          case CLEAR_SCREEN: // CTRL-L
            success = clearScreen();
            break;

          case KILL_LINE_PREV: // CTRL-U
            success = resetLine();
            break;

          case NEWLINE: // enter
            moveToEnd();
            printNewline(); // output newline
            return finishBuffer();

          case DELETE_PREV_CHAR: // backspace
            success = backspace();
            break;

          case DELETE_NEXT_CHAR: // delete
            success = deleteCurrentCharacter();
            break;

          case MOVE_TO_END:
            success = moveToEnd();
            break;

          case PREV_CHAR:
            success = moveCursor(-1) != 0;
            break;

          case NEXT_CHAR:
            success = moveCursor(1) != 0;
            break;

          case NEXT_HISTORY:
            success = moveHistory(true);
            break;

          case PREV_HISTORY:
            success = moveHistory(false);
            break;

          case REDISPLAY:
            break;

          case PASTE:
            success = paste();
            break;

          case DELETE_PREV_WORD:
            success = deletePreviousWord();
            break;

          case PREV_WORD:
            success = previousWord();
            break;

          case NEXT_WORD:
            success = nextWord();
            break;

          case START_OF_HISTORY:
            success = history.moveToFirstEntry();
            if (success) setBuffer(history.current());
            break;

          case END_OF_HISTORY:
            success = history.moveToLastEntry();
            if (success) setBuffer(history.current());
            break;

          case CLEAR_LINE:
            moveInternal(-(buf.buffer.length()));
            killLine();
            break;

          case INSERT:
            buf.setOvertyping(!buf.isOvertyping());
            break;

          case UNKNOWN:
          default:
            if (c != 0) { // ignore null chars
              ActionListener action =
                  (ActionListener) triggeredActions.get(new Character((char) c));
              if (action != null) action.actionPerformed(null);
              else putChar(c, true);
            } else success = false;
        }

        if (!(success)) {
          beep();
        }

        flushConsole();
      }
    } finally {
      terminal.afterReadLine(this, this.prompt, mask);
    }
  }
Пример #9
0
 /**
  * Query the terminal to find the current width;
  *
  * @see Terminal#getTerminalHeight
  * @return the height of the current terminal.
  */
 public int getTermheight() {
   return Terminal.setupTerminal().getTerminalHeight();
 }
Пример #10
0
 /**
  * Query the terminal to find the current width;
  *
  * @see Terminal#getTerminalWidth
  * @return the width of the current terminal.
  */
 public int getTermwidth() {
   return Terminal.setupTerminal().getTerminalWidth();
 }
Пример #11
0
  /**
   * Create a new reader.
   *
   * @param in the input
   * @param out the output
   * @param bindings the key bindings to use
   * @param term the terminal to use
   */
  public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
      throws IOException {
    this.terminal = term;
    setInput(in);
    this.out = out;

    if (bindings == null) {
      try {
        String bindingFile =
            System.getProperty(
                "jline.keybindings",
                new File(System.getProperty("user.home", ".jlinebindings.properties"))
                    .getAbsolutePath());

        if (new File(bindingFile).isFile()) {
          bindings = new FileInputStream(new File(bindingFile));
        }
      } catch (Exception e) {
        // swallow exceptions with option debugging
        if (debugger != null) {
          e.printStackTrace(debugger);
        }
      }
    }

    if (bindings == null) {
      bindings = terminal.getDefaultBindings();
    }

    this.keybindings = new short[Character.MAX_VALUE * 2];

    Arrays.fill(this.keybindings, UNKNOWN);

    /**
     * Loads the key bindings. Bindings file is in the format:
     *
     * <p>keycode: operation name
     */
    if (bindings != null) {
      Properties p = new Properties();
      p.load(bindings);
      bindings.close();

      for (Iterator i = p.keySet().iterator(); i.hasNext(); ) {
        String val = (String) i.next();

        try {
          Short code = new Short(val);
          String op = (String) p.getProperty(val);

          Short opval = (Short) KEYMAP_NAMES.get(op);

          if (opval != null) {
            keybindings[code.shortValue()] = opval.shortValue();
          }
        } catch (NumberFormatException nfe) {
          consumeException(nfe);
        }
      }

      // hardwired arrow key bindings
      // keybindings[VK_UP] = PREV_HISTORY;
      // keybindings[VK_DOWN] = NEXT_HISTORY;
      // keybindings[VK_LEFT] = PREV_CHAR;
      // keybindings[VK_RIGHT] = NEXT_CHAR;
    }
  }
Пример #12
0
 public ConsoleReader(final InputStream in, final Writer out, final InputStream bindings)
     throws IOException {
   this(in, out, bindings, Terminal.getTerminal());
 }