/* Agent Tcl
   Bob Gray
   24 June 1995

   tcpipBasicCmd.cc

   This file implements the TCP/IP commands and the Tcpip_Init procedure.

   Copyright (c) 1995, Robert S. Gray Dartmouth College

   See the file "agent.terms" for information on usage and redistribution
   of this file and for a DISCLAIMER OF ALL WARRANTIES.
*/

#include "platPorting.h"
#include "mesgTcpip.h"
#include "genConversion.h"
#include "genUtility.h"
#include "tcl.h"
#include "tclParse.h"
#include "tcpipTcl.h"
#include "truefalse.h"

    /* forward declarations */

int Tcpip_GetipCmd 
	(ClientData dummy, Tcl_Interp *interp, int argc, char **argv);
 
    /* command info */

struct COMMAND_INFO
{
    char *name;
    Tcl_CmdProc *proc;  
};

    /* Tcl commands */

COMMAND_INFO tcpip_commands[] = 
{
    {"getip"		, Tcpip_GetipCmd       },
    { NULL 		, (Tcl_CmdProc *) NULL }
};

/* Tcpip_GetipCmd

   Tcl syntax: getip hostname [-time seconds]

      Purpose: Get the IP number of a host

        Input: dummy  = unused client data
			(ClientData)

               interp = the current interpreter
			(struct Tcl_Interp *)

               argc   = number of command arguments
			(int)

               argv   = the command arguments
			(char **)

       Output: The procedure returns TCL_ERROR and sets the interpreter
               result to an appropriate error message on error.  Otherwise
               the procedure returns TCL_OK and sets the interpreter result
               to the IP number in dotted decimal form.
*/

int Tcpip_GetipCmd (ClientData /*UNUSED*/, Tcl_Interp *interp, int argc, char **argv)
{
    UINT_32 ip;
    char *string;
    double seconds = DEFAULT_TCPIP_SECONDS;

	/* check the number of arguments */

    if ((argc != 2) && (argc != 4)) {
	Tcl_AppendResult (interp, "wrong # of args: should be \"", argv[0], " machine\"", (char *) NULL);
	Tcl_SetErrorCode (interp, "SYNTAX", (char *) NULL);
	return TCL_ERROR;
    }

	/* get the timeout if one was specified */

    if (argc == 4) {
	if (parseShortTimeout (interp, seconds, argc, argv, 2) < 0) {
	    return TCL_ERROR;
	}
    }

	/* get the IP number */

    if (Tcpip_Getip (interp, seconds, argv[1], &ip) != TCL_OK) {
	return TCL_ERROR;
    }

	/* set the intepreter result */

    if ((string = tcpip_IPToString (ip)) == NULL) {
	return TCL_ERROR;
    }

    Tcl_SetResult (interp, string, TCL_DYNAMIC);
    return TCL_OK;
}  

/* Tcpip_Init

   Purpose: Initialize the TCP/IP  TCL package

     Input: interp = the current interpreter
		     (struct Tcl_Interp *)

    Output: The procedure returns TCL_ERROR on initialization failure.
            The procedure returns TCL_OK    on initialization success.
*/
         
static void Tcpip_CommandInit (Tcl_Interp *interp)
{
    COMMAND_INFO *ptr;

	/* assertions */

    assert (interp != NULL);

	/* fill in the commands */

    for (ptr = tcpip_commands; ptr -> name != NULL; ptr++) {
	Tcl_CreateCommand (interp, ptr -> name, ptr -> proc, 
	    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
    }
}
    
int Tcpip_Init (Tcl_Interp *interp)
{
	/* assertions */

    assert (interp != NULL);

	/* get the master interpreter -- this could be NULL */

    Tcl_Interp *masterInterp = Tcl_GetMaster (interp);

	/* define the commands */

    Tcpip_CommandInit (interp);

    if (masterInterp != NULL) {
	Tcpip_CommandInit (masterInterp);
    }
 
    return TCL_OK;
}
