/*****************************************************************************
******************************************************************************
**<AUTO>
**
** FILE:	tclDiag.c
**
**<HTML>
**	This file contains TCL functions to support target selection
**	diagnostics.
**</HTML>
**</AUTO>
**
**
** ENVIRONMENT:
**	ANSI C.
**
******************************************************************************
******************************************************************************
*/

#include "taCalibObj.h"
#include "taDiag.h"


/*============================================================================
**<AUTO EXTRACT>
**
** TCL VERB: diagNew
**
**<HTML>
**	Return a handle to a new TA_DIAG
**</HTML>
**
**</AUTO>
**============================================================================
*/
static char diagNewCmd[] = "diagNew";
static int diagNewFlg = FTCL_ARGV_NO_LEFTOVERS;
static ftclArgvInfo diagNewTbl[] = {
   {NULL, FTCL_ARGV_HELP, NULL, NULL,
    "Return a handle to a new TA_DIAG\n"},
   {NULL, FTCL_ARGV_END, NULL, NULL, NULL}
};

static int
tclDiagNew(ClientData clientData,
	   Tcl_Interp *interp,
	   int argc,
	   char **argv
	   )
{
  char name[HANDLE_NAMELEN];
  TA_DIAG *diag;
  int status;

  /* Parse the arguments */
  if ((status = shTclParseArgv(interp, &argc, argv, diagNewTbl,
			       diagNewFlg, diagNewCmd))
      != FTCL_ARGV_SUCCESS)
    return status;
   
  /* Get a handle for our new TA_DIAG */
  if (shTclHandleNew (interp, name, "TA_DIAG", (void *) (diag = taDiagNew()))
      != TCL_OK)
    {
      if (diag != NULL) taDiagDel(diag);
      return(TCL_ERROR);
    }
   
  Tcl_SetResult(interp, name, TCL_VOLATILE);
  return(TCL_OK);
}

/*============================================================================
**<AUTO EXTRACT>
**
** TCL VERB: diagDel
**
**<HTML>
**	Delete a TA_DIAG.
**</HTML>
**
**</AUTO>
**============================================================================
*/
static char diagDelCmd[] = "diagDel";
static int diagDelFlg = FTCL_ARGV_NO_LEFTOVERS;
static ftclArgvInfo diagDelTbl[] = {
   {NULL, FTCL_ARGV_HELP, NULL, NULL,
    "Delete a TA_DIAG\n"},
   {"<handle>", FTCL_ARGV_STRING, NULL, NULL,
    "Handle of TA_DIAG to delete"},
   {NULL, FTCL_ARGV_END, NULL, NULL, NULL}
};

static int
tclDiagDel(ClientData clientData,
	   Tcl_Interp *interp,
	   int argc,
	   char **argv
	   )
{
  TA_DIAG *diag = NULL;
  char *name;
  int status;
  
  /* Parse the arguments */
  diagDelTbl[1].dst = &name;
  if ((status = shTclParseArgv(interp, &argc, argv, diagDelTbl,
			       diagDelFlg, diagDelCmd))
      != FTCL_ARGV_SUCCESS)
    return status;
   
  if (shTclAddrGetFromName (interp, name, (void *)&diag, "TA_DIAG") != TCL_OK)
    return(TCL_ERROR);
  
  if (diag != NULL) taDiagDel(diag);
  p_shTclHandleDel(interp, name);

  Tcl_SetResult(interp,"",TCL_STATIC);
  return(TCL_OK);
}

/*****************************************************************************
 *
 * Declare my new tcl verbs to tcl
 */
void
taTclDiagDeclare(Tcl_Interp *interp) 
{
  char *tclHelpFacil = "ta";
  shTclDeclare(interp, diagNewCmd,
	       (Tcl_CmdProc *)tclDiagNew,
	       (ClientData) 0,	(Tcl_CmdDeleteProc *)NULL, tclHelpFacil,
	       shTclGetArgInfo(interp, diagNewTbl, diagNewFlg, diagNewCmd),
	       shTclGetUsage(interp, diagNewTbl, diagNewFlg, diagNewCmd));
  shTclDeclare(interp, diagDelCmd,
	       (Tcl_CmdProc *)tclDiagDel,
	       (ClientData) 0,	(Tcl_CmdDeleteProc *)NULL, tclHelpFacil,
	       shTclGetArgInfo(interp, diagDelTbl, diagDelFlg, diagDelCmd),
	       shTclGetUsage(interp, diagDelTbl, diagDelFlg, diagDelCmd));
}
