/*************************************************************************\
* 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  2003/10/30 21:41:39  soliday
 * First version.
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <tclRPN.h>
#include "mdb.h"
#include "SDDS.h"

/*****************************/
/* Local Function Prototypes */
/*****************************/

int tclRPN_if2pf (Tcl_Interp *interp, int argc, char *argv[]) {
  static char pfix[IFPF_BUF_SIZE];
  static char ifix[IFPF_BUF_SIZE];

  if (argc != 3) {
    Tcl_SetResult(interp, "usage: rpn if2pf <command sequence>",NULL);
    return TCL_ERROR;
  }

  sprintf(ifix, "%s", argv[2]);
  if (if2pf(pfix, ifix, sizeof pfix)) {
    Tcl_SetResult(interp, "rpn if2pf: problem converting command sequence",NULL);
    return TCL_ERROR;    
  }
  Tcl_SetResult(interp, pfix, NULL);    
  return TCL_OK;
}

int tclRPN_Expr(Tcl_Interp *interp, int argc, char *argv[]) {
  static char tmp[SDDS_MAXLINE];
  static char result[SDDS_MAXLINE];
  double answer=0;

  if (argc != 3) {
    Tcl_SetResult(interp, "usage: rpn expr <mathematical expression>",NULL);
    return TCL_ERROR;
  }

  sprintf(tmp, "%s", argv[2]);
  answer = rpn(tmp);
  sprintf(result, "%.15lg", answer);

  Tcl_SetResult(interp, result, NULL);    
  return TCL_OK;
}

int tclRPN_Clear(Tcl_Interp *interp, int argc, char *argv[]) {
  if (argc != 2) {
    Tcl_SetResult(interp, "usage: rpn clear",NULL);
    return TCL_ERROR;
  }
  rpn_clear();

  return TCL_OK;
}


/******************************************
 *
 *****************************************/
int tclRPNInit(Tcl_Interp *interp) {
  char *defns;

  defns=getenv("RPN_DEFNS");
  rpn(defns);

  return TCL_OK;
}


