//============================================================================== // TestInterp.java //============================================================================== package tribble.net.ftp.shell; // System imports import java.io.FileReader; import java.io.InputStreamReader; import java.io.IOException; import java.io.Reader; import java.io.PrintWriter; import java.lang.Exception; import java.lang.String; import java.lang.System; import java.text.ParseException; /******************************************************************************* * Test driver for class {@link Interp}. * * * @version $Revision: 1.3 $ $Date: 2007/05/19 22:27:24 $ * @since API 1.0, 2007-03-16 * @author David R. Tribble (david@tribble.com). *

* Copyright ©2007 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated by * by the United States Department of State as a terrorist, or terrorist * government or agency, to use and distribute this source code provided * that the original copyright notice remains present and unaltered. */ abstract class TestInterp { // Identification /** Revision information. */ static final String REV = "@(#)tribble/net/ftp/shell/TestInterp.java $Revision: 1.3 $ $Date: 2007/05/19 22:27:24 $\n"; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static variables /** Standard output stream. */ private static PrintWriter s_stdout = new PrintWriter(System.out, true); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Static methods /*************************************************************************** * Test driver for class {@link Interp}. * *

* Usage *

* * java tribble.net.ftp.shell.TestInterp [-option...] file * * *

* Options: * *

*
-n *
Do not execute, parse only. * *

*

-p *
Display the parse tree. * *

*

-v *
Generate verbose output during execution. *
* *

* The file is a script containing FTP commands. * * @since 1.1, 2007-03-16 */ public static void main(String[] args) throws Exception { String fname = null; Reader in = null; boolean printTree = false; boolean verbose = false; boolean noExec = false; int i; // Check args for (i = 0; i < args.length && args[i].charAt(0) == '-'; i++) { if (args[i].equals("-")) break; else if (args[i].equals("-p")) printTree = true; else if (args[i].equals("-n")) noExec = true; else if (args[i].equals("-v")) verbose = true; else i = args.length; } // Check usage if (i >= args.length) { // Display a usage message s_stdout.println("Parse an FTP command script file."); s_stdout.println(); s_stdout.println("usage: java " + TestInterp.class.getName() + " [-p][-v] file"); System.exit(255); } try { CommandParser parser; CommandNode cmds; Interp interp; int rc; // Open the command input stream fname = args[i]; if (fname.equals("-")) in = new InputStreamReader(System.in); else in = new FileReader(fname); // Parse the command script file parser = new CommandParser(in, s_stdout); parser.setSourceName(fname); cmds = parser.parse(); if (parser.m_nErrors + parser.m_nWarnings > 0) { s_stdout.println(fname + ": Errors: " + parser.m_nErrors + ", warnings: " + parser.m_nWarnings); s_stdout.println(); s_stdout.flush(); } if (printTree) { // Print the resulting parse tree s_stdout.println("Parse tree:"); if (cmds != null) cmds.print(s_stdout); else s_stdout.println(""); s_stdout.println(); s_stdout.flush(); } // Execute (interpret) the parsed commands if (!noExec && cmds != null) { interp = new Interp(cmds, System.out, System.err); interp.m_verbose = verbose; rc = interp.execute(); s_stdout.println(); s_stdout.print("Commands executed: " + interp.m_nExecs); s_stdout.println(", exit status: " + rc); } else s_stdout.println("*** Execution cancelled"); } catch (ParseException ex) { s_stdout.println(fname + ":" + ex.getErrorOffset() + ": error: " + ex.getMessage()); s_stdout.flush(); //throw (ex); } finally { // Clean up if (in != null && !fname.equals("-")) in.close(); s_stdout.flush(); } } } // End TestInterp.java