/* test pashunfingaz' rms routines */

#include	<stdio.h>
#include	<rms200.h>
#include	<algol68.h>

int		i;		/* */
int		intbuf[50];	/* */
unsigned	handle[42];	/* rab addresses */
char		reply[100];	/* for humans */
unsigned	chan;		/* channel number for this operation */
struct rabsyn *	rp;		/* handle (rab pointer) for this op */
extern int	rmssts;		/* the o$sts we last got */
extern int	rmsstv;		/* the o$stv we last got */
char		buf[1000];	/* user's buffer */
char		filnam[100];	/* file name  */
char		key[257];	/* key */
int		rights;		/* O$SHR:O$FAC access rights */
unsigned	keylen;
unsigned	datlen;
int		keynum;
int		letter;
int		match;
int		len0;
int		len1;
int		pos0;
int		pos1;
int		reclen;

main()
{
	/* we must select a server task if we use remote rms */
	rmscs();	/* choose server - will put rad50/RRTTnn/ into rmssrv */
 printf("H for help\n");
 while	(TRUE)	obey();
}

obey()
{char	cmnd;

 printf("command:");
 gets(reply);
 sscanf(reply,"%c",&cmnd);
 switch	(tolower(cmnd))
	{
 case 'd':	rp = drop(handp());
		if	(rp)	/* non-zero return means fail */
			{chksts();
			}
		;
		break;
 case 'f':	freeee(handp());
		chksts();
		break;
 case 'g':	printf("what key:");
		gets(key);
		zero(buf,sizeof(buf));
		geti(handp(),key,buf);
		chksts();
		printf("buffer=\"%s\"\n",buf);
		break;
 case 'h':
		printf("D drop = de-access file\n");
		printf("F free\n");
		printf("G get\n");
		printf("H help\n");
		printf("K multi-key get\n");
		printf("L list lengths of record and key(s)\n");
		printf("M create multi-key file\n");
		printf("N get next record after {key} for {keynum}\n");
		printf("O open = re-access file\n");
		printf("P put\n");
		printf("Q quit\n");
		printf("R open, read-only\n");
		printf("S create single-key file\n");
		printf("U update\n");
		printf("X delete\n");
		printf("* xoldi() test [internal use]\n");
		break;
 case 'k':	printf("what index:");
		gets(reply);
		sscanf(reply,"%d",&keynum);
		printf("what key:");
		gets(key);
		printf("match = as well as > ?");
		gets(reply);
		sscanf(reply,"%c",&letter);
		match = ((toupper(letter)=='Y')?RB$KGE:RB$KGT);
		zero(buf,sizeof(buf));
		getmi(handp(),key,buf,keynum,match);
		chksts();
		printf("buffer=\"%s\"\n",buf);
		break;
 case 'l':	wotli(handp(),intbuf);
		printf("record length = %d.  number of keys = %d.\n",intbuf[1],intbuf[0]);
		FOR	(i=0;	i<intbuf[0];	i++)
		DO	printf("length of key %d. is %d.\n",i,intbuf[2+i]);
		OD;
		chksts();/* should never fail - thats why we are testing it ! */
	/* (just because you are paranoid doesn't mean they won't get you) */
		break;
 case 'm':	printf("enter file name:");
		gets(filnam);
		printf("how many bytes in key 0?");
		gets(reply);
		sscanf(reply,"%d",&len0);
		printf("bytes to left of key 0?");
		gets(reply);
		sscanf(reply,"%d",&pos0);
		printf("how many bytes in key 1?");
		gets(reply);
		sscanf(reply,"%d",&len1);
		printf("bytes to left of key 1?");
		gets(reply);
		sscanf(reply,"%d",&pos1);
		printf("how many bytes in whole of record?");
		gets(reply);
		sscanf(reply,"%d",&reclen);
		printf("channel number:");
		gets(reply);
		sscanf(reply,"%d",&chan);
		rp = newmi(chan,filnam,reclen,len0,pos0,len1,pos1,0);
		if	(!rp)
			{printf("\7no memory!\n");
			}
		else
			{handle[chan]=rp;
			 chksts();
			}
		;
		break;
 case 'n':	printf("what key number (>=0):");
		gets(reply);
		sscanf(reply,"%d",&keynum);
		printf("what key:");
		gets(key);
		zero(buf,sizeof(buf));
/*		nxti(handp(),key,buf);
*/		getmi(handp(),key,buf,keynum,RB$KGT);
		chksts();
		printf("buffer=\"%s\"\n",buf);
		break;
 case 'o':	printf("file name:");
		gets(filnam);
		printf("channel number:");
		gets(reply);
		sscanf(reply,"%d",&chan);
		rp = oldi(chan,filnam);
		if	(!rp)
			{printf("\7no memory!\n");
			}
		else
			{chksts();
			 handle[chan]=rp;
			}
		;
		break;
 case 'p':	getbuf();
		puti(handp(),buf);
		chksts();
		break;
 case 'q':	exit();
 case 'r':	printf("file name:");
		gets(filnam);
		printf("channel number:");
		gets(reply);
		sscanf(reply,"%d",&chan);
		rp = oldri(chan,filnam);
		if	(!rp)
			{printf("\7no memory!\n");
			}
		else
			{chksts();
			 handle[chan]=rp;
			}
		;
		break;
 case 's':	printf("enter file name:");
		gets(filnam);
		printf("how many bytes in key?");
		gets(reply);
		sscanf(reply,"%d",&keylen);
		printf("how many bytes in rest of record?");
		gets(reply);
		sscanf(reply,"%d",&datlen);
		printf("channel number:");
		gets(reply);
		sscanf(reply,"%d",&chan);
		rp = newi(chan,filnam,keylen,datlen);
		if	(!rp)
			{printf("\7no memory!\n");
			}
		else
			{handle[chan]=rp;
			 chksts();
			}
		;
		break;
 case 'u':	getbuf();
		updi(handp(),buf);
		chksts();
		break;
 case 'x':	deli(handp());
		chksts();
		break;
 case '*':	printf("file name:");
		gets(filnam);
		printf("channel number:");
		gets(reply);
		sscanf(reply,"%d",&chan);
		printf("rights mask (octal):");
		gets(reply);
		sscanf(reply,"%o",&rights);
		rp = xoldi(chan,filnam,rights);
		if	(!rp)
			{printf("\7no memory!\n");
			}
		else
			{chksts();
			 handle[chan]=rp;
			}
		;
		break;
 default:	printf("\7I don't understand '%c' !\n",cmnd);
		break;
	}
 ;
}

int	handp()
{printf("channel number:");
 gets(reply);
 sscanf(reply,"%d",&chan);
 return(rp=handle[chan]);
}

chksts()			/* check status */
				/* in: rmssts --- o$sts from last op */
{if	(rmssts!=SU$SUC)
	{/* woops ... */
	 printf("\7status= %d. %oo for [rab]=%oo channel=%d.\n",rmssts,rmssts,rp,chan);
	}
 ;
}

getbuf()
{
 printf("enter record:");
 gets(buf);
}
