/*
	Copyright (C) 1994,1995	Edward Der-Hua Liu, Hsin-Chu, Taiwan
*/

#include <stdio.h>
#include <memory.h>
#include <sys/types.h>
#include "xi.h"
#include "constant.h"

extern void *malloc(size_t size);
extern void ser_b5(u_char *ch, u_char *tt);
extern void free(void *ptr);
extern void SORTFUNC(void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));
int unlink(const char *pathname);


unsigned char *bf;

	/*  	 a b c d e f g h i j  */
	/* 	 k l m n o p q r s t  */
	/*	 u v w x y z . / ; ,  */
#define KeyNum (31)
u_long MaxPress=4;
char keymap[]=" abcdefghijklmnopqrstuvwxyz./;,1234567890";
char tran[]= "  1-5v3v3-3^4-5-6-8^7-"
		"8-9-7v6v9^0^1^4^2-5^"
		"7^4v2^2v6^1v9v0v0-8v"
		"1 2 3 4 5 6 7 8 9 0 ";

typedef struct {
	u_char key[4];
	u_char ch[2];
	u_short oseq;
} ITE;

int qcmp(a, b)
  ITE *a, *b;
{
	if (CONVT(a->key) > CONVT(b->key)) return 1;
	if (CONVT(a->key) < CONVT(b->key)) return -1;
	if (a->oseq > b->oseq) return 1;
	if (a->oseq < b->oseq) return -1;
	return 0;
}

int main()
{
FILE *fp,/**fw,*/*ff;
/*unsigned long kk;*/
unsigned long k1,k2,k3,k4/*,k5,tt*/;
unsigned long /*q,r,*/ofs;
unsigned char c1,c2;
int i,j,k,fsize;
ITE *itar;
/*u_short idx1[31];*/
u_char ttt[3];
/*char def1[31];*/
u_short dupc; 
u_long st_ofs;
u_long kkk;
u_short arseq;
u_char tst[0x400][2];


for(i=0;i<sizeof(tran);i++)
  if (tran[i]=='^') tran[i]=0x18;
  else
  if (tran[i]=='v') tran[i]=0x19;

puts("%NoBosh 1");
puts("%ename array30");
puts("%prompt [¦æ¦C30]");
puts("%selkey 1234567890");
puts("%dupsel 9");
puts("%keyname begin");
for(i=1;i<=40;i++)
	printf("%c %c%c\n", keymap[i], tran[i*2], tran[i*2+1]);
puts("%keyname end");

if ((fp=fopen("array30.tab","r"))==NULL) {
  puts("open err");
  exit(1);
}
if ((bf=(unsigned char *)malloc(40000))==NULL) {
  puts("malloc err");
  exit(1);
}

fread(ttt,2,1,fp);
dupc=ttt[0]|((u_short)ttt[1]<<8);
fread(bf,1,st_ofs=((dupc*2+4)/3)*3-2,fp);


{
u_char *p=bf;

for(i=0;i<dupc;i++) {
  tst[i][0]=*p;
  tst[i][1]=*(p+1);
  p+=2;
}

puts("%quick begin");
for(i=0;i<30;i++) {
	printf("%c ", keymap[i+1]);
	for(j=0;j<10;j++)
	{
		ser_b5(&tst[i*10+j+98][0],ttt);
		printf("%s", ttt);
	}
	puts("");
}
puts("%quick end");
puts("%sel1st begin");
#if	0	/* no longer since the ch is raised to the 1st. */
for(i=0;i<98;i++) 
if (tst[i][0]!=0 || tst[i][1]!=0) {
		ser_b5(&tst[i][0],ttt);
		puts(ttt);
}
#endif
puts("%sel1st end");
}


fsize=fread(bf,1,40000,fp);
fclose(fp);

if ((fp=fopen("ar.tmp1","w"))==NULL) {
  puts("create");
  exit(-1);
}

/* 11111222 22333334 4444xxxx */
arseq=0;
for(i=0;i<fsize;i+=3) {
	k1=bf[i]>>3;
	k2=((bf[i]&7)<<2)| (bf[i+1] >> 6);
	k3=(bf[i+1] >> 1)&31;
	k4=((bf[i+1]&1)<<4) | (bf[i+2]>>4); 

	ofs=i/3;
	ofs+=0x8800;
	c1=ofs>>8;
	c2=ofs&255;

#define ct(tt) (tran[tt<<1])
#define cu(tt) (tran[(tt<<1)+1])
	kkk=(k1<<24)|(k2<<18)|(k3<<12)|(k4<<6);
       
	fwrite(&kkk,4,1,fp);
	fwrite(&c1,1,1,fp);
	fwrite(&c2,1,1,fp);
	for(k=0;k<98;k++)
	if (tst[k][0]==c1 && tst[k][1]==c2){
		break;
	}
	if (k<98) {
		ttt[0]=ttt[1]=0;
		fwrite(ttt,2,1,fp);
	}
	else
		fwrite(&arseq,2,1,fp);
	arseq++;
#if	0
	{
       printf("%c%c%c%c%c%c%c%c: %s\n",
ct(k1),cu(k1), ct(k2), cu(k2), ct(k3), cu(k3), ct(k4), cu(k4), tt);
	}
#endif	0
#undef ct
#undef cu
}

fsize/=3;
if ((ff=fopen("xusrar30.tbl","r"))==NULL) {
	puts("Cannot open xusrar30.tbl");
	exit(1);
}
fread(bf,1,256,ff);
i=fread(bf,1,100000,ff);
for(j=0;j<i;j+=8)
{
#define kbf(k) ((u_long)bf[j+k])
       kkk=(kbf(4)<<24)|(kbf(5)<<18)|(kbf(6)<<12)|(kbf(7)<<6);
#undef kbf
	fwrite(&kkk,4,1,fp);
	fwrite(&bf[j+3],1,1,fp);
	fwrite(&bf[j+2],1,1,fp);
	for(k=0;k<98;k++)
	if (tst[k][0]==bf[j+3] && tst[k][1]==bf[j+2]){
		break;
	}
	if (k<98) {
		ttt[0]=ttt[1]=0;
		fwrite(ttt,2,1,fp);
	}
	else
		fwrite(&arseq,2,1,fp);
	arseq++;
	fsize++;
}
fclose(ff);
fclose(fp);


if ((fp=fopen("ar.tmp1","r"))==NULL) {
  puts("ar.tmp1 open");
  exit(-1);
}

free(bf);
i=0;
if ((itar=(ITE *)malloc((fsize+1)*sizeof(ITE)))==NULL) {
	puts("-- malloc err 2");
	exit(-1);
}

fsize=fread(itar, sizeof(ITE), fsize, fp);
/* printf("size: %d %d\n", sizeof(ITE), fsize); */
fclose(fp);
unlink("ar.tmp1");
SORTFUNC(itar,fsize,sizeof(ITE),qcmp);

for(i=0;i<fsize;i++) {
  u_long kk=CONVT(itar[i].key);
  char tt[3];
  ser_b5(itar[i].ch, tt);
  k1=(kk>>24)&0x1f;
  k2=(kk>>18)&0x1f;
  k3=(kk>>12)&0x1f;
  k4=(kk>>6)&0x1f;
#define tk(kk) (keymap[kk])

if (k1==27 && !k2 && !k3 && !k4) puts(". ¡C");
if (k1==30 && !k2 && !k3 && !k4) puts(", ¡A");

printf("%c", tk(k1));
if (k2) printf("%c", tk(k2));
if (k3) printf("%c", tk(k3));
if (k4) printf("%c", tk(k4));
printf(" %s\n", tt);
}

exit(0);
}
