/* rms.h - common code between rms.c, rrms.c */
/* all the boring functions live here */

/*	E D I T   H I S T O R Y

27-Sep-84	DLE	created
14-Jan-85	DLE	added oldi() oldri() which use xoldi()
15-Jan-85	DLE	oldri() now gives O$SHR FB$GET

*/
/*		BUGS

this links as one kludge: normally RMS.C doesn't need gsafre() etc but they
are included anyway. rrms.c,rms.c should be re-written as seperate modules.
this would allow tkb to select minimum code to do a job.

*/

struct	rabsyn	*newi(lun,name,keylen,datlen)	/* may return struct fab * */
char		lun;				/* RMS channel number to use */
						/* "C" uses 1,2 & 3 !! */
char		*name;				/* ascii file name */
unsigned	keylen;				/* number of bytes in key */
unsigned	datlen;				/* number of bytes in data
						   not counting key bytes */
{struct rabsyn *newmi();
 return(newmi(lun,name,keylen+datlen,keylen,0,0));
}

struct rabsyn	*newmi(lun,name,reclen,extra)	/* may return struct fab */
char		lun;				/* RMS channel number to use */
char		*name;				/* ascii file name */
unsigned	reclen;				/* whole record length (bytes) */
int		extra;				/* 1st of many args:
						   eg l1,p1,l2,p2,0	*/
{struct rabsyn	*newmi_();
 return(newmi_(lun,name,reclen,&extra));
}

unsigned	geti(r,key,buf)			/* return o$sts */
struct	rabsyn	*r;
char		*key;				/* full key */
char		*buf;				/* full record appears here */
{unsigned	getmi();
 return(getmi(r,key,buf,0,RB$KGE));
}

unsigned	nxti(r,key,buf)			/* return o$sts */
struct	rabsyn	*r;
char		*key;				/* full key */
char		*buf;				/* full record appears here */
{unsigned getmi();
 return(getmi(r,key,buf,0,RB$KGT));
}

gsafre(thing)					/* free an object */
						/* abort if error */
	char	*	thing;
	{
#ifdef gsawatch
	 if	(!isalloc(thing))	error("\7gsafre:thing=%oo\n",thing);
	 $$mchk();
#endif
	 mfree(thing);
	}

xxxfre(thing)					/* free an RMS object */
	struct	rmssum	*	thing;
	{
	 struct rmssum * nextthing;

	 nextthing = 0;
	 if	(thing->o$cod==FB$BID && thing->o$bln==FB$BLN)
		{nextthing = ((struct rmsfab *)thing) -> o$xab;
		}
	 else
		{if	( thing->o$cod==RB$BID && (thing->o$bln==RB$BLN || thing->o$bln==RB$BLL) )
			{nextthing = ((struct rabsyn *)thing) -> o$fab;
			}
		 else
			{

			 switch	(thing->o$cod)
				{
			 case XB$SUM:
			 case XB$KEY:
			 case XB$DAT:
			 case XB$ALL:
			 case XB$PRO:
					nextthing =		      thing -> o$nxt;	break;
		
			 default:
					error("xxxfre:\7unknown RMS object @ %oo\n",thing);
				}
			 ;
			}
		 ;
		}
	 ;
	 if	(thing)		gsafre(thing);
	 if	(nextthing)	gsafre(nextthing);
	}

struct	rabsyn	*oldi(lun,name)			/* may return struct fab * */
char		lun;				/* RMS channel number to use
						   "C" uses 1,2,3 !!	*/
char		*name;				/* ascii file name string */
{
 struct rabsyn * xoldi();
 return( xoldi(lun,name,(FB$WRI<<8)|FB$DEL|FB$UPD|FB$PUT) );
			/* FB$WRI is traditional - no real need */
			/* programs now rely on FB$WRI not FB$GET (sigh) */
}

struct	rabsyn	*oldri(lun,name)		/* may return struct fab * */
char		lun;				/* RMS channel number to use
						   "C" uses 1,2,3 !!	*/
char		*name;				/* ascii file name string */
{
 struct rabsyn * xoldi();
 return( xoldi(lun,name,(FB$WRI<<8)|FB$GET) );
}

/* end: rms.h */
