#include <fcntl.h>
#include<ctype.h>
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
static char	tcap[430][100],tc[430][100];

extern	int	errno;

#define	TICS	25
static	char	*tic[] ={"ho","cm","so","se","cl","li","kb","ku","kd","kl","kr","GS","GE","G1","G2","G3","G4","GV","GH","mb","me","k1","k2","k3","k4","k5"};
int	sclen=24,start=0,cmpar=0;

main (argc,argv,envp)

	int	argc;
	char	*argv[],*envp[];
{
	int	k,i,r,j,l,n,Tx;
	char	line[100],c,tmp[100],*t;
	FILE	*fp,*fopen();

	for(k=0;envp[k];k++) if (!strncmp(envp[k],"TERMCAP=",8)) break;
	if (!envp[k])
	{
		fprintf (stderr,"NO SE ENCUENTRA VARIABLE \"TERMCAP\"\n");
		exit(1);
	}
	i=8;
	r=0;
	j=0;
	do
	{
		if (envp[k][i]!=0 && envp[k][i]!=':') line[r++]=envp[k][i];
		else 
		{
			line[r]=0;
			r=0;
			strcpy(tcap[j],line);
			j++;
			r=0;
		}
	}
	while(envp[k][i++]);
	for (i=0;i<TICS;i++)
	{
		for(r=1;r<j;r++) if (!strncmp(tic[i],tcap[r],2)) break;
		if (r<j)
		{
		 	t=tcap[r]+2;
			if (*t == '#')
			{
				sscanf(t+1,"%d",&sclen);
				sclen--;
				tc[i][0]=sclen;
				tc[i][1]=0;
				
			}
			else
			{
				l=0;
				t++;
				while (isdigit(*t)) t++;
				while (*t)
				{
					c= *t;
					switch(c)
					{

				case '\\':
						t++;
						if ( *t == 'E' || *t == 'e' )
						{
							line[l++]=27;
							t++;
						}
						else if ( *t == 'r')
						{
							line[l++]=13;
							t++;
						}
						else if (*t=='^')
						{
							line[l++]='^';
							t++;
						}
						else if (isdigit(*t)) 
						{
							n=0;
							while(isdigit(*t)&& n<3) tmp[n++]= *t++;
							tmp[n]=0;
							if (tmp[0]=='2') tmp[0]='0';
							sscanf(tmp,"%o",&c);
							line[l++]=c;
						}
						break;
				case '^' :
						t++;
						c=toupper(*t);
						if (c >63 && c<96)
						{
							line[l++]=c-64;
							t++;
						}
						break;
				case '%' :
						t++;
						if (*t == 'i')
						{
							start=1;
							t++;
						}
						if (*t == 'd')
						{
							line[l++]='%';
							line[l++]='d';
							t++;
						}
						if (*t == '.' || *t == 'c')
						{
							line[l++]='%';
							line[l++]='c';
						}
						if (*t == '+')
						{
							line[l++]='%';
							line[l++]='c';
								t++;
							if (*t == '\\')
							{
								n=0;
								t++;
								if(isdigit(*t))
								{
									while(isdigit(*t)) tmp[n++]= *t++;
									tmp[n]=0;
									sscanf(tmp,"%o",&c);
									cmpar=c;
								}
							}
							else cmpar= *t;
						}
						break;
				default :
					line[l++]= *t++;
					}
				}
				line[l]=0;
				l=0;
				strcpy(tc[i],line);
			}
		}
	}
	if (!tc[6])
	{
				tc[6][0]=8;
				tc[6][1]=0;
	}
	sprintf(tc[TICS],"/tmp/%s",ttyname(0));
	if ((fp=fopen(tc[TICS],"w")) == (FILE *) NULL) exit (1);
	fprintf (fp,"%c%c%c",TICS,start,cmpar);
	for (i=0;i<TICS;i++) fprintf (fp,"%s%c",tc[i],0);
	fclose(fp);

	Tx=0;
	if ((fcntl(Tx,F_SETFL,O_RDONLY | O_NDELAY))<0)
	{
		fprintf (stderr,"FCNTL Not Succeffully SET %d\n",errno);
		exit(1);
	}
	sprintf(tc[TICS],"exec %s %s",argv[1],argv[2]);
	printf ("%s\n",tc[TICS]);
	system(tc[TICS]);
	Tx=0;
	if ((fcntl(Tx,F_SETFL,O_RDONLY))<0)
	{
		fprintf (stderr,"FCNTL Not Succeffully SET %d\n",errno);
		exit(1);
	}
	exit(0);
}
