/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution. 
\*************************************************************************/

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  1999/06/10 19:43:52  soliday
 * Moved to oag tree.
 *
 * Revision 1.1  1998/05/26 15:49:00  borland
 * First version.  Not actually used because we have routines written in
 * Tcl that provide the same functions.  However, the code here does work.
 *
 */

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include <tclRunControl.h>

static Tcl_HashTable tclRunControlCmdTable;

typedef struct {
  char *name;     /* os xxx command name */
  int (*proc)();  /* command handling function */
} CMD_INFO;

static CMD_INFO tclRunControlCommands[] = {
  {"init", tclRunControlInitRec},
  {"ping", tclRunControlPing},
  {"exit", tclRunControlExit},
  {"logMsg", tclRunControlLogMsg},
  {(char *)NULL, (int (*)())NULL}
};


/* Function Prototypes */
int tclRunControlCmd(ClientData dummy, Tcl_Interp *interp, int argc, char *argv[]);
int TclRunControl_Init(Tcl_Interp *interp);

int tclRunControlCmd(ClientData dummy, Tcl_Interp *interp, int argc, char *argv[]) {
  Tcl_HashEntry *entryPtr;
  int (*handler)();

  if (argc < 2) {
    Tcl_SetResult(interp, "runControl: must specify a sub-command\nOne of init, ping, exit, or logMsg", 
		  (Tcl_FreeProc *) NULL); 
    return TCL_ERROR;
  }

  entryPtr = Tcl_FindHashEntry(&tclRunControlCmdTable, argv[1]);
  if (entryPtr == NULL) {
    Tcl_SetResult(interp, "Unknown RunControl command ", (Tcl_FreeProc *) NULL);
    return TCL_ERROR;
  } else {
    handler = (int(*)())Tcl_GetHashValue(entryPtr);
    return ((*handler)(interp, argc-1, argv+1));
  }

}

int TclRunControl_Init(Tcl_Interp *interp) {
  CMD_INFO *cmd;

  Tcl_CreateCommand(interp, "runControl", tclRunControlCmd, (ClientData *) NULL,
		    (Tcl_CmdDeleteProc *)NULL);

  Tcl_InitHashTable(&tclRunControlCmdTable, TCL_STRING_KEYS);
  for (cmd=tclRunControlCommands ; cmd->name != NULL ; cmd++) {
    int newEntry;
    Tcl_HashEntry *entryPtr;
    entryPtr = Tcl_CreateHashEntry(&tclRunControlCmdTable, cmd->name, &newEntry);
    Tcl_SetHashValue(entryPtr, cmd->proc);
  }
  tclRunControlInit(interp);
  
  return TCL_OK;
}




