/* Agent Tcl
   Bob Gray
   2 August 1995 

   tclTimeCmd.cc

   This file implements the Tcl command that puts an agent to sleep.

   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 "tcl.h"
#include "agentTcl.h"
#include "agentTclInt.h"

/* Agent_SleepCmd

   Tcl syntax: agent_sleep seconds

      Purpose: Sleep for a certain of seconds 

        Input: dummy  = client data (unused)
               interp = the current interpreter
               argc   = the number of command arguments
               argv   = the command arguments

       Output: The procedure returns TCL_ERROR and sets the interpreter result
               to an appropriate error message on error.  Otherwise the 
               procedure tries to sleep for the specified number of seconds.
               If no interrupts occur, the procedure sleeps for the full time,
               returns TCL_OK and sets the interpreter result to 0.  If an
               interrupt occurs, the procedure returns TCL_OK and sets the
               interpreter result to the number of seconds left to sleep.
*/

int Agent_SleepCmd (ClientData, Tcl_Interp *interp, int argc, char **argv)
{
    double seconds;       /* number of seconds to sleep */

        /* check the number of command arguments */
  
    if (argc != 2) {
        Tcl_AppendResult (interp, "wrong # of args: should be \"", argv[0], " seconds\"", (char *) NULL);
        return TCL_ERROR;
    }

        /* get the number of seconds */

    if (Tcl_GetDouble (interp, argv[1], &seconds) != TCL_OK) {
        Tcl_AppendResult (interp, ": number of seconds must be a real number\"", (char *) NULL);
        return TCL_ERROR;
    } else if (seconds < 0.0) {
        Tcl_AppendResult (interp, "number of seconds must be 0.0 or greater", (char *) NULL);
	return TCL_ERROR;
    }
 
        /* sleep */

    return (Agent_Sleep (interp, seconds)); 
}

/* Agent_TimeCmd

   Tcl syntax: agent_time < -start | -current | -elapsed > [wall | cpu]

      Purpose: Get the number of seconds that have elapsed since the
               agent started executing on the current machine

        Input: dummy  = client data (unused)
			(ClientData)

               interp = the current interpreter
		        (struct Tcl_Interp *)

               argc   = the 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 empty string.
*/

int Agent_TimeCmd (ClientData, Tcl_Interp *interp, int argc, char **argv)
{
    char temp[32];
    double seconds;
    BOOLEAN wantCpu;

	/* check the number of command arguments */

    if ((argc != 2) && (argc != 3)) {
	Tcl_AppendResult (interp, "wrong # of args: should be \"", argv[0], " < -start | -current | -elapsed > [wall | cpu]\"", (char *) NULL);
	return TCL_ERROR;
    }

	/* see if we want wall or cpu information */

    if (argc == 2) {
	wantCpu = e_FALSE;
    } else if (!strcmp (argv[2], "wall")) {
	wantCpu = e_FALSE;
    } else if (!strcmp (argv[2], "cpu")) { 
	wantCpu = e_TRUE;
    } else {
	Tcl_AppendResult (interp, "unknown qualifier \"", argv[2], "\": expected \"wall\" or \"cpu\"", (char *) NULL);
	return TCL_ERROR;
    } 

	/* get starting, current or elapsed */

    if (!strcmp (argv[1], "-start")) {

	seconds = wantCpu ? Agent_StartCpuTime (interp) : 
		 	    Agent_StartTime    (interp);
	
    } else if (!strcmp (argv[1], "-current")) {

	seconds = wantCpu ? Agent_CurrentCpuTime (interp) :
			    Agent_CurrentTime    (interp);

    } else if (!strcmp (argv[1], "-elapsed")) { 

	seconds = wantCpu ? Agent_ElapsedCpuTime (interp) :
			    Agent_ElapsedTime    (interp);

    } else {

	Tcl_AppendResult (interp, "unknown option \"", argv[1], "\": should be -start, -current or -elapsed", (char *) NULL);
	return TCL_ERROR;
    }

	/* put the elapsed time into the interpreter result */

    sprintf (temp, "%0.2f", seconds);
    Tcl_SetResult (interp, temp, TCL_VOLATILE);
    return TCL_OK;
}
