/* 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][25];

	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;
	int	mm,db,i,j,k,m,c,fi,nh,nr,nc,df,z,flag,debug,shf,fnd,fndg;
	FILE	*fh,*fopen();
	FILE	*fd,*fopen();
	FILE	*fp,*fopen();


	int	hhdd[150],ddhh[150],qyl[150],flag0;
	char	dp[150],qys[150][100];

	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]);
		strcpy(qys[k-2],argv[k]+4);
		qyl[k-2]=strlen(qys[k-2]);
		i=0;
		while(qys[k-2][i])
		{
			qys[k-2][i]=tolower(argv[k][4+i]);
			i++;
		}
	}
	flag0=-1;
#include "rdpar"
#include "rddbr"
	for (k=2;k<argc;k++) if(qys[k-2][0]) flag0=k-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+");
	rgh=0L;
	lseek(fi,0L,0);
	read(fi,m1,12);
	m1[12]=0;
	sscanf(m1,"%ld",&rgh1);
        if ((fr=open("H",O_RDONLY)) >0)
        {
		rgh=0L;
                lseek (fr,0,0);
                while ((read(fr,ptr[0][rgh],ph[0][2]))) rgh++;
                close(fr);
        }
        if ((fr=open("S",O_RDONLY)) >0)
        {
		rgh=0L;
                lseek (fr,0,0);
                while ((read(fr,ptr[11][rgh],ph[4][2]))) rgh++;
                close(fr);
		rgh=0L;
                while (ptr[11][rgh][0]!=0)
		{
			i=0;
			while (ptr[11][rgh][i]!=32) i++;
			ptr[11][rgh][i]=0;
			rgh++;
		}
        }
	rgh=0L;
	if(ddhh[flag0]==0)
	{
		flag= -1;
		for(count=rgh;count<rgh1;count++)
		if (!strncmp(qys[0],ptr[0][count],ph[0][2]))
		{
			rgh=count;
			rgh1=count+1;
			break;
		}
	}
	if(ddhh[flag0]==4)
	{
		rgh=0L;
		for(count=rgh;count<rgh1;count++)
		if (!strcmp(qys[flag0],ptr[11][count]))
		{
			rgh=count;
			rgh1=count+1;
		}
	}
	for(count=rgh;count<rgh1;count++)
	{
		c=1;
		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]);
			}
		}
		if (flag)
		for (k=0;k<nr;k++)
		{
			if (p[k] > -1)
			{
			fndg=1;
			for(j=0;j<argc-2;j++) if(qys[j][0]!=0)
			{
				fnd=0;
				for (i=0;i<ph[ddhh[j]][2];i++)
				{
					if (hhdd[j]=='h' || hhdd[j]=='H')
					{
					m1[i]=tolower(head[ddhh[j]][i]);
					m1[i+1]=0;
					}
					if (hhdd[j]=='d' || hhdd[j]=='D')
					{
					m1[i]=tolower(doc[k][ddhh[j]][i]);
					m1[i+1]=0;
					}
				}
				if (hhdd[j]=='h' || hhdd[j]=='H')
				for (i=0;i<ph[ddhh[j]][2]-qyl[j]+1;i++)
					if(!strncmp(m1+i,qys[j],qyl[j])) fnd=1;
				if (hhdd[j]=='d' || hhdd[j]=='D')
				for (i=0;i<pd[k][ddhh[j]][2]-qyl[j]+1;i++)
					if(!strncmp(m1+i,qys[j],qyl[j])) fnd=1;
				if(fnd==0) fndg=0;
			}
			if(fndg)
			{
				for(j=0;j<argc-2;j++)
				{
					if (hhdd[j]=='h' || hhdd[j]=='H') printf ("%s",head[ddhh[j]]);
					if (hhdd[j]=='d' || hhdd[j]=='D') printf ("%s",doc[k][ddhh[j]]);
					if (dp[j]!=0) printf ("%c",dp[j]);
				}
				printf ("\n");
			}
			}
		}
		else
		{
			fndg=1;
			for(j=0;j<argc-2;j++) if(qys[j][0]!=0)
			{
				fnd=0;
				for (i=0;i<ph[ddhh[j]][2];i++)
				{
					m1[i]=tolower(head[ddhh[j]][i]);
					m1[i+1]=0;
				}
				for (i=0;i<ph[ddhh[j]][2]-qyl[j]+1;i++)
					if (!strncmp(m1+i,qys[j],qyl[j])) fnd=1;
				if(fnd==0) fndg=0;
			}
			if(fndg)
			{
				for(j=0;j<argc-2;j++)
				{
					if (hhdd[j]=='h' || hhdd[j]=='H') printf ("%s",head[ddhh[j]]);
					if (dp[j]!=0) printf ("%c",dp[j]);
				}
				printf ("\n");
			}
		}
	}
}




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);
}
