/* cawput.c *** Put a waveform */

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.2  2000/05/18 16:35:43  soliday
 * Removed Solaris and Linux compiler warnings.
 *
 * Revision 1.1  1996/08/12 14:43:38  evans
 * First time.
 * */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#ifdef SUNOS4
#include <malloc.h>
#endif
#include <math.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

#include "ecaroutines.h"

/* Function Prototypes */

int main(int argc, char **argv);
void usage(void);

/* Declare external variables */

void (*ecaerrmsg)(char *fmt, ...)=ecadefaulterrmsg;

/* Global Variables */

struct ECA ecavals;
char pvname[ECANAMESIZE] ="";
double *vals;


/**************************** main ****************************************/
int main(int argc, char **argv)
{
    int i,nspec=0,count=0;
    
  /* Check command-line arguments */
    if(argc < 2) {
	usage();
	exit(1);
    }
  /* Allocate space assuming all arguments but 2 are numbers */
    vals=(double *)calloc(argc-2,sizeof(double));
  /* Parse command line */
    for (i=1; i < argc; i++) {
	if(nspec == 0) {
	    nspec++;
	    strncpy(pvname,argv[i],ECANAMESIZE);
	    pvname[ECANAMESIZE-1]='\0';
	}
	else {
	    vals[count++]=atof(argv[i]);
	    
	}
    }
  /* Check command line information */
    if(!(*pvname)) {
	ecaerrmsg("No process variable name specified");
	exit(1);
    }
    if(count == 0) {
	ecaerrmsg("No values specified");
	exit(1);
    }
  /* Allocate space for the structure */
    ecavals.n=1;
    ecavals.count=count;
    if(!ecaallocspace(&ecavals)) exit(1);
  /* Fill in the structure */
    strcpy(ecavals.names[0],pvname);
    for(i=0; i < count; i++) {
	ecavals.vals[0][i]=vals[i];
    }
  /* Put the values */
    if(!ecasearch(&ecavals)) {
	ecaprintnotconnected(&ecavals);
	exit(1);
    }
    if(!ecaputvals(&ecavals)) exit(1);
  /* Exit */
    return(0);
}    
/**************************** usage ***************************************/
void usage(void)
{
    printf("Puts a waveform record to a process variable\n");
    printf("Usage is: "
      "cawput pvname val1 val2 ...\n");
    printf("  The numbers will be converted to doubles before the put.\n");
    printf("    (Allow for roundoff if the native type is integer or enum.)\n");
}
