All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class calypso.util.Shell

java.lang.Object
   |
   +----calypso.util.Shell

public class Shell
extends Object
implements Runnable
A class for easy implementation of command line interfaces. The Shell reads and parses command lines typed by the user (or read from any input stream) and executes the given commands. Commands must be pre-registered to the shell as ShellCommand implementations.

The shell is easily extended with inner classes:

 ...
 Shell sh = new Shell();
 sh.registerCommand("echo",
   new ShellCommand("echoes all given parameters")
   {
     public void execute(StringTokenizer parameters_, Shell sh_)
     {
       while (parameters_.hasMoreTokens())
       {
         sh_.getOut().print(parameters_.nextToken() + " ");
       }
       sh_.getOut().print("\n");
     }
   });
 sh.run();
 

Version:
$Reveision: $
Author:
Juhana Räsänen
See Also:
ShellCommand

Variable Index

 o DEF_CMDNAME
The default command name.
 o DEF_PROMPT
The default prompt.

Constructor Index

 o Shell()
Constructs a new shell that uses System.out and System.in as streams and default prompt.
 o Shell(boolean)
Constructs a new shell that uses System.out and System.in as streams and default prompt with current time.
 o Shell(InputStream, PrintStream)
Constructs a shell with given streams and the default prompt.
 o Shell(InputStream, PrintStream, String)
Constructs a new shell with given streams and prompt.
 o Shell(String)
Constructs a new shell with given prompt.
 o Shell(String, boolean)
Constructs a new shell with given prompt and current time.

Method Index

 o clone()
Makes a shallow copy of the Shell.
 o getIn()
Returns the input stream
 o getOut()
Returns the output stream
 o main(String[])
A main method for testing and executing simple scripts.
 o registerCommand(String, ShellCommand)
Registers a new command to the shell.
 o run()
The main loop of the shell.
 o setExitFlag(boolean)
Sets the exit status flag.
 o setPrompt(String)
Sets the prompt shown to the user.
 o unregisterCommand(String)
Unregisters a command so that the shell does not recognize it anymore.

Variables

 o DEF_CMDNAME
 public static final String DEF_CMDNAME
The default command name. The default command is run when the user gives a command that is not stored in the command table.

 o DEF_PROMPT
 public static final String DEF_PROMPT
The default prompt.

Constructors

 o Shell
 public Shell()
Constructs a new shell that uses System.out and System.in as streams and default prompt.

 o Shell
 public Shell(boolean showTime_)
Constructs a new shell that uses System.out and System.in as streams and default prompt with current time.

Parameters:
showTime_ - true if current time is to be shown
 o Shell
 public Shell(String prompt_)
Constructs a new shell with given prompt. System.in and System.out are used as the streams.

Parameters:
prompt_ - the user prompt
 o Shell
 public Shell(String prompt_,
              boolean showTime_)
Constructs a new shell with given prompt and current time. System.in and System.out are used as the streams.

Parameters:
prompt_ - the user prompt
showTime_ - true if current time is to be shown
 o Shell
 public Shell(InputStream is_,
              PrintStream os_)
Constructs a shell with given streams and the default prompt.

Parameters:
is_ - the input stream
os_ - the output stream
 o Shell
 public Shell(InputStream is_,
              PrintStream os_,
              String prompt_)
Constructs a new shell with given streams and prompt. This constructor is called eventually by the other constructors and it also creates the default shell commands (as anonymous inner classes).

Parameters:
is_ - the input stream
os_ - the output stream
prompt_ - the user prompt

Methods

 o clone
 public Object clone()
Makes a shallow copy of the Shell. The command table is copied using Hashtable.clone().

Returns:
The new copy of the Shell
Overrides:
clone in class Object
 o registerCommand
 public void registerCommand(String cmdname_,
                             ShellCommand cmd_)
Registers a new command to the shell. If there is an existing command with the given name, the previous command is discarded.

Parameters:
cmdname_ - the name of the command (must be a signle word)
cmd_ - the actual command implementation
 o unregisterCommand
 public void unregisterCommand(String cmdname_)
Unregisters a command so that the shell does not recognize it anymore.

Parameters:
cmdname_ - the name of the command
 o setExitFlag
 public void setExitFlag(boolean value_)
Sets the exit status flag. If set as true, the shell command loop will exit the next round

Parameters:
value_ - true or false
 o setPrompt
 public void setPrompt(String prompt_)
Sets the prompt shown to the user.

Parameters:
prompt_ - the new prompt string
 o getOut
 public PrintStream getOut()
Returns the output stream

Returns:
the output stream
 o getIn
 public InputStream getIn()
Returns the input stream

Returns:
the input stream
 o run
 public void run()
The main loop of the shell. Executes until the exitFlag becomes true. Prints user the command prompt, reads a new command line from the input stream, parses it and executes the command if found.

 o main
 public static void main(String argv[])
A main method for testing and executing simple scripts.

Parameters:
command - line parameters (not used)

All Packages  Class Hierarchy  This Package  Previous  Next  Index