key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
*/

package key.commands;

import key.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.Hashtable;

/**
  *  This command implements *basic* alias handling.  Ultimately
  *  I'd like shell style substitution, with %1, %2 and all of
  *  those handled correctly to remap badly placed words and
  *  so forth.
  *  <p>
  *  Maybe consider command combinations, too?  Or the ability
  *  to put inline \n's somehow...
  *  <p>
  *  Going any *further* than that very basic, simple script
  *  requires java code, of course.
 */
public class Alias extends Command
{
	public static final AtomicElement[] ELEMENTS =
	{
		AtomicElement.construct( Alias.class, String.class, "command",
			AtomicElement.PUBLIC_FIELD,
			"the command to be executed by the alias (use %m for user args) (can be pipe seperated)" )
	};
	
	public static final AtomicStructure STRUCTURE = new AtomicStructure( Command.STRUCTURE, ELEMENTS );
	
	public static final char messageCode = 'm';
	
	public String command = "";
	
	public Alias()
	{
		setKey( "alias" );
		usage = "";
	}
	
	public AtomicStructure getDeclaredStructure()
	{
		return( STRUCTURE );
	}
	
	public String getWhichId()
	{
		return( getId() + " (command alias for '" + command + "')" );
	}
	
	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		if( command == null || command.length() == 0 )
		{
			ic.sendError( "This alias is incorrectly set up" );
			return;
		}
		
		if( args.hasMoreTokens() )
			p.putCode( messageCode, args.nextToken( "" ) );
		else
			p.putCode( messageCode, "" );
		
		StringTokenizer st = new StringTokenizer( command, "|" );
		
		while( st.hasMoreTokens() )
			p.command( Grammar.substitute( st.nextToken(), p.getCodes() ), ic, false );
	}
}