import java.io.*;
import java.util.*;
import com.novell.service.session.*;
import com.novell.service.session.util.Debug;
import com.novell.service.session.xplat.PersistenceService;
import com.novell.service.session.xplat.Version;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.spi.*;
import com.novell.java.lang.HasRootCause;
import com.novell.service.session.xplat.Xplat;
import com.novell.service.session.xplat.Address;
import com.novell.java.security.*;
import com.novell.service.security.Password;
import com.novell.service.security.PasswordIdentity;
import com.novell.service.security.PasswordIdentityFactory;
public class shell
{
public static final String helpStr = "SessionShell (c) 1997-1999 " +
"Novell, Inc.";
public static void main (String[] argv)
{
System.out.println (shell.helpStr);
boolean continueWStdIn = false;
boolean verbose = false;
boolean startDS = false;
String iniFile = "shell.ini";
int startArgs = 0;
for (int a = 0; a < argv.length; a++)
{
if (argv[a].startsWith ("-"))
{
String option = argv[a].substring (1);
if (option.equals ("verbose"))
verbose = true;
else if (option.equals ("ini"))
iniFile = argv[++a];
else if (option.toUpperCase ().equals ("H") ||
option.equals ("?"))
{
String[] helpStrs =
{
"usage: java shell [-verbose] [-ds] [<input> [output]]",
"",
" -verbose stack trace printed on all exceptions",
" -ini <file> specify file to retrieve settings from " +
"(default: -ini shell.ini)",
" input input file from which to read commands",
" output output file to which to write results",
"",
"If an input file is specified, but no output file,",
"then control is returned to the terminal after",
"executing all input commands.",
"",
"'#' and ';' are comment characters."
};
for (int x = 0; x < helpStrs.length; x++)
System.out.println (helpStrs[x]);
return;
}
else
{
System.out.println ("error: invalid option: " + option);
return;
}
startArgs = a + 1;
}
else
break;
}
Properties defaults = new Properties();
defaults.put(
SessionEnv.INITIAL_SESSION_FACTORY,
"com.novell.service.session.nds.NDSInitialSessionFactory:" +
"com.novell.service.session.bindery.BinderyInitialSessionFactory");
defaults.put(
SessionEnv.ALLOW_BACKGROUND_VALIDATION,
"false");
defaults.put(
"private",
"false");
defaults.put(
"debug.properties",
"shell.deb");
Properties props = new Properties (defaults);
File file = new File (iniFile);
if (file.exists ())
{
try
{
props.load (new FileInputStream (file));
}
catch (IOException e)
{
System.out.println ("error: reading shell startup properties");
}
}
InputStream in = System.in;
OutputStream out = System.out;
try
{
if (argv.length - startArgs > 0)
{
in = new FileInputStream (new File (argv[startArgs]));
continueWStdIn = true;
}
if (argv.length - startArgs > 1)
{
out = new FileOutputStream (new File (argv[startArgs + 1]));
continueWStdIn = false;
}
runShell (in, out, props, continueWStdIn, verbose, startDS);
}
catch (Throwable t)
{
System.out.println ("exception: " + t.toString ());
if (verbose)
{
Debug.dumpException(t);
}
}
// System.gc ();
System.exit (0);
}
public static void runShell (InputStream in, OutputStream out,
Properties props, boolean continueWStdIn, boolean verbose,
boolean startDS)
throws SessionException
{
Debug.readDebugProperties(
props.getProperty("debug.properties"));
Debug.writeDebugProperties(
props.getProperty("debug.properties"));
SessionShell shell;
SessionEnv env = new SessionEnv();
env.modify(props);
Boolean privateSM = new Boolean(props.getProperty("private"));
SessionManager sm;
if (true == privateSM.booleanValue())
sm = SessionManagerFactory.getPrivate(env);
else
sm = SessionManagerFactory.getSessionManager(env);
shell = new SessionShell (sm, in, out, continueWStdIn, verbose);
shell.addCommand ("load", new LoadCommand ());
shell.addCommand ("unload", new UnloadCommand ());
shell.addCommand ("dir", new DirCommand ());
shell.addCommand ("cd", new CdCommand ());
shell.addCommand ("find", new FindCommand ());
shell.addCommand ("attr", new AttrCommand ());
shell.addCommand ("debug", new DebugCommand ());
shell.addCommand ("final", new FinalizerCommand ());
shell.addCommand ("trace", new TraceCommand ());
shell.addCommand ("close", new CloseCommand ());
shell.addCommand ("keep", new KeepCommand ());
shell.addCommand ("update", new UpdateCommand ());
shell.addCommand ("loging", new LogingCommand ());
shell.addCommand ("login", new LoginCommand ());
shell.addCommand ("logoutg", new LogoutgCommand ());
shell.addCommand ("logout", new LogoutCommand ());
shell.addCommand ("pause", new PauseCommand ());
shell.addCommand ("shell", new ShellCommand ());
shell.addCommand ("exec", new ExecCommand ());
shell.addCommand ("v", new VerboseToggleCommand ());
shell.addCommand ("exit", new ExitCommand ());
shell.addCommand ("quit", new ExitCommand ());
shell.addCommand ("q", new ExitCommand ());
shell.addCommand ("help", new HelpCommand ());
shell.addCommand ("h", new HelpCommand ());
shell.addCommand ("?", new HelpCommand ());
shell.run ();
}
}
class LoadCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
if (argv.length < 2)
throw new ShellException ("error: command name not specified");
if (argv.length < 1)
throw new ShellException ("error: command class not specified");
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
Object obj = null;
try
{
obj = Class.forName (argv[0]).newInstance ();
}
catch (ClassNotFoundException e)
{
throw new ShellException ("error: class not found: " + argv[0]);
}
catch (Throwable e)
{
throw new ShellException ("error: could not instantiate class: " +
argv[0]);
}
if (!(obj instanceof Command))
{
throw new ShellException ("error: class is not a Command: " +
argv[0]);
}
shell.addCommand (argv[1], (Command) obj);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("Command sucessfully loaded and bound to '" +
argv[1] + "'");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <cmdclass> <cmdname>",
" cmdname: overwrites any command already bound to shell",
"Notes:",
"- Adds a command to the shell under the specified command name.",
"- The command class must implement the 'Command' interface.",
};
return (helpStrs);
}
}
class UnloadCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
if (argv.length < 1)
throw new ShellException ("error: command name not specified");
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Command oldCommand = shell.removeCommand (argv[0]);
if (oldCommand == null)
throw new ShellException ("error: no command bound to: " + argv[0]);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("Command sucessfully unbound");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <cmdname>",
"Notes:",
"- Removes a command from the shell."
};
return (helpStrs);
}
}
class DebugCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
if (1 <= argv.length)
{
if (argv[0].equalsIgnoreCase("on"))
Debug.setDebug(true);
else if (argv[0].equalsIgnoreCase("off"))
Debug.setDebug(false);
}
if (2 <= argv.length)
{
Debug.setDebug(true);
if (argv[1].equalsIgnoreCase("quiet"))
Debug.setQuiet(true);
if (argv[1].equalsIgnoreCase("loud"))
Debug.setQuiet(false);
}
if (!Debug.getDebug())
out.println("Debug: off");
else
out.println("Debug: on, " + (Debug.getQuiet() ? "quiet" : "loud"));
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [quiet|loud]",
" quiet: single-line debug messages",
" loud: include exception traces",
"Notes:",
"- Toggles debug mode and optionally sets quiet mode",
};
return (helpStrs);
}
}
class FinalizerCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
if (argv.length > 0)
throw new ShellException ("error: too many arguments");
System.runFinalization();
out.println("Running finalizers...");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Runs finalization",
};
return (helpStrs);
}
}
class TraceCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
Debug.traceInstructions(false);
Debug.traceMethods(false);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
boolean traceInstructions = false;
boolean traceMethods = false;
if (argv.length > 0)
{
if (argv[0].toLowerCase().indexOf('i') != -1)
traceInstructions = true;
if (argv[0].toLowerCase().indexOf('m') != -1)
traceMethods = true;
}
out.println(
"TI: " + (traceInstructions ? "on" : "off") + " " +
"TM: " + (traceMethods ? "on" : "off"));
Debug.traceInstructions(traceInstructions);
Debug.traceMethods(traceMethods);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [i|m]",
" i: instruction tracing on",
" m: method tracing on",
" : ",
"Notes:",
"- Turns on instruction and/or method tracing",
"- If either param is missing, it is turned off",
};
return (helpStrs);
}
}
class DirCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Dir:");
Hashtable args = new Hashtable ();
String path = "";
String name = null;
Vector attrIDVector = new Vector ();
boolean dump = false;
boolean refs = false;
String refAttr[] = {Xplat.CONN_CONNECTION_REFERENCE_ATTR_ID};
for (int i = 0; i < argv.length; i++)
{
String arg = argv[i];
if (arg.startsWith ("["))
{
}
else if (arg.equals ("-dump"))
dump = true;
else if (arg.equals ("-refs"))
refs = true;
else if (name == null)
name = arg;
else
throw new ShellException ("error: too many arguments");
}
String[] attrIDs;
if (attrIDVector.size () == 0)
attrIDs = null;
else
{
attrIDs = new String[attrIDVector.size ()];
for (int i = 0; i < attrIDs.length; i++)
attrIDs[i] = (String) attrIDVector.elementAt (i);
}
Session session = shell.getCurrSession();
if (name != null)
{
session = session.getSession(name);
out.println ("Name: " + name);
}
SessionAttrs as = new SessionAttrs();
SessionEnumerator enum = shell.getCurrSession().search(as);
while (enum.hasMoreElements())
{
Session found = enum.next();
StringBuffer output = new StringBuffer(found.getDomainName());
if (refs)
{
try
{
Integer ref =
(Integer)found.getAttributes(refAttr).getValue(refAttr[0]);
if (null != ref)
output.append(" : " + getString(ref));
}
catch (SessionAttrNotFoundException e)
{
}
}
out.println(output);
if (dump)
{
SessionAttrs attrs = found.getAttributes ();
SessionAttrEnumerator enuma = attrs.getAttributes ();
while (enuma.hasMoreElements ())
{
SessionAttr a = enuma.next();
out.println (" " + a.getSessionAttrId() +
" : " + getString(a.getValue()));
}
}
}
}
private String getString(Object object)
{
if (object instanceof Integer)
{
int i = ((Integer)object).intValue();
return "0x" + Integer.toHexString(i) + " (" + i + ")";
}
if (object instanceof Version)
{
Version v = (Version)object;
return
Long.toString (v.majorVersion) + "." +
Long.toString (v.minorVersion) + "." +
Long.toString (v.revision);
}
return object.toString();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [-refs] [-dump][[attrid,...]]",
" refs: print the requester's reference",
" dump: print attributes for each session",
" [attrid,...]: search criteria",
"Notes:",
"- List sessions below current session." };
return (helpStrs);
}
}
class UpdateCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Update");
shell.getCurrSession().validateLinks();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c ",
"Notes:",
"- Updates session links." };
return (helpStrs);
}
}
class CdCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
// throw new ShellException ("error: too many arguments");
if (0 == argv.length)
{
Session sm = shell.getCurrSession();
while (sm.hasParent())
sm = sm.getParent();
shell.setCurrSession(sm);
shell.setPrompt (sm.getDomainName());
}
else
{
Session newSession;
out.println("cd: " + argv[0]);
newSession = shell.getCurrSession();
if (argv[0].equals(".."))
{
if (newSession.hasParent())
newSession = shell.getCurrSession().getParent();
}
else
{
SessionEnv env = new SessionEnv();
if (argv.length > 1)
{
int type = Address.TYPE_WILD;
int radix = 10;
if (argv[1].equals("ipx"))
{
type = Address.TYPE_IPX;
radix = 16;
}
else if (argv[1].equals("ddp"))
type = Address.TYPE_DDP;
else if (argv[1].equals("asp"))
type = Address.TYPE_ASP;
else if (argv[1].equals("udp"))
type = Address.TYPE_UDP;
else if (argv[1].equals("tcp"))
type = Address.TYPE_TCP;
else if (argv[1].equals("udp6"))
type = Address.TYPE_UDP6;
else if (argv[1].equals("tcp6"))
type = Address.TYPE_TCP6;
byte addr[] = new byte[argv.length-2];
for (int i = 2; i < argv.length; i++)
{
addr[i-2] = (byte)Short.parseShort(argv[i], radix);
}
Address address = new Address(type, addr);
env.add(Xplat.DOMAIN_ADDRESS, address);
out.println("...using: " + address);
}
if ('\\' == argv[0].charAt(0))
{
out.println("...from top");
newSession = newSession.getSessionTop(argv[0].substring(1), env);
}
else
newSession = newSession.getSession(argv[0], env);
}
shell.setCurrSession(newSession);
shell.setPrompt(newSession.getDomainName());
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [[\\]domain [type address]]",
" domain: relative domain (getSession)'..' directives are allowed",
" \\domain: absolute domain (GetSessionTop)",
" type: ipx, ddp, asp, udp, tcp, udp6, tcp6",
" address: sequence of byte values for address",
"Notes:",
"- Changes current session to a session containing domain",
};
return (helpStrs);
}
}
class FindCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
// throw new ShellException ("error: too many arguments");
if (0 == argv.length)
{
Session sm = shell.getCurrSession();
while (sm.hasParent())
sm = sm.getParent();
shell.setCurrSession(sm);
shell.setPrompt (sm.getDomainName());
}
else
{
Session newSession;
out.println("cd: " + argv[0]);
newSession = shell.getCurrSession();
if (argv[0].equals(".."))
{
if (newSession.hasParent())
newSession = shell.getCurrSession().getParent();
}
else
{
if ('\\' == argv[0].charAt(0))
{
out.println("...from top");
newSession = newSession.findSessionTop(argv[0].substring(1));
}
else
newSession = newSession.findSession(argv[0]);
}
if (null != newSession)
{
shell.setCurrSession(newSession);
shell.setPrompt(newSession.getDomainName());
}
else
out.println("Session not found.");
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [[\\]domain]",
" domain: relative domain (findSession)'..' directives are allowed",
" \\domain: absolute domain (findSessionTop)",
"Notes:",
"- Changes current session to a session containing domain name",
};
return (helpStrs);
}
}
class CloseCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
if (argv.length > 0)
throw new ShellException ("error: too many arguments");
Session closingSession, currSession;
currSession = closingSession = shell.getCurrSession();
if (closingSession.hasParent())
{
currSession = closingSession.getParent();
shell.setCurrSession(currSession);
}
shell.setPrompt (currSession.getDomainName());
out.println("close");
closingSession.close();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Closes this session",
};
return (helpStrs);
}
}
class KeepCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
if (argv.length > 0)
throw new ShellException ("error: too many arguments");
Session currSession;
currSession = shell.getCurrSession();
PersistenceService s = (PersistenceService)currSession.getService(
PersistenceService.KEY);
out.println("keep");
s.persist();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Keeps this session at the system level",
};
return (helpStrs);
}
}
class AttrCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Attributes:");
String name = null;
Vector attrIDVector = new Vector ();
boolean ids = false;
boolean verbose = false;
for (int i = 0; i < argv.length; i++)
{
String arg = argv[i];
if (arg.startsWith ("["))
{
arg = arg.trim ();
if (!arg.endsWith ("]"))
{
throw new ShellException ("error: attr. ID list missing " +
"ending bracket");
}
arg = arg.substring (1, arg.length () - 1);
int commaPos = 0;
while (commaPos >= 0)
{
int savePos = commaPos;
commaPos = arg.indexOf (',', commaPos);
if (commaPos == -1)
attrIDVector.addElement (arg.substring (savePos));
else
{
attrIDVector.addElement (arg.substring (savePos,commaPos));
commaPos++;
}
}
}
else if (arg.equals ("-ids"))
ids = true;
else if (arg.equals ("-verbose"))
verbose = true;
else if (name == null)
name = arg;
else
throw new ShellException ("error: too many arguments");
}
String[] attrIDs;
if (attrIDVector.size () == 0)
attrIDs = null;
else
{
attrIDs = new String[attrIDVector.size ()];
for (int i = 0; i < attrIDs.length; i++)
attrIDs[i] = (String) attrIDVector.elementAt (i);
}
Session session = shell.getCurrSession();
if (name != null)
{
session = session.getSession(name);
out.println ("Name: " + name);
}
SessionAttrs attrs;
if (attrIDs == null)
attrs = session.getAttributes ();
else
attrs = session.getAttributes (attrIDs);
if (attrs == null)
throw new ShellException ("error: attribute set null for this " +
"Session");
if (ids)
{
Enumeration enum = attrs.getSessionAttrIds ();
while (enum.hasMoreElements ())
{
out.println (enum.nextElement() + ":");
}
}
else
{
SessionAttrEnumerator enum = attrs.getAttributes ();
while (enum.hasMoreElements ())
{
SessionAttr a = enum.next();
out.println (a.getSessionAttrId() + " : " + getString(a.getValue()));
}
}
if (verbose)
{
out.println ("verbose: size: " + attrs.count());
}
}
private String getString(Object object)
{
if (object instanceof Integer)
{
int i = ((Integer)object).intValue();
return "0x" + Integer.toHexString(i) + " (" + i + ")";
}
if (object instanceof Version)
{
Version v = (Version)object;
return
Long.toString (v.majorVersion) + "." +
Long.toString (v.minorVersion) + "." +
Long.toString (v.revision);
}
return object.toString();
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [name] [-ids] [-verbose] [[attrid,...]] ",
" ids: print just the IDs returned by Attribute.getIDs()",
" verbose: print info on Attributes instance returned",
" [attrid,...]: specify limited set of string attr. IDs",
"Notes:",
" Attribute returned by getAttributes() called with the",
" specified name."
};
return (helpStrs);
}
}
class LoginCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Login:");
if (argv.length > 2)
throw new ShellException ("error: too many arguments");
String userName = "";
String password = "";
if (argv.length > 0)
{
userName = argv[0];
if (argv.length > 1)
password = argv[1];
}
Session session = shell.getCurrSession();
if (session instanceof Authenticatable)
{
Identity id = session.createIdentity (userName);
if (id instanceof PasswordIdentityFactory)
{
id = (Identity) ((PasswordIdentityFactory) id).getPasswordIdentityInstance ();
((PasswordIdentity) id).setPassword (new Password (password));
}
Authenticator.login (id);
out.println("successful");
}
else
out.println("not available");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c user_name password",
" user name: user name to login as",
" password: password for user name",
"Notes:",
"- Attempts to login the session's domain by authenticating",
" the session via the authenticator.",
};
return (helpStrs);
}
}
class LogingCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Login:");
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
String userName;
if (argv.length > 0)
userName = argv[0];
else
userName = "";
Session session = shell.getCurrSession();
if (session instanceof Authenticatable)
{
Authenticator.login(session.createIdentity(userName));
out.println("successful");
}
else
out.println("not available");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c user_name",
" user name: user name to login as",
"Notes:",
"- Attempts to login the session's domain by authenticating",
" the session via the authenticator.",
};
return (helpStrs);
}
}
class PauseCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
// PrintWriter out = new PrintWriter (System.err, true);
if (argv.length > 0)
throw new ShellException ("error: too many arguments");
out.println("Pausing...hit enter to continue");
InputStreamReader in = new InputStreamReader(System.in);
in.read();
out.println("...done pausing.");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Pauses the shell",
};
return (helpStrs);
}
}
class LogoutCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Logout:");
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Session session = shell.getCurrSession();
if (session instanceof Authenticatable)
{
Identity id = session.createIdentity ("");
if (id instanceof PasswordIdentityFactory)
{
id = (Identity) ((PasswordIdentityFactory) id).getPasswordIdentityInstance ();
}
Authenticator.logout(id);
// s.unauthenticate();
out.println("successful");
}
else
out.println("not available");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Attempts to logout from session's domain by unauthenticating",
" the session via the authenticator.",
};
return (helpStrs);
}
}
class LogoutgCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println("Logout:");
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
Session session = shell.getCurrSession();
if (session instanceof Authenticatable)
{
Authenticatable s = session;
s.unauthenticate();
out.println("successful");
}
else
out.println("not available");
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Attempts to logout from session's domain by unauthenticating",
" the session via the authenticator.",
};
return (helpStrs);
}
}
class ShellCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
if (argv.length < 1)
throw new ShellException ("error: cmd. script must be specified");
InputStream in = null;
try
{
in = new FileInputStream (argv[0]);
}
catch (FileNotFoundException e)
{
throw new ShellException ("error: could not open file: " + argv[0]);
}
SessionShell child = new SessionShell (shell, null, in, null);
child.run ();
shell.clearCommands ();
String[] cmdNames = child.getCommandNames ();
for (int i = 0; i < cmdNames.length; i++)
shell.addCommand (cmdNames[i], child.getCommand (cmdNames[i]));
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <script>",
"Notes:",
"- Reads shell commands from the specified script file.",
"- The currently executing shell's environment is duplicated and",
" passed to a child shell.",
"- Output of child shell goes to output of current shell.",
"- Parent shell's command table set to child's at exit."
};
return (helpStrs);
}
}
class ExecCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
Process p = Runtime.getRuntime ().exec (argv);
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
int exitCode = p.waitFor ();
out.println ("External process exited: " + exitCode);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c <cmd>",
"Notes:",
"- All parameters passed to this command are passed to the local",
" OS as a command line parameter."
};
return (helpStrs);
}
}
class VerboseToggleCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
if (argv.length > 0)
{
PrintWriter out = new PrintWriter (shell.getOutputStream (), true);
out.println ("error: too many arguments");
}
shell.setVerbose (!shell.getVerbose ());
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Toggles the verbose flag in the current shell."
};
return (helpStrs);
}
}
class ExitCommand implements CommandWithHelp
{
public void execute (SessionShell shell, String[] argv)
throws Exception
{
throw new ShellException (true);
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c",
"Notes:",
"- Exits the current shell; any parent shell resumes control."
};
return (helpStrs);
}
}
class HelpCommand implements CommandWithHelp
{
String substituteCommandName (String cmdName, String syntax)
{
int cmdPos;
if ((cmdPos = syntax.indexOf ("%c")) != -1)
{
return (syntax.substring (0, cmdPos) + cmdName +
syntax.substring (cmdPos + 2));
}
else
return (syntax);
}
void sortStringArr (String[] arr)
{
for (int i = arr.length; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (arr[i-1].compareTo (arr[j]) < 0)
{
String temp = arr[j];
arr[j] = arr[i-1];
arr[i-1] = temp;
}
}
}
}
public void execute (SessionShell s, String[] argv)
throws Exception
{
if (argv.length > 1)
throw new ShellException ("error: too many arguments");
PrintWriter out = new PrintWriter (s.getOutputStream (), true);
if (argv.length == 0)
{
out.println (shell.helpStr);
String[] cmdNames = s.getCommandNames ();
String[] helpStrs;
Command cmd;
sortStringArr (cmdNames);
for (int i = 0; i < cmdNames.length; i++)
{
out.print (cmdNames[i] + ": ");
cmd = s.getCommand (cmdNames[i]);
if (cmd instanceof CommandWithHelp)
{
CommandWithHelp hcmd = (CommandWithHelp) cmd;
helpStrs = hcmd.getHelpStrs ();
if (helpStrs.length > 0)
{
out.print (substituteCommandName (cmdNames[i],
helpStrs[0]));
}
}
else
{
out.print ("no help available (" +
cmd.getClass ().getName () + ")");
}
out.println ();
}
}
else if (argv.length == 1)
{
Command cmd = s.getCommand (argv[0]);
if (cmd == null)
throw new ShellException ("error: command undefined: "+ argv[0]);
if (!(cmd instanceof CommandWithHelp))
throw new ShellException ("error: command does not support " +
"help: " + argv[0]);
CommandWithHelp hcmd = (CommandWithHelp) cmd;
String[] helpStrs = hcmd.getHelpStrs();
for (int i = 0; i < helpStrs.length; i++)
out.println (substituteCommandName (argv[0], helpStrs[i]));
}
}
public String[] getHelpStrs ()
{
String[] helpStrs =
{
"Syntax: %c [cmdname]",
" cmdname: name of command for which to display help",
"Notes:",
"- If executed with no parameters, displays syntax of all commands."
};
return (helpStrs);
}
}