#include "../sccshead/sfile.h"
#include "../sccshead/sint.h"

char dorelt__[] "~|^`dorelt.c:	3.2";
/*
	Routine to process the release table.  It allocates the pkt->Papply
	jagged array and writes out a new or modified release table
	entry.  Also, pkt->Plev is set to the highest level of the
	specified release if it is -1 on entry, and pkt->Prel is
	set to the default release if it is -1.

	pkt->Pnoprop is set if the specified release is less than the
	highest release in the release table.

	Note that neither dimension of the pkt->Papply array needs a
	zero subscript, so that space is used for dimension sizes:

		pkt->Papply[0][0].Adt = number of releases
		pkt->Papply[r][0].Adt = number of levels in release r
		pkt->Papply[r] = 0 if no deltas in release r
*/

doreltab(pkt)
register struct Packet *pkt;
{
	register struct Reltab *reltab;
	struct Reltab nreltab;
	register int first;
	int found;

	first = 1;
	found = 0;
	do {
		if(rdrec(pkt) == 1) break;
		reltab = pkt->Pibuf.Irecptr;
		if(!pkt->Papply) {
			pkt->Papply = alloc(2*(reltab->Rrel+1),1);
			pkt->Papply[0] = alloc(2,1);
			pkt->Papply[0][0].Adt = reltab->Rrel;
		}
		if(reltab->Rrel) {
			pkt->Papply[reltab->Rrel] =
			  alloc(sizeof(**pkt->Papply)*(reltab->Rlevs+1),1);
			pkt->Papply[reltab->Rrel][0].Adt = reltab->Rlevs;
		}
		if(first) {
			first = 0;
			if(pkt->Prel == -1) {
				pkt->Prel = pkt->Phdr.Hrdef;
				if(pkt->Prel == 0) pkt->Prel = reltab->Rrel;
				if(pkt->Prel == 0) pkt->Prel = 1;
			}
			if(pkt->Prel < reltab->Rrel) pkt->Pnoprop = 1;
		}
		if(pkt->Prel == reltab->Rrel) {
			if(pkt->Plev == -1)
				pkt->Plev = reltab->Rlevs;
			else if(pkt->Plev > reltab->Rlevs)
				fatal("level too big (48)");
			reltab->Rlevs++;
			found = 1;
			continue;
		}

		if(pkt->Prel > reltab->Rrel && !found) {
			nreltab.Rrel = pkt->Prel;
			nreltab.Rlevs = 1;
			wrtrec(pkt,&nreltab,sizeof(*reltab));
			if(pkt->Plev == -1)
				pkt->Plev = 0;
			else if(pkt->Plev)
				fatal("level too big (48)");
			found = 1;
			continue;
		}
	} while(reltab->Rrel);

	if (pkt->Pverbose&RLACCESS) printf("%d.%d\n",pkt->Prel,pkt->Plev);
	wrtrec(pkt,0,0);
}


rlvalid(pkt,r,l)
register struct Packet *pkt;
register int r, l;
{
	if(r > 0 && r <= pkt->Papply[0][0].Adt
	  && l > 0 && l <= pkt->Papply[r][0].Adt)
		return(&pkt->Papply[r][l]);

	return(0);
}


maxrel(pkt)
register struct Packet *pkt;
{
	return(pkt->Papply[0][0].Adt);
}


maxlev(pkt,r)
register struct Packet *pkt;
register int r;
{
	if(pkt->Papply[r] == 0) return(0);
	return(pkt->Papply[r][0].Adt);
}
