/*	file:	test.c

*/

#include <stdio.h>

/*FILE	*mid;*/
int	mid;
int	val;
int	err;
unsigned char	buf[80];
char	flush[] = "01";		/*0=flush input, 1=flush output*/
char	timer[] = "2xx";	/*2=set timout limit to xx (binary value)*/
unsigned short	timlim;
struct	midistat		/*structure of a midi ioctl return*/
{
    short	prt;
    short	stat;
    short	timeo;
    short	erval;
    short	eroff;
    short	ifirst;
    short	iavail;
    short	ibuf;
    short	ofirst;
    short	oavail;
    short	obuf;
};

struct midistat	retstr;

main(argc,argv)
int argc;
char    *argv[];
{
    if (argc<2)
    {
        printf("usage: TREAD mididriver\n");
        exit(1);
    }
    mid=mopen(argv[1]);		/*open driver for normal read/write*/
    if (mid==0)
    {
        printf("unable to open device %s\n",argv[1]);
        exit(2);
    }
    printf("Flush buffer? ");	/*ask user yes or no on flush*/
    fgets(buf,79,stdin);
    strupr(buf);
    if (buf[0]=='Y')
        mioctlw(flush,2,mid);		/*flush both buffers*/
    printf("Timeout delay in miliseconds (65534=max, 65535=never): ");
    scanf("%u",&timlim);			/*get time in unsigned int*/
    memcpy(&(timer[1]),&timlim,2);	/*copy into midioctl string*/
    mioctlw(timer,3,mid);
    while(1)
    {
        if ((err=mread(buf,1,mid))==1)	/*read like any other file*/
            printf("%x ",buf[0]);
        else
        {
            mioctlr(&retstr,sizeof(struct midistat),mid);
            printf("\7<%x %x %x> ",err,buf[0],retstr.erval);
            if (retstr.erval&4)			/*if it was overflow,*/
                mioctlw(flush,2,mid);		/*flush both buffers*/
        }
    }
}
