org.gnu.readline

Class Readline

public class Readline extends Object

This class implements basic functionality of the GNU-readline interface. It uses native method calls if available, otherwise it defaults to normal I/O using a BufferedReader.

A typical implementation could look like:

 try {
     Readline.load(ReadlineLibrary.GnuReadline);
 }
 catch (UnsatisfiedLinkError ignore_me) {
     System.err.println("couldn't load readline lib. Using simple stdin.");
 }

 Readline.initReadline("myapp");

 Runtime.getRuntime()                       // if your version supports
   .addShutdownHook(new Thread() {          // addShutdownHook (since 1.3)
      public void run() {
        Readline.cleanup();
      }
    });

 while (true) {
     try {
         line = Readline.readline("myprompt> ");
         if (line == null)
             System.out.println("no input");
         else
             processLine();
     } 
     catch (EOFException e) {
         break;
     } 
     catch (Exception e) {
         doSomething();
     }
 }
 Readline.cleanup();  // see note above about addShutdownHook

Version: $Revision: 1.31 $

Author: $Author: bablokb $

Field Summary
static ReadlineConstIntHISTORY_BASE
Constant to access history_base using getVar() or setVar().
static ReadlineConstIntHISTORY_LENGTH
Constant to access history_length using getVar() or setVar().
static ReadlineConstIntHISTORY_MAX_ENTRIES
Constant to access history_max_entries using getVar() or setVar().
static ReadlineConstStringHISTORY_NO_EXPAND_CHARS
Constant to access history_no_expand_chars using getVar() or setVar().
static ReadlineConstIntHISTORY_QUOTES_EXPANSION
Constant to access history_quotes_inhibit_expansion using getVar() or setVar().
static ReadlineConstStringHISTORY_SEARCH_DELIMITERS
Constant to access history_search_delimiter_chars using getVar() or setVar().
static ReadlineConstStringHISTORY_WORD_DELIMITERS
Constant to access history_word_delimiters using getVar() or setVar().
static ReadlineConstIntRL_ALREADY_PROMPTED
Constant to access rl_already_prompted using getVar() or setVar().
static ReadlineConstIntRL_ATTEMPTED_COMPLETION_OVER
Constant to access rl_attempted_completion_over using getVar() or setVar().
static ReadlineConstStringRL_BASIC_QUOTE_CHARACTERS
Constant to access rl_basic_quote_characters using getVar() or setVar().
static ReadlineConstStringRL_BASIC_WORD_BREAK_CHARACTERS
Constant to access rl_basic_word_break_characters using getVar() or setVar().
static ReadlineConstIntRL_CATCH_SIGNALS
Constant to access rl_catch_signals using getVar() or setVar().
static ReadlineConstIntRL_CATCH_SIGWINCH
Constant to access rl_catch_sigwinch using getVar() or setVar().
static ReadlineConstStringRL_COMPLETER_QUOTE_CHARACTERS
Constant to access rl_completer_quote_characters using getVar() or setVar().
static ReadlineConstStringRL_COMPLETER_WORD_BREAK_CHARACTERS
Constant to access rl_completer_word_break_characters using getVar() or setVar().
static ReadlineConstIntRL_COMPLETION_APPEND_CHARACTER
Constant to access rl_completion_append_character using getVar() or setVar().
static ReadlineConstIntRL_COMPLETION_MARK_SYMLINK_DIRS
Constant to access rl_completion_mark_symlink_dirs using getVar() or setVar().
static ReadlineConstIntRL_COMPLETION_QUERY_ITEMS
Constant to access rl_completion_query_items using getVar() or setVar().
static ReadlineConstIntRL_COMPLETION_SUPPRESS_APPEND
Constant to access rl_completion_suppress_append using getVar() or setVar().
static ReadlineConstIntRL_COMPLETION_TYPE
Constant to access rl_completion_type using getVar() or setVar().
static ReadlineConstIntRL_DISPATCHING
Constant to access rl_dispatching using getVar() or setVar().
static ReadlineConstIntRL_DONE
Constant to access rl_done using getVar() or setVar().
static ReadlineConstIntRL_EDITING_MODE
Constant to access rl_editing_mode using getVar() or setVar().
static ReadlineConstIntRL_END
Constant to access rl_end using getVar() or setVar().
static ReadlineConstIntRL_ERASE_EMPTY_LINE
Constant to access rl_erase_empty_line using getVar() or setVar().
static ReadlineConstStringRL_EXECUTING_MACRO
Constant to access rl_executing_macro using getVar() or setVar().
static ReadlineConstIntRL_EXPLICIT_ARG
Constant to access rl_explicit_arg using getVar() or setVar().
static ReadlineConstIntRL_FILENAME_COMPLETION_DESIRED
Constant to access rl_filename_completion_desired using getVar() or setVar().
static ReadlineConstStringRL_FILENAME_QUOTE_CHARACTERS
Constant to access rl_filename_quote_characters using getVar() or setVar().
static ReadlineConstIntRL_FILENAME_QUOTING_DESIRED
Constant to access rl_filename_quoting_desired using getVar() or setVar().
static ReadlineConstIntRL_GNU_READLINE_P
Constant to access rl_gnu_readline_p using getVar() or setVar().
static ReadlineConstIntRL_IGNORE_COMPLETION_DUPLICATES
Constant to access rl_ignore_completion_duplicates using getVar() or setVar().
static ReadlineConstIntRL_INHIBIT_COMPLETION
Constant to access rl_inhibit_completion using getVar() or setVar().
static ReadlineConstIntRL_INSERT_MODE
Constant to access rl_insert_mode using getVar() or setVar().
static ReadlineConstStringRL_LIBRARY_VERSION
Constant to access rl_library_version using getVar() or setVar().
static ReadlineConstStringRL_LINE_BUFFER
Constant to access rl_line_buffer using getVar() or setVar().
static ReadlineConstIntRL_MARK
Constant to access rl_mark using getVar() or setVar().
static ReadlineConstIntRL_NUMERIC_ARG
Constant to access rl_numeric_arg using getVar() or setVar().
static ReadlineConstIntRL_NUM_CHARS_TO_READ
Constant to access rl_num_chars_to_read using getVar() or setVar().
static ReadlineConstIntRL_PENDING_INPUT
Constant to access rl_pending_input using getVar() or setVar().
static ReadlineConstIntRL_POINT
Constant to access rl_point using getVar() or setVar().
static ReadlineConstStringRL_PROMPT
Constant to access rl_prompt using getVar() or setVar().
static ReadlineConstStringRL_READLINE_NAME
Constant to access rl_readline_name using getVar() or setVar().
static ReadlineConstIntRL_READLINE_STATE
Constant to access rl_readline_state using getVar() or setVar().
static ReadlineConstIntRL_READLINE_VERSION
Constant to access rl_readline_version using getVar() or setVar().
static ReadlineConstStringRL_SPECIAL_PREFIXES
Constant to access rl_special_prefixes using getVar() or setVar().
static ReadlineConstStringRL_TERMINAL_NAME
Constant to access rl_terminal_name using getVar() or setVar().
Method Summary
static voidaddToHistory(String line)
Add a line to the in-memory history.
static voidcleanup()
Reset the readline library and with it, the terminal.
static voidclearHistory()
Clear the history buffer.
static ReadlineCompletergetCompleter()
Query current completer function.
static StringgetEncoding()
Query current encoding of fallback BufferedReader.
static voidgetHistory(Collection collection)
Get the history buffer in a supplied Collection.
static StringgetHistoryLine(int i)
Get the specified entry from the history buffer.
static intgetHistorySize()
Get the size, in elements (lines), of the history buffer.
static StringgetLineBuffer()
Query the current line buffer.
static booleangetThrowExceptionOnUnsupportedMethod()
Query behavior in case an unsupported method is called.
static intgetVar(ReadlineConstInt c)
Query integer readline-variable.
static StringgetVar(ReadlineConstString c)
Query string readline-variable.
static StringgetWordBreakCharacters()
Query word break characters.
static booleanhasTerminal()
Return if we have a terminal.
static voidinitReadline(String applicationName)
Initialize the GNU-Readline library.
static voidload(ReadlineLibrary lib)
Load an implementing backing library.
static booleanparseAndBind(String line)
Parse argument string as if it had been read from `inputrc' file and perform key bindings and variable assignments found.
static voidreadHistoryFile(String filename)
Reads a history file into memory

Supporting implementations:

  • GNU-Readline
  • Editline

static voidreadInitFile(String filename)
Read keybindings and variable assignments from a file.
static Stringreadline(String prompt)
Display a prompt on standard output and read a string from standard input.
static Stringreadline(String prompt, boolean addToHist)
Display a prompt on standard output and read a string from standard input.
static voidsetCompleter(ReadlineCompleter rlc)
Set your completer implementation.
static voidsetEncoding(String encoding)
Set current encoding of fallback BufferedReader.
static voidsetThrowExceptionOnUnsupportedMethod(boolean flag)
Configure behavior in case an unsupported method is called.
static intsetVar(ReadlineConstInt c, int value)
Set integer readline-variable.
static StringsetVar(ReadlineConstString c, String value)
Set string readline-variable.
static voidsetWordBreakCharacters(String wordBreakCharacters)
Set word break characters.
static voidwriteHistoryFile(String filename)
Writes a history file to disc

Supporting implementations:

  • GNU-Readline
  • Editline

Field Detail

HISTORY_BASE

public static final ReadlineConstInt HISTORY_BASE
Constant to access history_base using getVar() or setVar().

Supporting implementations:

HISTORY_LENGTH

public static final ReadlineConstInt HISTORY_LENGTH
Constant to access history_length using getVar() or setVar().

Supporting implementations:

HISTORY_MAX_ENTRIES

public static final ReadlineConstInt HISTORY_MAX_ENTRIES
Constant to access history_max_entries using getVar() or setVar().

Supporting implementations:

HISTORY_NO_EXPAND_CHARS

public static final ReadlineConstString HISTORY_NO_EXPAND_CHARS
Constant to access history_no_expand_chars using getVar() or setVar().

Supporting implementations:

HISTORY_QUOTES_EXPANSION

public static final ReadlineConstInt HISTORY_QUOTES_EXPANSION
Constant to access history_quotes_inhibit_expansion using getVar() or setVar().

Supporting implementations:

HISTORY_SEARCH_DELIMITERS

public static final ReadlineConstString HISTORY_SEARCH_DELIMITERS
Constant to access history_search_delimiter_chars using getVar() or setVar().

Supporting implementations:

HISTORY_WORD_DELIMITERS

public static final ReadlineConstString HISTORY_WORD_DELIMITERS
Constant to access history_word_delimiters using getVar() or setVar().

Supporting implementations:

RL_ALREADY_PROMPTED

public static final ReadlineConstInt RL_ALREADY_PROMPTED
Constant to access rl_already_prompted using getVar() or setVar().

Supporting implementations:

RL_ATTEMPTED_COMPLETION_OVER

public static final ReadlineConstInt RL_ATTEMPTED_COMPLETION_OVER
Constant to access rl_attempted_completion_over using getVar() or setVar().

Supporting implementations:

RL_BASIC_QUOTE_CHARACTERS

public static final ReadlineConstString RL_BASIC_QUOTE_CHARACTERS
Constant to access rl_basic_quote_characters using getVar() or setVar().

Supporting implementations:

RL_BASIC_WORD_BREAK_CHARACTERS

public static final ReadlineConstString RL_BASIC_WORD_BREAK_CHARACTERS
Constant to access rl_basic_word_break_characters using getVar() or setVar().

Supporting implementations:

RL_CATCH_SIGNALS

public static final ReadlineConstInt RL_CATCH_SIGNALS
Constant to access rl_catch_signals using getVar() or setVar().

Supporting implementations:

RL_CATCH_SIGWINCH

public static final ReadlineConstInt RL_CATCH_SIGWINCH
Constant to access rl_catch_sigwinch using getVar() or setVar().

Supporting implementations:

RL_COMPLETER_QUOTE_CHARACTERS

public static final ReadlineConstString RL_COMPLETER_QUOTE_CHARACTERS
Constant to access rl_completer_quote_characters using getVar() or setVar().

Supporting implementations:

RL_COMPLETER_WORD_BREAK_CHARACTERS

public static final ReadlineConstString RL_COMPLETER_WORD_BREAK_CHARACTERS
Constant to access rl_completer_word_break_characters using getVar() or setVar().

Supporting implementations:

RL_COMPLETION_APPEND_CHARACTER

public static final ReadlineConstInt RL_COMPLETION_APPEND_CHARACTER
Constant to access rl_completion_append_character using getVar() or setVar().

Supporting implementations:

RL_COMPLETION_MARK_SYMLINK_DIRS

public static final ReadlineConstInt RL_COMPLETION_MARK_SYMLINK_DIRS
Constant to access rl_completion_mark_symlink_dirs using getVar() or setVar().

Supporting implementations:

RL_COMPLETION_QUERY_ITEMS

public static final ReadlineConstInt RL_COMPLETION_QUERY_ITEMS
Constant to access rl_completion_query_items using getVar() or setVar().

Supporting implementations:

RL_COMPLETION_SUPPRESS_APPEND

public static final ReadlineConstInt RL_COMPLETION_SUPPRESS_APPEND
Constant to access rl_completion_suppress_append using getVar() or setVar().

Supporting implementations:

RL_COMPLETION_TYPE

public static final ReadlineConstInt RL_COMPLETION_TYPE
Constant to access rl_completion_type using getVar() or setVar().

Supporting implementations:

RL_DISPATCHING

public static final ReadlineConstInt RL_DISPATCHING
Constant to access rl_dispatching using getVar() or setVar().

Supporting implementations:

RL_DONE

public static final ReadlineConstInt RL_DONE
Constant to access rl_done using getVar() or setVar().

Supporting implementations:

RL_EDITING_MODE

public static final ReadlineConstInt RL_EDITING_MODE
Constant to access rl_editing_mode using getVar() or setVar().

Supporting implementations:

RL_END

public static final ReadlineConstInt RL_END
Constant to access rl_end using getVar() or setVar().

Supporting implementations:

RL_ERASE_EMPTY_LINE

public static final ReadlineConstInt RL_ERASE_EMPTY_LINE
Constant to access rl_erase_empty_line using getVar() or setVar().

Supporting implementations:

RL_EXECUTING_MACRO

public static final ReadlineConstString RL_EXECUTING_MACRO
Constant to access rl_executing_macro using getVar() or setVar().

Supporting implementations:

RL_EXPLICIT_ARG

public static final ReadlineConstInt RL_EXPLICIT_ARG
Constant to access rl_explicit_arg using getVar() or setVar().

Supporting implementations:

RL_FILENAME_COMPLETION_DESIRED

public static final ReadlineConstInt RL_FILENAME_COMPLETION_DESIRED
Constant to access rl_filename_completion_desired using getVar() or setVar().

Supporting implementations:

RL_FILENAME_QUOTE_CHARACTERS

public static final ReadlineConstString RL_FILENAME_QUOTE_CHARACTERS
Constant to access rl_filename_quote_characters using getVar() or setVar().

Supporting implementations:

RL_FILENAME_QUOTING_DESIRED

public static final ReadlineConstInt RL_FILENAME_QUOTING_DESIRED
Constant to access rl_filename_quoting_desired using getVar() or setVar().

Supporting implementations:

RL_GNU_READLINE_P

public static final ReadlineConstInt RL_GNU_READLINE_P
Constant to access rl_gnu_readline_p using getVar() or setVar().

Supporting implementations:

RL_IGNORE_COMPLETION_DUPLICATES

public static final ReadlineConstInt RL_IGNORE_COMPLETION_DUPLICATES
Constant to access rl_ignore_completion_duplicates using getVar() or setVar().

Supporting implementations:

RL_INHIBIT_COMPLETION

public static final ReadlineConstInt RL_INHIBIT_COMPLETION
Constant to access rl_inhibit_completion using getVar() or setVar().

Supporting implementations:

RL_INSERT_MODE

public static final ReadlineConstInt RL_INSERT_MODE
Constant to access rl_insert_mode using getVar() or setVar().

Supporting implementations:

RL_LIBRARY_VERSION

public static final ReadlineConstString RL_LIBRARY_VERSION
Constant to access rl_library_version using getVar() or setVar().

Supporting implementations:

RL_LINE_BUFFER

public static final ReadlineConstString RL_LINE_BUFFER
Constant to access rl_line_buffer using getVar() or setVar().

Supporting implementations:

RL_MARK

public static final ReadlineConstInt RL_MARK
Constant to access rl_mark using getVar() or setVar().

Supporting implementations:

RL_NUMERIC_ARG

public static final ReadlineConstInt RL_NUMERIC_ARG
Constant to access rl_numeric_arg using getVar() or setVar().

Supporting implementations:

RL_NUM_CHARS_TO_READ

public static final ReadlineConstInt RL_NUM_CHARS_TO_READ
Constant to access rl_num_chars_to_read using getVar() or setVar().

Supporting implementations:

RL_PENDING_INPUT

public static final ReadlineConstInt RL_PENDING_INPUT
Constant to access rl_pending_input using getVar() or setVar().

Supporting implementations:

RL_POINT

public static final ReadlineConstInt RL_POINT
Constant to access rl_point using getVar() or setVar().

Supporting implementations:

RL_PROMPT

public static final ReadlineConstString RL_PROMPT
Constant to access rl_prompt using getVar() or setVar().

Supporting implementations:

RL_READLINE_NAME

public static final ReadlineConstString RL_READLINE_NAME
Constant to access rl_readline_name using getVar() or setVar().

Supporting implementations:

RL_READLINE_STATE

public static final ReadlineConstInt RL_READLINE_STATE
Constant to access rl_readline_state using getVar() or setVar().

Supporting implementations:

RL_READLINE_VERSION

public static final ReadlineConstInt RL_READLINE_VERSION
Constant to access rl_readline_version using getVar() or setVar().

Supporting implementations:

RL_SPECIAL_PREFIXES

public static final ReadlineConstString RL_SPECIAL_PREFIXES
Constant to access rl_special_prefixes using getVar() or setVar().

Supporting implementations:

RL_TERMINAL_NAME

public static final ReadlineConstString RL_TERMINAL_NAME
Constant to access rl_terminal_name using getVar() or setVar().

Supporting implementations:

Method Detail

addToHistory

public static void addToHistory(String line)
Add a line to the in-memory history.

Supporting implementations:

Parameters: line The line to add to the history

Throws: UnsupportOperationException if underlying library doesn't support a history

cleanup

public static void cleanup()
Reset the readline library and with it, the terminal.

Supporting implementations:

clearHistory

public static void clearHistory()
Clear the history buffer.

Supporting implementations:

getCompleter

public static ReadlineCompleter getCompleter()
Query current completer function.

Returns: Current ReadlineCompleter object

getEncoding

public static String getEncoding()
Query current encoding of fallback BufferedReader.

Returns: current encoding

getHistory

public static void getHistory(Collection collection)
Get the history buffer in a supplied Collection.

Supporting implementations:

Parameters: collection where to store the history

Throws: UnsupportOperationException if underlying library doesn't support a history

getHistoryLine

public static String getHistoryLine(int i)
Get the specified entry from the history buffer. History buffer entries are numbered from 0 through (getHistorySize() - 1), with the oldest entry at index 0.

Supporting implementations:

Parameters: i the index of the entry to return

Returns: the line at the specified index in the history buffer

Throws: ArrayIndexOutOfBoundsException index out of range

getHistorySize

public static int getHistorySize()
Get the size, in elements (lines), of the history buffer.

Supporting implementations:

Returns: the number of lines in the history buffer

getLineBuffer

public static String getLineBuffer()
Query the current line buffer. This returns the current content of the internal line buffer. You might need this in a ReadlineCompleter implementation to access the full text given so far.

Supporting implementations:

getThrowExceptionOnUnsupportedMethod

public static boolean getThrowExceptionOnUnsupportedMethod()
Query behavior in case an unsupported method is called.

Returns: configuration flag

getVar

public static int getVar(ReadlineConstInt c)
Query integer readline-variable.

Parameters: c symbolic constant of readline-variable

Returns: value of variable

getVar

public static String getVar(ReadlineConstString c)
Query string readline-variable.

Parameters: c symbolic constant of readline-variable

Returns: value of variable

getWordBreakCharacters

public static String getWordBreakCharacters()
Query word break characters.

Supporting implementations:

hasTerminal

public static boolean hasTerminal()
Return if we have a terminal. This requires, that any of the native libraries have been loaded yet (so call Readline.load()) first, otherwise this will always return true.

Supporting implementations:

initReadline

public static void initReadline(String applicationName)
Initialize the GNU-Readline library. This will also set the application name, which can be used in the initialization files of Readline (usually /etc/inputrc and ~/.inputrc) to define application specific keys. See the file ReadlineTest.java int the test subdir of the distribution for an example.

Supporting implementations:

Parameters: applicationName Name of application in initialization file

load

public static final void load(ReadlineLibrary lib)
Load an implementing backing library. This method might throw an UnsatisfiedLinkError in case the native libary is not found in the library path. If you want to have a portable program, just catch and ignore that error. JavaReadline will then just use the pure Java fallback solution.

Parameters: lib An object (constant) of type ReadlineLibrary

Throws: UnsatisfiedLinkError if the shared library could not be found. Add it to your LD_LIBRARY_PATH.

See Also: ReadlineLibrary

parseAndBind

public static boolean parseAndBind(String line)
Parse argument string as if it had been read from `inputrc' file and perform key bindings and variable assignments found.

Supporting implementations:

Parameters: line Simulated line from inputrc file

Returns: boolean False in case of error

readHistoryFile

public static void readHistoryFile(String filename)
Reads a history file into memory

Supporting implementations:

Parameters: filename Name of history file to read

readInitFile

public static void readInitFile(String filename)
Read keybindings and variable assignments from a file. This method is a wrapper to rl_read_init_file(char *filename). Throws IOException if something goes wrong.

Supporting implementations:

Parameters: filename Name of file to read bindings from

Returns: void

readline

public static String readline(String prompt)
Display a prompt on standard output and read a string from standard input. This method returns 'null', if there has been an empty input (user pressed just [RETURN]) and throws an EOFException on end-of-file (C-d). This versino of readline() automatically adds the line to the in-memory history; use the other version of readline() if you want explicit control over that feature.

Supporting implementations:

Parameters: prompt Prompt to display

Returns: The string the user entered or 'null' if there was no input.

Throws: EOFException on end-of-file, i.e. CTRL-d input.

See Also: Readline Readline

readline

public static String readline(String prompt, boolean addToHist)
Display a prompt on standard output and read a string from standard input. This method returns 'null', if there has been an empty input (user pressed just [RETURN]) and throws an EOFException on end-of-file (C-d).

Parameters: prompt Prompt to display addToHist true to add the line to the history automatically; false to refrain from adding the line to the history. (You can manually add the line to the history by calling addHistory().)

Returns: The string the user entered or 'null' if there was no input.

Throws: EOFException on end-of-file, i.e. CTRL-d input.

See Also: readline Readline

setCompleter

public static void setCompleter(ReadlineCompleter rlc)
Set your completer implementation. Setting this to null will result in the default behaviour of readline which is filename completion.

Supporting implementations:

Parameters: rlc An object implementing the ReadlineCompleter interface

setEncoding

public static void setEncoding(String encoding)
Set current encoding of fallback BufferedReader.

Parameters: encoding encoding to use

setThrowExceptionOnUnsupportedMethod

public static void setThrowExceptionOnUnsupportedMethod(boolean flag)
Configure behavior in case an unsupported method is called. If argument is true, unsupported methods throw an UnsupportedOperationException.

Parameters: flag configuration flag

setVar

public static int setVar(ReadlineConstInt c, int value)
Set integer readline-variable.

Parameters: c symbolic constant of readline-variable value new value of readline-variable

Returns: old value of readline-variable

setVar

public static String setVar(ReadlineConstString c, String value)
Set string readline-variable.

Parameters: c symbolic constant of readline-variable value new value of readline-variable

Returns: old value of readline-variable

setWordBreakCharacters

public static void setWordBreakCharacters(String wordBreakCharacters)
Set word break characters.

Supporting implementations:

Parameters: wordBreakCharacters A string of word break characters

writeHistoryFile

public static void writeHistoryFile(String filename)
Writes a history file to disc

Supporting implementations:

Parameters: filename Name of history file to write

Released under the LGPL, (c) Bernhard Bablok, Henner Zeller 1998-2002
Homepage: http://java-readline.sourceforge.net/