/* PROGRAMA PROTOTIPO PARA MANTENEDORES EN MODELO 80386 */
/* COMPILAR CON COMANDO C                               */

#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#define		NHEAD	155
#define		NDOC	20
#define		RDOC	99

static char	ptr[12][50000][30];

	char	dbr[20][2][150],m2[512];
	int	fr,dbd[20][4],cl1,debug;

main (argc,argv,envp)

	int	argc;
	char	*argv[];
	char	*envp[];

{
	static short	ph[NHEAD][7],pd[RDOC][NDOC][7];
	static	char	m1[512],hd[2048],dc[2048];
	static char	head[NHEAD][256],doc[RDOC][NDOC][128],clc[10][512];
	long		p[RDOC],lh,ld,rgh,rgh1,count,rgd,flag;
	long		folio,tp;
	int	mm,db,i,j,k,m,c,fi,nh,nr,nc,df,z,debug,shf,fnd,fndg;
	FILE	*fh,*fopen();
	FILE	*fd,*fopen();
	FILE	*fp,*fopen();


	int	hhdd[150],ddhh[150],qyl[150],fuser;
	char	dp[140],qysh[NHEAD][150],qysd[RDOC][NDOC][150],user[30];
	int	rdonly,blk,terc,terv;
	char	busca[128],intc[100];


        debug=0;
        i=2;
        folio= -1;
        rdonly=0;
        busca[0]=0;
        blk=0;
        while (i <argc)
        {
                if (argv[i][0]=='-' && argv[i][1]=='b') blk=1;
                if (argv[i][0]=='-' && argv[i][1]=='r') rdonly=1;
                if (argv[i][0]=='-' && argv[i][1]=='5') sscanf(argv[i]+2,"%s",busca);
                if (argv[i][0]=='-' && argv[i][1]=='a') sprintf(intc,"/tmp/%s",argv[i]+2);
                if (argv[i][0]=='-' && argv[i][1]=='d') sscanf(argv[i]+2,"%d",&debug);
                if (argv[i][0]=='-' && argv[i][1]=='N') sscanf(argv[i]+2,"%ld",&folio);
                if (argv[i][0]=='-' && argv[i][1]=='t')
                {
                        sscanf(argv[i]+2,"%d",&terc);
                        sscanf(argv[i+1],"%d",&terv);
                        i++;
                }
                i++;
        }

	fh=fopen(argv[2],"r");
	while (!feof(fh))
	{
		fgets(dp,2,fh);
		if(dp[0]=='H')
		{
			fscanf(fh,"%2d",&i);
			fgets(qysh[i],200,fh);
			qysh[i][strlen(qysh[i])-1]=0;
		}
		if(dp[0]=='D')
		{
			fscanf(fh,"%2d%2d",&i,&j);
			fgets(qysd[i][j],200,fh);
			qysd[i][j][strlen(qysd[i][j])-1]=0;
		}
	}
#include "rdpar"
/*
	for (k=2;k<argc;k++)
	{
		hhdd[k-2]=argv[k][0];
		dp[k-2]=argv[k][3];
		sscanf(argv[k]+1,"%2d",&ddhh[k-2]);
		sprintf(m1,"%%-%d.%ds",ph[ddhh[k-2]][2],ph[ddhh[k-2]][2]);
		i=3;
		while(argv[k][++i]) if(argv[k][i]==3) argv[k][i]=32;
		sprintf(qys[k-2],m1,argv[k]+4);
	}
*/
#include "rddbr"

	sprintf(user,"user/%s",getenv("REMOTE_ADDR"));

	tp=time ((long *) 0);
	strncat(user,ctime(&tp)+3,7);

	if ((fuser=open(user,2)) < 0 )
	{
		fuser=creat(user,0777);
		close(fuser);
		if ((fuser=open(user,2)) < 0 ) exit(2);
	}
	lseek(fuser,0L,2);

	if ((fi=open("INFO",O_RDWR)) < 1)
	{
		printf ("No permission ... \r\n");
		sleep(1);
		exit (0);
	}
	fh=fopen("HEAD","r+");
	fd=fopen("DOC","r+");
	c=1;
	rgh=0L;
	lseek(fi,0L,0);
	read(fi,m1,12);
	m1[12]=0;
	sscanf(m1,"%ld%ld",&rgh1,&rgd);
	if ((fr=open("H",O_RDONLY)) >0)
	{
		lseek (fr,0,0);
		while ((read(fr,ptr[0][rgh],ph[0][2]))) rgh++;
		close(fr);
	}
	for (j=0;j<nr;j++) p[j]= -1;
	for(count=rgh;count<rgh1;count++)
	{
		fseek(fh,count*lh,0);
		fgets(ptr[0][count],1+ph[0][2],fh);
	}
	for(count=0;count<rgh1;count++) if(!strcmp(ptr[0][count],qysh[0]))
	{
		fseek(fh,count*lh,0);
		for (j=0;j<nh;j++) if (ph[j][3] <30 || ph[j][3]>49) fgets(head[j],1+ph[j][2],fh);
		for (j=0;j<nh;j++)
		{
			if (ph[j][3]>49)
			{
				/*
				m=ph[j][3]-49;
				if (!recover (m,head[j],ph[j][2]))
				{
					c='k';
					sprintf(m1,"%%-%d.%ds",dbd[m][2],dbd[m][2]);
					sprintf(head[j]+ph[j][2],m1,"");
				}
				else
				{
					sprintf(m1,"%%-%d.%ds",dbd[m][2],dbd[m][2]);
					sprintf(head[j]+ph[j][2],m1,dbr[m][1]);
				}
				*/
				for (k=0;k<nh;k++) if (ph[k][3]>39 && ph[k][3]<50 && ph[k][4]==j)
				{
					m=ph[k][3]-39;
					if (c!='k') recov (m,head[ph[k][4]],ph[ph[k][4]][2],ph[k][5],ph[k][6]);
					else sprintf (dbr[m][1],"");
					sprintf(m1,"%%-%d.%ds",ph[k][2],ph[k][2]);
					sprintf (head[k],m1,dbr[m][1]);
					if (isdigit(head[k][0])) while(head[k][0] && head[k][strlen(head[k])-1]==32) head[k][strlen(head[k])-1]=0;
c='e';
				}
			}
		}
		for (j=0;j<nh;j++) if (ph[j][3]==30)
		{
			for (m=0;m<cl1;m++) if (clc[m][0]=='h' && clc[m][1]==j)
			{
				k=1;
				for (z=0;z<512;z++)
				m1[z]=0;
				while (clc[m][++k])
				if (clc[m][k]!='h') strncat(m1,clc[m]+k,1);
				else
				{
					k++;
					sscanf(clc[m]+k,"%2d",&mm);
					k++;
					if (head[mm][0]=='-')
					{
						strcat(m1," 0 ");
						strcat(m1,head[mm]);
						strcat(m1," - ");
					}
					else strcat(m1,head[mm]);
				}
			}
			pol(m1,head[j]);
		}
		for (j=0;j<nr;j++) fscanf(fh,"%6ld",&p[j]);
		flag=0;
		for (i=0;i<nr;i++) if (p[i]> -1)
		{
			fseek(fd,p[i]*ld,0);
			for (j=0;j<nc;j++) if (pd[i][j][3] <30 || pd[i][j][3]>49) fgets(doc[i][j],1+pd[i][j][2],fd);
			if (doc[i][0][0]==32) p[i]= -1;
			else flag =1;
			if (p[i] > -1) for (j=0;j<nc;j++) if (pd[i][j][3]>49)
			{
				m=pd[i][j][3]-49;
				c=0;
				if (!recover (m,doc[i][j],pd[i][j][2])) c='k';
				for (k=0;k<nc;k++) if (pd[0][k][3]>39 && pd[0][k][3]<50 && pd[0][k][4]==j)
				{
					m=pd[0][k][3]-39;
					if (c!='k') recov (m,doc[i][pd[0][k][4]],pd[0][pd[0][k][4]][2],pd[0][k][5],pd[0][k][6]);
					else sprintf (dbr[m][1],"");
					sprintf(m1,"%%-%d.%ds",pd[i][k][2],pd[i][k][2]);
					sprintf (doc[i][k],m1,dbr[m][1]);
					if (isdigit(doc[i][k][0])) while(doc[i][k][0] && doc[i][k][strlen(doc[i][k])-1]==32) doc[i][k][strlen(doc[i][k])-1]=0;
				}
			}
			if ( p[i] > -1) for (j=0;j<nc;j++) if (pd[i][j][3] ==30)
			{
				for (m=0;m<cl1;m++) if (clc[m][0]=='d' && clc[m][1]==j)
				{
					k=1;
					for (z=0;z<512;z++)
					m1[z]=0;
					while (clc[m][++k])
					if (clc[m][k]!='h' && clc[m][k]!='d') strncat(m1,clc[m]+k,1);
					else
					{
						k++;
						sscanf(clc[m]+k,"%2d",&mm);
						k++;
						if (clc[m][k]=='h') strcat(m1,head[mm]);
						else strcat(m1,doc[i][mm]);
					}
				}
				pol(m1,doc[i][j]);
			}
		}
		break;

	}
	if (count == rgh1)
	{
		flag= -1;
		sprintf(m1,"%s",ctime(&tp));
		sprintf(m2,"%s:ING:%2.2s%2.2s%2.2s\n",head[0],m1+11,m1+14,m1+17);
		write(fuser,m2,strlen(m2));
	}
	else
	{
		flag=count;
		for (i=0;i<nh;i++)
		if (strcmp(head[i],qysh[i]))
		{
			sprintf(m1,"%s",ctime(&tp));
			sprintf(m2,"%s:%.2d:%s:%s:%2.2s%2.2s%2.2s\n",head[0],i,head[i],qysh[i],m1+11,m1+14,m1+17);
			write(fuser,m2,strlen(m2));
		}
		for (i=0;i<nr;i++) for(j=0;j<nc;j++)
		if (strcmp(doc[i][j],qysd[i][j]))
		{
			sprintf(m1,"%s",ctime(&tp));
			sprintf(m2,"%s:%.2d:%.2d:%s:%s:%2.2s%2.2s%2.2s\n",head[0],i,j,doc[i][j],qysd[i][j],m1+11,m1+14,m1+17);
			write(fuser,m2,strlen(m2));
		}
	}
	for (i=0;i<nh;i++) sprintf (head[i],"%s",qysh[i]);
	for (i=0;i<nr;i++) for(j=0;j<nc;j++) sprintf (doc[i][j],"%s",qysd[i][j]);
#include "rest"
}
recover (m,var1,len)



	int	m,len;
	char	*var1;

{
	int	k;
	long	count;

	for (k=0;k<dbd[m][3];k++) if (!strncmp(ptr[m][k],var1,len)) break;
	if (k<dbd[m][3])
	{
		count = k;
		count *=dbd[m][0];
		count +=dbd[m][1];
		fr=open(dbr[m][0],O_RDONLY);
		lseek(fr,count,0);
		read(fr,dbr[m][1],dbd[m][2]);
		dbr[m][1][dbd[m][2]]=0;
		close(fr);
		k=1;
	}
	else k=0;
	return (k);
}
recov (m,var1,len,p1,p2)

	int	m,len;
	short p1,p2;
	char	*var1;
{
	int	k;
	long	count;

	for (k=0;k<dbd[m][3];k++) if (!strncmp(ptr[m][k],var1,len)) break;
	if (k<dbd[m][3])
	{
		count = k;
		count *=dbd[m][0];
		count +=p1;
		fr=open(dbr[m][0],O_RDONLY);
		lseek(fr,count,0);
		read(fr,dbr[m][1],p2);
		dbr[m][1][p2]=0;
		close(fr);
		k=1;
	}
	else k=0;
	return (k);
}

pol(string,l)
char string[],l[];
{
	double stk[30];
	int	i=0,j=0,k=0,z=0,s=0;
	char	ls[40][15],c;
	
	
	while (c=string[j++])
	{

		if (isdigit(c) || c=='.')
		{
			ls[i][k++]=c;
			ls[i][k]=0;
		}
		else  if (c==32)
		{
					if (k) i++;
					k=0;
		}
		else
		{
					if (k) i++;
					ls[i][0]=c;
					ls[i++][1]=0;
					k=0;
		}
	}
	
	for (z=0;z<i;z++)
	{
		if(isdigit(ls[z][0]) || ls[z][0]=='.') sscanf(ls[z],"%lf",&stk[s++]);
		else
		{
			j= --s;
			k= j--;
			if (ls[z][0]=='+') stk[j] +=stk[k];
			if (ls[z][0]=='-') stk[j] -=stk[k];
			if (ls[z][0]=='/')
			{
				if (stk[k]) stk[j] /=stk[k];
				else
				{
					sprintf(l,"/BY0");
					return (1);
				}
			}
			if (ls[z][0]=='*') stk[j] *=stk[k];
			stk[k]=0;
		}
	}
	sprintf(l,"%.2lf",stk[0]);
	return(0);
}
dbgr(stt,mess)

	char *mess;
	int	stt;
{
	char	t;

	if (debug || stt==0)
	{
		coment(mess);
		if (!stt) sleep(2);
		else t=getin1();
		if (t==27) exit(1);
	}
	return (0);
}
