⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandinterpreter.java

📁 It is the Speech recognition software. It is platform independent. To execute the source code,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 1999-2002 Carnegie Mellon University.   * Portions Copyright 2002 Sun Microsystems, Inc.   * Portions Copyright 2002 Mitsubishi Electric Research Laboratories. * All Rights Reserved.  Use is subject to license terms. *  * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL  * WARRANTIES. * */package edu.cmu.sphinx.util;import java.util.Map;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Iterator;import java.util.Set;import java.util.TreeSet;import java.util.regex.Pattern;import java.util.regex.Matcher;import java.util.regex.PatternSyntaxException;import java.io.BufferedReader;import java.io.BufferedReader;import java.io.PrintWriter;import java.io.InputStreamReader;import java.io.StreamTokenizer;import java.io.IOException;import java.io.FileReader;import java.io.StringReader;import java.net.Socket;import java.io.*;import java.util.*;/** * This class is a command interpreter. It reads strings from an * input stream, parses them into commands and executes them, results * are sent back on the output stream. * * @see CommandInterpreter */public class CommandInterpreter extends Thread {    private Map commandList;    private int totalCommands = 0;    private BufferedReader in;    private PrintWriter out;    private String prompt;    private boolean done = false;    private boolean trace = false;    private CommandHistory history = new CommandHistory();    private Socket socket = null;       /**     * Creates a command interpreter that reads/writes on the given    * streams.    *    * @param in	the input stream.    * @param out the output stream.    */    public  CommandInterpreter(BufferedReader in, PrintWriter out) {	init(in, out);    }   /**    * Sets the trace mode of the command interpreter.    *    *  @param trace 	true if tracing.    */    public void setTrace(boolean trace) {	this.trace = trace;    }   /**     * Creates a command interpreter that won't read a stream.    *    */    public CommandInterpreter() {	BufferedReader bin = 		new BufferedReader(new InputStreamReader(System.in));	PrintWriter pw = new PrintWriter(System.out);	init(bin, pw);    }    /**     * Initializes the CI     */    private  void init(BufferedReader in, PrintWriter out) {	commandList = new HashMap();	addStandardCommands();	setStreams(in, out);    }   /**    * Sets the I/O streams    * @param 	in	the input stream.    * @param	out	the output stream.    */    public void setStreams(BufferedReader in, PrintWriter out) {	this.in = in;	this.out = out;    }    /**     * Returns the Socket this CommandInterpreter uses.     */    public Socket getSocket() {	return socket;    }        /**     * Sets the Socket for this CommandInterpreter.     * @param    skt    the Socket this CommandInterpreter uses     */    public void setSocket(Socket skt) {	socket = skt;    }        /**     * Adds the set of standard commands     *     */    private void addStandardCommands() {	add("help", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		dumpCommands();		return "";	    }            public String getHelp() {                return "lists available commands";            }	});	add("history", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		history.dump();		return "";	    }            public String getHelp() {                return "shows command history";            }	});	add("status", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		putResponse("Total number of commands: " + totalCommands);		return "";	    }            public String getHelp() {                return "shows command status";            }	});	add("echo", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		StringBuffer b = new StringBuffer(80);		for (int i = 1; i < args.length; i++) {		   b.append(args[i]);		   b.append(" ");		}		putResponse(b.toString());		return "";	    }            public String getHelp() {                return "display a line of text";            }	});        if (false) {            add("argtest", new CommandInterface() {                public String execute(CommandInterpreter ci, String[] args) {                    StringBuffer b = new StringBuffer(80);                    System.out.println("arg length is " + args.length);                    for (int i = 0; i < args.length; i++) {                       b.append(args[i]);                       b.append("\n");                    }                    putResponse(b.toString());                    return "";                }                public String getHelp() {                    return "argument test";                }            });        }	add("quit", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		done = true;		return "";	    }            public String getHelp() {                return "exit the shell";            }	});	add("on_exit", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		return "";	    }            public String getHelp() {                return "command executed upon exit";            }	});	add("version", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		putResponse("Command Interpreter - Version 1.1 ");		return "";	    }            public String getHelp() {                return "displays version information";            }	});	add("gc", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {	        Runtime.getRuntime().gc();		return "";	    }            public String getHelp() {                return "performs garbage collection";            }	});	add("memory", new CommandInterface() {	     public String execute(CommandInterpreter ci, String[] args) {		long totalMem = Runtime.getRuntime().totalMemory();		long freeMem = Runtime.getRuntime().freeMemory();		putResponse("Free Memory  : "			 + freeMem / (1024.0 * 1024) + " mbytes");		putResponse("Total Memory : "			 + totalMem / (1024.0 * 1024) + " mbytes");		return ""; 	    }            public String getHelp() {                return "shows memory statistics";            }	 });	add("delay", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		if (args.length == 2) {		    try {			float seconds = Float.parseFloat(args[1]);			Thread.sleep((long) (seconds * 1000));		    }  catch (NumberFormatException nfe) {		        putResponse("Usage: delay time-in-seconds");		    }  catch (InterruptedException ie) {		    }		} else {		    putResponse("Usage: delay time-in-seconds");		}		return "";	    }            public String getHelp() {                return "pauses for a given number of seconds";            }	});	add("repeat", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		if (args.length >= 3) {		    try {			int count = Integer.parseInt(args[1]);			String[] subargs = new String[args.length -2];			System.arraycopy(args, 2, subargs, 0, subargs.length);                        for (int i = 0; i < count; i++) {                            putResponse(                               CommandInterpreter.this.execute(subargs));                        }		    } catch (NumberFormatException nfe) {		        putResponse("Usage: repeat count command args");		    }		} else {		    putResponse("Usage: repeat count command args");		}		return "";	    }            public String getHelp() {                return "repeatedly execute a command";            }	});	add("load", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		if (args.length == 2) {		    if (!load(args[1])) {		        putResponse("load: trouble loading " + args[1]);		    }		} else {		    putResponse("Usage: load filename");		}		return "";	    }            public String getHelp() {                return "load and execute commands from a file";            }	});	add("chain", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		if (args.length > 1) {		    String[] subargs= new String[args.length - 1];		    List commands = new ArrayList(5);		    int count = 0;		    for (int i = 1; i < args.length; i++) {			if (args[i].equals(";")) {			    if (count > 0) {				String[] trimmedArgs = new String[count];				System.arraycopy(subargs, 0, trimmedArgs, 				    0, trimmedArgs.length);				commands.add(trimmedArgs);				count = 0;			    }			}			else {			    subargs[count++] = args[i];			}		    }		    if (count > 0) {			String[] trimmedArgs = new String[count];			System.arraycopy(subargs, 0, trimmedArgs, 			    0, trimmedArgs.length);			commands.add(trimmedArgs);			count = 0;		    }		    for (Iterator i = commands.iterator(); i.hasNext(); ) {			putResponse(CommandInterpreter.this.execute(				(String[])i.next()));		    }		} else {		    putResponse("Usage: chain cmd1 ; cmd2 ; cmd3 ");		}		return "";	    }            public String getHelp() {                return "execute multiple commands on a single line";            }	});	add("time", new CommandInterface() {	    public String execute(CommandInterpreter ci, String[] args) {		if (args.length > 1) {		    String[] subargs = new String[args.length - 1];		    System.arraycopy(args, 1, subargs, 0, subargs.length);		    long startTime = System.currentTimeMillis();		    long endTime;		    putResponse(CommandInterpreter.this.execute(subargs));		    endTime = System.currentTimeMillis();		    putResponse("Time: "+ ((endTime - startTime) / 1000.0)		     + " seconds");		} else {		        putResponse("Usage: time cmd [args]");		}		return "";	    }            public String getHelp() {                return "report the time it takes to run a command";            }	});    }    /**     * Dumps the commands in the interpreter     *     */     private void dumpCommands() {	Set unsortedKeys = commandList.keySet();	Set sortedKeys = new TreeSet(unsortedKeys);	for (Iterator i = sortedKeys.iterator(); i.hasNext(); ) {	    String cmdName = (String) i.next();            String help = ((CommandInterface)                    commandList.get(cmdName)).getHelp();	    putResponse(cmdName + " - " + help);	}     }    /**     * Adds the given command to the command list.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -