/* getpvtype.c *** Get the native type of a process variable */

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1.1.1  1996/03/25 18:53:10  evans
 * Imported files.
 * */

#include "ecaroutines.h"

/* Function prototypes */

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

/* Declare external variables */

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

/* Global variables */

char string[BUFSIZ];
struct ECA eca;
chid ch;
char pvname[ECANAMESIZE]="";
int nspec=0;

/**************************** main ****************************************/
int main(int argc, char **argv)
{
    int i,j;
    
/* Parse command line */
    if(argc < 2) {
	usage();
	exit(1);
    }
    for (i=1; i < argc; i++) {
	if (argv[i][0] == '-') {
	    switch(argv[i][1]) {
	    case 'h':
	    case '?':
		usage();
		exit(0);
		break;
	    default:
		ecaerrmsg("setbcorr: Invalid option: %s",argv[i]);
		usage();
		exit(1);
	    }
	}
	else if(nspec == 0) {
	    nspec++;
	    strncpy(pvname,argv[i],ECANAMESIZE);
	    pvname[ECANAMESIZE-1]='\0';
	}
	else {
	    usage();
	    exit(1);
	}
    }

/* Check command line information */
    if(!(*pvname)) {
	ecaerrmsg("No process variable name specified");
	usage();
	exit(1);
    }

/* Initialize channel access */
    if(!ecainit()) {
	printf("Cannot initialize channel access\n");
	exit(1);
    }

/* Allocate space */
    if(!eca.data) {
	eca.n=eca.count=1;
	if(!ecaallocspace(&eca)) {
	    printf("Memory Error\n");
	    exit(1);
	}
    }

/* Search */
    strcpy(eca.names[0],pvname);
    if(!ecasearch(&eca)) {
	printf("Not Found\n");
	exit(1);
    }

/* Print the native type */    
    ch=eca.data->chid;
    printf("%s\n",dbr_text[ca_field_type(ch)]);

/* Exit */    
    return(0);
}
/**************************** usage ***************************************/
void usage(void)
{
    int i;
    
    printf("Gets the native type of a process variable\n");
    printf("Usage is: "
      "getpvtype [Options] corrector-name value\n");
    printf("  Options:\n");
    printf("    -h           Help\n");
    printf("Possible types:\n");
    for(i=0; i < dbr_text_dim; i++) {
	printf("  %s\n",dbr_text[i]);
    }
}
