#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

int	TICS,start,cmpar,fuser,ddoc;
char	tc[40][10],user[30];

tcp(par)
	int	par;

{
	int	i,j;
	FILE	*fp;
	long	tloc,time();
	char	c,line[15];

	sprintf(line,"/tmp/%s",ttyname(0));
	if ((fp=fopen(line,"r")) != (FILE *)0)
	{
	c=getc(fp);
	TICS=c;
	c=getc(fp);
	start=c;
	c=getc(fp);
	cmpar=c;
	i=0;
	j=0;
	while((c=getc(fp))!= EOF) if (!c)
	{
		tc[i][j]=0;
		i++;
		j=0;
	}
	else
		tc[i][j++]=c;
	fclose(fp);
	}
	if (par)
	{
		if(strcmp("(null)",getlogin())!=0) 
		sprintf(user,"user/%-6.6s",getlogin());
		else sprintf(user,"user/%s",getenv("REMOTE_ADDR"));
		tloc=time((long *) 0);
		strncat(user,ctime(&tloc)+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);
	}
	else fuser=0;
}
/* imprime linea de comentario */

coment (lc)

	char lc[];
{
	mens(lc,tc[5][0]);
}

/* posicionamiento de cursor */

pos (row,col)
		
	int row,col;
{ 
	if (start) printf(tc[1],row+1,col+1);
	else if (cmpar) printf(tc[1],row+cmpar,col+cmpar);
	else  printf(tc[1],row,col);
	return;
}


/*		ingreso de datos


	row : posicion eje y 
	col : posicion eje x
	len : largo maximo de la variable
	type: tipo 0 -alfanumerico  1 -numerico
	l   : arreglo string de I/O
*/

input(row,col,len,type,l,ret)

	int row,col,len,type,ret;
	char l[];
{
	char l1[256];
	char l2[256];
	int point=0;
	int j,i,t,n,da,mo,ye;
	i=strlen(l);

	pos(row,col);
	for(j=0;j<len;j++) printf("_");
	if (type==8 || type ==9) printf ("  ");
	if (type>0 && type <6 && ddoc==0) printf ("    ");
	pos(row,col);
	printf("%s",l);
	j=0;
	while(1)
	{
		n=getin1();
		if (n==39)
		{
			t=getin1();
			switch (t)
			{
				case 'a' :
						n=160;
						break;
				case 'e' :
						n=130;
						break;
				case 'i' :
						n=161;
						break;
				case 'o' :
						n=162;
						break;
				case 'u' :
						n=163;
						break;
				case 'n' :
						n=164;
						break;
				case 'N' :
						n=165;
						break;
				case 'A' :
						n=166;
						break;
				case 'O' :
						n=167;
						break;
				case '2' :
						n=171;
						break;
				case '4' :
						n=172;
						break;
				default :
						printf("\07");
						n=t;
			}
		}
		if (n==':') n=';';
		if (n=='~')
		{
			if (l[0] && type==0)
			{
				if (l[i] >64 && l[i] <91)
				{
					l[i] +=32;
					n=l[i];
				}
				else if (l[i] >96 && l[i] <123)
				{
					l[i] -=32;
					n=l[i];
				}
				n=6;
			}
		}
		if (n==20||n==27||n==10||n==26||n==13||n==9||n==1||n=='\\'||n==12||n==25||n==4||n==ret || n >200 || n=='~')
		{
			if (type >0 && type <6)
			{
				pos(row,col);
				l2[0]=0;
				if (l[0] ) punto(l,l2,type-1);
				sprintf (l1,"%%%d.%ds",len,len);
				printf (l1,l2);
			}
			if ((type == 9 || type == 8) && l[0] && n!=17)
			{
				da=0;
				mo=0;
				ye=0;
				sscanf(l,"%2d%2d%d",&da,&mo,&ye);
				
				if (ye <15) ye +=2000;
				else if(ye <100) ye +=1900;
				if (ye<1901) ye= -1;
				if (mo <1 || mo >12) mo= -1;
				if (da <1 || da >31) da= -1;
				if ((mo==4 ||mo==6 ||mo==9 ||mo==11) && da>30) da = -1;
				if (mo==2 && da>28&& ye%4) da = -1;
				if (da <0 || mo <0 || ye <0)
				{
					if (da <0) coment ("ERROR EN CAMPO FECHA  -  NO ACEPTA DIA");
					else if (mo <0) coment ("ERROR EN CAMPO FECHA  -  NO ACEPTA MES");
					else if (ye <0) coment ("ERROR EN CAMPO FECHA  -  NO ACEPTA ANO");
					getin1();
					return('e');
				}
					pos(row,col);
					printf ("%.2d/%.2d/%d",da,mo,ye);
					sprintf (l,"%.2d%.2d%d",da,mo,ye);
					return(n);
			}
			else return(n);
		}
		switch (n)
		{
	case '\025' :
			if (i>0)
			{
				j++;
				strcpy(l1,l+i-1);
				i--;
				putchar('\b');
			}
			else printf("\07");
			break;
	case '\06' :
			if (j)
			{
				j--;
				printf("%c",l[i]);
				i++;
				if(!j) l1[0]=0;
				else strcpy(l1,l+i);
			}
			else printf("\07");
			break;

	case '\b' :
			if (i>0)
			{
				if(!j) printf("\b_\b");
				if(l[--i]=='.') point=0;
				l[i]=0;
				if(j)
				{
					printf("\b%s_",l1);
					pos(row,col+i);
					strcat(l,l1);
				}
			}
			else printf("\07");
			break;
	case '-' :
			if (len==i+j)
			{
				printf("\07");
				break;
			}
			if (type>0 && type<6)
			if (i || l[0]=='-')
			{
				printf("\07");
				break;
			}
			else
			{
				l[i++]=n;
				printf("-");
				l[i]=0;
				if (j)
				{
					printf("%s",l1);
					pos(row,col+i);
					strcat(l,l1);
				}
				break;
			}
			else 
			{
				l[i++]=n;
				printf("-");
				l[i]=0;
				if (j)
				{
					printf("%s",l1);
					pos(row,col+i);
					strcat(l,l1);
				}
				break;
			}
	case '.' :
			if (len==i+j)
			{
				printf("\07");
				break;
			}
			if (type<1 || type>5 || point!=1)
			{
				l[i++]=n;
				if (type>0 && type <6) point=1;
				printf(".");
				l[i]=0;
				if (j)
				{
					printf("%s",l1);
					pos(row,col+i);
					strcat(l,l1);
				}
			}
			else printf("\07");
			break;
	default:
			if (len==i+j)
			{
				printf("\07");
				break;
			}
			if (type>0 && type<6)
			{
				if (n>47 && n <58)
				{
					l[i++]=n;
					l[i]=0;
					printf("%c",n);
					if (j)
					{
						printf("%s",l1);
						pos(row,col+i);
						strcat(l,l1);
					}
				}
				else printf("\07");
				break;
			}
			else if (n > 31 && n < 173)
			{
				printf("%c",n);
				l[i++]=n;
				l[i]=0;
				if (j)
				{
					printf("%s",l1);
					pos(row,col+i);
					strcat(l,l1);
				}
			}
			if (n < 32 || n > 172) printf("\07");
			break;
		}
	}
}

box(starty,startx,endy,endx)


	int	startx,starty,endx,endy;

{
	int i;

	printf ("%s",tc[11]);
	pos(starty,startx);

	for (i=startx;i<endx;i++) printf("%s",tc[18]);
	pos(endy,startx);
	for (i=startx;i<endx;i++) printf("%s",tc[18]);
	pos(starty,startx);
	printf("%s",tc[14]);
	pos(starty,endx);
	printf("%s",tc[13]);
	for (i=starty+1;i<endy;i++)
	{
		pos(i,startx);
		printf("%s",tc[17]);
		pos(i,endx);
		printf("%s",tc[17]);
	}
	pos(endy,startx);
	printf("%s",tc[15]);
	pos(endy,endx);
	printf("%s",tc[16]);
	printf ("%s",tc[12]);
}

/* imprime linea de comentario
   en linea row       */

mens (lc,row)

	char lc[];
	int     row;
{
	int n;
	char ll[41],l1[2];
	n=strlen(lc)+1;
	l1[0]=0;
	pos(row,1);
	printf(tc[2]);
	sprintf(ll,"%%%ds%%-%ds",39-n/2,39+n/2);

	printf(ll,l1,lc);
	printf(tc[3]);
	fflush(stdout);
	return(0);
}
getin1 ()

{
	char nn[10],n;
	int u,k,r,c,jj,np;

	np=0;

	n=getchar();
	for (k=6;k<TICS;k++) if (n==tc[k][0]) break;
	if (k<TICS)
	{
		c=1;
		u=0;
		do
		{
			if (c > -1)
			{
				nn[u++]=n;
				nn[u]=0;
			}
			r=1;
			for (k=6;k<TICS;k++) if (!strncmp(nn,tc[k],u)) break;
			if (k<TICS && u==strlen(tc[k]))
			{
				r=1;
				if (k==6) n=8;
				if (k==7) return (26);
				if (k==8) return (10);
				if (k==9) n=21;
				if (k==10) n=6;
				if (k==21) return(27);
				if (k>20) return(k+180);
			}
			else if (k<TICS)
			{
				jj=0;
				r=0;
				c= -1;
				while(jj<50)
				{
					c=getchar();
					if (c > 0)
					{
						n=c;
						break;
					}
					else jj++;
				}
				if (jj>49) r=1;
			}
		}
		while (!r);
	}
	return(n);
}
punto (str,pun,dec)

	char	str[],pun[];
	int	dec;

{
	int	i,j,k;
	char	l1[30],l2[10],fr[10],d[10];
	double	limi;

	limi=0;
	sscanf (str,"%lf",&limi);
	pun[0]=0;
	l1[0]=0;
	if (str[0]==0) return(1);
	j=0;
	if (limi <0)
	{
		limi= -limi;
		j=1;
	}
	sprintf (fr,"%%.%dlf",dec);
	sprintf (l1,fr,limi);
	i=strlen(l1);
	if (dec)
	{
		i -=dec+1;
		sprintf(d,"%s",l1+i+1);
		
	}
	l1[i]=0;
	if (i < 4)  sprintf (pun,"%s",l1);
	if (i > 3 && i <7 )
	{
		strncpy(l2,l1,i-3);
		l2[i-3]=0;
		sprintf (pun,"%s.%3.3s",l2,l1+i -3);
	}
	if (i > 6 && i <10 )
	{
		strncpy(l2,l1,i-6);
		l2[i-6]=0;
		sprintf (pun,"%s.%3.3s.%3.3s",l2,l1+i -6,l1+i -3);
	}
	if (i > 9 && i <12)
	{
		strncpy(l2,l1,i-9);
		l2[i-9]=0;
		sprintf (pun,"%s.%3.3s.%3.3s.%3.3s",l2,l1+i-9,l1+i -6,l1+i -3);
	}
	if (j)
	{
		sprintf (l1,"-%s",pun);
		sprintf (pun,"%s",l1);
	}
	pun[strlen(pun)]=0;
	if (dec)
	{
		sprintf(l1,"%s,%s",pun,d);
		sprintf (pun,"%s",l1);
	}
	return (1);
}
